The Talent500 Blog
lambda

Automate Browser Testing With LambdaTest

While transferring tests from a local Selenium Grid to a cloud-based Selenium Grid and assessing technologies to speed Selenium Testing, automation engineers are constantly concerned about the scope of the changes. This worry is alleviated by LambdaTest, which requires just “infrastructural modifications” in the source code. The concurrency calculator in LambdaTest is an excellent starting point for calculating the number of concurrent sessions required to provide the ‘best’ test coverage.

LambdaTest is a platform that is used to speed up cross-browser testing operations. It works with a variety of browsers, browser versions, and desktop computers, as well as mobile devices for Appium testing. It is really simple to create an account on LambdaTest, after which you must build the necessary browser and platform capabilities using the LambdaTest Capabilities Generator.

 

Automate Browser Testing With LambdaTest 1

Automation test results are available in LambdaTest’s Automation Dashboard. An overall test summary, exceptions encountered, console logs, network activity, and overall test information are all included in each test execution. The Automation Dashboard also includes a video clip of the testing procedure.

To execute the Test, below are the pre-requisites:

  • Account on LambdaTest
  • Java Development Kit (1.6 or higher)
  • Selenium Jars
  • TestNG Jars

To create a Maven project in IntelliJ IDEA, follow these steps:

Step 1: Go to File->New Project->Maven

Step 2: Select the Project SDK. The JDK is already installed on your machine.

Automate Browser Testing With LambdaTest 2

Step 3: As shown in the below image, put the required Artifact coordinates.

Automate Browser Testing With LambdaTest 3

Step 4: Once the project is created, we will make a .class file named GoogleSearchTest in the package named org.selenium4. The below image shows how the project structure will look after the creation of the file:

Automate Browser Testing With LambdaTest 4

Step 5: Insert the necessary project dependencies in pom.xml

FileName – pom.xml

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

 <project xmlns=”http://maven.apache.org/POM/4.0.0″

                xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

                xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

<modelVersion>4.0.0</modelVersion>

 <groupId>org.example</groupId>

<artifactId>org.selenium4.CrossBrowserTest</artifactId>

<version>1.0-SNAPSHOT</version>

 <properties>

               <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

 <dependencies>

              <!– https://mvnrepository.com/artifact/com.github.lambdatest/lambdatest-tunnel-binary –>

              <dependency>

                       <groupId>org.testng</groupId>

                       <artifactId>testng</artifactId>

                       <version>6.9.10</version>

                       <scope>test</scope>

              </dependency>

              <dependency>

                       <groupId>org.seleniumhq.selenium</groupId>

                       <artifactId>selenium-java</artifactId>

                       <version>4.0.0-alpha-6</version>

              </dependency>

              <dependency>

                       <groupId>org.seleniumhq.selenium</groupId>

                       <artifactId>selenium-chrome-driver</artifactId>

                       <version>4.0.0-alpha-6</version>

               </dependency>

               <dependency>

                       <groupId>io.github.bonigarcia</groupId>

                       <artifactId>webdrivermanager</artifactId>

                       <version>4.1.0</version>

                </dependency>

               <!– https://mvnrepository.com/artifact/org.slf4j/slf4j-nop –>

               <dependency>

                        <groupId>org.slf4j</groupId>

                        <artifactId>slf4j-nop</artifactId>

                        <version>1.7.28</version>

                        <scope>test</scope>

                </dependency>

               <!– https://mvnrepository.com/artifact/junit/junit –>

               <dependency>

                        <groupId>junit</groupId>

                        <artifactId>junit</artifactId>

                        <version>4.13</version>

                        <scope>test</scope>

                </dependency>

                <dependency>

                         <groupId>com.github.lambdatest</groupId>

                         <artifactId>lambdatest-tunnel-binary</artifactId>

                         <version>1.0.4</version>

                 </dependency>

 </dependencies>

 <build>

       <defaultGoal>install</defaultGoal>

       <plugins>

             <plugin>

                    <artifactId>maven-compiler-plugin</artifactId>

                    <version>3.0</version>

                    <configuration>

                          <source>1.8</source>

                          <target>1.8</target>

                    </configuration>

              </plugin>

              <plugin>

                    <groupId>org.apache.maven.plugins</groupId>

                    <artifactId>maven-surefire-plugin</artifactId>

                    <version>2.12.4</version>

                    <configuration>

                          <suiteXmlFiles>

                                 <!– TestNG suite XML files –>

                                 <suiteXmlFile>testng.xml</suiteXmlFile>

                          </suiteXmlFiles>

                     </configuration>

              </plugin>

              <plugin>

                      <groupId>org.apache.maven.plugins</groupId>

                      <artifactId>maven-surefire-report-plugin</artifactId>

                      <version>3.0.0-M5</version>

              </plugin>

         </plugins>

    </build>

</project>

Step 6: Insert the code below into GoogleSearchTest.java.
FileName – GoogleSearchTest.java

package org.selenium4;

import org.junit.Assert;

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.remote.RemoteWebDriver;

import org.testng.IExecutionListener;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

import java.net.MalformedURLException;

import java.net.URL;

public class GoogleSearchTest implements IExecutionListener {

        String URL = “https://www.google.com/”;

        String search_string = “Talent500”;

        WebDriver driver = null;

        WebElement search_box;

        String exp_title = “Talent 500”;

        public static String status = “passed”;

        // Update the username and access-key values with your fetched username and access key

        String username = “user-name”;

        String access_key = “access-key”;

        // You can also store them in your System variables and fetch them from there using below command

       // String username = System.getenv(“LT_USERNAME”);

       // String authkey = System.getenv(“LT_ACCESS_KEY”);

       @Override

      public void onExecutionStart() {

               System.out.println(“onExecutionStart”);

      }

      @BeforeTest

     @Parameters(value={“browser”,”version”,”platform”, “resolution”})

      public void testSetUp(String browser, String version, String platform, String resolution) throws Exception {

            DesiredCapabilities capabilities = new DesiredCapabilities();

            capabilities.setCapability(“build”, “Google Search – Talent500 using LambdaTest”);

            capabilities.setCapability(“name”, “Google Search – Talent500 using LambdaTest”);

            capabilities.setCapability(“platform”, platform);

            capabilities.setCapability(“browserName”, browser);

            capabilities.setCapability(“version”,version);

            capabilities.setCapability(“tunnel”,false);

            capabilities.setCapability(“network”,true);

            capabilities.setCapability(“console”,true);

            capabilities.setCapability(“visual”,true);

            try {

                 driver = new RemoteWebDriver(new URL(“http://” + username + “:” + access_key + “@hub.lambdatest.com/wd/hub”), capabilities);

             } catch (MalformedURLException e) {

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

             }

            System.out.println(“Started session”);

     }

    @Test

    public void test_Selenium4_GoogleSearch() throws InterruptedException {

           driver.navigate().to(URL);

           driver.manage().window().maximize();

           try {

                   /* Enter the search term in the Google Search Box */

                  search_box = driver.findElement(By.xpath(“//input[@name=’q’]”));

                  search_box.sendKeys(search_string);

                  search_box.submit();

                  /* driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); */

                  /* Not a good programming practice, added for demonstration */

                  Thread.sleep(3000);

                  /* Click on the first result which will open up the LambdaTest homepage */

                  WebElement lt_link = driver.findElement(By.xpath(“//span[.=’Talent 500 …’]”));

                  lt_link.click();

                  /* driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); */

                  /* Not a good programming practice, added for demonstration */

                  Thread.sleep(5000);

                  String curr_window_title = driver.getTitle();

                  Assert.assertEquals(curr_window_title, exp_title);

           } catch (Exception e) {

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

            }

     }

    @AfterTest

    public void tearDown() {

        if (driver != null) {

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

             driver.quit();

        }

    }

    @Override

        public void onExecutionFinish() {

               System.out.println(“onExecutionFinish”);

    }

}

Step 7: Because we are utilising the TestNG framework, we utilise testng.xml to organise the tests. We used the same method to create test suites and tests.

The browser and operating system combinations against which we must execute a Google search for ‘talent500’ are specified in testng.xml.

FileName – testng.xml

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

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

<suite name=”Jenkins Maven Integration Tests” thread-count=”5″ parallel=”tests”>

<!– Add the location where the project is stored in your machine –>

<listeners>

         <listener class-name=”org.selenium4.GoogleSearchTest”/>

</listeners>

<test name=”Google Search Test – 1″>

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

        <parameter name=”version” value=”latest”/>

        <parameter name=”platform” value=”Windows 10″/>

        <parameter name=”resolution” value=”1600×1200″/>

        <classes>

              <class name=”org.selenium4.GoogleSearchTest”/>

        </classes>

</test>

 <test name=”Google Search Test – 2″>

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

        <parameter name=”version” value=”84.0″/>

        <parameter name=”platform” value=”Windows 10″/>

        <parameter name=”resolution” value=”1920×1080″/>

        <classes>

               <class name=”org.selenium4.GoogleSearchTest”/>

        </classes>

</test>

 <test name=”Google Search Test – 3″>

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

       <parameter name=”version” value=”latest”/>

       <parameter name=”platform” value=”macOS Mojave”/>

       <parameter name=”resolution” value=”2048×1536″/>

       <classes>

             <class name=”org.selenium4.GoogleSearchTest”/>

       </classes>

</test>

 <test name=”Google Search Test – 4″>

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

       <parameter name=”version” value=”latest”/>

       <parameter name=”platform” value=”OS X Mavericks”/>

       <parameter name=”resolution” value=”2560×1440″/>

       <classes>

              <class name=”org.selenium4.GoogleSearchTest”/>

       </classes>

</test>

</suite>

In testng.xml, the Thread Count is set to ‘5’. This implies that we can only run a maximum of 5 tests on the Selenium Grid at the same time. It is crucial to remember, however, that the number of concurrent sessions on LambdaTest is determined by the plan chosen.

Step 8: The LambdaTest capabilities generator is used to produce the browser capabilities. While defining the browser capabilities, we must pick ‘Java’ as the language. The “visual” field of browser capabilities is set to “Yes” since we want to record the whole test execution.

Because the tests will be run on a distant Grid, we must utilise Remote Selenium WebDriver instead of the local Selenium WebDriver. The LambdaTest Selenium Grid is accessed via a login and access-key combination (accessible in the LambdaTest profile area).

driver = new RemoteWebDriver(new URL(“http://” + username + “:” + access_key + “@hub.lambdatest.com/wd/hub”), capabilities);

…………………………..

…………………………..

…………………………..

driver.navigate().to(URL);

driver.manage().window().maximize();

With everything in place, we are ready to run the tests on the Selenium Grid. To run the tests, right-click on testng.xml and select “Run…testng.xml,” as shown below.

Automate Browser Testing With LambdaTest 5

To examine the execution status of a test on LambdaTest, navigate to the Automation Dashboard and pick the test(s) whose status you wish to monitor. Because our plan enables ‘5’ concurrent sessions, all 5 test scenarios are conducted in parallel on the LambdaTest Selenium Grid.

Automate Browser Testing With LambdaTest 6

We could upgrade our LambdaTest account so the test team members can run more concurrent sessions on the LambdaTest Selenium Grid.

Step 10:Choose the test and examine the logs, exceptions, and so on to verify the activities taken throughout the test. You may also watch the “execution video” to see how the test execution went.

Automate Browser Testing With LambdaTest 7

The following is the final execution snapshot from IntelliJ IDEA and LambdaTest, which confirms that the tests were successfully executed:

Automate Browser Testing With LambdaTest 8

Automate Browser Testing With LambdaTest 9

We have just shown how the benefits of Parallel Testing in Selenium may be expanded by shifting the test execution to a cloud-based Selenium Grid by LambdaTest. As a developer, you have the option of leveraging the benefits by using the programming language and test automation framework offered by LambdaTest.

Manual Testing With LambdaTest

Here are different methods for performing manual cross-browser testing using LambdaTest:

#1) Real-Time Browser Testing

Real-time testing on LambdaTest, as the name implies, is useful for testing your website or web app across many browsers, operating systems, and device emulators.

Along with major browsers like Chrome, Firefox, and Microsoft Edge, you may also test on browsers like Internet Explorer, Opera, Yandex, and others.

Get started with LambdaTest real-time testing,

Step 1: Go to LambdaTest real-time test and select the configuration on which you wish to perform testing. 

Step 2: Select the device type (like mobile or desktop), browsers (like Firefox, Internet Explorer, Chrome, etc.), display resolution and browser version.

Automate Browser Testing With LambdaTest 10

Step 3: When running a real-time test, you may change the test setup, save screenshots to the gallery, record a video of the test, and more.

Automate Browser Testing With LambdaTest 11

#2) Visual User Interface (UI) Testing

Visual bugs may be a huge turn-off for an online product’s clients or audience. There is a chance that the UI may appear good on a few browser and OS combinations but will look out of place on others. Responsive design testing is also an important aspect of visual UI testing since it allows you to evaluate the responsiveness of the UI on various platforms, browsers, and screen sizes.

LambdaTest may be used to test the following sorts of visual user interfaces:

  1. a) Screenshot Testing

It allows you to record screenshots for a variety of browsers and devices. To execute the test, input the test URL and pick the test setup as in real-time testing. You may also test your website with old browsers such as Internet Explorer.

Automate Browser Testing With LambdaTest 12

You will also receive an email notification after the screenshots have been created, allowing you to save the screenshots for future use.

Automate Browser Testing With LambdaTest 13

b) Responsive Testing

Responsive Testing on LambdaTest is excellent for determining the responsiveness of the UI across various devices and viewports. The responsiveness may be tested in Landscape and Portrait modes.

Automate Browser Testing With LambdaTest 14

If you can’t locate the DeviceName in the ‘device list,’ you may add a custom viewport size on which to run responsive testing. If you encounter a problem with the responsiveness of your website, mark it as a bug so that your team members are aware of the problem.

Automate Browser Testing With LambdaTest 15

There is an opportunity to designate a specific location as a Bug and provide comments so that the developers are aware of the problem right away.

Automate Browser Testing With LambdaTest 16

Conclusion

If the goal is to test the code against a large number of browser and operating system combinations, test execution on a local Selenium Grid might become difficult. To avoid the expenditures associated with Selenium Grid’s ongoing maintenance and upgrade, adopting a cloud-based Selenium Grid is a more viable and cost-effective choice.

LambdaTest offers a cloud-based Selenium Grid that is scalable and secure, and it supports a wide range of programming languages and test automation frameworks. QA automation engineers may use LambdaTest to test their code against 2,000+ browser, platform, and device permutations.

Besides scalability and performance, LambdaTest’s great customer service, expanding LambdaTest community, and in-depth technical LambdaTest blogs help you expedite your testing efforts.

 

0
Afreen Khalfe

Afreen Khalfe

A professional writer and graphic design expert. She loves writing about technology trends, web development, coding, and much more. A strong lady who loves to sit around nature and hear nature’s sound.

Add comment