How to integrate automated test cases using SauceLabs Cloud network?

tudip-logo

Tudip

08 June 2020

Testing is to ensure that the website is capable of performing once it is live. The quality factors like functionality, usability and navigation, performance and compatibility must be considered for creating quality applications. To check the compatibility of software applications, industry uses the cloud testing as a process of software testing in which web applications use cloud environments or cloud servers to check their factors with real user traffic.

Benefits of cloud testing

The benefits of cloud testing is rapid provisioning test environment, offsite data storage, productivity anywhere on demand services, customization of hardware and effective user resources like by testing in multiple architectures. Thus testing in the cloud reduces the risk of failure and increases the confidence in the quality of the software applications.

SauceLabs_cloud_network_1

Challenges faced during cloud testing

The cloud testing challenges can be time consuming in uploading the scripts, difficulty in performing parallel testing. To accomplish parallel testing, the scripts need to be modified to sue the cloud requirement which may increase the cost and effort. Another challenges are users will be charged for boot times and even for unused launched instances

 Why cloud testing is important?

Basically you can execute selenium test cases in the VMs which are not in our control. For example you want to execute your selenium script in the firefox version 24 which is not available for you so the infrastructure company provided for you is limited but the client asking you to execute the selenium test cases in different browser combinations and OS combinations. Now there comes a cloud testing importance, who has their infrastructure and have hosted on their servers. Now we need to access those servers and we execute our selenium script from our local server to those cloud servers.

What is SauceLabs?

Now coming to the selenium, there is a cloud testing component called Saucelabs i.e. basically designed for testing either Automation and Manual both. We can execute the actual test cases on the Sauce Cloud servers. Sauce Labs provides setup on their network. You can run your web and mobile tests across hundreds of real browsers and platforms instantly. Instead of using your local chrome you can use the driver of your specified browsers on their cloud itself.

Saucelabs is a free testing tool. You will get to know “Run your first test now” after you logged into the saucelabs where you have an option to run test cases either for Automated scripts, Manual script and Mobile scripts. Whenever you run your test cases, the job will be opened in the dashboard page where you can see their status.

Integration with Selenium automated scripts 

Before starting with SauceLabs integration, you need to know about the desired capabilities class. The desired capabilities class is used basically when you are running test cases on a server or a VM outside of a local PC scope or when you use cloud computing as well as when you are running your appium test cases so here you will use the concept of the desired capabilities. What are Desired Capabilities? – The desired capability is a series of key/value pairs that stores the browser properties like browser name, browser version, the trail of the browser driver within the system, etc. to determine the behavior of the browser at run time.

For example, I want to run scripts on a windows machine, also I want to run these on firefox browser so these are my desired capabilities. For this, I will pass my desired capabilities to this Desired Capabilities class to run the automated scripts. In short, it reads the capabilities of what the host is expecting to run the test cases i.e. OS and browsers. Then it will go and connect to the SauceLabs and it checks all their VMs wherever these requirements are satisfied. Now in saucelabs there may be hundreds of VMs i.e. combinations of windows, linux, Mac and different browsers. Suppose if i have provided the windows version XP and browser as internet explorer then when it runs and connects with the server, then it will fetch the VMs for combination of the XP and IE in their cloud infrastructure.
Now coming to the scripting part, let us take an example we need to hit the URL “http://google.com”  and then get the title of website, so for this you would create an instance of WebDriver for any specific browser i.e. ChromeDriver, IEDriver, GeckoDriver etc.

But here you need to run your script in the remote server, so you need to create an instance of the RemoteWebDriver.

public class SauceLabsTest {
public static void main(String[] args) {
WebDriver driver = new RemoteWebDriver(new URL(), cap);
driver.get("http://google.com");
System.out.println(driver.getTitle());
}
}

Here, the RemoteWebDriver expects two arguments i.e. connections for remote webdriver and capability object which tells you the desired capabilities. But all the remote VMs are provided by SauceLabs so that SauceLabs server connection you have to send it in your first arguments so that first it connects to your saucelabs and then reads the capability object then it fetches the respective VM to execute the script.

Now you need to create an object of the Desired Capability class and set the required capabilities into the capability object i.e. platform as windows 7 and chrome version 51.0.

DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability("platform", "Windows 7");
cap.setCapability("version", "51.0");

So the script will looks like this given below:

public class SauceLabsTest {
public static void main(String[] args) {

DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability("platform", "Windows 7");
cap.setCapability("version", "51.0");

WebDriver driver = new RemoteWebDriver(new URL(), cap);
driver.get("http://google.com");
System.out.println(driver.getTitle());
}
}

Next step left only is to provide SauceLabs connection, so every user have their own connection so if you sign up with SauceLabs, it will generate an access key which will be unique for your account, with that access key you will be able to form an connection to a SauceLabs. You will get the access key in the User Settings page. You need to regenerate the new Access Key from there itself.

SauceLabs_cloud_network_2

Also, you need to add the SauceLabs connection URL to set up the connection. You will need to add this Driver Creation string to your script in order to create a new test automation session with SauceLabs.

SauceLabs_cloud_network_3

After adding all the SauceLabs details in the DesiredCapability connection arguments, the script will look like this.

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class SauceLabsTest {

public static final String USERNAME = "exampleHost";
public static final String ACCESS_KEY = "ab6b0f48-1a5d-499f-be4d-536f35207515";
public static final String URL = "http://" + USERNAME + ":" + ACCESS_KEY + "@ondemand.saucelabs.com:443/wd/hub";

public static void main(String[] args) {

DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability("platform", "Windows 7");
cap.setCapability("version", "51.0");

WebDriver driver = new RemoteWebDriver(new URL(URL), cap);
driver.get("http://google.com");
System.out.println(driver.getTitle());
}
}

Now, when you run the script, you will see the new saucelabs session running on the Automated tests which provides you the results, environment details on the Dashboard given below.
SauceLabs_Cloud_Network_4

If you navigate to the SauceLabs automated session, you can check the performance of the API events run during the scripting, their load time.

SauceLabs_cloud_network_5

You can check the logs of the session performed during the automated script.

SauceLabs_cloud_network_6

You can check the metadata of the session performed during the automated script.

SauceLabs_cloud_network_7

Request a quote