How to set browser arguments conditionally (Selenium-Jupiter specific)

256 Views Asked by At

I have tried so many things to set the proxy in a chrome-in-docker browser.

I finally found something that works, but it isn't the best solution.

@BeforeEach
public void beforeEach(@Arguments("--proxy-server=server:portNum") WebDriver driver) {
    this.registrationPage = new RegistrationPage(driver);
    this.registrationPage.navigateTo();
}

This works when I run the tests in Jenkins (Needs proxy there) but will fail the tests when running locally.

Is there a better way to set the proxy server, or to set it conditionally?

My code runs in Java with maven. I would be fine with adding a system property to Jenkins (-Dis.CI=true or whatever) but I can only figure out how to set these arguments as a method paramter. That definitly won't work for conditionally setting them.

Any other way to set the --proxy-server is greatly appreciated. I would also prefer a way to set this globally. Having to set it in every test class would be a nightmare.

I have tried using WebDriverManager.globalConfig().setProxy("...") and it had no effect. I'm under the assumption that the proxy in the config is different than the proxy-server.

1

There are 1 best solutions below

2
Michiel Bugher On BEST ANSWER

I ended up setting this explicitly in ChromeOptions. This isn't ideal, but it is the best solution that I could find. I would still like to find a more generic solution that will work across all browsers.

I also made a is.CI system property that I set when I run in Jenkins. This is necessary because the proxy does not work locally.

@ExtendWith(SeleniumExtension.class)
public class BaseTest {
@Options
static ChromeOptions options = new ChromeOptions();

@BeforeAll
public static void beforeAll() {
    Boolean isCI = Boolean.getBoolean("is.CI");

    if (isCI) {
        options.addArguments("--proxy-server=server:portNum");
    }
  }
}