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.
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.
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.
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.
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.
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.
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.
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
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