Running Selenium Scripts On LambdaTest Cloud | Automated Cross Browser Testing

In this post, we see how to run Selenium Scripts on LambdaTest Cloud Platform. Before that let’s learn why do we execute Selenium WebDriver tests on LambdaTest. In this post, we will learn the following.

Many times, we face a situation where we have to run selenium scripts with multiple version of browsers. There are always limitations to run on local machines. We cannot install thousands of browsers in our local machine to perform automated cross-browser testing. Remote execution in the cloud is the solution to overcome this limitation. LambdaTest Selenium Automation Grid is a cloud-based, scalable Selenium testing platform which enables users to run their automation scripts on more than 2000 browsers and operating system. In the earlier post (LambdaTest Review), we didn’t mention how to run Java Selenium automated test cases on LambdaTest cloud. Here in this article, we will be looking how to configure and run Java-based automation test scripts on LambdaTest Selenium cloud platform.

What is LambdaTest

LambdaTest is a Cross-Browser Testing Cloud, allows developers and testers to perform Cross Browser Testing on 2000+ Real Browsers and Operating System Online in varying screen resolutions. LambdaTest allows us to test on latest mobile and desktop browsers on the cloud.

Features of LambdaTest

  • It supports Continuous Testing with Continuous Integration tools such as Jenkins, Buildbot, Circle CI, Codeship, Continua, Cruise Control, Bamboo, GOCD, Solano CI, Teamcity, Travis CI

What is Cross Browser Testing

Cross Browser Testing is a type of non-functional test which helps us ensure that our website or web application works as expected in various web browsers. We could do Cross Browser Testing on different browsers both manual and automated way. To do Cross Browser Testing manually, we (Software Testers) create tests for each browser and execute it manually on each browser. To do it in an automated way, we could create Selenium tests with multiple conditional statements that execute test cases based on a specified browser type. While testing a website, we need to ensure that our website is appearing same across all the browsers. To do this we need to have all the browsers. Fortunately, there are some tools (e.g., CrossBrowserTesting, LambdaTest) to perform cross-browser testing without testing individually in a manual way.

Prerequisites to perform Cross Browser Testing Using Selenium

  1. LambdaTest Account — Here is a signup link

Run Selenium Tests on LambdaTest Cloud Platform

Step 1: Understanding LambdaTest’s Selenium Grid capabilities is very important in using LambdaTest Cloud platform. LambdaTest’s Selenium Grid uses remote webdriver instead of normal Selenium client browser driver. You have to invoke LambdaTest Selenium remote Webdriver. You have to specify details such as browser, browser version, OS and resolution you wish to run your test on, along with LambdaTest specific capabilities.

Step 2: Create a maven project — check this post for the same

Step 3: Copy the below code and Run Selenium Test on LambdaTest. The below code tests a simple to-do application.

Given clear explanation in the comments section within the program itself. Please go through it to understand the flow.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

package tests;

import java.net.MalformedURLException;

import java.net.URL;

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.remote.RemoteWebDriver;

public class JavaTodo {

String username = “YOUR_USERNAME”; // Add your username

String accesskey = “YOUR_ACCESS_KEY”; // Add your access key

static RemoteWebDriver driver = null;

String gridURL = “@hub.lambdatest.com/wd/hub”;

boolean status = false;

public static void main(String[] args) {

new JavaTodo().test();

}

public void test() {

// To Setup driver

setUp();

try {

//Change it to production page

driver.get(“https://lambdatest.github.io/sample-todo-app/");

//Let’s mark done first two items in the list.

driver.findElement(By.name(“li1”)).click();

driver.findElement(By.name(“li2”)).click();

// Let’s add an item in the list.

driver.findElement(By.id(“sampletodotext”)).sendKeys(“Yey, Let’s add it to list”);

driver.findElement(By.id(“addbutton”)).click();

// Let’s check that the item we added is added in the list.

String enteredText = driver.findElementByXPath(“/html/body/div/div/div/ul/li[6]/span”).getText();

if (enteredText.equals(“Yey, Let’s add it to list”)) {

status = true;

}

} catch (Exception e) {

System.out.println(e.getMessage());

} finally {

tearDown();

}

}

private void setUp() {

/*In this code, we are passing browser, browser version, and operating system information,

along with LambdaTest Selenium grid capabilities via capabilities object.

The capabilities object in the above code is defined as:*/

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(“browserName”, “chrome”);

capabilities.setCapability(“version”, “70.0”);

capabilities.setCapability(“platform”, “win10”); // If this cap isn’t specified, it will just get any available one.

capabilities.setCapability(“build”, “LambdaTestSampleApp”);

capabilities.setCapability(“name”, “LambdaTestJavaSample”);

capabilities.setCapability(“network”, true); // To enable network logs

capabilities.setCapability(“visual”, true); // To enable step by step screenshot

capabilities.setCapability(“video”, true); // To enable video recording

capabilities.setCapability(“console”, true); // To capture console logs

/*’browserName’, ‘version’, and ‘platform’ are the most important capabilities. These define which browser environment you wish to run the test on.

Rest of the capabilities are important in test management and debugging.*/

try {

/*if you are planning to run your scripts on Firefox browser in your local machine,

you would be using Firefox browser driver. i.e., FirefoxDriver driver = new FirefoxDriver();

However, to run your scripts on LambdaTest Selenium grid,

you would have to change it remote WebDriver and at the same time pass capabilities related to browser, browser versions etc.

It looks as shown below */

driver = new RemoteWebDriver(new URL(“https://” + username + “:” + accesskey + gridURL), capabilities);

} catch (MalformedURLException e) {

System.out.println(“Invalid grid URL”);

} catch (Exception e) {

System.out.println(e.getMessage());

}

}

private void tearDown() {

if (driver != null) {

((JavascriptExecutor) driver).executeScript(“lambda-status=” + status);

driver.quit(); //really important statement for preventing your test execution from a timeout.

}

}

}

Step 4: You have to generate Access token. Make a note of your User name and Access token and modify it in the above code.

Run Selenium Tests on Multiple Browsers in Parallel on LambdaTest

If you have more than one concurrent session, you can run your test cases on more than one machine at a time. This way you can save your test execution time a lot.

Copy the below code and Run Selenium Test on multiple browsers in parallel on LambdaTest

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

package tests;

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.Platform;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.remote.RemoteWebDriver;

import org.testng.Assert;

import org.testng.annotations.AfterClass;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.Test;

import java.net.MalformedURLException;

import java.net.URL;

public class TestNGTodo {

public String username = “YOUR_USERNAME”;

public String accesskey = “YOUR_ACCESS_KEY”;

public static RemoteWebDriver driver = null;

public String gridURL = “@hub.lambdatest.com/wd/hub”;

boolean status = false;

@BeforeClass

@org.testng.annotations.Parameters(value={“browser”,”version”,”platform”})

public void setUp() throws Exception {

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(“browserName”, “chrome”);

capabilities.setCapability(“version”, “70.0”);

capabilities.setCapability(“platform”, “win10”); // If this cap isn’t specified, it will just get the any available one

capabilities.setCapability(“build”, “LambdaTestSampleApp”);

capabilities.setCapability(“name”, “LambdaTestJavaSample”);

capabilities.setCapability(“network”, true); // To enable network logs

capabilities.setCapability(“visual”, true); // To enable step by step screenshot

capabilities.setCapability(“video”, true); // To enable video recording

capabilities.setCapability(“console”, true); // To capture console logs

try {

driver = new RemoteWebDriver(new URL(“https://” + username + “:” + accesskey + gridURL), capabilities);

} catch (MalformedURLException e) {

System.out.println(“Invalid grid URL”);

} catch (Exception e) {

System.out.println(e.getMessage());

}

}

@Test

public void testSimple() throws Exception {

try {

//Change it to production page

driver.get(“https://lambdatest.github.io/sample-todo-app/");

//Let’s mark done first two items in the list.

driver.findElement(By.name(“li1”)).click();

driver.findElement(By.name(“li2”)).click();

// Let’s add an item in the list.

driver.findElement(By.id(“sampletodotext”)).sendKeys(“Yey, Let’s add it to list”);

driver.findElement(By.id(“addbutton”)).click();

// Let’s check that the item we added is added in the list.

String enteredText = driver.findElementByXPath(“/html/body/div/div/div/ul/li[6]/span”).getText();

if (enteredText.equals(“Yey, Let’s add it to list”)) {

status = true;

}

} catch (Exception e) {

System.out.println(e.getMessage());

}

}

@AfterClass

public void tearDown() throws Exception {

if (driver != null) {

((JavascriptExecutor) driver).executeScript(“lambda-status=” + status);

driver.quit();

}

}

}

TestNG suite file:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<?xml version=”1.0" encoding=”UTF-8"?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd">

<suite thread-count=”3" name=”LambaTestSuite” parallel=”tests”>

<test name=”WIN8TEST”>

<parameter name=”browser” value=”firefox”/>

<parameter name=”version” value=”62.0"/>

<parameter name=”platform” value=”WIN8"/>

<classes>

<class name=”lambdatest.TestNGTodo “/>

</classes>

</test> <! — Test →

<test name=”WIN10TEST”>

<parameter name=”browser” value=”chrome”/>

<parameter name=”version” value=”70.0"/>

<parameter name=”platform” value=”WIN10"/>

<classes>

<class name=”lambdatest.TestNGTodo “/>

</classes>

</test> <! — Test →

<test name=”MACTEST”>

<parameter name=”browser” value=”safari”/>

<parameter name=”version” value=”11.0"/>

<parameter name=”platform” value=”macos 10.13"/>

<classes>

<class name=”lambdatest.TestNGTodo”/>

</classes>

</test> <! — Test →

</suite> <! — Suite →

You can run your test suite on multiple browsers with simple annotations and parameters of TestNG, without changing the browsers parameters in the script every time.

Conclusion:
Cross browser testing comes as part of our job that too running Selenium Scripts in parallel makes our life easy in terms of saving time. Have you tried running Selenium Scripts on LambdaTest cloud platform. Share your experience by commenting below in the comments section. If you like this post, share it with your friends.

Source : softwaretestingmaterial.com

Engineer by degree and Writer by choice.