How to Run Selenium Testing Script In Cloud

Selenium cloud testing because more and more popular these days. This article will tell you how to run selenium script in cloud with CrossBrowserTesting.

1. Why Selenium Cloud Testing?

  1. Your website users come from all over the world. They use different devices, operating systems, and different web browsers.
  2. There is a total of 1500+ web browsers in the world, the browser is used from iPhone, iPad, Android devices, Windows, Linux, macOS, etc.
  3. So to make your web application perfect you had better verify how your web application behaves on all those various different devices, operating systems, and web browsers.
  4. But it is too difficult to maintain so many devices and browsers to verify your web application features effectively in selenium automation test scripts.
  5. CrossBrowserTesting also called CBT  is coming. It is a cloud-based platform that supports almost all of the current devices, operating systems, and web browsers.
  6. You can test both web or mobile applications with real physical machines, mobile devices, or web browser simulators in the selenium cloud testing environment.
  7. You can run your selenium cloud testing automation scripts through thousands of browsers and devices with CBT. You do not need to maintain those operating systems, devices, and browsers. You can fully focus on writing your selenium testing scripts. CBT will do other things for you.

2. How To Use Selenium Cloud Testing Service Provided By CrossBrowserTesting.

  1. Below are the steps to use CrossBrowserTesting to start your selenium cloud testing.
  2. Register for a free CrossBrowserTesting account. It is so easy.
  3. Login to the CrossBrowserTesting dashboard, click your account icon on the web page top-right corner to display the Account & Settings menu list。
  4. Then click the Manage Account menu item in the Account & Settings menu list.
  5. Find your account AuthKey value in the Manage Account page right side User Profile section.
  6. Now you can create a RemoteWebDriver object in your java source code with your CrossBrowserTesting login username and AuthKey to use the selenium cloud testing service to run your local selenium testing script. Below is just a code snippet, you can read the full source code later.
     DesiredCapabilities caps = new DesiredCapabilities();
     
     //Set the build number.
     caps.setCapability("build", "1.0");
     
     //Set the browser type which is IE Edge. This is the must-set field. This data will tell CBT which web browser to use.
     caps.setCapability("browser_api_name", "Edge20");
     
     //Set the operating system. This is the must-set field. This data will tell CBT which os to use.
     caps.setCapability("os_api_name", "Win10");
     
     //Set the screen display resolution.
     caps.setCapability("screen_resolution", "1024x768");
     
     //Set the selenium webdriver library version.
     caps.setCapability("selenium_version", "2.53.1");
    
     driver = new RemoteWebDriver(new URL("http://" + username + ":" + authkey + "@hub.crossbrowsertesting.com:80/wd/hub"), caps);
    
  7. Below is the full source code in java, you can read code comments for detailed explanation.
    package com.dev2qa.webdriver;
    
    import java.net.URL;
    import org.junit.jupiter.api.Assertions;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    
    public class TestCrossBrowserTestingCloud {
    
    	public static void main(String[] args) {
    		/* This is the user name you registered in crossbrowsertesting.com, 
    		 * it is usually an email address, but the @ charactor is replaced with %40.*/
    		String username = "happyzhaosong%40gmail.com";
    		
    		/*This is the authkey you obtained from crossbrowsertesting.com when you register your account.*/
    	    String authkey = "uce752255c8c954e";
    
    		/*Because we will run selenium testing script in the cloud so we use RemoteWebDriver.*/
    		RemoteWebDriver driver = null;
    		
    		try {
    			
    			/* Create an instance of DesiredCapabilities, this object is used to transfer detail 
    			 * information about the device, browser etc to crossbrowsertesting.com, then crossbrowsertesting.com
    			 * use these information to initiate the web browser for you.*/
    			DesiredCapabilities caps = new DesiredCapabilities();
    			
    			//Set the name attribute.
    			caps.setCapability("name", "Selenium Automation Testing Example");
    			
    			//Set the build number.
    			caps.setCapability("build", "1.0");
    			
    			//Set the browser type which is IE Edge. This is the must-set field. This data will tell CBT which web browser to use.
    			caps.setCapability("browser_api_name", "Edge20");
    			
    			//Set the operating system. This is the must-set field. This data will tell CBT which os to use.
    			caps.setCapability("os_api_name", "Win10");
    			
    			//Set the screen display resolution.
    			caps.setCapability("screen_resolution", "1024x768");
    			
    			//Set the selenium webdriver library version.
    			caps.setCapability("selenium_version", "2.53.1");
    	
    			/*Create the remote webdriver object use above information. Because the web browser will be running in the remote server so
    			  we should use RemoteWebDriver instead of WebDriver which is used to initiate web browser locally. */
    			String remoteUrl = "http://" + username + ":" + authkey + "@hub.crossbrowsertesting.com:80/wd/hub";
    			driver = new RemoteWebDriver(new URL(remoteUrl), caps);
    			
    			//Print out session id.
    			System.out.println("Session id : " + driver.getSessionId());
    					
    			//Start to load the web pages we want to test.
    			System.out.println("Loading test pages");
    			
    			//The loaded test page is an example page in crossbrowsertesting.com
    			driver.get("http://crossbrowsertesting.github.io/selenium_example_page.html");
    			
    			//Make the web browser's windown in the cloud platform maximized.
    			System.out.println("Make the web browser's windown in the cloud platform maximized.");
    			driver.manage().window().maximize();
    			
    			//Verify the loaded web page title to see whether the page loaded correctly or not.
    			Assertions.assertEquals(driver.getTitle(), "Selenium Test Example Page");
    			
    		} catch (AssertionError aex) {
    			/* If an assertion exception occured, just print the exception trace. 
    			 * We will show you how to take the screen snapshot in later articles.*/
    			aex.printStackTrace();
    		} catch (Exception ex) {
    			/* If exception occured, just print the exception trace. 
    			 * We will show you how to take the screen snapshot in later articles.*/
    			ex.printStackTrace();
    		} finally {
    			//Close and quit the web browser in the cloud platform.
    			driver.quit();
    		}
    	}
    
    }
    

3. How To Check The Selenium Cloud Testing Status In CrossBrowserTesting.

  1. Log in to crossbrowsertesting.com.
  2. On the page left side, click the Automation —> View Test Results menu item to see the selenium cloud testing results.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.