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");
}
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):
abstract class UiTest {
@RegisterExtension
static SeleniumExtension seleniumExtension = new SeleniumExtension();
@BeforeAll
static void setUpOnce() {
boolean isRunningInCiEnvironment = ...
if( isRunningInCiEnvironment ) {
// this will force Selenium Jupiter to use a RemoteWebDriver
seleniumExtension.getConfig().setSeleniumServerUrl("http://...");
}
// without above condition, a FirefoxDriver will be used locally
seleniumExtension.addBrowsers(BrowserBuilder.firefox().build(););
}
}
class MyTest extends UiTest {
// Use WebDriver interface in test method: concrete browser detected
// at execution time (via @BeforeAll, inherited from parent class)
@Test
void my_test_Case(WebDriver webDriver) {
webDriver.get(...)
Assert.(...)
}
}
The problem in length is decribed here.