Inject WebDrivers in TestNG @before test with @guice

I am new with configure selenium. Looked for passing drivers find this solution https://stackoverflow.com/a/35101914/7104440 I wonder is possible to @inject many drivers from browsers in this way. Is possible to bind different drivers? I got error with this code:

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

1) Binding to null instances is not allowed. Use toProvider(Providers.of(null)) if this is your intended behaviour.
at assecobs.driver.DriverModule.configure(DriverModule.java:31)

2) A binding to org.openqa.selenium.WebDriver was already configured at assecobs.driver.DriverModule.configure(DriverModule.java:31).
at 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;

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

I have been using Guice + WebDriver for a while. You can inject webdriver as you have shown in the ClientTest.java.

Check here for the detailed steps. – http://www.testautomationguru.com/selenium-webdriver-dependency-injection-using-guice/