How to efficiently carry out automatic test browser startup

Here's an easy way to do all the things you need to do before you start your browser to run automated web tests using Selenium. The explanation is based on the assumption that Selenide will be used.

Preface

As a preliminary work for testing with Selenium, you need to start the browser, of course. However, it is necessary to follow the procedure below before starting the browser and starting the test.

There is nothing you can't do even if you do your best manually, but if there are multiple terminals that execute the test, it is troublesome to update the driver one by one, and in the first place it hinders regular and stable test execution. So, this time, I will describe how to implement these efficiently.

How to specify the browser

When testing the Web, it is necessary to specify the browser to be tested. There are two ways to do this, one is to specify it in code and the other is to specify it with run-time parameters.

When specified in the code

In Selenide, the setting is stored in the Configuration class, and when specifying the browser on the source code, describe as follows.

Configuration.browser = WebDriverRunner.CHROME

You can switch browsers by writing.

When specifying with run-time parameters

However, it should usually be based on the need to run the same test in multiple browsers. Therefore, it is common to switch the browser under test with run-time parameters rather than specifying it directly in the code.

You can specify the execution browser by writing as follows using selenide.browser. java -Dselenide.browser=Chrome

However, when you specify a browser, you also want to make settings specific to that browser. And I don't want to write browser settings in the test code one by one. Here's how to deal with such cases.

Setting method for each browser

This can be solved by calling the code that sets the parameters before the browser startup process. Below is the sample code.

IEWebDriverProvider.java


package com.driver;


public class IEWebDriverProvider implements WebDriverProvider {
  @Override
  public WebDriver createDriver(final DesiredCapabilities desiredCapabilities) {
    InternetExplorerDriverManager.iedriver().arch32().setup();
    desiredCapabilities.setCapability("ignoreProtectedModeSettings", true);
    InternetExplorerOptions opt = new InternetExplorerOptions(desiredCapabilities)
        .destructivelyEnsureCleanSession().withAttachTimeout(10, TimeUnit.SECONDS);
    opt.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

    return new InternetExplorerDriver(opt);
  }
}

Prepare a class that implements WebDriverProvider, and give the code path as a parameter at runtime as shown below. -Dselenide.browser=com.driver.IEWebDriverProvider

By doing so, the timing to start the browser, specifically WebDriverRunner.getAndCheckWebDriver(); The above code that configures the browser settings is also executed while is executed.

This time, an example of starting IE is described, but it is possible to prepare a class for setting in advance for each browser. The advantage of this method is that you can reuse the configuration code in multiple tests to improve the visibility of the test code by avoiding writing the necessary presets for the browser in individual tests.

Automatic driver update

When executing a test, the test may fail because the version of the driver installed on the terminal on which the test is executed is old.

However, Selenide includes WebdriverManager and has a built-in process to automatically update the browser, so you can run the test without being aware of it. It checks whether the driver of the terminal you are using is the latest driver, and if it is not the latest, it will automatically download it.

Even if you are not using Selenide, you can get the same effect by using WebdriverManager.

When specifying the driver version

If the latest driver version and the browser version of the test execution terminal do not match, the test cannot be executed as it is, so it is necessary to specify the driver version individually.

In that case, you can also specify the driver version as follows. java -Dwdm.chromeDriverVersion=1.99

You may also want to stop the automatic update process because it is clear that the driver is up to date, the number of tests is huge, and you want to shorten the test time as much as possible. Even in such a case, you can stop the update process by setting as follows. java -Dselenide.driverManagerEnabled=false

in conclusion

So far, we have introduced how to eliminate various troubles related to the browser, which is a prerequisite for starting the test. We hope that you will have a comfortable test automation life with reference to this.

Recommended Posts

How to efficiently carry out automatic test browser startup
[Ruby] How to comment out
How to unit test Spring AOP
[RSpec] How to write test code
How to filter JUnit Test in Gradle
How to put out the error bundling
How to test private scope with JUnit
Try to carry out paiza skill check
How to write an RSpec controller test
[SpringBoot] How to write a controller test
How to test a class that handles application.properties with SpringBoot (request: pointed out)
[Java] How to test for null with JUnit
How to test interrupts during Thread.sleep with JUnit
How to use "sign_in" in integration test (RSpec)
JUnit 5: How to write test cases in enum
[Swift] How to implement the fade-in / out function
How to write test code with Basic authentication
How to get resource files out with spring-boot