0

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.

Michiel Bugher
  • 1,005
  • 1
  • 7
  • 23

1 Answers1

1

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");
    }
  }
}
Michiel Bugher
  • 1,005
  • 1
  • 7
  • 23
  • I would have simplified it by using an environment variable. System.getProperty("proxy.port", "8888") or better yet, load that value from a properties file using the Typesafe lib. Property files could be the source of a quite complex testing config and seems like it would work well. – djangofan Mar 25 '20 at 19:20
  • We use maven to run it in Jenkins. It's very simple to pass -Dis.CI=true when running in Jenkins and default it to false in the POM file. It also means we can change the property when it runs without having to modify any files. – Michiel Bugher Dec 09 '20 at 16:39