Dario Zamuner
Dario Zamuner

Reputation: 1041

Add chrome options at launch in Vaadin 24 Testbench

I'm using Vaadin 24 TestBench, Spring Boot 3.1 and JUnit5. I need to add some custom arguments to chrome driver, particularly the option disable-software-rasterizer, otherwise the browser opens with a transparent window and the test freezes until timeout. That's due to my video card not compatible with the software rasterizer. If I switch video card everything works fine, but I have some graphic artifacts, so the former is the way to go. Adding -Dcom.vaadin.testbench.Parameters.headless=true as a system property is not enough because nevertheless the test freezes until timeout. Probably the driver needs some sort of rendering even if the window doesn't show up.

Furthermore I need other arguments as well, like remote-allow-origins=* and window-size=1920,1080.

I have a init method annotated with @BrowserConfiguration; it's called twice but it doesn't work. I suspect the reason is that my arguments are ignored, since the only setup code inside the class LocalDriver (a class in Vaadin's framework) is

} else if (BrowserUtil.isChrome(desiredCapabilities)) {
        // Tells chrome not to show warning
        // "You are using an unsupported command-line flag:
        // --ignore-certifcate-errors".
        // #14319
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--test-type ");

        if (Parameters.isHeadless()) {
            options.addArguments("--headless=new");
        }
        driver = new ChromeDriver(options);
}

It doesn't even reach the method annotated with @BeforeEach because the browser window is opened before it. Adding System.setProperty("vaadin.launch-browser", "false"); at static initialization, as per documentation, doesn't help.

This is the code I'm talking about. It freezes before reaching the setup method

public class LoginTest extends BrowserTestBase {

static {
    // Prevent Vaadin Development mode to launch browser window
    System.setProperty("vaadin.launch-browser", "false");
}

@BrowserConfiguration
public List<DesiredCapabilities> init() {
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("headless");
    chromeOptions.addArguments("window-size=1920,1080");
    chromeOptions.addArguments("remote-allow-origins=*");
    chromeOptions.addArguments("disable-gpu");
    chromeOptions.addArguments("disable-software-rasterizer");
    chromeOptions.addArguments("disable-infobars"); // disabling infobars
    chromeOptions.addArguments("disable-extensions"); // disabling extensions
    chromeOptions.addArguments("disable-dev-shm-usage"); // overcome limited resource problems
    return List.of(new DesiredCapabilities(chromeOptions));
}

@BeforeEach
public void setup() throws Exception {
    getDriver().get("http://localhost:8080/");
}

// Please note that since TestBench 9 test methods
// must be annotated with helper @BrowserTest annotation.
@BrowserTest
public void clickButton() {
    // Find the first button (<vaadin-button>) on the page
    ButtonElement button = $(ButtonElement.class).first();

    // Click it
    button.click();

    // Check that text of the button is "Clicked"
    Assertions.assertEquals("Clicked", button.getText());
  }
}

Using vanilla Selenium without TestBench I was able to make it work, but it would be nice to have TestBench additional features.

Is there any way to add custom configuration options at browser start?

Upvotes: 0

Views: 89

Answers (0)

Related Questions