0

I have a project which is based on the serenity-bdd/serenity-cucumber-starter project. I'm using test-containers to start a couple of Docker containers as well as a Selenium Grid container to run the test against.

new GenericContainer<>(SELENIUM_IMAGE)
            ...
            .withExposedPorts(SELENIUM_CONTAINER_PORT, SELENIUM_CONTAINER_NOVNC_PORT)
            ...
);

When the tests start, test-containers will ramp up the containers and bind random host ports to all exposed ports of the containers.

Because of that, I cannot define a fixed value in serenity.conf for the url of the remote driver

webdriver.remote.url = "http://localhost:????/wd/hub"

Thus I need a way to set webdriver.remote.url programmatically.

One option would be to use the FixedHostPortGenericContainer, which allows you define the host port on which the container exposed port will be bound to.

I'd rather would like to use a different approach though, as the developers state that

While this works, we strongly advise against using fixed ports, since this will automatically lead to integrated tests (which are an anti pattern).

So the question is: How can I modify the value of webdriver.remote.url at runtime? Is there any option provided by serenity-bdd to reload the net.thucydides.core.util.SystemEnvironmentVariables at runtime?

Thomas Schmidt
  • 1,248
  • 2
  • 12
  • 37

1 Answers1

1

Faced recently the same issue, but was lucky enough to find a solution:

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;

import net.serenitybdd.core.webdriver.driverproviders.FirefoxDriverCapabilities;
import net.thucydides.core.guice.Injectors;
import net.thucydides.core.util.EnvironmentVariables;
import net.thucydides.core.webdriver.DriverSource;

public class CustomWebDriverFactory implements DriverSource {

    @Override
    public WebDriver newDriver() {
        try {
            String ip = "your_dynamic_ip";
            return new RemoteWebDriver(
                    new URL("http://" + ip + ":4444/wd/hub"),
                    new FirefoxDriverCapabilities(Injectors.getInjector().getProvider(EnvironmentVariables.class).get()).getCapabilities());
    }
        catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean takesScreenshots() {
        return true;
    }
}

So you have to add such factory implementation and define in serenity.properties:

webdriver.driver = provided
webdriver.provided.type = mydriver
webdriver.provided.mydriver = <your_factory_package>.CustomWebDriverFactory
thucydides.driver.capabilities = mydriver