Execute your Automation Tests in the Cloud using Selenium

In this article, will talk about CrossBrowserTesting which lets us to test website across 1500+ browsers - Android devices, iPhones, iPads, Windows, & OSX. Check list of Operating systems, mobile devices and browsers supported by CrossBrowsersTesting.

The end users across the globe have their choice of using different browsers to access web applications. So, it is very important to verify applications on various browsers and versions. It is always a good practice to verify how the application behaves on various browsers and Operating System. Maintaining infrastructure which supports different combinations of browsers and Operating systems with different resolutions is very difficult.

CrossBrowserTesting (CBT) is one of the cloud-based platforms for testing both web and mobile applications with real physical devices and simulators. Using CrossBrowserTesting we can run Selenium automation scripts across hundreds of browsers and operating systems and focus ONLY on writing test scripts.

It's very simple to set-up CBT, no additional software required, we can start running tests immediately and share test report snapshots, videos and network traffic information right in one easy report.

Let's getting started with CrossBrowserTesting and will look at how to get Selenium script running on the CrossBrowserTesting Cloud.

Step 1:- Sign up for CrossBrowserTesting account. You can Start Your Free Trial - No Credit Card Required.

Step 2:- Get your authkey, which you can find under Account -> Manage Account once you login.

Step 3:- To run selenium example test from your machine, first add required dependencies.
You may need to add the below dependencies to your project.
selenium-server-standalone-vXXX.jar
unirest-java-vXXX.jar
json-XXX.jar

Step 4:- Define capabilities like Browser, OS, Resolution etc.

Step 5:- Copy paste the below code

Step 6:- Run the test as Java application

Now we will look into the below example on how we can connect to CrossBrowserTesting and execute basic Selenium script using Java.

Below is the basic Selenium code using Java to execute on CBT.

package easyCBTExample;

import static org.junit.Assert.*;

import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class CBTExampleTest {

	static String username = "kdevxxxxxxxju.com"; // Your username
	static String authkey = "xx51xx15xx1a9acb"; // Your authkey

	public static void main(String[] args) throws Exception {

		// Created object of DesiredCapabilities
		DesiredCapabilities caps = new DesiredCapabilities();

		caps.setCapability("name", "Selenium Test Example");
		caps.setCapability("build", "1.0");
		caps.setCapability("browser_api_name", "Edge20");
		caps.setCapability("os_api_name", "Win10");
		caps.setCapability("screen_resolution", "1024x768");
		caps.setCapability("selenium_version", "2.53.1");

		RemoteWebDriver driver = new RemoteWebDriver(
				new URL("http://" + username + ":" + authkey + "@hub.crossbrowsertesting.com:80/wd/hub"), caps);
		System.out.println(driver.getSessionId());
		// we wrap the test in a try catch loop so we can log assert failures in our system
		try {
			// load the page url
			System.out.println("Loading Url");
			driver.get("http://crossbrowsertesting.github.io/selenium_example_page.html");
			// maximize the window - DESKTOPS ONLY
			System.out.println("Maximizing window");
			driver.manage().window().maximize();
			// Check the page title (try changing to make the assertion fail!)
			assertEquals(driver.getTitle(), "Selenium Test Example Page");
		} catch (AssertionError ae) {
			// if we have an assertion error, take a snapshot of where the test
			// fails
		} catch (Exception a) {
			// if we have an other exception, take a snapshot of where the test
			// fails
		} finally {
			// and quit the driver
			driver.quit();
		}
	}
}

When we run the above test, it will show the test status as running with the capabilities mentioned.

Status of the test running

What have we Done?

Firstly, we have specified CBT username ( which is the email address with which you signed up for your account) and authkey to interact with CrossBrowserTesting.

To run tests on remote machines i.e on CrossBrowserTesting Cloud, WebDriver has to use the instance of the RemoteWebDriver and DesiredCapabilities in order to specify browser name, version and platform to execute tests.

Generally to run tests on our local machine, we will just specify as WebDriver driver = new FirefoxDriver(); to run on Firefox browser.

To execute tests on remote machine, we need to use remotewebdriver, here we have changed that to run tests on CBT.

RemoteWebDriver driver = new RemoteWebDriver(new URL("http://" + username + ":" + authkey + "@hub.crossbrowsertesting.com:80/wd/hub"), caps);

"Not all server implementations will support every WebDriver feature. Therefore, the client and server should use JSON objects with the properties listed below when describing which features a user [requests](JsonWireProtocol#POST_/session.md) that a session support. "

DesiredCapabilities helps us to customize and configure a browser session using key-value pair. When using CBT, we have to specify os_api_name and browser_api_name mandatorily. For example OS can be defined for Windows 10 as 'Win10' and Windows 8.1 as Win8.1 so on. And browser name can be defined as 'FF46' for Firefox Version 46 and 'Chrome53' for chrome version 53. You can use CBT wizard / via an API call to view specific capabilities that you need when building test.

We have to add capabilities 'name' and 'build' to specify test name and build number to make more meaningful. And all remaining capabilities are optional to customize your test more. Below is the screenshot which shows the capabilities added to the test.

Capabilities added to the test running

If you want test to run with specific resolution, then you need to add 'caps.setCapability("screen_resolution", "1280x768");'. Else tests will be executed on default screen resolution '1024x768'.

If you don't specify 'browser_api_name' / browser_api_name, it will throw an WebdriverExpection as 'Capabilities must specify either a browserName or CrossBrowserTesting's browser_api_name so we know what browser to launch.' or 'os_api_name needs to be a string'

Check list of possible Selenium capabilities that we can use when executing tests across CrossBrowserTesting’s service.

We have wrapped test in a try catch loop, so that we can log assert failures and capture snapshots. In the above test, there is only one assertion to check the page title.

Check Part two on How to capture screenshots and how to run tests parallely using TestNG on CBT Cloud Service.

Selenium Tutorials: 

Comments

Its very useful :D Thanks

Add new comment