Skip to content

How To Execute Selenium Tests On Cloud

Why you should execute your Selenium tests on cloud platforms? Selenium execution on the cloud enables QA to perform extensive cross-browser, cross-platform testing without relying on the local infrastructure. This ensures that the application is tested thoroughly and extensively before end-users access it. It allows numerous benefits as:

  • Execute automation tests faster by leveraging the power of parallel testing.
  • Scalable and cost-effective as you pay per resource.
  • Use browser capabilities to test on new as well as outdated/old web browsers, operating systems, and devices.
  • Provide multiple real mobile devices and browsers.
  • Easy debugging with built-in logs mechanism.
  • Provide video recording and screenshots for reference.
  • Just like real browsers network logs are also available for debugging.
  • Provide seamless integration with CI/CD tools.

Multiple platforms are available which you can choose as per your convenience like BrowserStack, SauceLabs, LambdaTest, Bitbar Cloud etc.

You can check this [Affiliate Link] or click on the below image to check the LambdaTest platform for your automation suite execution.

lambdatest

First, you need to create an account on the chosen cloud platform.Once a user account is created successfully then you will get a unique access key. The next thing you need to do is to set up desired capabilities as follows:

Desired Capabilities

Desired capabilities are a set of key-value pairs that are used to provide desired information to the web driver. For example, we have defined resolution, desired browser name, browser version, operating system etc in desired capabilities.

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("os_version", "10");
caps.setCapability("resolution", "1920x1080");
caps.setCapability("browser", browserName);
caps.setCapability("browser_version", "latest");
caps.setCapability("os", "Windows");
caps.setCapability("name", "Cloud Test");

What is Remote WebDriver?

Next, you need to call RemoteWebDriver.RemoteWebDriver is a class that implements a Web driver interface. It is used to execute the browser automation suite on a remote machine in distributed environments. It can control the browser in the grid by configuring the Node and Hub. To Leverage the Remote WebDriver you need to add the desired capabilities and provide the URL just as the Selenium Web driver.

WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
driver.get("https://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("BrowserStack Test");
element.submit();

You can configure multiple capabilities in cloud platforms as per your need.

  • caps.setCapability(“browserstack.debug”, true) — Enables visual log(Screenshots) on browserStack.
  • caps.setCapability(“browserstack.video”, false) — Disables video logs.
  • caps.setCapability(browserstack.high_contrast,true) –To Enable High Contrast Mode.

To run your tests on BrowserStack create a class named CloudTest.java. Add the capabilities mentioned above in the java class as follows:

public class CloudTest {

WebDriver driver;

private static final String URL = "https://" + BrowserStack_USERNAME + ":" + BrowserStack_ACCESS_KEY+ "@hub-cloud.browserstack.com/wd/hub";

@BeforeClass
public void setupTest(@Optional("chrome") String browserName) throws Exception {

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("os_version", "10");
caps.setCapability("resolution", "1920x1080");
caps.setCapability("browser", browserName);
caps.setCapability("browser_version", "latest");
caps.setCapability("os", "Windows");
caps.setCapability("name", "Cloud Test");
caps.setCapability("browserstack.debug", true);

driver = new RemoteWebDriver(new URL(URL), caps);
    }
@BeforeMethod
public void openBrowser()
{
driver.get("https://www.google.com");
}

@Test
public void TestSearch()
{
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("BrowserStack Test");
element.submit();
}

@AfterClass
public void TearDown()
{
driver.quit();
}
}

The user needs to provide his username and access key in the URL. On the basis of input provided by the user the RemoteWebDriver establish the connection with the BrowserStack and script execution will start accordingly.

You may face challenges while executing your file-handling script on Selenium Grid or Cloud platforms like SauceLabs or BrowserStack. This is because you are trying to upload files from your local machine and your scripts are executing on a remote server.

To resolve this problem either you need to use the files stored in the remote server itself or you need to use Local File Detector to send your files to the remote machine.

The Local File Detector enables file transfers between the client computer and the remote server. For instance, if a test requires the uploading of a file to a web application, a remote WebDriver can automatically transfer the file during execution from the local computer to the remote web server.

driver.setFileDetector(new LocalFileDetector());

Sample code for uploading a file to the web application.

@Test
public void test_FileUpload() throws Exception {
driver.get("https://blueimp.github.io/jQuery-File-Upload/");
WebElement addFile = driver.findElement(By.xpath(".//input[@type='file']"));
addFile.sendKeys(path + "src/test/resources/image.jpeg");
		driver.findElement(By.xpath(".//span[text()='Startupload']")).click();
if(driver.findElement(By.xpath(".//a[text()='c1.jpeg']")).isDisplayed()) 
{
Assert.assertTrue(true, "Image Uploaded");
}
else {
Assert.assertTrue(false, "Image not Uploaded");
 }
}

Tags: