All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.webdevice.support.SpringDeviceRegistry Maven / Gradle / Ivy

There is a newer version: 0.0.11
Show newest version
package io.webdevice.support;

import io.webdevice.device.Device;
import io.webdevice.device.DeviceNotProvidedException;
import io.webdevice.device.DeviceProvider;
import io.webdevice.device.DeviceRegistry;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import static io.cucumber.spring.CucumberTestContext.SCOPE_CUCUMBER_GLUE;

@Component
@Scope(SCOPE_CUCUMBER_GLUE)
public class SpringDeviceRegistry
        implements DeviceRegistry {
    private final BeanFactory factory;

    @Autowired
    public SpringDeviceRegistry(BeanFactory factory) {
        this.factory = factory;
    }

    @Override
    public  Device provide(String device) {
        DeviceProvider provider = providerOf(device);
        return provider.get();
    }

    @Override
    public  void release(Device device) {
        DeviceProvider provider = providerOf(device.getName());
        provider.accept(device);
    }

    @SuppressWarnings("unchecked")
    private  DeviceProvider providerOf(String device) {
        try {
            return factory.getBean(device, DeviceProvider.class);
        } catch (BeansException e) {
            throw new DeviceNotProvidedException(device, e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy