Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

SEO Metadata
titleGrid Execution with Selenium

There are two main aspects of running Selenium-based tests in the grid. The first aspect is initiating a test session and the other is running tests in parallel on multiple devices.

Note

The following browsers have moved their default mode to be W3C standard:

  • Chrome - Starting from version 75 
  • Safari - Starting from version 12 
  • Edge - Starting from version 17763 

Tests written in JAVA require Selenium jar version 3.14 and above.

...

There are two main aspects of running Selenium-based tests in the grid. The first aspect is initiating a test session and the other is running test tests in parallel on multiple devices.

...

    • testName - Specifies the test name. This name will appear in SeeTest Reporter and in the generated report
    • generateReport - By default every Selenium test generates a report and takes screenshots after each command. To disable reports (and screenshots) pass this capability with a false value
    • takeScreenshots - take a screenshot of reports and the device reflection in the Grid page
    • newCommandTimeout - change the default timeout for each command in the test (default is 300 seconds)
    • newSessionWaitTimeout - change the default timeout for creating a new driver (default is 300 seconds)
    • platformName - run the test on the specified platform (Mac, Windows, iOS, etc...)
    • browserName - run the test on the specified browser (default is chrome)
    • browserVersion - run the test on the specified browser version

      Info

      browserVersionBrowserVersion

      browserVersion BrowserVersion can also accept the value "latest" for the latest version or "latest-X" for versions before the latest. e.g if 'latest' is 70 then 'latest-3' will be 67

    • seleniumScreenshot - if set to true will take a screenshot using selenium grid and not via native OS API. By default set to false. This means the test will take longer.

      Info

      SeleniumScreenshot

      add this capability in tests that handle multiple browser windows in order to get a correct report

    • closePopups - provide information on which native popups to close (and how).
    • agentName - the name of the agent that the test will run on.
    • maxScreenshotInterval - the maximum time between consecutive screenshots (default is 10 seconds)
    • reportType - the type of the report that should be generated for the test: video or screenshot (default is both)


The returned capabilities include:

    • reportUrl - that provides a link to view the test report in SeeTest Reporter (if the test is configured to generate a report).
    • sessionId - the session ID of the test.
    • viewUrl - that provides a link to view the test's execution (Windows 10 and macOS only).

If the selenium Selenium test is configured to generate a report, the driver capabilities will include a capability named reportUrl that provide provides a link to view the test report in SeeTest Reporter.

...

Code Block
languagejava
themeEclipse
titleSelenium- Grid Execution
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("username", "<cloudUsername>");
dc.setCapability("password", "<cloudPassword>");
dc.setCapability("projectName", "<cloudProject>"); //only required if your user has several projects assigned to it. Otherwise, exclude this capability.
//dc.setCapability("accessKey", "<accessKey>"); // can be use instead of username,password and project.
dc.setCapability("generateReport", true);
dc.setCapability("testName", "<testName>");
dc.setCapability(CapabilityType.PLATFORM, Platform.WIN10);
dc.setCapability(CapabilityType.BROWSER_VERSION, "57.0.4");
dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
dc.setCapability("newCommandTimeout", 500);
dc.setCapability("newSessionWaitTimeout", 500);
dc.setCapability("agentName", "<agentName>");
dc.setCapability("maxScreenshotInterval", 10);
//dc.setCapability("seleniumScreenshot", true);
dc.setCapability("reportType", "video");
RemoteWebDriver driver = new RemoteWebDriver(new URL(cloudurl + "/wd/hub/"), dc);
String testReportUrl = driver.getCapabilities().getCapability("reportUrl");
String sessionId =  driver.getCapabilities().getCapability("sessionId"); //will return the session id as String.

...

  1. If the test fails on the client-side (and not on Selenium Grid side) the test report generated will be with Success status.
  2. If the test can fail but the test code catches the exception and the test developer would not like this failure to set the report status to Fail.

To set the test report status from the client code, one can use the command drivercommands:

Code Block
driver.executeScript("seetest:client.setReportStatus(<test-status>, <error-message>)")
driver.executeScript("seetest:client.setReportStatus(<test-status>, <error-message>, <stacktrace>)")

...

where test-status is:

  • "true" or "passed" for success
  • "false" or "failed" skipped" for skipped
  • everything else for fail

For example

Code Block
languagejava
driver.executeScript("seetest:client.setReportStatus(\"Skipped\", \"This test should skip\")");
driver.executeScript("seetest:client.setReportStatus(\"Failed\", \"Element text is wrong\",
                     \"java.lang.Throwable at ClientSideFailureTest.clientSideFailure(ClientSideFailureTest.java:82) at ClientSideFailureTest.test(ClientSideFailureTest.java:76)\")");

Add a step to test report with the status

User Users can add a step in the report from the client code.

...

Code Block
languagejava
driver.executeScript("seetest:client.report("\"This step should passed\",\"true\")");
or
driver.executeScript("seetest:client.report("\"This step should failed\",\"false\")");

Add steps group to the report

User can add a group of steps in the report.

To begin a steps group, call the following command:

Code Block
languagejava
driver.executeScript("seetest:client.startStepsGroup("\"Name of steps group\")");

To end a steps group, call the following command:

Code Block
languagejava
driver.executeScript("seetest:client.stopStepsGroup()");

All the steps that appear between 'startStepsGroup' and 'stopStepsGroup' will be part of the group of the steps.

For example:

Code Block
languagejava
driver.executeScript("seetest:client.startStepsGroup("\"Name of steps group\")");
step_1
step_2
...
step_n
driver.executeScript("seetest:client.stopStepsGroup()");

add a step to test report before every 'it'

...

Code Block
exports.config = {  
...
        onPrepare: function() {
            jasmine.getEnv().addReporter({
                specStarted: async function(result) {
                    await browser.executeScript('seetest:client.startGroup('+result.description+'));');
                }
            });
        },

 onComplete: () => {
      browser.executeScript('seetest:client.stopGroup());');
        console.log('onComplete');
    },
...    
        specs: ['test.js']
}

Add test property (tag) to report

Users can add a test property (tag) to the report from the client code.

For example:

Code Block
driver.executeScript("seetest:client.addTestProperty(\"key\", \"value\")");


Handling native OS popups

If during a selenium web test, the browser or OS displays a native popup that the user wants to close, he can specify how to close this popup.

Using the capability closePopups the user can specify one or more expressions that specify which native popups to look for and if found which button to click.

...

Download a file to your client computer in a grid session

User Users can download a file from the Selenium Agent machine to the local machine in the grid session.

File size up to 50MB.

...

Code Block
languagejava
byte[] content = null;
Object res = driver.executeScript("seetest:client.getFile("\"<SELENIUM_AGENT_FILE_NAME>\")");
content = Base64.getDecoder().decode((String) res);
FileUtils.writeByteArrayToFile(new File("<CLIENTS_PATH_+_FILE_NAME>"), content);

List all your session's files in a grid session (Chrome only)

Users can list all the files that their session downloaded in the grid session.

the returned type is List<String>.

For example:

Code Block
languagejava
List<String> files = (List<String>) driver.executeScript("seetest:client.getFileList()");
System.out.println("list: " + files);

Running with custom Firefox profile

Mostly used for client certificates.

Firefox saves client certificates in the profile but Selenium uses a temporary profile. To pass this issue we need to add a capability that will make Selenium run with our custom profile.

First, we need to make a profile and load our certificate on it:

  • Launch Firefox and click the menu icon found in the upper-right corner of the browser. After that, click the Preferences icon.
  • Go to the Certificates tab and then click the View Certificates button.
  • The previous step should have launched the Firefox Certificate Manager. Click the Your Certificates tab and then click the Import button.

firefox-your-certificates-importImage Added

  • The previous step should launch a browser that you can use to navigate to the directory where your client certificate file (usually a PKCS12 file with the .p12 or .pfx extension) is stored. Select the file and enter the required password. If you succeed, you should see a notification that says "Successfully restored your security certificate(s) and private key(s)." (or something to that effect). Click OK to proceed.

firefox-successfully-imported-client-certificateImage Added






  • You should then see your newly imported certificate under the Your Certificates collection of your Firefox Certificate Manager.

Now our custom profile is done.

To use it with selenium we need to take the only the files named cert9.db and key4.db and pack them in a zip file (You can zip the entire profile and not just the certificate part, but it is not recommended):

Image Added

Now all that is left is to add the profile to the desired capabilities:

Code Block
languagejava
File file = new File("[PATH_TO_PROFILE_ZIP_FILE]");
String firefoxProfile = "";
try {
	firefoxProfile = Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(file));
} catch (IOException e) {
	e.printStackTrace();
}
dc.setCapability(FirefoxDriver.PROFILE, firefoxProfile);

We will get a driver with the custom profile we supplied.