我是配置硒新手。寻找通过的驱动程序找到此解决方案https://stackoverflow.com/a/35101914/7104440我想知道是否可以通过这种方式从浏览器中注入许多驱动程序。可以绑定其他驱动程序吗?我收到此代码错误:

encom.google.inject.CreationException: Unable to create injector, see the following errors:


1)不允许绑定到空实例。如果这是您预期的行为,请使用toProvider(Providers.of(null))。
  在assecobs.driver.DriverModule.configure(DriverModule.java:31)

2)已经在assecobs.driver.DriverModule.configure(DriverModule.java:31)上配置了到org.openqa.selenium.WebDriver的绑定。
  在assecobs.driver.DriverModule.configure(DriverModule.java:31)

DriverModule.class

private DriverSetup driverSetup = new DriverSetup();

@BeforeSuite
@Override
public void configure(Binder binder) {

    for (BrowserNames browserName : BrowserNames.values()) {
        System.out.println(" bind " + browserName.toString());
        WebDriver driver = driverSetup.initDriver(browserName.toString());
        binder.bind(WebDriver.class).toInstance(driver);
    }
}
}


DriverSetup.class

 @SneakyThrows
public WebDriver initDriver(String browser) {
    if (browser.equalsIgnoreCase("chrome")) {
        capabilities = chromeCapabilities();
        driver = initChromeDriver(capabilities);

    } else if (browser.equalsIgnoreCase("firefox")) {
        capabilities = firefoxCapabilities();
        driver = initFirefoxDriver(capabilities);

    } else if (browser.equalsIgnoreCase("opera")) {
        capabilities = operaCapabilities();
        driver = initOperaDriver(capabilities);

    } else {
            capabilities = firefoxCapabilities();
            return driver = initFirefoxDriver(capabilities);
        }

        return driver;
    }


ClientTest.class

@Guice(modules = {DriverModule.class})
public class ClientTest extends DriverSetup {

@Inject
WebDriver driver;

最佳答案

我已经使用Guice + WebDriver已有一段时间了。您可以按照ClientTest.java中所示注入webdriver。

在此处查看详细步骤。 -http://www.testautomationguru.com/selenium-webdriver-dependency-injection-using-guice/

08-27 08:56