I use selenium-jupiter. I am getting a webdriver from method arguments like this:
@Test
public void testWithChrome(ChromeDriver chromeDriver) {
chromeDriver.get("someUrlHere");
}
Now I want to run tests on grid so I need to use webdriver based on environment. For example when developing tests on my PC I want to use (local) ChromeDriver, but when running tests on grid with Jenkins, I want to use RemoteDriver. So I need something like this: (That gives me local Chrome when env = 0 or gives me remote Chrome when env = 1 but it's not working)
int env = 0;
@Test
public void testWithChrome(
(env == 0 ? ChromeDriver driver : RemoteDriver driver)) {
driver.get("someUrlHere");
}
I think what would be better here is to have a method that is executed before any test (annotated with
@BeforeAll
) that determines what environment the script is being run in. It probably reads from some config file local vs grid. Once that is determined, assign thedriver
variable either an instance ofChromeDriver
orRemoteDriver
. From then on, your tests will pass around thedriver
instance which will be of typeWebDriver
because bothChromeDriver
andRemoteDriver
inherit from it.You can do that with WebDriverManager that comes with this extension.
In short: When configuring your Selenium extension programmatically you can force usage of a Selenium Grid by configuring its URL as follows (using JUnit 5 annotations):
The problem in length is decribed here.