<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vipulgupta9090</title>
    <description>The latest articles on DEV Community by Vipulgupta9090 (@vipulgupta9090).</description>
    <link>https://dev.to/vipulgupta9090</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F921105%2F387f5670-9741-4495-9604-071323a89d6e.png</url>
      <title>DEV Community: Vipulgupta9090</title>
      <link>https://dev.to/vipulgupta9090</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vipulgupta9090"/>
    <language>en</language>
    <item>
      <title>How To Write Test Cases in Java</title>
      <dc:creator>Vipulgupta9090</dc:creator>
      <pubDate>Mon, 12 Aug 2024 09:00:10 +0000</pubDate>
      <link>https://dev.to/vipulgupta9090/how-to-write-test-cases-in-java-3chd</link>
      <guid>https://dev.to/vipulgupta9090/how-to-write-test-cases-in-java-3chd</guid>
      <description>&lt;p&gt;Writing test cases is a fundamental skill for ensuring the reliability and robustness of your code. Test cases can help identify bugs and validate that the code works as intended.&lt;/p&gt;

&lt;p&gt;Test cases can be written using any programming language like Java, Python, C#, and more. However, many testers prefer writing test cases in Java due to its strong community support and alignment with project needs. Automating these test cases in Java helps testers save execution time and catch errors in the early stages of testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is a Test Case in Java?
&lt;/h2&gt;

&lt;p&gt;Java is one of the most used programming languages for automation testing. Writing a &lt;a href="https://www.lambdatest.com/learning-hub/test-case?utm_source=medium&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_12&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;test case&lt;/a&gt; in Java means building an entire automation project that follows and implements OOP concepts. This makes the project and test case more robust, and the whole code is categorized into many classes and methods, making it more maintainable and easy to use.&lt;/p&gt;

&lt;p&gt;Automated test cases help to test the feature quickly in case of regression and help save a lot of manual effort. These test cases execute some flow on the webpage and then use asserts to verify if the output matches the desired output.&lt;/p&gt;

&lt;p&gt;For example, For the Two Input Fields section on the &lt;a href="https://www.lambdatest.com/selenium-playground/simple-form-demo?utm_source=medium&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_12&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Selenium Playground&lt;/a&gt; page, a few of the test cases would look like the below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Verify all the labels, text input boxes, and buttons are present on the page.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4rvfjIvv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2800/0%2A5juEvqCzxfOOVSlO.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4rvfjIvv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2800/0%2A5juEvqCzxfOOVSlO.png" width="800" height="246"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Verify that the sum is displayed correctly under Result when entering both numbers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xz4SvTgN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2800/0%2AengaFCchLOyoVLqD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xz4SvTgN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2800/0%2AengaFCchLOyoVLqD.png" width="800" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Verify that the error message is displayed under Result if one or both numbers are not entered.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X2eCf-vS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2800/0%2ArdNoiEjQ5mON0cQL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X2eCf-vS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2800/0%2ArdNoiEjQ5mON0cQL.png" width="800" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While writing test cases in Java, several things need to be considered, like the test flow, the test scope, naming, assertions, test data, grouping, locating WebElements, etc. When used accurately, all these make a good test case that is easy to understand and maintain in the future as more changes come.&lt;/p&gt;

&lt;p&gt;However, the initial action to start with the test flow is to locate a WebElement for which you must write an effective test case in Java.&lt;/p&gt;

&lt;h2&gt;
  
  
  Locating WebElements in Java
&lt;/h2&gt;

&lt;p&gt;Another thing to learn about writing test cases in Java is locating the WebElements. This is required as the test case flow will interact with these WebElements to perform actions like entering data, clicking a button, retrieving data, etc., to get the results. There are multiple ways to find the WebElement locators in Selenium, such as &lt;em&gt;ID, Name, CSS, XPath, linkText,&lt;/em&gt; etc.&lt;/p&gt;

&lt;p&gt;Before demonstrating how to write test cases in Java, let’s learn how different WebElements used in the test flow can be located using their locator strategies. We will use &lt;em&gt;XPath&lt;/em&gt; or &lt;em&gt;ID&lt;/em&gt; locators to locate all the WebElements for this test case.&lt;/p&gt;

&lt;p&gt;You can use one of the most common methods to find elements, i.e., &lt;em&gt;findElement()&lt;/em&gt; in Selenium.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VcpX9Cb3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2ArwganeiecFMo1KJL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VcpX9Cb3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2ArwganeiecFMo1KJL.png" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To locate the &lt;strong&gt;Two Input Fields&lt;/strong&gt; label from the LambdaTest Selenium Playground page using XPath, you can use the following XPath expression:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(By.xpath("//*[contains(text(),'Two')]"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To locate the &lt;strong&gt;Enter first value&lt;/strong&gt; label from the LambdaTest Selenium Playground page using XPath, you can use the following XPath expression:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(By.xpath("//*[contains(text(),'first value')]"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To locate the &lt;strong&gt;Enter second value&lt;/strong&gt; label from the LambdaTest Selenium Playground page using XPath, you can use the following XPath expression:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(By.xpath("//*[contains(text(),'first value')]"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To locate the &lt;strong&gt;Get Sum&lt;/strong&gt; button from the LambdaTest Selenium Playground page using XPath, you can use the following XPath expression:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(By.xpath("//*[contains(text(),'Sum')]"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To locate the &lt;strong&gt;First value&lt;/strong&gt; textbox from the LambdaTest Selenium Playground page using ID, you can use the following ID expression:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(By.id("sum1"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To locate the &lt;strong&gt;Second value&lt;/strong&gt; textbox from the LambdaTest Selenium Playground page using ID, you can use the following ID expression:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(By.id("sum2"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To locate the &lt;strong&gt;Result&lt;/strong&gt; label from the LambdaTest Selenium Playground page using ID, you can use the following ID expression:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(By.id("user-message"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To locate the &lt;strong&gt;Result/Error Message&lt;/strong&gt; from the LambdaTest Selenium Playground page using ID, you can use the following ID expression:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(By.id("addmessage"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also refer to this &lt;a href="https://www.lambdatest.com/learning-hub/selenium-locators?utm_source=medium&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_12&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium locator&lt;/a&gt; guide to learn how to locate WebElements when writing test cases in Java.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mRsU960z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2A-XspLQZcDHVkxDkq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mRsU960z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2A-XspLQZcDHVkxDkq.png" alt="*By Louise J Gibbs*" width="624" height="441"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How To Write a Test Case in Java?
&lt;/h2&gt;

&lt;p&gt;Before writing an effective test case in Java, you must set up the project with all the required libraries and tools. We will use Eclipse IDE to demonstrate the setup for a Maven project. Additionally, we will add TestNG as the testing framework to handle test execution and implement setup and teardown methods.&lt;/p&gt;

&lt;p&gt;In this demonstration, we will initially write 3 test cases to cover all the test cases in Java mentioned and implement all the best practices as applicable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Scenario 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to the Two Input Fields section on the Selenium Playgrounds (Simple Form Demo)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Assert all the WebElement of this section for visibility&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Identify the locator for the &lt;strong&gt;Two Input Fields&lt;/strong&gt; label.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Identify the locator for the **first **and **second **value labels.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Identify the locator for the **first **and **second **value input boxes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Identify the locator for the &lt;strong&gt;Get Sum&lt;/strong&gt; button.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt; label.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Test Scenario 2 :&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to the &lt;strong&gt;Two Input Fields&lt;/strong&gt; section on the Selenium Playgrounds (Simple Form Demo) page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter valid values in both input boxes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the &lt;strong&gt;Get Sum&lt;/strong&gt; button.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Assert the sum is correct and displayed under the **Result **label.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Test Scenario 3 :&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to the &lt;strong&gt;Two Input Fields&lt;/strong&gt; section on the Selenium Playgrounds (Simple Form Demo) page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter the value in any one of the input boxes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the &lt;strong&gt;Get Sum&lt;/strong&gt; button.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The assert error message is displayed under the &lt;strong&gt;Result&lt;/strong&gt; label.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s start installing the necessary libraries as we have the test flow in place and are ready to use the tools and libraries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project Setup:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As mentioned, we will use Eclipse IDE to write the test case in Java. However, you can use any IDE to write the test cases. You just need to create a new Maven project.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Launch Eclipse IDE and open the New Project wizard by clicking File &amp;gt; New &amp;gt; Project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Under Wizards, expand Maven, select Maven Project, and click Next.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on Create a simple project checkbox and click on Next.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On this New Maven Project wizard, enter the Group Id and Artifact Id and click on Finish to create your project. Here, Group Id is the unique name that helps identify one project group from another, and Artifact Id is the unique name given to the project we will create.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once the project is created, the project structure will include a pom.xml file in the root folder. Here, we will add the dependencies needed to write and execute automation test cases in Java. We will add its dependency since we will use TestNG as the testing framework. Additionally, we will add the Selenium dependency, as the test case we will write involves web automation.&lt;/p&gt;

&lt;p&gt;You can fetch the latest TestNG and Selenium dependencies from the &lt;a href="https://mvnrepository.com/" rel="noopener noreferrer"&gt;Maven Repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The updated pom.xml would look like the one below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;lt;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 https://maven.apache.org/xsd/maven-4.0.0.xsd"&amp;gt;
     &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
     &amp;lt;groupId&amp;gt;demo.lambdaTest&amp;lt;/groupId&amp;gt;
     &amp;lt;artifactId&amp;gt;JavaTestCase&amp;lt;/artifactId&amp;gt;
     &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;
     &amp;lt;dependencies&amp;gt;
       &amp;lt;dependency&amp;gt;
         &amp;lt;groupId&amp;gt;org.testng&amp;lt;/groupId&amp;gt;
         &amp;lt;artifactId&amp;gt;testng&amp;lt;/artifactId&amp;gt;
         &amp;lt;version&amp;gt;7.10.1&amp;lt;/version&amp;gt;
         &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
       &amp;lt;/dependency&amp;gt;
       &amp;lt;dependency&amp;gt;
         &amp;lt;groupId&amp;gt;org.seleniumhq.selenium&amp;lt;/groupId&amp;gt;
         &amp;lt;artifactId&amp;gt;selenium-java&amp;lt;/artifactId&amp;gt;
         &amp;lt;version&amp;gt;4.21.0&amp;lt;/version&amp;gt;
       &amp;lt;/dependency&amp;gt;
     &amp;lt;/dependencies&amp;gt;
    &amp;lt;/project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step is to add the test class files where we will write the test case in Java. Add 2 Java class files under src/test/java package.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;BaseTest.java:&lt;/strong&gt; This file will have the &lt;em&gt;setup()&lt;/em&gt; and &lt;em&gt;tearDown()&lt;/em&gt; methods to create and remove the *WebDriver *instances for test cases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**TwoInputFieldTests.java: **This file will be the test class file where all the test cases discussed above will be written.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The completed BaseTest.java would look like the one below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package LocalGrid;


    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.*;


    public class BaseTest {
       public static WebDriver driver;

       @BeforeMethod
       public void setup()
       {
           driver = new ChromeDriver();

           System.out.println("Navigating to Two Input Field section");
           driver.get("https://www.lambdatest.com/selenium-playground/simple-form-demo");
       }

       @AfterMethod
       public void tearDown()
       {
           driver.quit();
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Below is the complete code walkthrough to understand the code written in the above BaseTest.java file.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In this class, create a &lt;em&gt;WebDriver&lt;/em&gt; instance, which will be used to interact with the browser, then create the first method as &lt;em&gt;setup()&lt;/em&gt; and annotate it with the &lt;em&gt;@BeforeMethod. @BeforeMethod&lt;/em&gt; is a &lt;a href="https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/?utm_source=medium&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_12&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;TestNG&lt;/a&gt; annotation. This annotation helps invoke this method before executing each test method, i.e., each test case. This will help create a new &lt;em&gt;WebDriver&lt;/em&gt; instance for each test case in Java and navigate to the website.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aG212wjg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2AqnTlTgd367587xLE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aG212wjg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2AqnTlTgd367587xLE.png" width="800" height="349"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialize the &lt;em&gt;driver&lt;/em&gt; variable with the &lt;em&gt;ChromeDriver()&lt;/em&gt; class. This will execute all the test cases in Java on the Chrome browser.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---JhyAdbF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2A-HNKgYyNebG7KheC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---JhyAdbF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2A-HNKgYyNebG7KheC.png" width="800" height="214"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use the &lt;em&gt;get()&lt;/em&gt; method of Selenium WebDriver to navigate to the test webpage on LambdaTest Playground.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VJgrdkVk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2800/0%2Ajv6FHhyaHHsac_ff.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VJgrdkVk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2800/0%2Ajv6FHhyaHHsac_ff.png" width="800" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the next method, &lt;em&gt;tearDown()&lt;/em&gt;, and annotate it with &lt;em&gt;@AfterMethod&lt;/em&gt; annotation to execute it after each test case in Java is executed. &lt;em&gt;@AfterMethod&lt;/em&gt; is called the &lt;em&gt;quit()&lt;/em&gt; method, which quits the driver instance and closes the open browser windows.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--shi7cpKi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2Azdpjn_5xlLbqSW7T.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--shi7cpKi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2Azdpjn_5xlLbqSW7T.png" width="800" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this, the BaseTest file is created, and we are ready to create a test file to run the test case in Java.&lt;/p&gt;

&lt;p&gt;Create a file called TwoInputFieldTests.java. This file will contain all the code for the test scenarios discussed earlier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package LocalGrid;


    import org.openqa.selenium.By;
    import org.testng.Assert;
    import org.testng.annotations.Test;


    public class TwoInputFieldTests extends BaseTest
    {


       @Test(groups = {"uiTest"})
       public void shouldVerifyAllElementsWhenThePageIsLoaded()
       {
           System.out.println("Verify Two Input Fields title is displayed.");
           Assert.assertTrue(driver.findElement(By.xpath("//*[contains(text(),'Two')]")).isDisplayed());

           System.out.println("Verify enter first value label is displayed.");
           Assert.assertTrue(driver.findElement(By.xpath("//*[contains(text(),'first value')]")).isDisplayed());

           System.out.println("Verify enter first value textbox is displayed.");
           Assert.assertTrue(driver.findElement(By.id("sum1")).isDisplayed());

           System.out.println("Verify enter second value label is displayed.");
           Assert.assertTrue(driver.findElement(By.xpath("//*[contains(text(),'second value')]")).isDisplayed());

           System.out.println("Verify enter second value textbox is displayed.");
           Assert.assertTrue(driver.findElement(By.id("sum2")).isDisplayed());

           System.out.println("Verify Get Sum button is displayed.");
           Assert.assertTrue(driver.findElement(By.xpath("//*[contains(text(),'Sum')]")).isDisplayed());

           System.out.println("Verify result label is displayed.");
           Assert.assertTrue(driver.findElement(By.id("user-message")).isDisplayed());
       }

       @Test(groups = {"positiveTest", "sanity"})
       public void shouldReturnTheResultWhenBothNumbersAreValid()
       {
           System.out.println("entering first value");
           driver.findElement(By.id("sum1")).sendKeys("5");

           System.out.println("entering second value");
           driver.findElement(By.id("sum2")).sendKeys("10");

           System.out.println("click on sum");
           driver.findElement(By.xpath("//*[contains(text(),'Sum')]")).click();

           System.out.println("fetch the result and assert it");
           String result =  driver.findElement(By.id("addmessage")).getText();
           Assert.assertEquals(result, "15", "Sum is incorrect. Expected : 15 but Found : " + result);
       }

       @Test(groups = {"errorTest", "sanity"})
       public void shouldShowErrorWhenAnyNumberIsMissing()
       {
           System.out.println("entering first value");
           driver.findElement(By.id("sum1")).sendKeys("5");

           System.out.println("click on sum");
           driver.findElement(By.xpath("//*[contains(text(),'Sum')]")).click();

           System.out.println("fetch the error message and assert it");
           String errorMessage =  driver.findElement(By.id("addmessage")).getText();
           String expectedError = "Entered value is not a number";
           Assert.assertEquals(errorMessage, expectedError, "Incorrect error message. Expected : " + expectedError + " but Found : " + errorMessage);
       }  
    }
``

**Code Walkthrough:**

Below is the complete code walkthrough to understand the code written in the above TwoInputFieldTests.java file.

 1. Add a new class file as TwoInputFieldTests.java and extend BaseTest. This helps to inherit and access the *driver* variable and methods of the base class in all the test cases and prevents redundant code.

![](https://cdn-images-1.medium.com/max/2000/0*JZL0nPf0UImC1Id0.png)

 1. Add the first test case as *shouldVerifyAllElementsWhenThePageIsLoaded()* and *annotate* it with the *@Test* annotation. This annotation helps to identify a method as a test case and execute it as a TestNG case. We have used the *groups* attribute of *@Test* annotation here to mark this test as part of the *uiTests* group so that it gets executed whenever these group cases are triggered.

![](https://cdn-images-1.medium.com/max/2000/0*GlX1JPNMuoLofOo8.png)

 1. In this test case, we verify if all the Two Input Fields section elements are displayed when the web page is loaded.

![](https://cdn-images-1.medium.com/max/2800/0*EA4iLasTcTUWdlcY.png)

 1. For this, we use the *isDisplayed()* method of WebElement to check whether the element is displayed. This returns a boolean *true* value if the element is displayed and *false* if not.

![](https://cdn-images-1.medium.com/max/2366/0*RiQQJN2v6STGNyrO.png)

 1. To assert the same, we use the *assertTrue()* method of the *Assert* class. It passes if the passed value is true, meaning the element is displayed.

![](https://cdn-images-1.medium.com/max/2800/0*rZzpUe7-pAyduRlI.png)

Assertions are crucial in the automation testing process, ensuring the software functions as expected. To learn assertTrue(), follow this guide on assertTrue() in Java and gain valuable insights.

 1. Write a similar assert statement using the *isDisplayed()* method for all the web elements to verify the entire UI of this section.

![](https://cdn-images-1.medium.com/max/2800/0*lpbYo28S0JkC_7DB.png)

 1. Add the second test case as *shouldReturnTheResultWhenBothNumbersAreValid*(). Similar to the previous case, annotate it with the *@Test* annotation and add it to *positiveTest* and *sanity* groups. We have added 2 group names here, unlike 1 in the previous case. This means that this test case in Java will get executed as part of both these groups.

![](https://cdn-images-1.medium.com/max/2000/0*2QKhpdKdnPFDab3r.png)

 1. Enter the first and second values using the *sendKeys()* method. This method of Selenium WebDriver is used to enter data in any input text box field while working with web automation.

![](https://cdn-images-1.medium.com/max/2000/0*RQ-l0HoIOoD2z5p0.png)

 1. After entering the values, click the **Get Sum** button to get the result. For this, another method of WebDriver, *click()* is used.

![](https://cdn-images-1.medium.com/max/2224/0*IbS3nRVYVVh7Lw-e.png)

 1. Finally, we fetch the result using the *getText()* method and store it in a variable. This method retrieves any text value from the web page using Selenium web automation. Assert’s a*ssertEquals()* method compares this value with the expected result.

![](https://cdn-images-1.medium.com/max/2800/0*a-ZWuMt-_8RLHy0U.png)

This method is used to compare two values as part of an assertion. Learn how to use asserts in TestNG correctly to help you verify and validate your actual and expected results.

 1. Add the last test case and name it as *shouldShowErrorWhenAnyNumberIsMissing()*. Like the previous cases, annotate it with *@Test* and add it to the *negativeTest *and *sanity *groups.

![](https://cdn-images-1.medium.com/max/2000/0*I4LECAw3YERpc_zK.png)

 1. Enter any one value using the *sendKeys()* method. Here, we are entering the first value field.

![](https://cdn-images-1.medium.com/max/2000/0*yVfcS0EUO-Ecb_j4.png)

 1. Click on the **Get Sum** button.

![](https://cdn-images-1.medium.com/max/2198/0*BuB7-eQ6QLzHw4Y0.png)

 1. Fetch the error message using the *getText()* method and assert it to be the same as the expected error message using the *assertEquals()* method.

![](https://cdn-images-1.medium.com/max/2800/0*OC7FyNMPWW1D0niF.png)

**Test Execution And Results**

Right-click &amp;gt; Select Run As &amp;gt; TestNG, Test to execute the tests individually.

![](https://cdn-images-1.medium.com/max/2712/0*wSRnKyaBsNhsuBKo.png)

**Result:**

*shouldVerifyAllElementsWhenThePageIsLoaded()*

![](https://cdn-images-1.medium.com/max/2800/0*i-g-dOOvHCSm_IWt.png)

**Result:**

*shouldReturnTheResultWhenBothNumbersAreValid()*

![](https://cdn-images-1.medium.com/max/2800/0*Z1N0ZkDC5QbeFzeV.png)

**Result:**
*shouldShowErrorWhenAnyNumberIsMissing()*

![](https://cdn-images-1.medium.com/max/2800/0*o5xzpQYYwZIts82P.png)

Running tests locally is manageable when working with up to three or four browsers and one operating system. However, this limitation makes it difficult for testers to conduct tests on more than four browsers and multiple operating systems. To address this, testers can use a cloud-based platform where they do not have to worry about maintaining test infrastructure and can conduct comprehensive cross-browser and cross-platform testing seamlessly. One such platform is LambdaTest.

It is an AI-powered test execution platform that lets you run manual and automated [cross-browser testing](https://www.lambdatest.com/cross-browser-testing?utm_source=medium&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_12&amp;amp;utm_term=rj&amp;amp;utm_content=blog) at scale, with over 3000+ real devices, browsers, and OS combinations.

This platform provides the speed and resources to make test executions faster and more reliable. It facilitates parallel execution, allowing multiple tests to run simultaneously for large automation suites. This cloud grid platform also offers enhanced logging, maintenance, and debugging resources.

Now, let’s move the local test script to the LambdaTest platform with a few modifications to leverage all the features this cloud grid offers.

## How To Write a Test Case in Java for Cloud Execution?

Having understood the flow of writing and executing the first test case in Java using Selenium and TestNG, let us move ahead and learn how we can do the same on a cloud grid platform.

To use the LambdaTest cloud grid to run the Selenium automation test cases, we need to make some changes to the code to use Selenium *RemoteWebDriver*, which will help connect to the LambdaTest cloud grid and execute the cases over there.

Most of these changes involve configuring the WebDriver settings in BaseTest.java. The modified BaseTest.java with the cloud grid integration will look similar to the example below.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package CLoudGRid;


import java.net.*;
import java.util.HashMap;


import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.*;


public class BaseTest {
   public RemoteWebDriver driver = null;


   String username = System.getenv("LT_USERNAME") == null ? "&amp;lt;lambdatest_username&amp;gt;" : System.getenv("LT_USERNAME");
   String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "&amp;lt;lambdatest_accesskey&amp;gt;" : System.getenv("LT_ACCESS_KEY");

   String status = "failed";


   @BeforeMethod
   public void setup() {
       try {
           ChromeOptions chromeOptions = new ChromeOptions();
           chromeOptions.setPlatformName("Windows 10");
           chromeOptions.setBrowserVersion("125");


           HashMap&amp;lt;String, Object&amp;gt; ltOptions = new HashMap&amp;lt;String, Object&amp;gt;();
           ltOptions.put("build", "Test case in Java");
           ltOptions.put("name", "Test case in Java");
           ltOptions.put("w3c", true);
           chromeOptions.setCapability("LT:Options", ltOptions);


           driver = new RemoteWebDriver(
                   new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), chromeOptions);


           System.out.println("Navigating to Two Input Field section");
           driver.get("https://www.lambdatest.com/selenium-playground/simple-form-demo");


       } catch (MalformedURLException e) {
           e.printStackTrace();
       }
   }


   @AfterMethod
   public void tearDown() {
       driver.executeScript("lambda-status=" + status);
       driver.quit();
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


**Code Walkthrough:**

Below is the complete code walkthrough to understand the code written in the above BaseTest.java file.

 1. Start by creating required class objects and variables with public access to access them in the test classes. For this, first, create an object of *RemoteWebDriver* and initialize it to null.

![](https://cdn-images-1.medium.com/max/2000/0*op0j-cF96roH7ld5.png)

 1. Next, add the username and access key for your LambdaTest account to connect to the cloud grid for test execution. You can find these in the [Password &amp;amp; Security](https://accounts.lambdatest.com/security) section tab.

![](https://cdn-images-1.medium.com/max/2800/0*lAewimHoMT1xE08m.png)

 1. You can also configure these as environment variables and directly fetch them in code instead of defining them in plain text.

**For Windows:**
&amp;gt;  set LT_USERNAME=LT_USERNAME
set LT_ACCESS_KEY=LT_ACCESS_KEY

**For macOS and Linux:**
&amp;gt;  export LT_USERNAME=LT_USERNAME
export LT_ACCESS_KEY=LT_ACCESS_KEY

 1. Add a String variable as *status* and initialize it with value as *failed*. Before quitting the driver instance, this variable helps mark the test case’s final status as passed/failed on the LambdaTest dashboard. This value is updated to be *passed* in the test case if the test passes with no assertion failures or exceptions.

![](https://cdn-images-1.medium.com/max/2000/0*R5zUDdWmlgqgVlJ8.png)

 1. Update the code inside the *setup()* method to set the required browser properties and LambdaTest configurations to connect to the cloud grid.

 2. Create an object of the *ChromeOptions *class and set the OS and browser versions.

![](https://cdn-images-1.medium.com/max/2000/0*HVdfuVIZhf5IAgYJ.png)

 1. Next, create a *HashMap* type variable to specify the additional browser capabilities for the LambdaTest platform. This will help to identify the dashboard test results using the build name and other details.

![](https://cdn-images-1.medium.com/max/2000/0*CCXAp5caKB33BHL-.png)

You can fetch the required browser capabilities from the LambdaTest platform by navigating to the [Automation Capabilities Generator](https://www.lambdatest.com/capabilities-generator/). This helps by offering ready-to-use code for setting up browser capabilities that can be used in execution.

![](https://cdn-images-1.medium.com/max/2800/0*9GMkI4S4Ry4lTFB9.png)

 1. Finally, use the Selenium *RemoteWebDriver* to connect to the LambdaTest remote grid using your credentials and *ChromeOptions* class object containing all specified browser capabilities.

![](https://cdn-images-1.medium.com/max/NaN/0*piISf2ZGBaAenDMt.png)

 1. Use this driver to navigate the Two Input Fields page on the LambdaTest Selenium Playground for test execution.



![](https://cdn-images-1.medium.com/max/2000/0*RU5GigHc8a_NTHMH.png)

 1. Inside the *tearDown()* method, add a step to execute the script to update the test case status on the LambdaTest dashboard.



![](https://cdn-images-1.medium.com/max/2000/0*Nq-KEWqz9Wlxr0B1.png)

 1. Next, move to the test class TwoInputFieldTests.java and add the step to update the status to *passed *after the execution of all test cases.

![](https://cdn-images-1.medium.com/max/2800/0*CLwA0CmlF_4P7Wsq.png)

![](https://cdn-images-1.medium.com/max/2800/0*5pAfEyqzyf-KeMUv.png)

![](https://cdn-images-1.medium.com/max/2800/0*QAeAKc20sMNaLTDh.png)

**Results:**

We can execute the test cases similarly to how we did for local execution. The console output in the IDE will remain unchanged, but the primary focus will shift to monitoring the execution results on the LambdaTest dashboard.

The execution results are on the LambdaTest Dashboard under the **Automation &amp;gt; Web Automation** tab.

![](https://cdn-images-1.medium.com/max/2800/0*I5iLpBNPJZ6gpxJ8.png)

![](https://cdn-images-1.medium.com/max/2800/0*3wV40bGiaiUtBtRY.png)

![](https://cdn-images-1.medium.com/max/2800/0*pMZADnQkc5DRzQrd.png)

![](https://cdn-images-1.medium.com/max/2800/0*geLMoQkFsBbXCb5U.png)

## Best Practices for Writing Test Cases in Java

All the test executions we’ve learned above are written following best practices. Adhering to these practices helps avoid repeating the same or similar mistakes and ensures a smooth test flow in your automation projects.

Some of the best practices are given below.

* **Understanding the Requirements**

The first and foremost requirement of writing a good test case is understanding the changes and features clearly. The tester needs to know what is expected from the code based on the input. We can refer to the product requirement documentation or the acceptance cases to know the expected behavior.

* **Maintainable and Reusable Code**

The test cases should be readable and follow clean coding principles. This helps to keep test cases maintainable and allows quick refactoring in case of requirement changes without much hassle.

* **Clear and Descriptive Test Case Names**

The names should be clear and meaningful so that anyone who sees the code or test case can understand what a particular test is supposed to do. The name should be descriptive enough to describe the purpose of the test case. One of the naming conventions one can follow is *should[ExpectedResult*]*When[Condition]*. For example, *shouldReturnTheResultWhenBothNumbersAreValid *or *shouldShowErrorWhenAnyNumberIsMissing*.

* **Arrange-Act-Assert Structure**

Try to follow the AAA structure for your test cases for effective test management by splitting it into three parts.

* **Arrange:** Define and set up the test environment and test data on which we want to execute the test.

* **Act:** Write the execution steps which interact with the feature using the above data.

* **Assert:** Write proper assertions which verify the actual and expected results.

* **One Test Case, One Assertion**

Use specific and meaningful assertions for each test case, which helps to verify the required details. One test should not be used to verify multiple flows and should not have a dependency on any other test case. For example, valid results for valid input and error responses for invalid input should be two separate test cases. You can use assertions like *assertEquals(), assertTrue(), assertFalse(), assertNotNull()*, etc., to assert to-the-point data and provide meaningful messages to better understand the failures.

* **Using Setup and Teardown Methods**

Use *@Before* and *@After* annotations of JUnit or TestNG, depending on your framework, to write setup and teardown methods. These methods help to prevent duplicity by making the common code, like driver setup, browser launch, driver termination, etc, reusable across all the test cases.

* **Using Mock Data and Stubs for Error Cases**

Mock data or stubs means to simulate a function behavior in the software to return some particular result as desired by the tester to test some particular test cases. We can use mocking frameworks like Mockito or write a stub function to mock dependencies and verify test cases for external dependency failures.

* **Covering Edge and Boundary Value Cases**

Write test cases to cover all the edge cases or unusual scenarios due to software or hardware downtimes. You can use mocking for such types of test cases. Also, testing for boundary values by passing various test data helps verify if the code and software function properly for limiting cases.

* **Using Tags, Groups, and Categories**

Use tags, groups, or categories to group the related test cases together that serve some common purpose, flow, or requirement in general. For instance, you can group all test cases for login flow, positive and negative cases, etc.

## Conclusion

With this, we have concluded this blog on how to write a test case in Java. In this blog, we learned how a good test case can be written with a basic understanding of Java language and test case fundamentals like flow, assertions, and WeElement locators. We learned the working examples on local and cloud grids and understood how following the best practices makes the test case robust and reliable. Now, it’s time for you to go ahead and try writing your first [test case](https://www.lambdatest.com/learning-hub/test-case?utm_source=medium&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_12&amp;amp;utm_term=rj&amp;amp;utm_content=blog) in Java.

Happy learning!!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>java</category>
      <category>testing</category>
    </item>
    <item>
      <title>How To Scroll Down in Selenium WebDriver</title>
      <dc:creator>Vipulgupta9090</dc:creator>
      <pubDate>Tue, 06 Aug 2024 15:17:31 +0000</pubDate>
      <link>https://dev.to/vipulgupta9090/how-to-scroll-down-in-selenium-webdriver-51c8</link>
      <guid>https://dev.to/vipulgupta9090/how-to-scroll-down-in-selenium-webdriver-51c8</guid>
      <description>&lt;p&gt;Scrolling in Selenium is crucial for interacting with elements beyond the visible viewport. It ensures proper visibility and accessibility of elements during test execution, especially in scenarios involving dynamic content or lazy loading. Before diving into operations like how to scroll up and scroll down in Selenium, it’s important to understand the classes and interfaces in Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;Selenium WebDriver is mainly represented by the WebDriver interface. This interface defines a set of methods for web browsers, allowing users to automate their interactions with WebElements and perform various testing operations. We will learn more about the &lt;em&gt;SeleniumWebDriver&lt;/em&gt; interfaces detailed in this tutorial on how to scroll down in &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_6&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this blog, we will explore performing scrolling actions on web pages using the &lt;em&gt;JavaScriptExecutor&lt;/em&gt; interface in Selenium, focusing on how to scroll down in Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;We will use Java as a programming language to demonstrate these scroll actions, covering scenarios such as scrolling to a specific element and horizontal scrolling.&lt;br&gt;
This straightforward guide on incorporating scroll functionality into your Selenium automation scripts will help you build confidence when scrolling down in Selenium.&lt;/p&gt;
&lt;h2&gt;
  
  
  Understanding the hierarchy of Selenium interfaces
&lt;/h2&gt;

&lt;p&gt;WebDriver is the foundation for building browser automation scripts in different programming languages. Other Selenium interfaces, such as &lt;em&gt;SearchContext&lt;/em&gt;, &lt;a href="https://www.lambdatest.com/blog/selenium-remotewebdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_6&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;*Remote WebDriver&lt;/a&gt;&lt;em&gt;, *TakesScreenshot&lt;/em&gt;, and &lt;em&gt;JavaScriptExecutor&lt;/em&gt; interfaces, extend the capabilities of Selenium WebDriver&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AZlKX53oGK8rn1N74.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AZlKX53oGK8rn1N74.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;SearchContext&lt;/em&gt; interface in Selenium ensures a consistent method for locating elements within the HTML structure during web automation testing, providing a standardized approach for interaction with WebElements. In addition to the core &lt;em&gt;WebDriver&lt;/em&gt; and &lt;em&gt;SearchContext&lt;/em&gt; interfaces, Selenium WebDriver incorporates two essential interfaces.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;TakesScreenshot&lt;/em&gt; interface enables users to capture screenshots during test execution, enhancing visual information in Selenium scripts.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;JavaScriptExecutor&lt;/em&gt; interface empowers users to execute JavaScript code within the Selenium framework, offering two methods: &lt;em&gt;executeScript()&lt;/em&gt; for synchronous JavaScript and &lt;em&gt;executeAsyncScript()&lt;/em&gt; for asynchronous JavaScript, further expanding the capabilities of Selenium scripts.&lt;/p&gt;

&lt;p&gt;Understanding this hierarchy and the relationships between these interfaces is crucial for effectively utilizing Selenium WebDriver for automation testing. It provides a structured approach to performing actions on WebElements and facilitates the creation of robust and maintainable test scripts.&lt;/p&gt;

&lt;p&gt;Let’s explore the basics of scrolling down in Selenium WebDriver to understand the significance of scrolling in Selenium.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is scrolling in Selenium?
&lt;/h2&gt;

&lt;p&gt;Scrolling in Selenium involves navigating up or down a web page to view its contents that extend beyond the visible area of the browser window. In the real-world scenarios of Selenium WebDriver automation, where conventional mouse or keyboard actions are not applicable, we rely on &lt;em&gt;JavaScriptExecutor&lt;/em&gt; to scroll down through the content or web pages.&lt;/p&gt;

&lt;p&gt;Scrolling in Selenium involves adjusting the view of a web page either vertically or horizontally to access hidden or dynamically loaded content. This becomes particularly useful when the elements you intend to interact with on a web page are not immediately visible and require scrolling into view before interaction is possible.&lt;/p&gt;

&lt;p&gt;When performing automation testing with Selenium, various scenarios may demand scrolling, and one crucial aspect is scrolling down in Selenium. These scenarios include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Locating and interacting with hidden elements:&lt;/strong&gt; This is a typical situation in Selenium automation. Some web pages have initially hidden elements that only become visible when you scroll down. Incorporating the scroll down in the Selenium technique is crucial to interact with these elements smoothly, ensuring they come into the viewport for effective automation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Infinite scroll:&lt;/strong&gt; Infinite scrolling is a feature on certain web pages where new content dynamically loads as the user scrolls down. In automated testing scenarios, there’s a need to scroll down in Selenium WebDriver to initiate the loading of additional content.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Navigating through dropdowns or menus:&lt;/strong&gt; Navigating through dropdowns or menus, especially those with numerous options, may necessitate scrolling down in Selenium to access the desired option effortlessly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Verifying page layout:&lt;/strong&gt; Verifying the page layout occasionally requires scrolling down in Selenium to ensure a comprehensive check of the appearance and arrangement of elements across different page sections.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To implement scroll down in Selenium, especially to an element and other relevant operations, the initial step involves importing the &lt;em&gt;org.openqa.selenium.JavascriptExecutor&lt;/em&gt; package into your code.&lt;/p&gt;

&lt;p&gt;Below is the syntax of &lt;em&gt;JavaScriptExecutor&lt;/em&gt; for Selenium, which is important for seamlessly incorporating scroll down in Selenium functionality and various related operations:&lt;/p&gt;

&lt;p&gt;The syntax for &lt;em&gt;JavaScriptExecutor&lt;/em&gt; is shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript(script,arguments);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the provided code snippet, we create an instance of the &lt;em&gt;JavascriptExecutor&lt;/em&gt; interface by casting the &lt;em&gt;WebDriver&lt;/em&gt; object. JavaScript, a language capable of communicating with HTML in a browser, is used through the &lt;em&gt;JavascriptExecutor&lt;/em&gt; interface for executing JavaScript code in Selenium automation.&lt;br&gt;
This interface acts as a bridge, enabling the &lt;em&gt;WebDriver&lt;/em&gt; to interact with HTML elements within the browser and facilitating various actions, such as scrolling, as this blog emphasizes.&lt;/p&gt;

&lt;p&gt;Subsequently, the &lt;em&gt;JavascriptExecutor&lt;/em&gt; object invokes the &lt;em&gt;executeScript()&lt;/em&gt; method. This method executes the specified test script as an anonymous function for the current browser window in focus.&lt;/p&gt;

&lt;p&gt;Notably, the &lt;em&gt;executeScript()&lt;/em&gt; method returns a WebElement if the script involves an HTML element and includes a return statement, which is relevant when incorporating the scroll down in Selenium WebDriver functionality.&lt;/p&gt;

&lt;p&gt;Some examples of this script would be.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;To click a button&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To enter some data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To scroll down in Selenium WebDriver&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that the basic premise is set let’s delve into scrolling a web page using Selenium WebDriver. In this blog on how to scroll down in Selenium WebDriver, we will learn to incorporate scroll down functionality for effective automation testing.&lt;/p&gt;
&lt;h2&gt;
  
  
  Scrolling a web page using Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;Effectively scrolling a web page using Selenium WebDriver is crucial, especially when confronted with elements that aren’t immediately visible or that trigger specific behaviors on a web page. Selenium WebDriver offers multiple methods for seamless scrolling, providing diverse techniques to simulate this action.&lt;/p&gt;

&lt;p&gt;There are various approaches to achieving this, including the natural integration of the scroll down in Selenium WebDriver.&lt;/p&gt;
&lt;h2&gt;
  
  
  scrollTo() method
&lt;/h2&gt;

&lt;p&gt;This &lt;em&gt;scrollTo()&lt;/em&gt; method is valuable for scrolling a web page to a specific location using Selenium WebDriver. This method requires the (x, y) coordinates of the desired location as arguments. For instance, to effortlessly scroll down to the bottom of the web page, you would pass the y-coordinate set as the maximum height of the document (web page).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript(“window.scrollTo(0, document.body.scrollHeight)”);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach proves particularly useful when dealing with scroll down in Selenium WebDriver functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  scrollBy() method
&lt;/h2&gt;

&lt;p&gt;This &lt;em&gt;scrollBy()&lt;/em&gt; method is handy when adjusting the view by a specific number of pixels in Selenium WebDriver. Similar to the &lt;em&gt;scrollTo()&lt;/em&gt; method, it also takes (x, y) coordinates as arguments. However, in the &lt;em&gt;scrollBy()&lt;/em&gt; method, the coordinates signify the number of pixels to scroll rather than the absolute position to scroll to. For instance, to smoothly scroll 50 pixels on the x-axis and 100 pixels on the y-axis, you would provide these values as arguments to the method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; js.executeScript(“window.scrollBy(50,100)”);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach proves effective, especially with scroll down in Selenium WebDriver functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  scrollIntoView() method
&lt;/h2&gt;

&lt;p&gt;This &lt;em&gt;scrollIntoView()&lt;/em&gt; method in Selenium WebDriver is a valuable approach for scrolling to a specific WebElement that is currently not visible on the screen. This method takes the script to scroll and the WebElement as arguments. Like the &lt;em&gt;scrollTo()&lt;/em&gt; and &lt;em&gt;scrollBy()&lt;/em&gt; methods, it effectively manages elements’ visibility during automation testing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript(“arguments[0].scrollIntoView();”, element);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This functionality becomes particularly relevant when seamlessly integrating the scroll down in the Selenium WebDriver technique.&lt;/p&gt;

&lt;p&gt;In the following section of this blog, we will learn how to scroll down in Selenium WebDriver with a demonstration and code walkthrough.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to scroll down to the bottom of the page in Selenium WebDriver?
&lt;/h2&gt;

&lt;p&gt;To achieve a smooth scroll down in Selenium WebDriver, we will obtain the height of the page. Once the page height is determined, we will perform a scroll down in Selenium to reach the end of the page. This method is frequently used in Selenium automation testing, where scrolling is essential to carry out relevant operations on the WebElements within the Document Object Model (DOM).&lt;/p&gt;

&lt;p&gt;The following code helps you scroll down to the bottom of the page using the &lt;em&gt;JavascriptExecutor&lt;/em&gt; interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript(“window.scrollTo(0, document.body.scrollHeight)”);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the following implementation, we navigate to the &lt;a href="https://www.lambdatest.com/selenium-playground/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_6&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Selenium Playground&lt;/a&gt; web page and execute a scroll down to the bottom of the page in Selenium WebDriver.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Scroll down to the bottom of the page in Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;Before proceeding with the demonstration of scrolling to the bottom using Selenium, let’s establish the basic setup for the project. This setup involves creating a Maven — TestNG project and configuring it to connect to a remote cloud grid where we will execute all the test cases and assess the results.&lt;/p&gt;

&lt;p&gt;We will be using a cloud-based platform to access Selenium Grid, and one such platform is LambdaTest. LambdaTest is an AI-powered test orchestration and execution platform that lets you run manual and automated tests at scale with over 3000+ real devices, browsers, and OS combinations. It also provides detailed execution reports that can be viewed on its dashboard and analytics for test execution trends and other key performance indicator metrics.&lt;/p&gt;

&lt;p&gt;This platform will assist us in executing and managing all the test cases over the cloud. Furthermore, this platform enables you to use the Selenium Grid for parallel testing. This preparation is crucial, especially when integrating the scroll down in Selenium technique later in the project.&lt;/p&gt;

&lt;p&gt;If you are new to automation testing over the cloud, follow this detailed video tutorial on using LambdaTest to run your tests. It provides valuable insights and step-by-step instructions, helping you start automation testing projects over a cloud platform.&lt;/p&gt;

&lt;p&gt;This platform will assist us in executing and managing all the &lt;a href="https://www.lambdatest.com/learning-hub/test-case" rel="noopener noreferrer"&gt;test cases&lt;/a&gt; over the cloud. Furthermore, this platform enables you to use the Selenium Grid for parallel testing. This preparation is crucial, especially when integrating the scroll down in Selenium technique later in the project.&lt;/p&gt;

&lt;p&gt;Let’s look at the project setup, including the configuration for connecting to the LambdaTest cloud grid. This setup will be utilized in all demonstrations involving scrolling using Selenium.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project Setup:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Below are the steps to set up your project and perform scroll down functionalities using Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; Launch Eclipse IDE or any other IDE of your choice. The steps for project setup and all demonstrations remain the same for the chosen IDE. If you do not have Eclipse installed, &lt;a href="https://www.eclipse.org/downloads/" rel="noopener noreferrer"&gt;download the IDE&lt;/a&gt; from their official website.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Create a new Maven project and name it as SeleniumScroll.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Inside the src package, add a new package as a test. This package will have all the Java test files for this blog demonstration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; Add a class file and name it &lt;em&gt;BaseTest.java&lt;/em&gt;. This Java file will contain all the common code for test execution, like setup and initializing the &lt;em&gt;WebDriver&lt;/em&gt;, declaring and initializing public variables for the project, handling driver termination after test completion, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Anj53fcAVWX95ptGf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Anj53fcAVWX95ptGf.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This file will have the details and steps required to connect to the LambdaTest cloud grid on which all the test cases will be executed.&lt;/p&gt;

&lt;p&gt;If you wish to learn how to &lt;a href="https://www.lambdatest.com/blog/how-to-install-testng-in-eclipse-step-by-step-guide/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_6&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;install TestNG in Eclipse&lt;/a&gt;, follow this detailed guide that covers all the steps needed to set up TestNG in Eclipse and start your automation testing process.&lt;/p&gt;

&lt;p&gt;As this project uses Selenium and TestNG as the testing framework, add the latest stable versions of Selenium 4 and TestNG dependencies inside the pom.xml for the best execution results. You can fetch the latest dependencies from the Maven repository.&lt;/p&gt;

&lt;p&gt;For an in-depth understanding of Selenium 4, including its architecture, types, and differences compared to Selenium 3, you can follow this comprehensive video tutorial. It provides valuable insights into the workings of Selenium 4.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/mMStkc3W9jY"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The updated &lt;em&gt;pom.xml&lt;/em&gt; should look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;lt;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 https://maven.apache.org/xsd/maven-4.0.0.xsd"&amp;gt;
     &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
     &amp;lt;groupId&amp;gt;SeleniumScroll&amp;lt;/groupId&amp;gt;
     &amp;lt;artifactId&amp;gt;SeleniumScroll&amp;lt;/artifactId&amp;gt;
     &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;
     &amp;lt;build&amp;gt;
       &amp;lt;sourceDirectory&amp;gt;src&amp;lt;/sourceDirectory&amp;gt;
       &amp;lt;plugins&amp;gt;
         &amp;lt;plugin&amp;gt;
           &amp;lt;artifactId&amp;gt;maven-compiler-plugin&amp;lt;/artifactId&amp;gt;
           &amp;lt;version&amp;gt;3.8.1&amp;lt;/version&amp;gt;
           &amp;lt;configuration&amp;gt;
             &amp;lt;release&amp;gt;17&amp;lt;/release&amp;gt;
           &amp;lt;/configuration&amp;gt;
         &amp;lt;/plugin&amp;gt;
       &amp;lt;/plugins&amp;gt;
     &amp;lt;/build&amp;gt;
     &amp;lt;dependencies&amp;gt;
           &amp;lt;dependency&amp;gt;
               &amp;lt;groupId&amp;gt;org.seleniumhq.selenium&amp;lt;/groupId&amp;gt;
               &amp;lt;artifactId&amp;gt;selenium-java&amp;lt;/artifactId&amp;gt;
               &amp;lt;version&amp;gt;4.16.1&amp;lt;/version&amp;gt;
           &amp;lt;/dependency&amp;gt;
           &amp;lt;dependency&amp;gt;
               &amp;lt;groupId&amp;gt;org.testng&amp;lt;/groupId&amp;gt;
               &amp;lt;artifactId&amp;gt;testng&amp;lt;/artifactId&amp;gt;
               &amp;lt;version&amp;gt;7.8.0&amp;lt;/version&amp;gt;
               &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
           &amp;lt;/dependency&amp;gt;
       &amp;lt;/dependencies&amp;gt;
    &amp;lt;/project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the finalized &lt;em&gt;BaseTest.java&lt;/em&gt; with all the cloud configuration and LambdaTest configurations should look like below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following code implementation consists of the cloud capabilities and establishes a common file containing the base code for performing scroll operations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package test;


    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.HashMap;


    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.openqa.selenium.JavascriptExecutor;
    import org.testng.annotations.*;


    public class BaseTest {


       public RemoteWebDriver driver = null;
    JavascriptExecutor js;
       String username = System.getenv("LT_USERNAME") == null ? "&amp;lt;lambdatest_username&amp;gt;" : System.getenv("LT_USERNAME");
       String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "&amp;lt;lambdatest_accesskey&amp;gt;" : System.getenv("LT_ACCESS");


       @BeforeTest
       public void setup() {
           try {
               ChromeOptions chromeOptions = new ChromeOptions();
               chromeOptions.setPlatformName("Windows 10");
               chromeOptions.setBrowserVersion("121.0");


               HashMap&amp;lt;String, Object&amp;gt; ltOptions = new HashMap&amp;lt;String, Object&amp;gt;();
               ltOptions.put("build", "Scroll with Selenium WebDriver");
               ltOptions.put("project", "Scroll with Selenium WebDriver");
               chromeOptions.setCapability("LT:Options", ltOptions);


               driver = new RemoteWebDriver(
                       new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), chromeOptions);
               js = (JavascriptExecutor) driver;
           } catch (MalformedURLException e) {
               e.printStackTrace();
           }
       }


       @AfterTest
       public void tearDown() {
           driver.quit();
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A2fPS7vMpn9uZD_mu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A2fPS7vMpn9uZD_mu.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we jump to the result, let us understand the above code in the step-by-step process below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The code below consists of the functions used to connect to the cloud grid, set up &lt;em&gt;RemoteWebDriver&lt;/em&gt;, add LambdaTest username and access key, and more.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ASyxeR6Vm42Vc22Yd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ASyxeR6Vm42Vc22Yd.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code creates an object of &lt;em&gt;RemoteWebDriver&lt;/em&gt; and initializes it as &lt;em&gt;null&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AP2jsebHWVfqcovOu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AP2jsebHWVfqcovOu.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code creates an object of the &lt;em&gt;JavascriptExecutor&lt;/em&gt; interface. We will initiate it after connecting to the cloud grid later.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AWfkdSFUE8wEboZr3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AWfkdSFUE8wEboZr3.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code displays how to add your username and access key once you have fetched it from the LambdaTest platform. To fetch the credentials, click Profile &amp;gt; Account Settings &amp;gt; &lt;a href="https://accounts.lambdatest.com/security" rel="noopener noreferrer"&gt;Password &amp;amp; Security&lt;/a&gt;, copy the details, and connect to the cloud grid for test execution.&lt;/p&gt;

&lt;p&gt;Alternatively, you can configure these as environment variables and directly fetch them in code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For macOS and Linux:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;export LT_USERNAME=LT_USERNAME&lt;br&gt;
 export LT_ACCESS_KEY=LT_ACCESS_KEY&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;For Windows:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;set LT_USERNAME=LT_USERNAME&lt;br&gt;
 set LT_ACCESS_KEY=LT_ACCESS_KEY&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AbYoEMT89G06sztEN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AbYoEMT89G06sztEN.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code creates a method for this class and names it &lt;em&gt;setup()&lt;/em&gt;. Annotate it with the &lt;em&gt;@BeforeTest&lt;/em&gt; &lt;a href="https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/" rel="noopener noreferrer"&gt;annotation in TestNG&lt;/a&gt; so that it is executed before each test.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AbohjMQOdiTVy05LI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AbohjMQOdiTVy05LI.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code creates an object of the &lt;em&gt;ChromeOptions&lt;/em&gt; class. This can be used to set the OS and browser versions.&lt;/p&gt;

&lt;p&gt;The above code creates a &lt;em&gt;HashMap&lt;/em&gt; type variable to pass the additional browser capabilities required by the LambdaTest platform to support test execution on their cloud grid. This will help identify the dashboard test results using the build name and other details.&lt;/p&gt;

&lt;p&gt;Lastly, fetch the required browser capabilities for the LambdaTest platform by navigating to the &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_6&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Automation Capabilities Generator&lt;/a&gt;. This helps by offering ready-to-use code for setting up browser capabilities that can be used in execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AorQhlnVSmslYNahj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AorQhlnVSmslYNahj.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AIxf-9oB5U21cPlxF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AIxf-9oB5U21cPlxF.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code uses the Selenium &lt;em&gt;RemoteWebDriver&lt;/em&gt; to connect to the LambdaTest remote Grid using the credentials and the ChromeOptions objects containing all specified browser capabilities.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A3Y4h96Xd-Y9YeBPC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A3Y4h96Xd-Y9YeBPC.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above code, the driver is initialized, which is used to initialize the &lt;em&gt;JavascriptExecutor&lt;/em&gt; object that we created towards the beginning as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A0SqGfRn-f2yAJDmM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A0SqGfRn-f2yAJDmM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code adds a new method to close the browser and terminate the WebDriver session after each test execution. Name it as &lt;em&gt;tearDown()&lt;/em&gt;, and annotate it with &lt;em&gt;@AfterTest&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;With this, the setup for the &lt;em&gt;BaseTest.java&lt;/em&gt; file to hold the common code is completed. We will extend this file in all test classes going forward in this blog to demonstrate different scrolling operations used with Selenium.&lt;/p&gt;

&lt;p&gt;Let’s proceed and explore the process of scrolling down to the bottom of a web page using Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Below is the code implementation of performing scroll down in Selenium WebDriver with Java.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package test;


    import org.testng.annotations.Test;


    public class ScrollDown extends BaseTest{

       @Test
       public void testScrollDown()
       {
           //launch the website
           driver.get("https://www.lambdatest.com/selenium-playground/");

           //get the height of the web page and scroll to the end
           js.executeScript("window.scrollTo(0, document.body.scrollHeight)");   
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us try to understand the above code’s workings in the step-by-step procedure below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AZVR1ycP3W33t1Cs5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AZVR1ycP3W33t1Cs5.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code extends the test class with the BaseTest.java file to inherit and use its functionalities, including the &lt;em&gt;WebDriver&lt;/em&gt; and &lt;em&gt;JavascriptExecutor.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AjPd6saj_q7ZTWFih.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AjPd6saj_q7ZTWFih.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code adds a new test method as &lt;em&gt;testScrollDown()&lt;/em&gt; and annotates it with &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;/em&gt; annotation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2276%2F0%2AngdieLbRsWdHhAJ1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2276%2F0%2AngdieLbRsWdHhAJ1.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2536%2F0%2ACviTDsOz8w8uD4Sy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2536%2F0%2ACviTDsOz8w8uD4Sy.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code uses the &lt;em&gt;JavascriptExecutor&lt;/em&gt; object to scroll down to the bottom of the page using the &lt;em&gt;scrollTo()&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On executing the above code, you can see output like the below on the local and the LambdaTest dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2ACHFWUKUjDz_O-zqR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2ACHFWUKUjDz_O-zqR.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AyYscKi7SzBdZGh2F.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AyYscKi7SzBdZGh2F.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to scroll down to the WebElement visibility in Selenium WebDriver?
&lt;/h2&gt;

&lt;p&gt;In any web page, several WebElements may load dynamically. To execute scroll down in Selenium, the relevant WebElement must be present or visible in the DOM.&lt;/p&gt;

&lt;p&gt;The JavaScript &lt;em&gt;scrollIntoView()&lt;/em&gt; interface is invaluable for this purpose, smoothly scrolling an element into the visible part of the window and enhancing the effectiveness of scroll down operations in Selenium WebDriver.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Locate the desired WebElement
    WebElement element = driver.findElement(By.linkText("Espresso Testing"));
    js.executeScript("arguments[0].scrollIntoView();", element);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the following implementation, we navigate to the LambdaTest Selenium Playground web page and execute a scroll down to the WebElement in Selenium WebDriver.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Scroll down to the WebElement in Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;Below is the code to cover the complete working of visibility of an element on the web page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The code below consists of the functions that perform scroll down in Selenium WebDriver for element visibility on the web page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package test;


    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.testng.annotations.Test;


    public class ScrollDownByElementVisibility extends BaseTest{

       @Test
       public void testScrollDownByElementVisibility()
       {
           //launch the website
           driver.get("https://www.lambdatest.com/selenium-playground/");

           //specify the WebElement till which the page has to be scrolled
           WebElement element = driver.findElement(By.linkText("Espresso Testing"));


           //get the height of the web page and scroll to the end
              js.executeScript("arguments[0].scrollIntoView();", element);  
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us understand the code in detail below before jumping to the result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2192%2F0%2AQz8huBT5tllIveWG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2192%2F0%2AQz8huBT5tllIveWG.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code creates a new test class and extends &lt;em&gt;BaseTest.java&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AOapucprsgpC-HcP9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AOapucprsgpC-HcP9.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code adds a test method &lt;em&gt;testScrollDownByElementVisibility()&lt;/em&gt; and annotates it with &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2276%2F0%2A91pZAKrYruHt1Gxy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2276%2F0%2A91pZAKrYruHt1Gxy.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code navigates to LambdaTest Selenium Playground.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2596%2F0%2AtIqsn1gAmjXry-46.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2596%2F0%2AtIqsn1gAmjXry-46.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code locates the WebElement to which we want to scroll. We have used the element’s Link Text to locate it.&lt;/p&gt;

&lt;p&gt;To learn more about how to &lt;a href="https://www.lambdatest.com/blog/find-element-by-text-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_6&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;*findElement()&lt;/a&gt;* by Link Text or Partial Link Text, follow this guide on using Link Text and Partial Link Text in Selenium and get valuable information.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2304%2F0%2A5Sp0MS3b4ACBWKXn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2304%2F0%2A5Sp0MS3b4ACBWKXn.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code uses the &lt;em&gt;JavascriptExecutor&lt;/em&gt; object to execute the &lt;em&gt;scrollIntoView()&lt;/em&gt; method with the element as the argument to scroll to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On executing the above code, you can see the result on the local and LambdaTest dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2304%2F0%2AzlUoDg-NJ8y3t2BM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2304%2F0%2AzlUoDg-NJ8y3t2BM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AH3ryD22HBkQqCFlU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AH3ryD22HBkQqCFlU.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To learn more about WebElements and how you can interact with them, follow the video tutorial and get practical insight on dealing with WebElements to make your automation testing process efficient.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Not sure how to optimize your website for all devices? Follow our &lt;a href="https://www.lambdatest.com/blog/best-practices-for-responsive-websites/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_6&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;responsive design best practices&lt;/a&gt; to ensure a seamless user experience.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How to scroll down by specified pixels in Selenium WebDriver?
&lt;/h2&gt;

&lt;p&gt;You can also scroll to a particular point by specifying the exact location or coordinates on the page. This can be done using the &lt;em&gt;scrollBy()&lt;/em&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript(“window.scrollBy(0,3000)”);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the following implementation, we navigate to the &lt;a href="https://www.lambdatest.com/selenium-playground/" rel="noopener noreferrer"&gt;LambdaTest Selenium Playground&lt;/a&gt; web page and execute a scroll down by specified pixels in Selenium WebDriver.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Scroll down by specified pixels in Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;Below is the code for scrolling down by a specified number of pixels on a web page using Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The provided code consists of functions that scroll down in Selenium WebDriver to a specified number of pixels on a web page using Selenium WebDriver.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package test;


    import org.testng.annotations.Test;


    public class ScrollDownByPixel extends BaseTest{


       @Test
       public void testScrollDownByPixel()
       {
           //launch the website
           driver.get("https://www.lambdatest.com/selenium-playground/");

           //specify the number of pixels the page has to be scrolled
           js.executeScript("window.scrollBy(0,3000)");
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us understand the working of the above code in detail below before jumping to the result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AW1LBXPkkxUJ-o29F.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AW1LBXPkkxUJ-o29F.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code creates a new test class and extends &lt;em&gt;BaseTest.java&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Ab_TPpBhPFsYSjiEH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Ab_TPpBhPFsYSjiEH.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above code, we use a test method, &lt;em&gt;testScrollDownByPixel()&lt;/em&gt;, which annotates it with &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2276%2F0%2A6yiI1LecoYp_iBKR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2276%2F0%2A6yiI1LecoYp_iBKR.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code navigates to LambdaTest Selenium Playground.&lt;/p&gt;

&lt;p&gt;The above code uses the &lt;em&gt;JavascriptExecutor&lt;/em&gt; object to execute the &lt;em&gt;scrollBy()&lt;/em&gt; method by specifying the location pixels we want to scroll.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On executing the above code, you can see output like the below on the local and the LambdaTest dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AdClzdfkALE7CcNq7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AdClzdfkALE7CcNq7.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2ArBEoMNMgEsFyX4SU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2ArBEoMNMgEsFyX4SU.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to scroll left in the horizontal direction in Selenium WebDriver?
&lt;/h2&gt;

&lt;p&gt;When performing automation testing for websites or web apps, there are times when it becomes crucial to scroll the page in the horizontal direction. Particularly, when focusing on horizontal scrolling, the requirement may arise to scroll to the page’s left or right.&lt;/p&gt;

&lt;p&gt;Use the below code snippet to scroll left horizontally during an automation test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript(“window.scrollBy(-3000,0)”);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the following implementation, we navigate to the &lt;a href="https://www.lambdatest.com/" rel="noopener noreferrer"&gt;LambdaTest&lt;/a&gt; website and scroll left in the horizontal direction in Selenium WebDriver.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Scroll left in the horizontal direction in Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;This demonstration is part of performing scroll down in Selenium, aiming to scroll a web page to the left in the horizontal direction using Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The provided code below consists of functions that perform a horizontal scroll to the left on a web page using Selenium WebDriver.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package test;


    import org.openqa.selenium.Dimension;
    import org.testng.annotations.Test;


    public class ScrollToHorizontalLeft extends BaseTest{


       @Test
       public void testScrollToHorizontalLeft()
       {
           //launch the website
           driver.get("https://www.lambdatest.com");


           //decrease the size of window to show both scoll bars
           driver.manage().window().setSize(new Dimension(450,630));

           //scroll to the right
           js.executeScript("window.scrollBy(6000,0)");


           //scroll to the left
           js.executeScript("window.scrollBy(-3000,0)");
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us understand the working of the above code in detail below before jumping to the result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AsClZlx6u89l2bHFK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AsClZlx6u89l2bHFK.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code creates a new class, &lt;em&gt;ScrollToHorizontalLeft&lt;/em&gt;, and extends &lt;em&gt;BaseTest.java&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AHWkcogLtUCKMN3pt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AHWkcogLtUCKMN3pt.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code adds the test method &lt;em&gt;testScrollToHorizontalLeft()&lt;/em&gt; and annotates it with the &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;/em&gt; annotation of TestNG.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AvOSfIa2tj5CeKR9n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AvOSfIa2tj5CeKR9n.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code navigates to the LambdaTest website.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Asajz6KnYcsx2NZGi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Asajz6KnYcsx2NZGi.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code resizes the browser window from a maximized size to a smaller one to make both scroll bars visible.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AEw2HwKgbgfBxtbLC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AEw2HwKgbgfBxtbLC.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code visualizes the movement of the scroll bar in the horizontal left direction, and we first scroll to the right and then to the left.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On executing the above code, you can see output like the below on the local and the LambdaTest dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AwSmzbMomb7Nr5tHk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AwSmzbMomb7Nr5tHk.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2A8hX1a-KZJNXzt_lI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2A8hX1a-KZJNXzt_lI.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to scroll right in the horizontal direction in Selenium WebDriver?
&lt;/h2&gt;

&lt;p&gt;To scroll the web page horizontally in the right direction, use the below code snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript(“window.scrollBy(2000,0)”);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the following implementation, we navigate to the &lt;a href="https://www.lambdatest.com/" rel="noopener noreferrer"&gt;LambdaTest&lt;/a&gt; website and scroll right in the horizontal direction in Selenium WebDriver.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Scroll right in the horizontal direction in Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;This demonstration is part of the scroll down in Selenium WebDriver, aiming to scroll a web page to the right in the horizontal direction using Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The provided code below consists of functions that perform the scroll right on a web page horizontally using Selenium WebDriver.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package test;


    import org.openqa.selenium.Dimension;
    import org.testng.annotations.Test;


    public class ScrollToHorizontalRight extends BaseTest{


       @Test
       public void testScrollToHorizontalRight()
       {
           //launch the website
           driver.get("https://www.lambdatest.com");


           //decrease the size of window to show both scroll bars
           driver.manage().window().setSize(new Dimension(450,630));

           //scroll to the right
           js.executeScript("window.scrollBy(6000,0)");  
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before delving into the results, let’s delve into a detailed breakdown of the code. The provided example illustrates scrolling to the right in the horizontal direction using Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2016%2F0%2AcHaW47PtIJD71Duv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2016%2F0%2AcHaW47PtIJD71Duv.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code is similar to the last demonstration; add a new class as &lt;em&gt;ScrollToHorizontalRight *and extend *BaseTest.java&lt;/em&gt; to it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2248%2F0%2AFtwPGg1v1xb-MM-Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2248%2F0%2AFtwPGg1v1xb-MM-Q.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code adds the test case as &lt;em&gt;testScrollToHorizontalRight()&lt;/em&gt; navigates to the LambdaTest website and resizes the browser window to show both scroll bars.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AvNxonxVaqyDuzHvk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AvNxonxVaqyDuzHvk.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code scrolls to the right using the *scrollBy() *method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On executing the above code, you can see output like the below on the local and the LambdaTest dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2A8OsUIx73-GQ0I1Nu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2A8OsUIx73-GQ0I1Nu.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AIMSWg_7hMSw0QzMy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AIMSWg_7hMSw0QzMy.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to scroll horizontally and vertically in Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;When working with Selenium for &lt;a href="https://www.lambdatest.com/learning-hub/automation-testing" rel="noopener noreferrer"&gt;automated testing&lt;/a&gt;, it’s essential to scroll horizontally and vertically on web pages. To accomplish this, use the following code snippet in your automation script.&lt;/p&gt;

&lt;p&gt;Here, values are set for both x and y coordinates, allowing for a natural scroll in horizontal and vertical directions.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript(“window.scrollBy(3000,50)”);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the following implementation, we navigate to the &lt;a href="https://www.lambdatest.com/" rel="noopener noreferrer"&gt;LambdaTest&lt;/a&gt; website and scroll horizontally and vertically in Selenium WebDriver.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Scroll horizontally and vertically in Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;This demonstration is part of scroll down in Selenium, aiming to scroll horizontally and vertically using Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The provided code below consists of functions that perform a scroll horizontally and vertically on a web page using Selenium WebDriver.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package test;


    import org.openqa.selenium.Dimension;
    import org.testng.annotations.Test;


    public class VerticalAndHorizontalScroll extends BaseTest{


       @Test
       public void testVerticalAndHorizontalScroll()
       {
           //launch the website
           driver.get("https://www.lambdatest.com");


           //decrease the size of window to show both scroll bars
           driver.manage().window().setSize(new Dimension(450,630));

           //scroll to the right and left
           js.executeScript("window.scrollBy(3000,50)");  
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before delving into the results, let’s thoroughly understand the code functionality in a step-by-step process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2158%2F0%2AfBnLjRAEVbwO1cGq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2158%2F0%2AfBnLjRAEVbwO1cGq.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code is similar to previous horizontal and vertical scroll demonstrations. We add a new class and test method, which navigates to the LambdaTest website and resizes the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A7tpvL48NRY2paaMS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A7tpvL48NRY2paaMS.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code performs a vertical and horizontal scroll by passing non-zero values for both the x and y coordinates of the &lt;em&gt;scrollBy()&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On executing the above code, you can see output like the below on the local and the LambdaTest dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AG5SII2-9oW3pjvwH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AG5SII2-9oW3pjvwH.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2ApoI6UsBCpidJWV19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2ApoI6UsBCpidJWV19.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to move the scroll bar in Selenium WebDriver?
&lt;/h2&gt;

&lt;p&gt;If a web page features a scroll bar, users can adjust its value and navigate it to their desired position.&lt;/p&gt;

&lt;p&gt;Below is the example taken from LambdaTest Selenium Playground to demonstrate the scroll bar activity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AFaZZZLg-pMoU-zVi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AFaZZZLg-pMoU-zVi.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To achieve this, it is necessary to locate the WebElement representing the scroll bar and execute the scrolling action.&lt;/p&gt;

&lt;p&gt;In the following implementation, we navigate the &lt;a href="https://www.lambdatest.com/selenium-playground/" rel="noopener noreferrer"&gt;LambdaTest Selenium Playground&lt;/a&gt; web page and move the scroll bar in Selenium WebDriver.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Move the scroll bar in Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;This demonstration is part of the scroll down in Selenium WebDriver, aiming to scroll the bar on a page using Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The provided code below consists of functions that perform a scroll bar on a web page using Selenium WebDriver.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package test;


    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebElement;
    import org.testng.annotations.Test;


    public class ScrollOnAPage extends BaseTest{


       @Test
       public void testScrollOnAPage()
       {
           //launch the website     driver.get("https://www.lambdatest.com/selenium-playground/drag-drop-range-sliders-demo");

           //locate the scroll bar element on page
           WebElement element = driver.findElement(By.xpath("(//input[@type='range'])[1]"));

           //scroll the range element on page
           element.sendKeys(Keys.END);
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us understand the working of the code snippet before looking into the results in the step-by-step process below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ArB4y4AMj06hRIh3X.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ArB4y4AMj06hRIh3X.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code ***&lt;em&gt;creates a new test class and extends *BaseTest&lt;/em&gt; to it. This class adds a test method &lt;em&gt;testScrollOnAPage()&lt;/em&gt; and annotates it with &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;/em&gt; annotation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2A1RhtiqxiDFrMn2ui.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2A1RhtiqxiDFrMn2ui.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code navigates to the Slider Demo page of LambdaTest Selenium Playground.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2594%2F0%2AEqz-W-_pVNHQZx7U.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2594%2F0%2AEqz-W-_pVNHQZx7U.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code identifies the range element on the web page for the intended scrolling action.&lt;/p&gt;

&lt;p&gt;Since this element appears multiple times, we utilize the index to interact specifically with the first occurrence.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AKxwlZr8eyWuSwRe6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AKxwlZr8eyWuSwRe6.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AqqGxavBwtNWEa1CZ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AqqGxavBwtNWEa1CZ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code uses the send*Keys()* function to scroll to the end of the range element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On executing the above code, you can see output like the below on the local and the LambdaTest dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AN2xOKLkkduNTzRHT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AN2xOKLkkduNTzRHT.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AfNrVCDPU7_t60uV9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AfNrVCDPU7_t60uV9.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to perform infinite scrolling in Selenium WebDriver?
&lt;/h2&gt;

&lt;p&gt;Many websites incorporate long or infinite scrolling to enhance user engagement. With infinite scrolling, users can seamlessly navigate through content without the need for repetitive clicks on the Previous or Next buttons. You can visit this &lt;a href="https://the-internet.herokuapp.com/infinite_scroll" rel="noopener noreferrer"&gt;Infinite Scrolling sample website&lt;/a&gt; to examine infinite scrolling briefly.&lt;/p&gt;

&lt;p&gt;To enable infinite scrolling, we obtain the page’s height and then scroll to that height. This process iterates until the maximum height and the current height of the web page are equal, signifying that we have reached the end of the web page.&lt;/p&gt;

&lt;p&gt;Below is the code snippet that enables infinite scrolling on a web page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Implementation:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    //find the initial page height
    long intialLength = (long) js.executeScript("return document.body.scrollHeight");

    //start the loop to perform infinite scroll to reach end of page
    while(true)
    {
        js.executeScript("window.scrollTo(0,document.body.scrollHeight)");

        //find the height of page again
        long currentLength = (long) js.executeScript("return document.body.scrollHeight");

        //check if its end of page
        if(intialLength == currentLength) {
            break;
        }
        intialLength = currentLength;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Demonstration: Infinite scrolling in Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;This demonstration is part of scroll down in Selenium, aiming to perform infinite length scrolling on a web page using Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The provided code below consists of functions that perform infinite scroll on a web page using Selenium WebDriver.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package test;


    import org.testng.annotations.Test;


    public class InfiniteScroll extends BaseTest
    {
       @Test
       public void testInfiniteScroll()
       {
           //launch the website
           driver.get("https://the-internet.herokuapp.com/infinite_scroll");

           //find the initial page height
           long intialLength = (long) js.executeScript("return document.body.scrollHeight");

           //start the loop to perform infinite scroll to reach end of page
           while(true)
           {
               js.executeScript("window.scrollTo(0,document.body.scrollHeight)");

               //find the height of page again
               long currentLength = (long) js.executeScript("return document.body.scrollHeight");

               //check if its end of page
               if(intialLength == currentLength) {
                   break;
               }
               intialLength = currentLength;
           }
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s delve into the functionality of the code before looking at the results. The following code is designed to perform the specific task of infinite scrolling. Let’s break down the steps involved in this process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2An00-Z7uli-3GtFl4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2An00-Z7uli-3GtFl4.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code creates a new test class, &lt;em&gt;testInfiniteScroll(),&lt;/em&gt; and adds a test method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2364%2F0%2AqIsLhJNM9Dpd21o6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2364%2F0%2AqIsLhJNM9Dpd21o6.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code navigates to the infinite scroll website to simulate infinite scrolling.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AJSoPlVIw8Wh-uaQD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AJSoPlVIw8Wh-uaQD.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code fetches the initial height of the page and stores it in a variable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2074%2F0%2A2utzzFMkrUOl-7PZ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2074%2F0%2A2utzzFMkrUOl-7PZ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code starts a loop to scroll till the end of the web page is reached.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2112%2F0%2AiDuIK3N7INwjyLyS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2112%2F0%2AiDuIK3N7INwjyLyS.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code executes the first *scrollTo() *script using the initial height to start scrolling.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2606%2F0%2Az2Azw7QAoRHo98dy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2606%2F0%2Az2Azw7QAoRHo98dy.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code fetches the web page height again and checks if the heights equal the initial height value. If heights are equal, it means the end of the web page is reached, and it breaks the loop and stops scrolling.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A89yTu-TIJ-H1TxR5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A89yTu-TIJ-H1TxR5.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code condition is responsible for checking if the height is equal or not equal, replacing the value, and continuing scrolling until the end of the web page is reached.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On executing the above code, you can see output like the below on the local and the LambdaTest dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2Ae5o1z4IsRnD62Ea4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2Ae5o1z4IsRnD62Ea4.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AM42csi3OeBsenqjr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AM42csi3OeBsenqjr.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Ready to enhance your UX? Explore the best &lt;a href="https://www.lambdatest.com/blog/top-usability-testing-tools-for-your-website/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_6&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;usability testing tools&lt;/a&gt; and ensure your site meets user needs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How to scroll up to the top in Selenium WebDriver?
&lt;/h2&gt;

&lt;p&gt;In addition to Selenium’s functionalities for scrolling down and scrolling to a specific element, scrolling to the top of the web page is a frequently used operation facilitated by the *JavaScriptExecutor *interface. There are various approaches to scrolling to the top of the page.&lt;/p&gt;

&lt;p&gt;To achieve this operation, we initially perform a scroll down using Selenium in Java, followed by a scroll up of the page.&lt;/p&gt;

&lt;p&gt;In the following implementation, we navigate to the &lt;a href="https://www.lambdatest.com/selenium-playground/" rel="noopener noreferrer"&gt;LambdaTest Selenium Playground&lt;/a&gt; web page and scroll up to the top in Selenium WebDriver.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Scroll up to the top in Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;This demonstration is part of the scroll down in the Selenium WebDriver, aiming to perform a scroll to the top of the web page using Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The provided code below consists of functions that perform a scroll to the top of a web page using Selenium WebDriver.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package test;


    import org.testng.annotations.Test;


    public class ScrollToTheTopOfPage extends BaseTest
    {
       public void navigateAndScrollToBottom()
       {
           //launch the website
           driver.get("https://www.lambdatest.com/selenium-playground/");

           //get the height of the web page and scroll to the end
           js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
       }

       @Test
       public void testScrollToTop_Method_1()
       {
           navigateAndScrollToBottom();

           js.executeScript("window.scrollTo(document.body.scrollHeight,0)");
       }

       @Test
       public void testScrollToTop_Method_2()
       {
           navigateAndScrollToBottom();

           js.executeScript("window.scrollTo(0,0)");
       }

       @Test
       public void testScrollToTop_Method_3()
       {
           navigateAndScrollToBottom();

           js.executeScript("window.scrollTo(0,-document.body.scrollHeight)");
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s delve into the functionality of the code before examining the results. The below walkthrough will give us a better understanding of the functions used in the code implementation above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Ay6VYQomIah8kDnnx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Ay6VYQomIah8kDnnx.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code adds a new test class and extends &lt;em&gt;BaseTest&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2536%2F0%2AAV4XBKFts4B0lUNJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2536%2F0%2AAV4XBKFts4B0lUNJ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The provided code introduces the first method named &lt;em&gt;navigateAndScrollToBottom()&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This is a common method for navigating the LambdaTest Selenium Playground and scrolling down the page. This method will be invoked in various approaches to showcase scrolling to the top.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AKKFIGJvF7H5gKQFu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AKKFIGJvF7H5gKQFu.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code in the first approach uses the &lt;em&gt;scrollTo()&lt;/em&gt; method with the x coordinate as the max scroll height and the y coordinate as the 0 value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AZnUj-125lKEeB1EY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AZnUj-125lKEeB1EY.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code uses the &lt;em&gt;scrollTo()&lt;/em&gt; method that uses x and y coordinates as 0.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2418%2F0%2AU8BDQG_jVHva5OPs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2418%2F0%2AU8BDQG_jVHva5OPs.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code passes the y-coordinate value as a negative max height to scroll to the top.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On executing the above code, you can see output like the below on the local and the LambdaTest dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AZY-EbBHi6h5f0N_i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AZY-EbBHi6h5f0N_i.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AvERznIJgNWiUAUPM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AvERznIJgNWiUAUPM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to scroll horizontally to a specific element in Selenium WebDriver?
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;scrollIntoView()&lt;/em&gt; method in JavaScript is used in conjunction with the WebElement to which horizontal scroll has to be performed. Their combination is passed to the &lt;em&gt;executeScript()&lt;/em&gt; method to realize the scroll to an element in Selenium.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript(“arguments[0].scrollIntoView();”, Element);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the following implementation, we navigate to the LambdaTest Selenium Playground web page and scroll horizontally to a specific element in Selenium WebDriver.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Ready to boost your web development skills? Explore the best &lt;a href="https://www.lambdatest.com/blog/top-python-web-development-frameworks/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_6&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Python web frameworks&lt;/a&gt; and take your projects to the next level.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Demonstration: Scroll horizontally to a specific element in Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;This demonstration is part of the scroll down in the Selenium WebDriver, aiming to perform a scroll operation to a specific page element using Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The provided code below consists of functions that perform the scroll to specific elements on a web page using Selenium WebDriver.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    package test;


    import org.openqa.selenium.*;
    import org.testng.annotations.Test;


    public class ScrollToSpecificElement extends BaseTest
    {


       @Test
       public void testScrollToSpecificElement()
       {
           //launch the website
           driver.get("https://www.lambdatest.com/selenium-playground");

           //locate the element
           WebElement element = driver.findElement(By.linkText("Changelog"));

           //scroll to the element
           js.executeScript("arguments[0].scrollIntoView();", element);


       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us understand the working code implementation above by breaking down the functionality of the code in step by step procedure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Ach9JZ5Oxk7IxpAU6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Ach9JZ5Oxk7IxpAU6.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code adds a new test class, &lt;em&gt;testScrollToSpecificElement(),&lt;/em&gt; and creates a test method by extending the *BaseTest *class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AoMz8SJUR48yDkvm-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AoMz8SJUR48yDkvm-.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code navigates to the LambdaTest Selenium Playground website and identifies the Changelog WebElement, which will be used for scrolling.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AWcbfZ4Qe4mFZSUuy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AWcbfZ4Qe4mFZSUuy.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A7WA4ZDvEeniHM6Z_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A7WA4ZDvEeniHM6Z_.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code scrolls to the element using the &lt;em&gt;scrollIntoView()&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On executing the above code, you can see output like the below on the local and the LambdaTest dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AzH9G_ZA5jM6zAgG2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AzH9G_ZA5jM6zAgG2.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AForpjYAJyKy3Q-TX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2800%2F0%2AForpjYAJyKy3Q-TX.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;To summarize, we have learned many ways to scroll that can be performed on web pages using Selenium with Java. Automating the page scroll operation using the &lt;em&gt;JavaScriptExecutor&lt;/em&gt; interface is one of the widely used operations for Selenium automation testing. Do let us know how you scroll down in Selenium for &lt;a href="https://www.lambdatest.com/blog/get-started-with-automated-browser-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_6&amp;amp;utm_term=rj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automated browser testing&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy Scrolling…!&lt;/p&gt;

</description>
      <category>selenium</category>
      <category>seleniumwebdriver</category>
    </item>
    <item>
      <title>How to Fix NoSuchElementException in Selenium</title>
      <dc:creator>Vipulgupta9090</dc:creator>
      <pubDate>Wed, 17 Jan 2024 07:57:41 +0000</pubDate>
      <link>https://dev.to/testmuai/how-to-fix-nosuchelementexception-in-selenium-4koh</link>
      <guid>https://dev.to/testmuai/how-to-fix-nosuchelementexception-in-selenium-4koh</guid>
      <description>&lt;p&gt;Dynamic websites have become prevalent in today’s digital world. They introduce many challenges related to digital experiences, such as the dynamic loading of pages and WebElements. The dynamic loading of web pages and elements has become a significant concern.&lt;/p&gt;

&lt;p&gt;These elements load asynchronously and at differing rates, often posing formidable challenges for automation engineers. The complexity of managing these slow-loading elements can turn into a recurring issue. The intermittently slow loading times can lead to test case failures, rendering automation scripts unreliable and flaky.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Optimize your data management workflow with our SQL to YAML Converter. Convert SQL queries to YAML files and easily manage and share data configurations. *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One of the top contributors to these failures is the &lt;em&gt;NoSuchElementException&lt;/em&gt;. This exception is raised when an element cannot be found in the &lt;a href="https://www.lambdatest.com/blog/document-object-model/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;DOM&lt;/a&gt; while working with Selenium for writing automation for lazy loading websites on which all the elements are not loaded instantly. Dealing with &lt;em&gt;NoSuchElementException&lt;/em&gt; is challenging as it’s an Unchecked Exception, necessitating graceful in-code handling due to its inability to be declared in a method’s throws clause.&lt;/p&gt;

&lt;p&gt;In this blog, we will dive deep into the &lt;em&gt;NoSuchElementException&lt;/em&gt; in &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; and understand the reasons for its occurrence. By the end of this blog, you will have a better understanding of how to handle *NoSuchElementException *in &lt;a href="https://www.lambdatest.com/learning-hub/selenium-java" rel="noopener noreferrer"&gt;Selenium Java&lt;/a&gt; and improve the reliability of automated test cases on dynamic websites.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a NoSuchElementException?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;NoSuchElementException&lt;/em&gt; is one of the most common &lt;a href="https://www.lambdatest.com/learning-hub/selenium-java?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;exceptions in Selenium&lt;/a&gt;, which occurs when an element is not found on the page. It indicates that the code could not locate and interact with a particular element on the page.&lt;/p&gt;

&lt;p&gt;In Selenium automation tests, &lt;em&gt;NoSuchElementException&lt;/em&gt; is thrown by &lt;em&gt;WebDriver.findElement()&lt;/em&gt; and &lt;em&gt;WebDriver.findElements()&lt;/em&gt; methods. It is derived from &lt;em&gt;NotFoundException&lt;/em&gt;. You can learn more about it through this blog on &lt;a href="https://www.lambdatest.com/blog/findelement-and-findelements-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;*findElement() and findElements() *methods in Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For example, if you navigate to the LambdaTest eCommerce Playground, you can notice that a few elements are loaded dynamically, and the page looks like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_7GtUl_Evq0igGSQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_7GtUl_Evq0igGSQ.png" width="800" height="538"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point, if WebDriver in the Selenium automation code tries to interact with one of the dynamic elements, it throws a &lt;em&gt;NoSuchElementException&lt;/em&gt;. So, we should wait for these slow-loading elements to be available first and then proceed further.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AaMqAa7jCxH423re5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AaMqAa7jCxH423re5.png" width="800" height="548"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reasons for NoSuchElementException in Selenium
&lt;/h2&gt;

&lt;p&gt;A &lt;em&gt;NoSuchElementException&lt;/em&gt; can occur due to failure in locating or interacting with a WebElement on a web page while working with Selenium automation scripts due to the reasons listed below:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Online &lt;a href="https://www.lambdatest.com/free-online-tools/css-to-stylus?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;CSS to STYLUS&lt;/a&gt; converter to convert CSS string to Styl. Get clean, readable code instantly. Start converting today! *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Invalid WebElement Locators
&lt;/h2&gt;

&lt;p&gt;Incorrect or wrongly written &lt;em&gt;WebElement *locators make for the most number of *NoSuchElementException&lt;/em&gt;(s). The *WebElement *locator given could be wrong or, at times, too broad that it references multiple elements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Aw0j-MBNr2OmZToUJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Aw0j-MBNr2OmZToUJ.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the screenshot above, the locator is correct, but it references 57 WebElements on the page, which could lead to *NoSuchElementException *when executing the tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Slow Loading WebElements
&lt;/h2&gt;

&lt;p&gt;Selenium tests are considerably faster when executed in good network conditions. This causes elements to be absent when the automation script tries to act on them.&lt;/p&gt;

&lt;p&gt;This can be resolved using waits such as &lt;a href="https://www.lambdatest.com/blog/types-of-waits-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Implicit waits and Explicit waits&lt;/a&gt;. We will be covering the same in the subsequent sections of the blog.&lt;/p&gt;

&lt;h2&gt;
  
  
  iFrame Issues
&lt;/h2&gt;

&lt;p&gt;Many times, the WebElement is present inside another Frame (or iFrame). Selenium script needs to switch to the frame to interact with the WebElement; otherwise, it will result in &lt;em&gt;NoSuchElementException&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Als6EUo4rl28NgZlt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Als6EUo4rl28NgZlt.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Design changes on the web page
&lt;/h2&gt;

&lt;p&gt;Website designs tend to be revamped regularly after the releases. This is mostly done to add new features and improve user experience and accessibility. Due to this HTML, the structure is impacted the most, and many WebElements could get a new reference path on the webpage or become obsolete in some cases. This leads to *NoSuchElementException *when the automation script still references the WebElement with old locators.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Need realistic sample data? Use our free online easy-to-use &lt;a href="https://www.lambdatest.com/blog/types-of-waits-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Random XML Generator&lt;/a&gt; to create custom XML data for testing and prototyping. No sign-up required. *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Best Practices to fix NoSuchElementException in Selenium
&lt;/h2&gt;

&lt;p&gt;Handling *NoSuchElementException *is important because it helps to make the test scripts more reliable and robust and also prevents them from failing intermittently due to the unavailability of the WebElement.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check if the element exists and is displayed
&lt;/h2&gt;

&lt;p&gt;Before directly interacting with the element, always make sure it is present on the page and is displayed. For this use, &lt;em&gt;findElementBy()&lt;/em&gt; and &lt;em&gt;isDisplayed()&lt;/em&gt; functions are provided by &lt;a href="https://www.lambdatest.com/learning-hub/webdriver?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium WebDriver&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;findElementBy()&lt;/em&gt; returns the object of the first matching WebElement of the given element locator on the webpage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;isDisplayed()&lt;/em&gt; method of Selenium returns true if a certain WebElement is present and displayed on the webpage. If not, it returns false.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Use waits
&lt;/h2&gt;

&lt;p&gt;Using waits is one of the most commonly used ways to prevent *NoSuchElementException *from occurring in case of slow-loading WebElements and pages. Waits help to make WebDriver wait for the presence of the element or for the page to be loaded before interacting with the element.&lt;/p&gt;

&lt;p&gt;Selenium WebDriver provides support for Implicit and Explicit waits. Implicit waits means to set a wait time for an element to be loaded and is implemented to all the elements in the automation script. Explicit waits, the recommended approach, allow waiting for a specific element to prevent unnecessary execution time and &lt;em&gt;NoSuchElementException&lt;/em&gt;. &lt;a href="https://www.lambdatest.com/blog/selenium-webdriverwait/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;*WebDriverWait&lt;/a&gt; *has been used in the demonstration section to showcase this better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use more robust locators
&lt;/h2&gt;

&lt;p&gt;Always use relative &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;XPath&lt;/a&gt; locators compared to absolute ones. This is because relative XPaths are less susceptible to changes when page structure gets modified and thus reduces the chances of exception.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AgNiBHOoRxwZ7B3-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AgNiBHOoRxwZ7B3-1.png" width="800" height="549"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In addition, you must try and use unique locators like ID or Name or a combination of both. These tend to remain the same for the WebElements. This prevents rework in case of cosmetic changes to handle the &lt;em&gt;NoSuchElementException&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;If these approaches are not suitable and your page content is highly dynamic, try to use a dynamic locator like &lt;em&gt;xpath[contains(text(),’text’)]&lt;/em&gt; to locate the element using text reference, which won’t change.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Need YAML data for testing? No need to stress about YAML data — Try our &lt;a href="https://www.lambdatest.com/free-online-tools/random-yaml-generator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;Random YAML Generator&lt;/a&gt; for a quick and effortless way to generate YAML data in seconds. *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Use try..catch blocks
&lt;/h2&gt;

&lt;p&gt;Wrap your code inside a try-catch block. This helps to catch the *NoSuchElementException *if it occurs and lets the code continue without making it stop abruptly. For this, add the automation code to fetch the WebElement and perform an action on it inside the try block. Then, add a catch block to catch the exception if it occurs and perform any required actions. This helps the code to gracefully handle an exception and prevent it from behaving abruptly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2712%2F0%2AqrMSTVvSQKGYFsWe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2712%2F0%2AqrMSTVvSQKGYFsWe.png" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Switch to Frames and iFrames
&lt;/h2&gt;

&lt;p&gt;Always check if the element is present inside a frame. If yes, use the driver method &lt;em&gt;switchTo()&lt;/em&gt; to change driver control to the required frame first and then interact with the WebElement. Once the action is completed, remember to switch the driver back to the main window or the parent frame using &lt;em&gt;driver.switchTo().defaultContent()&lt;/em&gt;. You can learn more about it through this blog on &lt;a href="https://www.lambdatest.com/blog/switch-iframes-in-selenium-java/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;iFrames in Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: How to fix NoSuchElementException in Selenium?
&lt;/h2&gt;

&lt;p&gt;To demonstrate the possible ways to fix and handle *NoSuchElementException *in Selenium, we will create a Java project with Maven and &lt;a href="https://www.lambdatest.com/learning-hub/testng?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;TestNG&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this blog, Eclipse IDE has been used for demonstration purposes. You can choose any IDE of your choice. The implementation remains the same on all of them till a Maven project is created.&lt;/p&gt;

&lt;p&gt;As we are working with the Selenium web automation script, we need to add Selenium dependency along with TestNG, which will be used for test execution to the pom.xml of the project.&lt;/p&gt;

&lt;p&gt;Once the project is created and dependencies are added, the final pom.xml will be like the one below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;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 https://maven.apache.org/xsd/maven-4.0.0.xsd"&amp;gt;
   &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
   &amp;lt;groupId&amp;gt;NoSuchElementException&amp;lt;/groupId&amp;gt;
   &amp;lt;artifactId&amp;gt;NoSuchElementException&amp;lt;/artifactId&amp;gt;
   &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;
   &amp;lt;build&amp;gt;
       &amp;lt;sourceDirectory&amp;gt;src&amp;lt;/sourceDirectory&amp;gt;
       &amp;lt;plugins&amp;gt;
           &amp;lt;plugin&amp;gt;
               &amp;lt;artifactId&amp;gt;maven-compiler-plugin&amp;lt;/artifactId&amp;gt;
               &amp;lt;version&amp;gt;3.8.1&amp;lt;/version&amp;gt;
               &amp;lt;configuration&amp;gt;
                   &amp;lt;release&amp;gt;19&amp;lt;/release&amp;gt;
               &amp;lt;/configuration&amp;gt;
           &amp;lt;/plugin&amp;gt;
       &amp;lt;/plugins&amp;gt;
   &amp;lt;/build&amp;gt;
   &amp;lt;dependencies&amp;gt;
       &amp;lt;dependency&amp;gt;
           &amp;lt;groupId&amp;gt;org.seleniumhq.selenium&amp;lt;/groupId&amp;gt;
           &amp;lt;artifactId&amp;gt;selenium-java&amp;lt;/artifactId&amp;gt;
           &amp;lt;version&amp;gt;4.8.0&amp;lt;/version&amp;gt;
       &amp;lt;/dependency&amp;gt;
       &amp;lt;dependency&amp;gt;
           &amp;lt;groupId&amp;gt;org.testng&amp;lt;/groupId&amp;gt;
           &amp;lt;artifactId&amp;gt;testng&amp;lt;/artifactId&amp;gt;
           &amp;lt;version&amp;gt;7.7.1&amp;lt;/version&amp;gt;
           &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
       &amp;lt;/dependency&amp;gt;
   &amp;lt;/dependencies&amp;gt;
&amp;lt;/project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next add a new Java test file TestSeleniumNoSuchElementException.java. We will add our &lt;a href="https://www.lambdatest.com/learning-hub/test-case?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test cases&lt;/a&gt; to this to see how to fix *NoSuchElementException *in Selenium.&lt;/p&gt;

&lt;p&gt;To handle *NoSuchElementException *in Selenium, we will use a &lt;a href="https://www.lambdatest.com/selenium-grid-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;cloud Selenium Grid&lt;/a&gt; like LambdaTest for &lt;a href="https://www.lambdatest.com/selenium-java-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium Java testing&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Need decimal fractions for your project or testing? Use our free and fast online &lt;a href="https://www.lambdatest.com/free-online-tools/random-decimal-fraction-generator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;Random decimal Fraction Generator&lt;/a&gt; to generate random decimal fractions in seconds! *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;LambdaTest is an AI-powered test orchestration and execution platform to run manual and automated tests at scale. The platform allows you to perform real-time and &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automation testing&lt;/a&gt; across 3000+ environments and real mobile devices. With parallel testing in &lt;a href="https://www.lambdatest.com/java-automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Java automation testing&lt;/a&gt; on LambdaTest, you can significantly reduce the time it takes to run your tests, especially if you have a large number of tests or need to test your application across a wide range of environments.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/SqQ8SugRDos"&gt;
&lt;/iframe&gt;
&lt;br&gt;
After adding all the WebDriver configurations and test case examples to demonstrate the fix for &lt;em&gt;NoSuchElementException&lt;/em&gt;, the completed file should look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;


import java.net.*;
import java.time.Duration;
import java.util.HashMap;


import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.*;


public class TestSeleniumNoSuchElementException {


   public RemoteWebDriver driver = null;


   String username = System.getenv("LT_USERNAME") == null ? "&amp;lt;lambdatest_username&amp;gt;" : System.getenv("LT_USERNAME");
   String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "&amp;lt;lambdatest_accesskey&amp;gt;"
           : System.getenv("LT_ACCESS_KEY");


   @BeforeTest
   public void setup() {
       try {
           ChromeOptions chromeOptions = new ChromeOptions();
           chromeOptions.setPlatformName("Windows 10");
           chromeOptions.setBrowserVersion("110.0");


           HashMap&amp;lt;String, Object&amp;gt; ltOptions = new HashMap&amp;lt;String, Object&amp;gt;();
           ltOptions.put("build", "NoSuchElementException in Selenium");
           chromeOptions.setCapability("LT:Options", ltOptions);


           driver = new RemoteWebDriver(
                   new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), chromeOptions);
       } catch (MalformedURLException e) {
           e.printStackTrace();
       }


   }


   @AfterTest
   public void tearDown() {
       driver.quit();
   }


   @Test
   public void testNoSuchElmentException() {
       driver.get("https://ecommerce-playground.lambdatest.io/");
       driver.findElement(By.id("entry_213244")).click();
   }


   @Test
   public void testNoSuchElmentException_fix_tryCatch() {
       driver.get("https://ecommerce-playground.lambdatest.io/");
       try {
           driver.findElement(By.id("entry_213244")).click();
       } catch (NoSuchElementException ex) {
           System.out.println("We are inside catch block");
           System.out.println("NoSuchElementException has been handled.");
       }
   }


   @Test
   public void testNoSuchElmentException_fix_wait() {
       driver.get("https://ecommerce-playground.lambdatest.io/");


       WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
       wait.until(ExpectedConditions.presenceOfElementLocated(By.id("entry_213244")));


       driver.findElement(By.id("entry_213244")).click();
       System.out.println("Web element located and clicked successfully.");
   }


   @Test
   public void testNoSuchElmentException_iframe() {
       driver.get("https://play1.automationcamp.ir/frames.html");
       driver.findElement(By.id("click_me_1")).click();
   }


   @Test
   public void testNoSuchElmentException_iframe_fix() {
       driver.get("https://play1.automationcamp.ir/frames.html");


       driver.switchTo().frame("frame1");
       System.out.println("Switched to frame 1");


       driver.findElement(By.id("click_me_1")).click();
       System.out.println("Web element located and clicked successfully.");


       driver.switchTo().defaultContent();
       System.out.println("Switched back to main window");
   }
}

       driver.findElement(By.id("click_me_1")).click();
       System.out.println("Web element located and clicked successfully.");


       driver.switchTo().defaultContent();
       System.out.println("Switched back to main window");
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; We create an instance of &lt;a href="https://www.lambdatest.com/blog/selenium-remotewebdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;RemoteWebDriver&lt;/a&gt; as we execute the code on the Selenium cloud Grid.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AMxefNQPUFgju8PIP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AMxefNQPUFgju8PIP.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Looking for an easy way to generate random octal numbers? Use our online &lt;a href="https://www.lambdatest.com/free-online-tools/random-octal-generator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;Random Octal Generator&lt;/a&gt; tool to generate random numbers in octal format. Try it now for free. *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Navigate to the LambdaTest Profile section once you create your account to fetch the username and access key. These will be used to make a connection to their cloud grid.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_zwqTw4VRBcANRIW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_zwqTw4VRBcANRIW.png" width="800" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also configure the username and access key as environment variables using the following commands and directly fetch them in code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For MacOS and Linux:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export LT_USERNAME=LT_USERNAME
export LT_ACCESS_KEY=LT_ACCESS_KEY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;For Windows:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set LT_USERNAME=LT_USERNAME
set LT_ACCESS_KEY=LT_ACCESS_KEY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Create a base method, name it &lt;em&gt;setup()&lt;/em&gt;, and annotate with &lt;em&gt;@BeforeTest&lt;/em&gt; &lt;a href="https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;annotation in TestNG&lt;/a&gt;. This method will be used for driver setup and opening the webpage before each test.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ADT0C6HyX79eg4Sdq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ADT0C6HyX79eg4Sdq.png" width="574" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this blog on Selenium Java, we have used &lt;a href="https://www.lambdatest.com/learning-hub/selenium-4?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium 4&lt;/a&gt;, which supports the &lt;em&gt;ChromeOptions&lt;/em&gt; class. In this function, an object of the same is created to define browser properties like browser version, OS version, etc. It also accepts the variables required for LambdaTest execution as a capability. These variables are used to set values like build, name, or any other browser property, which will help to identify the run on the LambdaTest Dashboard or modify behavior.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AicnBkqDcsOKYJIBf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AicnBkqDcsOKYJIBf.png" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; Finally, an instance of RemoteWebDriver is created to connect to the LambdaTest cloud grid using the *chromeOptions *and your LambdaTest username and the access key.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Ak5iB3TuvCDbfo49b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Ak5iB3TuvCDbfo49b.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; The LambdaTest platform also contributes by offering ready-to-use code for various OS and browser combinations and numerous browser configurations. To get an instantly valid code for your automation script, go to their &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Automation Capabilities Generator&lt;/a&gt; and make the necessary setup selections.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Want to decode a messy URL? Unleash the full potential of your website with our online &lt;a href="https://www.lambdatest.com/free-online-tools/url-parse?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;URL Parse&lt;/a&gt; tool. Optimize your website’s URLs to improve your site’s visibility. *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AeuC-HCJcI27YY3gb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AeuC-HCJcI27YY3gb.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6.&lt;/strong&gt; Add a method as &lt;em&gt;tearDown()&lt;/em&gt; and annotate it with &lt;em&gt;@AfterTest&lt;/em&gt; annotation. This will be used to close the session after each test case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AWWOpHBeHbb65qU6j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AWWOpHBeHbb65qU6j.png" width="648" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Example: NoSuchElementException in Selenium
&lt;/h2&gt;

&lt;p&gt;Add the first test case and name it &lt;em&gt;testNoSuchElmentException()&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In this test, we navigate to a website with a few slow loading elements. Then, we try to click on one such element and observe the results.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2ABhbMl6aeymYxh_rX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2ABhbMl6aeymYxh_rX.png" width="800" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On executing this, you can observe that the test case fails with *NoSuchElementException *as the element is not yet available to be clicked as it is not loaded yet, and hence the element is unavailable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AZLiRoFSj5hy0eOFf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AZLiRoFSj5hy0eOFf.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another reason could be that the passed WebElement locator is incorrect, and no element is present with that reference.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Need to &lt;a href="https://www.lambdatest.com/free-online-tools/extract-text-from-html?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;extract text from HTML&lt;/a&gt; code? Our free online tool makes it easy and fast. Save time by extracting only the text you need from HTML code in no time.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the next examples, we see how to fix this *NoSuchElementException *in Selenium.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fixing NoSuchElementException in Selenium using try-catch
&lt;/h3&gt;

&lt;p&gt;As mentioned in earlier sections, one of the most popular ways to handle *NoSuchElementException *in Selenium is using the try-catch block.&lt;/p&gt;

&lt;p&gt;You can enclose the WebElement interaction code within a try block and subsequently include a catch block to address any potential *NoSuchElementException *occurrences. Within the catch block, you can execute additional functions in alignment with your automation script’s needs.&lt;/p&gt;

&lt;p&gt;For this, add a new test case named &lt;em&gt;testNoSuchElmentException_fix_tryCatch()&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2104%2F0%2AOonn-dhsIj9xmVYJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2104%2F0%2AOonn-dhsIj9xmVYJ.png" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this, we use the same code as the previous example, which raised an exception, but place that inside the try block.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2160%2F0%2AifbR2btxARcFoEdX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2160%2F0%2AifbR2btxARcFoEdX.png" width="800" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add a catch block following this try block to catch the *NoSuchElementException *if it occurs and to continue the execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AVeRUKvXDYS8wzeFp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AVeRUKvXDYS8wzeFp.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s try to execute the code and see the output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A6LuWD-U6qqm6itb0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A6LuWD-U6qqm6itb0.png" width="800" height="545"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Fixing NoSuchElementException in Selenium using WebDriverWait
&lt;/h3&gt;

&lt;p&gt;Another popular way to fix &lt;em&gt;NoSuchElementException *in your code is to use *WebDriverWait&lt;/em&gt;. This should be used in case of slow loading elements on a webpage. These can also be helpful when we run automation code by &lt;a href="https://www.lambdatest.com/blog/test-mobile-websites-on-different-network-conditions/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;throttling the network speeds&lt;/a&gt; to simulate different conditions and check the behavior. It allows the automation script to wait for a given amount of time for a WebElement to be visible and then proceed with the interaction instead of directly throwing the &lt;em&gt;NoSuchElementException&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Using waits is very helpful when the automation script is running on slow internet networks. In such situations, we can analyze how long a certain WebElement takes to be present on the web page. This data can then be used to define wait duration in automation scripts for different WebElements.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/extract-text-from-json?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;**Extract text from JSON&lt;/a&gt; data with our powerful and easy-to-use online free tool. Get the data you need quickly and efficiently, no technical skills required. **&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;WebDriverWait *is a class provided as a part of Selenium dependency using an object to which we can add an &lt;a href="https://www.lambdatest.com/blog/expected-conditions-in-selenium-examples/" rel="noopener noreferrer"&gt;*ExpectedCondition&lt;/a&gt;&lt;/em&gt; on the WebElement before proceeding.&lt;/p&gt;

&lt;p&gt;For this, add another test case and navigate to the target website URL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2276%2F0%2ApWs9MqiQn-qBy2_X.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2276%2F0%2ApWs9MqiQn-qBy2_X.png" width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create an object of the *WebDriverWait *class and pass the driver object and the duration we want to wait for the element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2508%2F0%2AmH5FugebdpgkKq6G.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2508%2F0%2AmH5FugebdpgkKq6G.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using this object with the &lt;em&gt;until()&lt;/em&gt; function, we add an *ExpectedConditions *suggesting to wait for the presence of a given element by specifying the locator.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2768%2F0%2AQCeqy9wioj9j5bLx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2768%2F0%2AQCeqy9wioj9j5bLx.png" width="800" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since we have configured *WebDriverWait *time to be 5 seconds, this will make the WebDriver wait for a maximum of 5 seconds before throwing an exception if WebElement is not found.&lt;/p&gt;

&lt;p&gt;If the element is found before the given time, WebDriver continues the execution by passing the control to next code statements. This allows the interaction by clicking on the element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2452%2F0%2ALakurOeMl4QxAytx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2452%2F0%2ALakurOeMl4QxAytx.png" width="800" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Executing this would provide a successful output as the WebElement is present before we click on it.&lt;/p&gt;

&lt;p&gt;These *ExpectedConditions *are used to implement Explicit waits. These are dedicated to a single WebElement and, unlike Implicit waits, are not applied to all the WebElements on the webpage. Thus, these prevent *NoSuchElementException *from happening without adding unnecessary execution times.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AbQ72IUy8GsHbCzj4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AbQ72IUy8GsHbCzj4.png" width="800" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Example: NoSuchElementException in Selenium due to iFrames
&lt;/h2&gt;

&lt;p&gt;Another reason for *NoSuchElementException *is that the WebElement is inside an iFrame.&lt;/p&gt;

&lt;p&gt;To understand the same, consider the below example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2276%2F0%2AkhUp9ilrW-mzVURT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2276%2F0%2AkhUp9ilrW-mzVURT.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looking at this code, it seems quite similar to previous examples. But the difference here is that the WebElement is inside a child frame and not on the main window.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Get your CSS validated by our &lt;a href="https://www.lambdatest.com/free-online-tools/css-validator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;CSS Validator&lt;/a&gt; and eliminate syntax errors and other issues that could affect your website’s performance. Ensure your CSS code is error-free today! *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can check if a WebElement is inside an iFrame or not by going to the Inspect Tools.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A2t5pwmPA5EX31MdW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A2t5pwmPA5EX31MdW.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hence, executing this would result in a *NoSuchElementException *like the one below, as it will not be found by the driver in its focus window, which is the main window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AIXiZWQgnvKWltXT4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AIXiZWQgnvKWltXT4.png" width="800" height="556"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Fixing NoSuchElementException in Selenium due to iFrames
&lt;/h3&gt;

&lt;p&gt;You can identify if you are dealing with an iFrame by right-clicking next to the WebElement. If it has options like View &lt;em&gt;Frame Source&lt;/em&gt; or &lt;em&gt;Reload Frame&lt;/em&gt;, it is an iFrame or Frame.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AVSHgy-aMRHeeV9Wp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AVSHgy-aMRHeeV9Wp.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, to get the frame ID, click on Inspect from the same menu and try to locate the parent &amp;lt; *iframe *&amp;gt; tag for your WebElement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_GV0dF27ROWUrhW0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_GV0dF27ROWUrhW0.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Consider a new test case, &lt;em&gt;testNoSuchElmentException_iframe_fix()&lt;/em&gt;, and navigate to the website.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2160%2F0%2ANEKouPfB_fpn8-ld.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2160%2F0%2ANEKouPfB_fpn8-ld.png" width="800" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, switch to the iFrame by passing its &lt;em&gt;ID&lt;/em&gt; to the driver.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AfQC5CHjxq8RG2YTz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AfQC5CHjxq8RG2YTz.png" width="800" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After this, click on the element. This time, there will be no &lt;em&gt;NoSuchElementException *as the driver is focused on the current frame window, and the element is inside the same iFrame to which we switched earlier using the *switchTo()&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2452%2F0%2AGV08tj-zbdz40bx6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2452%2F0%2AGV08tj-zbdz40bx6.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lastly, switch back the driver to focus on the main window as this is from where all interactions are started.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AHjIwlOUMamKNPfnv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AHjIwlOUMamKNPfnv.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Protect your XML data with our online free &lt;a href="https://www.lambdatest.com/free-online-tools/xml-escape?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;XML Escape&lt;/a&gt; tool. Quickly escape your code and be assured from vulnerabilities with just a few clicks! *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Executing the above test case would give a successful response like the one below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2572%2F0%2AOSp5v80EadIjgBmz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2572%2F0%2AOSp5v80EadIjgBmz.png" width="800" height="556"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: NoSuchElementException in Java
&lt;/h2&gt;

&lt;p&gt;In Java, this exception can be thrown by various methods that can be used for data retrieval to indicate that the element requested does not exist.&lt;/p&gt;

&lt;p&gt;For example, if you try to retrieve data from an empty data structure or (n+1)th element from a data structure of n length, then *NoSuchElementException *is thrown.&lt;/p&gt;

&lt;p&gt;To fix the &lt;em&gt;NoSuchElementException&lt;/em&gt;, it should be ensured that the underlying data structure contains more elements before trying to retrieve the next element or by implementing proper exception handling using try-catch mechanisms.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fixing NoSuchElementException in Java
&lt;/h3&gt;

&lt;p&gt;Let’s look at the example to fix *NoSuchElementException *in Java. For this, we keep using the same project setup and add another test class to understand this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Scenario:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a HashMap.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add 2 elements to this HashMap.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create an object of Iterator to fetch keys from HashMap.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Execute the &lt;em&gt;next()&lt;/em&gt; function of the iterator 3 times to fetch data.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, since we have added only 2 elements, this example should throw a &lt;em&gt;NoSuchElementException&lt;/em&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Test
   public void testNoSuchElementException()
   {
       HashMap&amp;lt;Integer, Integer&amp;gt; map = new HashMap&amp;lt;&amp;gt;();
       map.put(1, 1);
       map.put(2, 2);

       Iterator itr = map.keySet().iterator();
       System.out.println("first element : " + itr.next());
       System.out.println("second element : " + itr.next());
       System.out.println("third element : " + itr.next());
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Executing above code you should get an output like below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AdAik07mOzy49OUpR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AdAik07mOzy49OUpR.png" width="800" height="689"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the above screenshot, you can see this test throws *NoSuchElementException *as there is no third element.&lt;/p&gt;

&lt;p&gt;To fix this &lt;em&gt;NoSuchElementException&lt;/em&gt;, we use the &lt;em&gt;hasNext()&lt;/em&gt; function of the Iterator. This function is used to fix this exception, as before trying to fetch and print the element, it helps to check whether an element is present. If it finds more elements, it continues to print. Otherwise, it helps to stop the flow without any exceptions.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Test
   public void testNoSuchElementExceptionFixed()
   {
       HashMap&amp;lt;Integer, Integer&amp;gt; map = new HashMap&amp;lt;&amp;gt;();

       map.put(1, 1);
       map.put(2, 2);

       Iterator itr = map.keySet().iterator();

       while(itr.hasNext()) {
           System.out.println("Element : "  + itr.next());
       }
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;On executing the same test with this fix, you can see that the test passed without &lt;em&gt;NoSuchElementException&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2404%2F0%2A28u6wxTwyNb2YVNC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2404%2F0%2A28u6wxTwyNb2YVNC.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Don’t let CSV errors slow you down. Validate and lint your CSV data with ease using our free online &lt;a href="https://www.lambdatest.com/free-online-tools/csv-validator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_17&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;CSV Validator&lt;/a&gt; tool. Get accurate and error-free results in seconds! *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With this, we have come to the end of this blog. In this blog on how to fix *NoSuchElementException *in Selenium, we have learned what a *NoSuchElementException *is and why it could occur while working with Selenium. We also take some example automation scripts and try to understand how it can be fixed and handled more gracefully to prevent abrupt results. So, it’s time to fix all such cases in your automation scripts and get started.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How To Perform Storybook Visual Testing</title>
      <dc:creator>Vipulgupta9090</dc:creator>
      <pubDate>Tue, 02 Jan 2024 10:49:21 +0000</pubDate>
      <link>https://dev.to/vipulgupta9090/how-to-perform-storybook-visual-testing-4ej8</link>
      <guid>https://dev.to/vipulgupta9090/how-to-perform-storybook-visual-testing-4ej8</guid>
      <description>&lt;p&gt;Consider working on a website to provide customers with the best possible user experience and seamless flow. While testing the website, you might find all automated tests passing on all browser and platform combinations with zero issues. However, as soon as the website goes live, bug reports come in large numbers.&lt;/p&gt;

&lt;p&gt;These bugs are not related to the website’s functionality but to its user interface and user experience. They affect the website’s appearance, making it less user-friendly regarding its usability, navigation, and visual appeal. This is because &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=webpage"&gt;automation testing&lt;/a&gt; typically only checks the flow, not whether the user interface (UI) appears correctly. Automation testers have been greatly concerned about this and are looking for ways to deal with it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Make your XML code easy to read and understand with our free online &lt;a href="https://www.lambdatest.com/free-online-tools/xml-prettify?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools"&gt;XML Prettify&lt;/a&gt; tool. Format your code for better readability and save time!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The solution to this problem is &lt;a href="https://www.lambdatest.com/learning-hub/visual-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=learning_hub"&gt;visual testing&lt;/a&gt;. It helps to verify the visual behavior of the AUT (Application Under Test) and checks if the visual aspects of the website, such as the shape, size, color, and positioning of the elements on the webpage, are correctly placed and aligned as per the expected design on different browsers and their versions, operating systems, and screen sizes.&lt;/p&gt;

&lt;p&gt;This is important because each platform, browser, and screen size has different properties, such as resolution and support for different fonts and colors. These properties must be verified to ensure the website looks and functions correctly on all platforms and devices, thus not restricting customers to certain platforms.&lt;/p&gt;

&lt;p&gt;One of the most popular &lt;a href="https://www.lambdatest.com/blog/visual-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=blog"&gt;visual testing tools&lt;/a&gt; is Storybook, which we will implement in this blog. Storybook is an open-source tool for developing and testing user interfaces (UIs) in isolation from the rest of an application. Each piece of UI in Storybook is an independent component.&lt;/p&gt;

&lt;p&gt;One advantage of components is that we don’t need to load the entire application to test a specific UI component. We can render the component in isolation and test it before moving on to the rest of the application. This helps us focus on each variation of a visual component, even the hard-to-reach edge cases. It also ensures that the component behaves consistently across platforms and versions.&lt;/p&gt;

&lt;p&gt;In this blog on Storybook visual testing, we will learn about visual testing and its importance for web applications. We will also know about Storybook, how it works, and how to integrate it with web projects. To demonstrate Storybook visual testing, we will use a cloud platform like LambdaTest to access various browsers and operating system versions to test the web interface for the best results.&lt;/p&gt;

&lt;p&gt;Let’s get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Visual Testing?
&lt;/h2&gt;

&lt;p&gt;Visual testing is a software testing technique that verifies and validates an application’s Graphical User Interface (GUI). It focuses on the visual aspects of the GUI, such as layouts, colors, fonts, images, and overall aesthetics, to ensure that they are consistent, correct, and visually appealing across different platforms and devices.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Need to convert hex to decimal? Our online &lt;a href="https://www.lambdatest.com/free-online-tools/hex-to-decimal?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools"&gt;Hex to Decimal&lt;/a&gt; Converter tool converts hexadecimal to decimal numbers quickly. Get your conversions done in no time.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Consider a scenario of login screen version 1 like the one below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bHRu3XHF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2A5TI5pRRcWrVyYLiT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bHRu3XHF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2A5TI5pRRcWrVyYLiT.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This screen was updated to accommodate two new buttons, but that leads to alignment issues like in below version 2:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2YoXwuq5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AdA_W6bSf4vmkW5cw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2YoXwuq5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AdA_W6bSf4vmkW5cw.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Such visual issues can be more prominent while working with web applications and lead to a bad user experience.&lt;/p&gt;

&lt;p&gt;Conducting visual testing helps to enhance the user experience, maintain brand consistency, and prevent visual defects in applications, ultimately leading to a more polished and user-friendly product.&lt;/p&gt;

&lt;p&gt;It can also identify issues related to responsiveness and multimedia elements, such as logos, icons, videos, and animations. Human testers can perform visual testing manually or using automated scripts and tools like Storybook. &lt;a href="https://www.lambdatest.com/automated-visual-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=webpage"&gt;Automated visual testing&lt;/a&gt; compares the UI under test with reference images or expected results to detect discrepancies.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0MGU8II---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2Ascdr58qp1DBNJ4FH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0MGU8II---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2Ascdr58qp1DBNJ4FH.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Conducting visual testing helps to enhance the user experience, maintain brand consistency, and prevent visual defects in applications, ultimately leading to a more polished and user-friendly product.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Keep your data safe and secure by using our free online &lt;a href="https://www.lambdatest.com/free-online-tools/base64-encode?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools"&gt;Base64 Encode&lt;/a&gt;. With our tool, you can easily convert any type of data into a secured Base64 string. Try it now! *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  How is Visual Testing different from Functional Testing?
&lt;/h3&gt;

&lt;p&gt;Visual and functional testing are two types of testing that verify different aspects of an application. Visual testing focuses on the appearance and user interface, while functional testing focuses on the application’s features. Functional testing verifies that the application works as expected, while visual testing verifies that the application’s visual elements appear correctly on different devices, browsers, and operating systems.&lt;/p&gt;

&lt;p&gt;Visual testing is often used in conjunction with functional testing to ensure that an application is both functional and visually appealing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FGy1474t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AF54KmyeHHKRsnTgDlkU5Xg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FGy1474t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AF54KmyeHHKRsnTgDlkU5Xg.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Visual Testing Work?
&lt;/h2&gt;

&lt;p&gt;Visual testing works by comparing screenshots. It does this by comparing the actual UI to a base screenshot of the page.&lt;/p&gt;

&lt;p&gt;The base image is captured and is used as a reference whenever the visual test is executed. Each test run generates a new screenshot, which is then compared to the base screenshot. If any differences are found, the test is marked as failed. Otherwise, it is passed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CvrSzSmM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AM1LFzSI8PjLBQbsT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CvrSzSmM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AM1LFzSI8PjLBQbsT.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the comparison above, the first image perfectly matches the baseline and passes, while the second image fails because of the many visual differences. These differences are all major and cannot be ignored, so the visual test is marked as failed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/binary-to-decimal?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools"&gt;**Binary to Decimal&lt;/a&gt; Converter is a tool that converts binary numbers into their equivalent decimal form. Simply enter a binary number and let this converter do it for you. &lt;a href="https://www.lambdatest.com/free-online-tools/binary-to-decimal?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools"&gt;https://www.lambdatest.com/free-online-tools/binary-to-decimal&lt;/a&gt;**&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, these differences can sometimes be minor, such as a slight change in color tone due to resolution differences or a different font due to an unsupported font on some platforms or versions. In these cases, the tool or framework used for visual testing should ideally have some built-in intelligence in the comparison algorithm to avoid such failures and only highlight major UI variations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is Visual Testing Important?
&lt;/h2&gt;

&lt;p&gt;Visual testing, like other &lt;a href="https://www.lambdatest.com/learning-hub/software-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=learning_hub"&gt;software testing&lt;/a&gt; techniques, is integral to working with web applications. It provides numerous benefits when performed and helps produce more visually reliable results. These include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;**Browser — Device Compatibility and Responsiveness&lt;br&gt;
**Visual testing ensures an application’s user interface (UI) looks and behaves as expected across different devices and platforms. This includes checking on various operating systems (OS), browsers, screen sizes, and orientations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Consistent User Experience&lt;br&gt;
**Visual testing ensures the visual aspects of a user interface (UI) are correct and consistent. It helps to identify and fix issues with the layout, colors, fonts, and other visual elements of a UI, which can lead to a better user experience and satisfaction.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Verify Images &amp;amp; multimedia&lt;br&gt;
**Verifying that all images and other multimedia elements are displayed correctly and in the intended resolutions is useful. This includes checking that the images are not pixelated or blurry on different platforms and screen sizes and that the videos play smoothly.&lt;br&gt;
Additionally, visual testing helps ensure that the images and videos are aligned correctly with the surrounding text and other elements on the page. By doing so, visual tests help to make web applications visually appealing and user-friendly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Testing Localization&lt;br&gt;
**Visual testing is strongly advised when developing applications for international customers with diverse languages, cultures, and formats. It aids &lt;a href="https://www.lambdatest.com/learning-hub/ui-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=learning_hub"&gt;UI testing&lt;/a&gt; and ensures element consistency for different locales with minimal effort.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the advantages of visual testing strongly advocate its usage while working with web applications to be used with a wide, varied customer base.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/decimal-to-binary?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools"&gt;**Decimal to Binary&lt;/a&gt; Converter is a free online tool that will give you the binary equivalent of any decimal number. Enter your number, then simply click ‘convert’. **&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When performing visual testing, it is essential to know its limitations. Visual testing is only suitable for certain types of applications, as it is limited to UI verification. Additionally, it can be time-consuming and resource-intensive for larger applications, which can add to maintenance overhead. Being mindful of these helps one make an informed decision on the extent of visual testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Storybook?
&lt;/h2&gt;

&lt;p&gt;Storybook is an open-source tool that lets you build, organize, and test user interface (UI) components in isolation. It is commonly used in web development, especially for building and testing components in JavaScript and related frameworks like React. Storybook allows testers to visualize, test, and debug the different states of UI components independently from the rest of the application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8T3rRjb_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AW7CJ761DBKkqFyMm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8T3rRjb_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AW7CJ761DBKkqFyMm.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Storybook, each webpage is considered a story. A story represents the state of web elements in different environments. In a Storybook project, the first execution is always stored as-is, and the images from it are considered the base images or baseline. In this case, no screenshots or images are compared, and all are marked as approved. In subsequent runs after this one, you can see passed/failed stories based on the mismatch percentage, as screenshots are compared to this baseline.&lt;/p&gt;

&lt;p&gt;The Storybook community is thriving with an impressive milestone. The Storybook Monorepo has surpassed 81,200 GitHub Stars, solidifying its position as one of the most popular repositories ever.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Story?
&lt;/h2&gt;

&lt;p&gt;In Storybook, each webpage is considered a story. A story represents the state of web elements in different environments. It captures the rendered state of a UI component.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a component?
&lt;/h2&gt;

&lt;p&gt;Each piece of UI in Storybook is an independent component. With components, we don’t need to load the entire application to test a specific UI. We can render the component in isolation and test it before moving on to the rest of the application. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;A tool that converts decimal numbers into equivalent octal representation. Input a decimal number and click ‘Convert &lt;a href="https://www.lambdatest.com/free-online-tools/decimal-to-octal?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools"&gt;Decimal to Octal&lt;/a&gt;’ to get the result in seconds! *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How does a Storybook compare different components and stories?
&lt;/h2&gt;

&lt;p&gt;In a Storybook project, the first execution is always stored as-is, and the images from it are considered the base images or baseline.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TBHQC5sf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3148/0%2AQns9n-YxcAEKVi4P.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TBHQC5sf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3148/0%2AQns9n-YxcAEKVi4P.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, no screenshots or images are compared, and all are marked as approved.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7V0WGi84--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2A7fRuPf1LIIz9OSGt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7V0WGi84--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2A7fRuPf1LIIz9OSGt.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In subsequent runs after this one, you can see passed/failed stories based on the mismatch percentage, as screenshots are compared to this baseline.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Oj75ItUw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2992/0%2AgQEMM0NJT-Uwt8Nt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Oj75ItUw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2992/0%2AgQEMM0NJT-Uwt8Nt.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;A tool that converts decimal numbers into equivalent Hexadecimal representation. Input a decimal number and click ‘Convert &lt;a href="https://www.lambdatest.com/free-online-tools/decimal-to-hex?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools"&gt;Decimal to Hex&lt;/a&gt;’ to get the result in seconds! *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Advantages of Using Storybook
&lt;/h2&gt;

&lt;p&gt;There are many reasons why Storybook is the best choice for visual testing. Some of them are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;**Component Development and Isolation&lt;br&gt;
**Storybook allows you to create, showcase, and test individual UI components in isolation. A component is a small piece of UI. This is especially useful when working with complex applications, as it allows you to focus and test a single component at a time without navigating through the entire application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Component Documentation&lt;br&gt;
**Storybook encourages the documentation of UI components by creating stories for each component. A story represents a component’s state and can include different variations for different environments. These stories serve as living documentation for the components, making it easier to understand and test how these should be used and how they look.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Automation and Regression Detection&lt;br&gt;
**Visual testing in Storybook can be automated using various tools, such as Chromatic or custom scripts. When code changes are made to the UI, visual testing tools can take screenshots and compare them to baseline images. If there are any differences, such as unexpected layout changes or styling issues, the tool can flag them, which helps to catch visual bugs early in the development process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Collaboration&lt;br&gt;
**Storybook is a tool that can be used by developers, designers, and testers to collaborate on projects. Testers can use it to perform visual tests to verify the UI, designers can use it to see how components will appear in the final application, and developers can use it to receive feedback and bugs and iterate on them more effectively.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;**Integration with Other Tools&lt;br&gt;
**Storybook can be integrated with various testing tools and platforms, including cloud-based platforms like &lt;a href="https://www.lambdatest.com/smart-visual-ui-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=webpage"&gt;LambdaTest SmartUI&lt;/a&gt;, which we will use in this blog. It can also be integrated with version control systems, continuous integration (CI) pipelines, and design systems to ensure consistency and quality in UI development.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Need to convert CSV to JSON? Try our free &lt;a href="https://www.lambdatest.com/free-online-tools/csv-to-json?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools"&gt;CSV to JSON&lt;/a&gt; converter tool to convert your CSV files to JSON format. Simple and easy to use. Try it now for free! *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Demonstration: How to perform Storybook Visual Testing?
&lt;/h2&gt;

&lt;p&gt;Having understood and learned the basics about visual testing and Storybook, it is time to move to the demonstration. This section will show how Storybook visual testing can be performed for your application.&lt;/p&gt;

&lt;p&gt;As mentioned previously, to perform Storybook visual testing, we will use SmartUI, a tool provided by the cloud-based LambdaTest platform.&lt;/p&gt;

&lt;p&gt;LambdaTest is an AI-powered test orchestration and execution platform that lets you run manual and automated tests at scale with over 3000+ real devices, browsers, and OS combinations.&lt;/p&gt;

&lt;p&gt;SmartUI is a LambdaTest tool that combines the robustness of cloud-based platforms with the accuracy of visual testing. It helps automate identifying and tracking visual changes in the user interface (UI) over time, which helps release more consistent applications. SmartUI uses cloud benefits to provide precision, collaboration, and scalability. Using LambdaTest enables you to test your application on a combination of 3000+ OS and browser versions, ideally serving the aim of Storybook visual testing for the visual elements.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/sOaQ-ttEBzs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Pre-requisites
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Install Storybook
&lt;/h3&gt;

&lt;p&gt;Install Storybook using the below command. You can check more details on installation on its official website.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx storybook@latest init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Make sure you install a version higher than 6.4.0 for best results. Refer to Storybook recent releases for more information on Storybook release versions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Node.js
&lt;/h3&gt;

&lt;p&gt;Install Node.js on your system by downloading the installer from the official website. For compatibility reasons, ensure the installed version is higher than 14.15.0. If you need more details on the previous versions, you can check here.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Looking to convert binary to hex? Convert binary numbers to hex with ease using our free online &lt;a href="https://www.lambdatest.com/free-online-tools/binary-to-hex?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools"&gt;Binary to Hex&lt;/a&gt; Converter tool. Perfect for developers and coders. *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  LambdaTest SmartUI credentials
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://accounts.lambdatest.com/register?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=webpage"&gt;Create your account on the LambdaTest&lt;/a&gt; platform to access SmartUI and perform Storybook visual testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  SmartUI Project Setup on LambdaTest
&lt;/h2&gt;

&lt;p&gt;After completing the prerequisite setup, the first step is to create a &lt;a href="https://smartui.lambdatest.com/projects?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=webpage"&gt;SmartUI Project&lt;/a&gt; on LambdaTest. In this project, we will combine all your executed builds for our Storybook visual test cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;. Login to your LambdaTest account and select &lt;strong&gt;Smart UI&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rm-XR14Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AiHzVVLC_lgherTTj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rm-XR14Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AiHzVVLC_lgherTTj.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Click on the &lt;strong&gt;New Project&lt;/strong&gt; button to create your SmartUI Storybook project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4feBHrqk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2APGSYObHudma436xN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4feBHrqk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2APGSYObHudma436xN.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; For Storybook tests, select the platform as &lt;strong&gt;Web&lt;/strong&gt;. Add project name, approvers for the changes, and tags for filters or easy navigation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N6U4NpVT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2ATn6EBVz_9a0mMC2q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N6U4NpVT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2ATn6EBVz_9a0mMC2q.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D_cBBkVP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AXRTHPF8JnPnwlQZu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D_cBBkVP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AXRTHPF8JnPnwlQZu.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; Click on &lt;strong&gt;Submit&lt;/strong&gt; to create your first project. You can see no build and the token is available inside the project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--32MoO3m9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AOx9xjivdsZEYEHGC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--32MoO3m9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AOx9xjivdsZEYEHGC.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This token will be used in the next steps to link the project execution results on SmartUI.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;From SQL to JSON in a snap! Convert SQL queries to JSON effortlessly with our &lt;a href="https://www.lambdatest.com/free-online-tools/sql-to-json?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools"&gt;SQL to JSON&lt;/a&gt; converter, enhancing compatibility and data interchange. Try it now! *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Storybook Code Setup and Execution: Creating Baseline
&lt;/h2&gt;

&lt;p&gt;For this blog demonstration on performing Storybook visual testing, we will reference the code from the GitHub repo below. This is a sample project to execute your first Storybook SmartUI project for visual testing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; Clone the project from this GitHub repo.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/LambdaTest/smartui-storybook-sample.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pT1-YqD2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2ALJhxpfhZEGnLlZXa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pT1-YqD2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2ALJhxpfhZEGnLlZXa.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Install the required NPM dependencies for LambdaTest Smart UI Storybook visual test execution.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @lambdatest/smartui-storybook -g
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j_mkrBz9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AEH4EYyRN09y5ysdM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j_mkrBz9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AEH4EYyRN09y5ysdM.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Verify that the &lt;em&gt;.storybook&lt;/em&gt; directory is added to your &lt;em&gt;main.js&lt;/em&gt; project. It should contain the following code. If not, add the same to it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
   features: {
     buildStoriesJson: true,
   },
 };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; Install the Storybook node module inside your project using npm.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once this is done, you should see &lt;em&gt;@storybook&lt;/em&gt; under your &lt;em&gt;&amp;lt; current-dir &amp;gt;/node_modules&lt;/em&gt; folder like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aSep-ocI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2A_11p9zqczAaIZ_SQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aSep-ocI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2A_11p9zqczAaIZ_SQ.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; Configure the SmartUI project token for the demo project that we created initially.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export PROJECT_TOKEN="123456#1234abcd-****-****-****-************" 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 6.&lt;/strong&gt; As mentioned, LambdaTest SmartUI provides configurations to execute visual tests on several available browser + OS combinations. These configurations are defined inside &lt;em&gt;smartui.json&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Execute the following command to generate this configuration JSON.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;smartui config create .smartui.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7gIE7_hm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AkAplClJq133tdiNu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7gIE7_hm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AkAplClJq133tdiNu.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the &lt;em&gt;smartui.json&lt;/em&gt; file is created, you can see the default configurations in the same.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
 "storybook": {
   "browsers": [
     "chrome",
     "firefox",
     "safari",
     "edge",
     // Add more browser configuration here
   ],
   "viewports": [
     [1920, 1080]        // Add more view ports to capture here
   ],
   "waitForTimeout": 0, // (Optional) Add wait time for the page to load
   "include": [],       // (Optional) Only compare limited stories
   "exclude": []        // (Optional) Don't compare the stories
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The configurations can be modified as per your requirements by changing the key-values on this JSON.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Stop using the same password for everything! Create strong and unique passwords that are difficult to guess with our &lt;a href="https://www.lambdatest.com/free-online-tools/random-password-generator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools"&gt;Random Password Generator&lt;/a&gt;. Try it now!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Step 7.&lt;/strong&gt; Build your Storybook application, in this case, the repo code, as a static web application. Storybook will create a static web application capable of being served by any web server for your code, which can be previewed locally on any system by sharing the application code.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run build-storybook
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Executing this command should create an output directory /storybook-static/ from which the code will be built for execution on SmartUI.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pGLu30uh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2ACHCqbC54ouyJG5JO.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pGLu30uh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2ACHCqbC54ouyJG5JO.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8.&lt;/strong&gt; Finally, we execute the Storybook visual tests on the SmartUI cloud using the storybook static build created in previous steps.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;smartui storybook ./storybook-static --config .smartui.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This command would trigger the run to create a Baseline, as this is the first execution. All stories would be approved automatically for this execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BgCy3Wpy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AExIB_FB4g3X5NZmI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BgCy3Wpy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AExIB_FB4g3X5NZmI.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can view the created baseline for Storybook visual tests on SmartUI by navigating to your project on the &lt;a href="https://appautomation.lambdatest.com/build?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=webpage"&gt;LambdaTest Dashboard&lt;/a&gt;. This build will be created on the master branch.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SgPW-DZX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AN6H99qKvTYg0ejmm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SgPW-DZX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AN6H99qKvTYg0ejmm.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TrC_Q4MK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2ARcGidfZzscx1jbQp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TrC_Q4MK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2ARcGidfZzscx1jbQp.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this, we have created a baseline for the stories to be executed further to perform Storybook visual tests. Let’s move forward to change any story, re-execute the run, and see results.&lt;/p&gt;

&lt;h2&gt;
  
  
  Storybook SmartUI Execution: Committed Code Changes
&lt;/h2&gt;

&lt;p&gt;In this section, we see how SmartUI triggers the storybook visual test on changes committed to the existing code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; Open &lt;em&gt;src/stories/Page.stories.jsx&lt;/em&gt; story file and make some changes to it. For this demonstration, I have updated the value from Log in to &lt;em&gt;Log out&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q6Ly4lSz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2316/0%2Ay575ynwlyAo3DBRt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q6Ly4lSz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2316/0%2Ay575ynwlyAo3DBRt.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Add the file with changes to GitHub. SmartUI uses the GitHub commitid for the comparison.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add src/stories/Page.stories.jsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Commit the changes with a proper commit message. This message will be visible on the LambdaTest SmartUI project to identify the execution of a particular commit.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git commit -m "Demo blog changes in Page.stories.mdx"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; Execute the Storybook visual tests on the SmartUI cloud again as previously done. This time, it will give the comparison results and highlight failures if any mismatches occur.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;smartui storybook ./storybook-static --config .smartui.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can see the output results on the CLI below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3YjEnNgZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2Abkkehi0t1LFh8vDQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3YjEnNgZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2Abkkehi0t1LFh8vDQ.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Execution results on the SmartUI dashboard can be viewed for the particular commit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V6tIBhq5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AQ0NSY_nmM8_aNKyL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V6tIBhq5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/3200/0%2AQ0NSY_nmM8_aNKyL.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this, we have learned how Storybook visual testing works with LambdaTest SmartUI. So, use this powerful integration of tools and platforms to build a robust visual testing architecture for your static web applications.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*&lt;em&gt;Generate custom QR codes for your business or personal needs with our fast and easy-to-use &lt;a href="https://www.lambdatest.com/free-online-tools/qr-code-generator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=free_online_tools"&gt;QR code generator&lt;/a&gt; online tool in seconds. Try it now! *&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This concludes our blog on how to perform Storybook visual testing. In this &lt;a href="https://www.lambdatest.com/blog/storybook-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_02&amp;amp;utm_term=vs&amp;amp;utm_content=webpage"&gt;Storybook testing&lt;/a&gt; tutorial, we learned about visual testing, its advantages and disadvantages, and why it is needed in web software development. We also understood about Storybook and its benefits for visual testing. Lastly, we saw how Storybook visual tests can be configured and executed on the LambdaTest SmartUI cloud platform to take advantage of a cloud platform and execute on various OS and browser combinations. It’s time for you to get started and perform Storybook visual tests on SmartUI for the best results and performance. Happy testing!&lt;/p&gt;

</description>
      <category>testing</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>devops</category>
    </item>
    <item>
      <title>How to Handle Auto Suggestions in Selenium Java</title>
      <dc:creator>Vipulgupta9090</dc:creator>
      <pubDate>Wed, 09 Aug 2023 11:03:55 +0000</pubDate>
      <link>https://dev.to/testmuai/how-to-handle-auto-suggestions-in-selenium-java-1g9h</link>
      <guid>https://dev.to/testmuai/how-to-handle-auto-suggestions-in-selenium-java-1g9h</guid>
      <description>&lt;p&gt;You may have observed that when searching on Google or browsing through an eCommerce platform, automatic suggestions appear almost instantly as you start typing. These suggestions, known as auto-suggestions, are intended to provide a rapid guide to potentially relevant keywords and top searches based on your input. As you type in the search box, the website presents a list of auto-suggestions to aid in refining your search and quickly accessing relevant information.&lt;/p&gt;

&lt;p&gt;Automating website interactions with &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; to handle auto-suggestions can present challenges due to the dynamic nature of these suggestions. Unlike static text on a web page, auto-suggestions form a constantly changing list, adapting to the input provided by users.&lt;/p&gt;

&lt;p&gt;This blog delves into the intricacies of handling auto-suggestions in &lt;a href="https://www.lambdatest.com/learning-hub/selenium-java?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium Java&lt;/a&gt; for &lt;a href="https://www.lambdatest.com/learning-hub/web-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;web automation&lt;/a&gt;. We will explore different techniques to automate these suggestions in web automation scripts while gaining valuable insights into the dynamics of auto-suggestions. Additionally, we’ll address potential challenges during the automation process.&lt;/p&gt;

&lt;p&gt;By the end of this blog on handling auto-suggestions in Selenium, you will be able to use different techniques to handle auto-suggestions and implement them in your automation scripts. So, let’s get started to explore the world of auto-suggestions!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Auto-Suggestion?
&lt;/h2&gt;

&lt;p&gt;Auto-suggestion or auto-complete functionality offers potential suggestions to users based on their input in a search box or any other input field on a webpage. These suggestions aim to assist users in finding relevant information or completing their input more efficiently.&lt;/p&gt;

&lt;p&gt;These are implemented as a drop-down list with a list of possible suggestions as the user proceeds to input. This helps the user select the desired term from the list without entering it completely.&lt;/p&gt;

&lt;p&gt;Dynamic lists of suggestions change with user input, facilitated by AJAX (Asynchronous JavaScript and XML) requests. This ensures a seamless user experience as they type, with suggestions fetched from a server or API in the background.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Also, learn the ins and outs of &lt;a href="https://www.lambdatest.com/learning-hub/test-execution-engine?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test execution engines&lt;/a&gt; through our extensive guide, and enhance your testing workflow with automation.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AKS-8WqjMSfD4W50O.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AKS-8WqjMSfD4W50O.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Auto-suggestions are quite helpful since they rely on AJAX queries to provide relevant options in advance, which helps to shorten the time it takes to search for a term. These auto-suggestions also have wide applications in eCommerce and fintech domains, facilitating quick product searches and helping list all related ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;This detailed guide explains how to detect &lt;a href="https://www.lambdatest.com/learning-hub/flaky-test?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;flaky tests&lt;/a&gt;, its causes, strategies to reduce flakiness and much more.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of Auto-Suggestions
&lt;/h2&gt;

&lt;p&gt;Auto-suggestions, or predictive search, offer several benefits by improving user experience using AJAX requests for quicker search. Here are some advantages that make auto-suggestions a popular strategy on any website.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Enhanced User Experience&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Improved Search Efficiency&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Higher Accuracy due to auto corrections&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mobile-Friendly Experience&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Discover how &lt;a href="https://www.lambdatest.com/learning-hub/test-runner?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;TestRunner&lt;/a&gt; is transforming the world of automated testing. Explore its key features and benefits. Improve testing processes today with TestRunner.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges of Handling Auto-Suggestions in Selenium
&lt;/h2&gt;

&lt;p&gt;Auto-suggestions offer significant advantages for enhanced performance, as they provide relevant information about website topics without loading the entire content. This helps to save user time and effort in browsing through all the content on a website.&lt;/p&gt;

&lt;p&gt;However, the content is dynamic; hence, we need to use the right approach in automation to handle the dynamism factor. Also, loading results might take time depending on external factors (particularly Internet speed). This becomes challenging while handling auto-suggestions in &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium automation&lt;/a&gt;. Let’s look at some of these common challenges before we learn handling auto-suggestions in &lt;a href="https://www.lambdatest.com/selenium-java-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium Java testing&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Delay in auto-suggestion list&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The auto-suggestion list depends on asynchronous calls to show results. Sometimes, this can lead to timing issues if Selenium tries to interact with it instantly, and the suggestion list may not have fully loaded. This can also happen due to a flaky Internet connection in the &lt;a href="https://www.lambdatest.com/blog/what-is-test-environment/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;test environment&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Highly dynamic content&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Auto-suggestions are dynamically generated based on the search term. Even the slightest change in the same search term, like adding an extra space or any other character, can change the suggestions in the list. This becomes challenging while working with Selenium automation if specific web element locators are used.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test native, hybrid, and web apps on any mobile OS with our free &lt;a href="https://www.lambdatest.com/android-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Android emulator online&lt;/a&gt;. Sign up to optimize app performance.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices for Handling Auto-Suggestions in Selenium
&lt;/h2&gt;

&lt;p&gt;To overcome the challenges discussed above and make handling auto-suggestions in Selenium robust, we follow many practices while writing automation tests. Here are some of these best practices we will follow in the next demonstration section.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Using Explicit Waits&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As previously mentioned in the challenges, the auto-suggestions list may experience delays in loading, potentially causing failures if the Selenium script attempts to interact with it immediately. To address these concerns, it is strongly advised to wait for the auto-suggestion list to fully load before interacting with it. &lt;a href="https://www.lambdatest.com/blog/types-of-waits-in-selenium/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Explicit waits in Selenium&lt;/a&gt; are particularly valuable for handling such situations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3084%2F1%2Apt8u0JwzjMNRWgKA6xoenw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3084%2F1%2Apt8u0JwzjMNRWgKA6xoenw.png" width="800" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Explicit waits and &lt;a href="https://www.lambdatest.com/blog/expected-conditions-in-selenium-examples/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;&lt;em&gt;ExpectedConditions in Selenium&lt;/em&gt;&lt;/a&gt; make the automation test wait for a certain element before proceeding. In our use case, this web element would be the auto-suggestion list.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Unique WebElement locators&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Handling auto-suggestions in Selenium can become a nightmare if proper and unique webElement locators are not used. This is because they are highly dynamic, and web element locators might keep changing based on input.&lt;/p&gt;

&lt;p&gt;So, it is suggested to use unique locators such as ID, name, or class, which are rarely changed and help to interact with the list irrespective of search input term. Also, always remember not to use any such &lt;a href="https://www.lambdatest.com/learning-hub/selenium-locators?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;locator strategies&lt;/a&gt; with the search term reference in it, as the term might vary on each run and would make the locator obsolete.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Verifying the suggestion value&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To select the correct term from the auto-suggestion list, you should always verify the value before selecting it to reach the results page. Consider the case-insensitive approach when making this comparison, as the results of different auto-suggestion runs may vary.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/assert-and-verify-in-selenium-webdriver/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium assertions&lt;/a&gt; can be used to verify that the search term on the results page is similar to the expected value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AyaCgbgHvOBf-bKmu3wx0BA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AyaCgbgHvOBf-bKmu3wx0BA.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Using keyboard events&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One of the most common ways for handling auto-suggestions in Selenium automation is to use &lt;a href="https://www.lambdatest.com/blog/handling-keyboard-actions-in-selenium-webdriver/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;keyboard events&lt;/a&gt; like the arrow down key and enter button. This is recommended when selecting the first or second element from the list in your automation case without comparing the value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2824%2F1%2ATqugru8f3_kCAgXaZRwvzQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2824%2F1%2ATqugru8f3_kCAgXaZRwvzQ.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the above code snippet, you can see that we want to click on the 2nd element from the list. For the same, a for loop has been created to press the arrow down key using the &lt;a href="https://www.lambdatest.com/blog/how-to-use-selenium-sendkeys/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;sendKeys() method in Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once the desired element from the list is reached, for loop breaks, the sendKeys() method is used again to hit the Enter button to select that option.&lt;/p&gt;

&lt;p&gt;For this, we can also use &lt;a href="https://www.lambdatest.com/blog/press-enter-without-webelement-selenium-python/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;ActionsChains in Selenium&lt;/a&gt; Java to create a chain of actions using the Actions class. This helps to perform low-level mouse actions like click, drag, hover, or keyboard actions using the WebDriver instance.&lt;/p&gt;

&lt;p&gt;In this blog, we cover only the sendKeys() method implementation for handling auto-suggestions in Selenium Java. This is because, in this scenario, ActionChains is not required as we only have two quick key actions to perform.&lt;/p&gt;

&lt;p&gt;So, we have understood the challenges and the best practices that would help in handling auto-suggestions in Selenium. Let us look at some practical implementations of how these can be used in Selenium Java automation projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Elevate Your &lt;a href="https://www.lambdatest.com/mobile-app-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Mobile App Test&lt;/a&gt; Quality: Make the most of your mobile application testing &amp;amp; Test on LambdaTest Cloud for Comprehensive Testing, Bug Detection &amp;amp; More.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Handling Auto-Suggestions in Selenium Java
&lt;/h2&gt;

&lt;p&gt;To demonstrate the best approaches for handling auto-suggestions in Selenium automation, we will create a Java project with Maven and TestNG using &lt;a href="https://www.lambdatest.com/learning-hub/selenium-4?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium 4.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following test scenarios will be automated in this blog to help understand all the approaches and best practices discussed so far.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Scenario — 1 (Handling auto-suggestions in Selenium by fetching auto-suggestion list)&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to &lt;a href="https://www.google.com/" rel="noopener noreferrer"&gt;https://www.google.com&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter &lt;strong&gt;lambdatest&lt;/strong&gt; as the search term in the search box.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wait for the auto-suggestion list to be available.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fetch WebElement reference to all the auto-suggestions and store in a List variable of type WebElement.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start a loop to traverse each WebElement from the list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Compare if the auto-suggestion matches the required term.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If it matches, select the same by clicking and breaking the loop. Else, continue until the end of the list Wait for the search results page to load.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Assert that the search term on the results page is the same as expected or not.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Test Scenario — 2 (Handling auto-suggestions in Selenium using sendkeys())&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to &lt;a href="https://www.google.com/" rel="noopener noreferrer"&gt;https://www.google.com&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter &lt;strong&gt;lambdatest&lt;/strong&gt; as the search term in the search box.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wait for the auto-suggestion list to be available.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start a loop to reach the 2nd suggestion from the auto-suggestions list by clicking the down arrow key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click Enter on this term.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wait for the search results page to load.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Assert that the search term on the results page contains the initially given search keyword.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Test Scenario — 3 (Handling auto-suggestions in Selenium using web scraping: Google)&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to &lt;a href="https://www.google.com/" rel="noopener noreferrer"&gt;https://www.google.com&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter **lambdatest **as the search term in the search box.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wait for the auto-suggestion list to be available.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fetch WebElement reference to the auto-suggestions list and store it in a variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start a loop to scrape (or scrap) the suggestion data for each term from the auto-suggestions list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Compare if the list data from web scraping matches the required term.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If it matches, select the same by clicking and breaking the loop. Else, continue until the end of the list&lt;br&gt;
Wait for the search results page to load.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Assert whether the search term on the results page is the same as expected.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Automate &lt;a href="https://www.lambdatest.com/cypress-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Cypress testing&lt;/a&gt; and perform browser automation testing with LambdaTest. Our cloud infrastructure has 3000+ desktop &amp;amp; mobile environments. Try for free!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;

&lt;p&gt;In this blog, Eclipse IDE is used for creating the project. You can also choose any other IDE of your choice.&lt;/p&gt;

&lt;p&gt;For all the test scenarios in this blog, the same project setup is being used. We keep adding new &lt;a href="https://www.lambdatest.com/learning-hub/test-case?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test cases&lt;/a&gt; for each scenario in a new test file. So do the setup carefully on your machine to avoid any errors at the execution stage.&lt;/p&gt;

&lt;p&gt;All the test scenarios demonstrated in this blog are Selenium web automation scripts. We will be executing them on a &lt;a href="https://www.lambdatest.com/selenium-grid-online?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;cloud Selenium Grid&lt;/a&gt;. Using a Selenium cloud Grid offers improved speed and scalability and makes &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automation testing&lt;/a&gt; faster and more reliable. It also allows executing multiple cases across various browsers and OS.&lt;/p&gt;

&lt;p&gt;To achieve the same, the LambdaTest cloud Selenium Grid is being used in this blog. LambdaTest is an AI-powered test orchestration and execution platform that empowers users to conduct &lt;a href="https://www.lambdatest.com/java-automation-testing?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Java automation testing&lt;/a&gt; for web and mobile applications across a vast array of over 3000+ real browsers, devices, and operating system combinations.&lt;/p&gt;

&lt;p&gt;Check out the documentation on how to get started with &lt;a href="https://www.lambdatest.com/support/docs/java-with-selenium-running-java-automation-scripts-on-lambdatest-selenium-grid/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium testing with Java on LambdaTest&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Additionally, it provides a diverse selection of &lt;a href="https://www.lambdatest.com/blog/automation-testing-tools/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automated testing tools&lt;/a&gt; and frameworks such as Selenium, &lt;a href="https://www.lambdatest.com/learning-hub/cypress-tutorial?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Cypress&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/learning-hub/cypress-tutorial?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Playwright&lt;/a&gt;, and &lt;a href="https://www.lambdatest.com/appium-mobile-testing?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Appium&lt;/a&gt;, enabling efficient execution of test cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; Launch Eclipse IDE and create a new Maven project, &lt;em&gt;HandlingAutoSuggestionsSelenium&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Add 2 new packages inside the src package and name them as &lt;em&gt;base *and *test&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Add a Java class file inside the base package and name it &lt;em&gt;BaseTest.java&lt;/em&gt;. This file will have the common code for all test cases, like initializing the &lt;a href="https://www.lambdatest.com/learning-hub/webdriver?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;WebDriver&lt;/a&gt;, browser interactions, and doing the initial search for the auto-suggestion list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; Add 3 Java class files under the test package. These will have the code for our different test scenarios.&lt;/p&gt;

&lt;p&gt;Once all these steps are done, the project structure should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A49UgEurF7vZP_uL8GZ-Geg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A49UgEurF7vZP_uL8GZ-Geg.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we use Selenium and &lt;a href="https://www.lambdatest.com/learning-hub/testng?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;TestNG&lt;/a&gt; for our project, add Selenium 4 and TestNG dependencies inside the pom.xml. Using the latest stable versions of the project is recommended for the best results.&lt;/p&gt;

&lt;p&gt;The final pom.xml would be like the one below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
    &amp;lt;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 https://maven.apache.org/xsd/maven-4.0.0.xsd"&amp;gt;
       &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
       &amp;lt;groupId&amp;gt;HandlingAutoSuggestionsSelenium&amp;lt;/groupId&amp;gt;
       &amp;lt;artifactId&amp;gt;HandlingAutoSuggestionsSelenium&amp;lt;/artifactId&amp;gt;
       &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;
       &amp;lt;build&amp;gt;
           &amp;lt;sourceDirectory&amp;gt;src&amp;lt;/sourceDirectory&amp;gt;
           &amp;lt;plugins&amp;gt;
               &amp;lt;plugin&amp;gt;
                   &amp;lt;artifactId&amp;gt;maven-compiler-plugin&amp;lt;/artifactId&amp;gt;
                   &amp;lt;version&amp;gt;3.8.1&amp;lt;/version&amp;gt;
                   &amp;lt;configuration&amp;gt;
                       &amp;lt;release&amp;gt;19&amp;lt;/release&amp;gt;
                   &amp;lt;/configuration&amp;gt;
               &amp;lt;/plugin&amp;gt;
           &amp;lt;/plugins&amp;gt;
       &amp;lt;/build&amp;gt;
       &amp;lt;dependencies&amp;gt;
           &amp;lt;dependency&amp;gt;
               &amp;lt;groupId&amp;gt;org.seleniumhq.selenium&amp;lt;/groupId&amp;gt;
               &amp;lt;artifactId&amp;gt;selenium-java&amp;lt;/artifactId&amp;gt;
               &amp;lt;version&amp;gt;4.8.3&amp;lt;/version&amp;gt;
           &amp;lt;/dependency&amp;gt;
           &amp;lt;dependency&amp;gt;
               &amp;lt;groupId&amp;gt;org.testng&amp;lt;/groupId&amp;gt;
               &amp;lt;artifactId&amp;gt;testng&amp;lt;/artifactId&amp;gt;
               &amp;lt;version&amp;gt;7.7.1&amp;lt;/version&amp;gt;
               &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
           &amp;lt;/dependency&amp;gt;
       &amp;lt;/dependencies&amp;gt;
    &amp;lt;/project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we move to &lt;em&gt;BaseTest.java&lt;/em&gt;. This file will have the primary driver functions like initialize driver, quit driver, etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package base;


    import java.net.MalformedURLException;
    import java.net.URL;
    import java.time.Duration;
    import java.util.HashMap;


    import org.openqa.selenium.By;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.openqa.selenium.safari.SafariOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;


    public class BaseTest {


       public RemoteWebDriver driver = null;
       public WebDriverWait wait;


       String username = System.getenv("LT_USERNAME") == null ? "&amp;lt;lambdatest_username&amp;gt;" : System.getenv("LT_USERNAME");
       String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "&amp;lt;lambdatest_accesskey&amp;gt;" : System.getenv("LT_ACCESS");


       @BeforeTest
       public void setup() {
           try {
               SafariOptions safariOptions = new SafariOptions();
               safariOptions.setPlatformName("MacOS Ventura");
               safariOptions.setBrowserVersion("16.0");


               HashMap&amp;lt;String, Object&amp;gt; ltOptions = new HashMap&amp;lt;String, Object&amp;gt;();
               ltOptions.put("build", "Auto Suggestions in Selenium");
               ltOptions.put("name", "Handling Auto Suggestions");
               ltOptions.put("w3c", true);
               safariOptions.setCapability("LT:Options", ltOptions);


               driver = new RemoteWebDriver(
                       new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), safariOptions);
           } catch (MalformedURLException e) {
               e.printStackTrace();
           }
       }


       @AfterTest
       public void tearDown() {
           driver.quit();
       }


       /*
        * 1. Navigate to Google 2. Enter search term 3. Wait for auto-suggestions list
        * to be visible
        */
       public void navigateToGoogleAndSearch() {
           // to navigate to the website and enter the search term
           System.out.println("Navigating to the website");
           driver.get("https://www.google.com");


           System.out.println("Entering search term as lambdatest");
           driver.findElement(By.name("q")).sendKeys("lambdatest");


           // explicit wait to wait for the auto suggestions list to be present
           wait = new WebDriverWait(driver, Duration.ofSeconds(10));
           wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@role='listbox']")));
           System.out.println("Auto-suggestions list found");
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;This guide explores &lt;a href="https://www.lambdatest.com/learning-hub/digital-transformation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Digital Transformation&lt;/a&gt;, its benefits, goals, importance and challenges involved in Digital Transformation.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Step 1. **Create an object of *RemoteWebDriver&lt;/em&gt; and initialize it as &lt;em&gt;null&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AvkEze1pdWjPQDkcp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AvkEze1pdWjPQDkcp.png" width="778" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, create an object of &lt;em&gt;WebDriverWait&lt;/em&gt;, which will be used at later stages to implement explicit waits. This wait object will allow the code to wait for a given time until a given condition is fulfilled before moving forward.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ATvdc2o3tiHQPNQOE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ATvdc2o3tiHQPNQOE.png" width="618" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Fetch the username and access key for your LambdaTest Account from the &lt;a href="https://accounts.lambdatest.com/security" rel="noopener noreferrer"&gt;LambdaTest Password &amp;amp; Security&lt;/a&gt; section to connect to their cloud grid.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AD1D2pzEzWpZwN4iK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AD1D2pzEzWpZwN4iK.png" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alternatively, you can configure these as environment variables and directly fetch them in code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For macOS and Linux:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; export LT_USERNAME=LT_USERNAME
    export LT_ACCESS_KEY=LT_ACCESS_KEY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;For Windows:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; set LT_USERNAME=LT_USERNAME
    set LT_ACCESS_KEY=LT_ACCESS_KEY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Next, add a method and name it &lt;em&gt;setup()&lt;/em&gt;. This will contain the code for WebDriver initialization using browser properties and LambdaTest cloud Selenium Grid properties. Annotate this with the &lt;em&gt;@BeforeTest&lt;/em&gt; &lt;a href="https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;annotation in TestNG&lt;/a&gt; to execute it before each test execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AotgabsIcc4FVThPn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AotgabsIcc4FVThPn.png" width="518" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Step 4. **Create an object of *SafariOptions&lt;/em&gt; class. This will help to set the required browser properties on which the tests are executed. Using this, set the OS and browser versions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AD25iN8gjcWnDKsvo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AD25iN8gjcWnDKsvo.png" width="800" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; Use a &lt;em&gt;Hashmap&lt;/em&gt; type variable to specify the additional browser capabilities required by the LambdaTest platform to support test execution on their Selenium cloud grid. This will help to identify the test results on the dashboard using the build name and other details.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AczJQmCWhgBH9khmy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AczJQmCWhgBH9khmy.png" width="800" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also fetch the required browser capabilities for the LambdaTest platform by navigating to &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Automation Capabilities Generator&lt;/a&gt;. This helps by offering ready-to-use code for setting up browser capabilities that can be used in execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AT4FOiyy6tMvzzzA5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AT4FOiyy6tMvzzzA5.png" width="800" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6.&lt;/strong&gt; Finally, use the &lt;a href="https://www.lambdatest.com/blog/selenium-remotewebdriver/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium RemoteWebDriver&lt;/a&gt; to connect to the LambdaTest remote grid using your credentials and safariOptions object containing all specified browser capabilities.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2768%2F0%2AOtV3vtfR3bKnqURh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2768%2F0%2AOtV3vtfR3bKnqURh.png" width="800" height="210"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7.&lt;/strong&gt; Add a new method, &lt;em&gt;tearDown()&lt;/em&gt;, and annotate it with &lt;em&gt;@AfterTest&lt;/em&gt;. This will be called after each test execution to close the browser and terminate the web driver session.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AGmEFosTr2Y36Z-jH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AGmEFosTr2Y36Z-jH.png" width="590" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8.&lt;/strong&gt; Finally, we add the last utility method to this base file, containing common steps for each test scenario.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AHGQ0Cbbuqy3LEkMI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AHGQ0Cbbuqy3LEkMI.png" width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8.1)&lt;/strong&gt; Use the &lt;em&gt;driver.get()&lt;/em&gt; method to navigate to the website under test.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ARTgN91vytxFxi0WW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ARTgN91vytxFxi0WW.png" width="800" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8.2)&lt;/strong&gt; Fetch the web element of the search input box and enter the search term.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2132%2F0%2AM1-MkIRblsmNO3M4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2132%2F0%2AM1-MkIRblsmNO3M4.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are using the *name *locator here to locate this web element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AJ52P6i5l32fBvO01.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AJ52P6i5l32fBvO01.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8.3)&lt;/strong&gt; Instantiate the &lt;em&gt;WebDriverWait&lt;/em&gt; class object we initially created, with the maximum time to wait for any condition as 10 sec. This will block the WebDriver for the given time, preventing it from performing any further action and unblocking it as soon as the condition is fulfilled.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2132%2F0%2A373lMOTTWjTpsTez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2132%2F0%2A373lMOTTWjTpsTez.png" width="800" height="246"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8.4)&lt;/strong&gt; Using this wait object, we implement &lt;em&gt;ExpectedConditions&lt;/em&gt; in Selenium, which tells the driver to wait for the given web element to be visible. In this case, we wait for the auto-suggestions list to be visible before proceeding with different approaches.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Aiz77ABS6PXupiwoq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Aiz77ABS6PXupiwoq.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3084%2F0%2AaUjULRvLnf6R9nzc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3084%2F0%2AaUjULRvLnf6R9nzc.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Which are the most wanted a&lt;a href="https://www.lambdatest.com/blog/automation-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;utomation testing tools&lt;/a&gt; in 2023 that have climbed the top of the ladder so far? Let’s take a look.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Auto-Suggestions in Selenium using the auto-suggestion list
&lt;/h2&gt;

&lt;p&gt;First and the most used approach for handling auto-suggestions in Selenium automation script is by traversing the whole auto-suggestion list and comparing the values to select the required one. This is highly useful when we already know the exact search term we are looking for and do not wish to select any random one.&lt;/p&gt;

&lt;p&gt;It’s crucial to remember that, depending on the geolocation, the same search query would provide a different auto-suggestion list. This occurs because the same term may have different references in different places, or occasionally it may refer to something in one place while generating no results in another. So be careful and determine an accurate search term for your test case while applying this strategy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;


    import java.util.List;


    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.testng.Assert;
    import org.testng.annotations.Test;


    import base.BaseTest;


    public class TestUsingSuggestionsList extends BaseTest {


       @Test
       public void testUsingAutoSuggestionsList() {


           String expectedSearchTerm = "lambdatest careers";


           navigateToGoogleAndSearch();


           // to fetch the webElement for all the suggestions from list
           List&amp;lt;WebElement&amp;gt; autoSuggestionList = driver.findElements(By.xpath("//*[@role='option']"));


           // to traverse the list and navigate to required suggestion search results
           for (WebElement autoSuggestion : autoSuggestionList) {
               System.out.println("Auto Suggestion Value : " + autoSuggestion.getText());
               if (autoSuggestion.getText().equalsIgnoreCase(expectedSearchTerm)) {
                   System.out.println("\nFound required value in auto suggestion list. Clicking on it now\n");
                   autoSuggestion.click();
                   break;
               }
           }


           // wait for the search results page to load and verify if it has the same term as we
           // expected.
           wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@type='search']")));
           Assert.assertEquals(driver.findElement(By.xpath("//*[@type='search']")).getText(), expectedSearchTerm,
                   "Valid Search result page.");
           System.out.println("Reached to google search result page for : " + expectedSearchTerm);
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Walkthrough&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Step 1.&lt;/strong&gt; Add a new Java file under the test package, &lt;em&gt;TestUsingSuggestionsList&lt;/em&gt;. This class will extend &lt;em&gt;BaseTest.java&lt;/em&gt; to inherit the common methods and variables from it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2044%2F0%2A9qqfTtisFskBSMCC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2044%2F0%2A9qqfTtisFskBSMCC.png" width="800" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Add the test case as &lt;em&gt;testUsingAutoSuggestionsList()&lt;/em&gt; and annotate it with &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; annotation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A5rHV2uGqLs13Rsm7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A5rHV2uGqLs13Rsm7.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Set the expected search term in a variable that we will find from the auto-suggestion list and then assert on the results page at the end.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ANbCXNo0afGMXjsXi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ANbCXNo0afGMXjsXi.png" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Step 4. **Call the *navigateToGoogleAndSearch()&lt;/em&gt; of &lt;em&gt;BaseTest.java&lt;/em&gt; to navigate to the website, enter the search term, and wait for the auto-suggestion list to be visible.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ADebWuM75AyGMNb-G.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ADebWuM75AyGMNb-G.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; Next, we fetch the list of all the auto-suggestion options using a web element locator and store them in a variable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3116%2F0%2AKIwvXeLZ3DNASzXV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3116%2F0%2AKIwvXeLZ3DNASzXV.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The screenshot below shows that we are using &lt;em&gt;role&lt;/em&gt; attributes to fetch the entire list. It gives a total number of occurrences, and each can be visited individually in the code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AdbNrPizrkD7gCuWl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AdbNrPizrkD7gCuWl.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6.&lt;/strong&gt; Start a loop to traverse this list of suggestions and fetch the values from it. We first log each value found and then compare it with the actual expected term.&lt;/p&gt;

&lt;p&gt;If both matches, the loop is stopped, and that particular auto-suggestion value is clicked. If it is not a match, we continue the loop.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AqUWgE1r8MIt7SIqo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AqUWgE1r8MIt7SIqo.png" width="800" height="294"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7.&lt;/strong&gt; Wait for the search results page for the selected term to load.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AFeOemZ5YdXafEiig.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AFeOemZ5YdXafEiig.png" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We use the same &lt;em&gt;WebDriverWait&lt;/em&gt; object again, but this time &lt;em&gt;ExpectedConditions&lt;/em&gt; is implemented to wait for a different element on the results page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3056%2F0%2AtIlW0qY7lQC6oMfA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3056%2F0%2AtIlW0qY7lQC6oMfA.png" width="800" height="171"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8.&lt;/strong&gt; Finally, assert that the search term on this page matches the expected search term. You can learn more about it through this blog on &lt;a href="https://www.lambdatest.com/blog/asserts-in-testng/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;assertions in TestNG&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Ao8VOxTKrfKXUjwhG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Ao8VOxTKrfKXUjwhG.png" width="800" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Executing this test case would give you an output like the below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AgezsSexFGj3JYQkv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AgezsSexFGj3JYQkv.png" width="800" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Execution logs on the &lt;a href="https://accounts.lambdatest.com/dashboard/" rel="noopener noreferrer"&gt;LambdaTest Dashboard&lt;/a&gt; for this would look like the below. You can notice that all the auto-suggestion list values are being fetched.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2A8UegJBUIUJeyWOlMs_XTlg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2A8UegJBUIUJeyWOlMs_XTlg.png" width="800" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/SqQ8SugRDos"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Don’t miss out on thе latеst tutorials on automation tеsting! Subscribe to thе &lt;a href="https://www.youtube.com/c/LambdaTest?sub_confirmation=1" rel="noopener noreferrer"&gt;LambdaTеst YouTubе Channеl&lt;/a&gt; for tutorials on &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Sеlеnium tеsting&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/playwright-testing?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Playwright testing&lt;/a&gt;, and more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;In this blog on the &lt;a href="https://www.lambdatest.com/blog/best-mobile-automation-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;mobile automation testing&lt;/a&gt; tools, we list the best 11 mobile automation testing tools with their advantages and business-friendly features.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Auto-Suggestions in Selenium using sendKeys()
&lt;/h2&gt;

&lt;p&gt;This approach to handling auto-suggestions in Selenium using keyboard key actions is useful when we do not have a predefined target search term to be selected. It is used in cases when only handling the auto-suggestions list and selecting a term is the requirement of our Selenium test case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;


    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.testng.Assert;
    import org.testng.annotations.Test;


    import base.BaseTest;


    public class TestUsingKeyActions extends BaseTest {


       @Test
       public void testUsingArrowKeys() {


           navigateToGoogleAndSearch();


           // if we want to specifically click on the second term from auto-suggestions
           // list
           for (int i = 1; i &amp;lt; 3; i++) {
               driver.findElement(By.name("q")).sendKeys(Keys.ARROW_DOWN);
               System.out.println("Pressing down arrow key to reach " + i + " search term");
           }
           System.out.println("Hitting enter key on the required search term");
           driver.findElement(By.name("q")).sendKeys(Keys.ENTER);


           // wait for the search results page to load and verify if it has the same term as we
           // expected.
           wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@type='search']")));
           System.out.println(
                   "Search term on results page: " + driver.findElement(By.xpath("//*[@type='search']")).getText());


           // verify that search term on results page consists of the initial input term given
           Assert.assertEquals(driver.findElement(By.xpath("//*[@type='search']")).getText().contains("lambdatest"), true,
                   "Valid Search result page.");
           System.out.println("Reached to search result page for given term");
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Walkthrough
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; Add a new Java test file in the same package and name it &lt;em&gt;TestHandleAutoSuggestions_UsingKeyActions.&lt;/em&gt; Just like the previous test file, this will also extend &lt;em&gt;BaseTest.java.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AjlDB-XpjiZmb5Xq0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AjlDB-XpjiZmb5Xq0.png" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Create the test method in this class, &lt;em&gt;testUsingArrowKeys()&lt;/em&gt;, and annotate it with &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;/em&gt; annotation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AbeNd-2Xz5tgbDMmK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AbeNd-2Xz5tgbDMmK.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; The first step in this test case will also be similar to the previous one, i.e., to call the &lt;em&gt;navigateToGoogleAndSearch()&lt;/em&gt; method of &lt;em&gt;BaseTest.java&lt;/em&gt; to navigate to the test website and get the auto-suggestion list for our search input term.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A7fZ_QHjec5tfI-nP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A7fZ_QHjec5tfI-nP.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; As already mentioned, in this approach, we do not have any specified term to select from the auto-suggestions list. We will use Key actions to press keyboard buttons to reach the 2nd item from the list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2824%2F0%2AaquaHun6bAJVja02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2824%2F0%2AaquaHun6bAJVja02.png" width="800" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.1)&lt;/strong&gt; Initial position for the driver will be the search input box. Keyboard actions will be performed about it. That is why the web element reference on the code is the locator for this box.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A2B-wLCGsoVHvmAR4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A2B-wLCGsoVHvmAR4.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.2)&lt;/strong&gt; Now, to reach the first value from the list, &lt;em&gt;LambdaTest&lt;/em&gt;, we need to press the arrow down key. At this point value for &lt;em&gt;i = 1&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3144%2F0%2A58VHSee-mWoxW-g3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3144%2F0%2A58VHSee-mWoxW-g3.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.3)&lt;/strong&gt; After this the value for &lt;em&gt;i&lt;/em&gt; gets incremented to 2, which is valid to execute the loop again. The keyboard down arrow key is pressed again to reach the next value, &lt;em&gt;lambdatest noida&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3150%2F0%2AzJH8EdocitC6XnvP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3150%2F0%2AzJH8EdocitC6XnvP.png" width="800" height="812"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.4)&lt;/strong&gt; At this step, the value of &lt;em&gt;i = 3&lt;/em&gt;, and our condition for the loop to continue fails. This means driver focus has reached the required element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; Use the Keys action again to press the enter key on this value and reach the search results page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2452%2F0%2AAtTS1rD3pSxWEoEd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2452%2F0%2AAtTS1rD3pSxWEoEd.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6.&lt;/strong&gt; Wait for the search results page to load with the help of the &lt;em&gt;WebDriverWait&lt;/em&gt; object by using &lt;em&gt;ExpectedConditions&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A5G5zvswFmbzdptP1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A5G5zvswFmbzdptP1.png" width="800" height="180"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7.&lt;/strong&gt; Once the search results page is loaded, fetch the search term value from the page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ApxUITY3USBpwm4ru.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ApxUITY3USBpwm4ru.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Assert that this value contains the initial given search term.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AnID-_Xql-mEkebC0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AnID-_Xql-mEkebC0.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An important point to note here is that, unlike the last test case, we would not assert an exact matching term as the code did not select a particular value from the auto-suggestions list by matching it.&lt;/p&gt;

&lt;p&gt;On executing this test case, you get output like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3096%2F0%2AB9Rqv9Zq7QWSlmap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3096%2F0%2AB9Rqv9Zq7QWSlmap.png" width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Explore our in-depth guide comparing &lt;a href="https://www.lambdatest.com/learning-hub/protractor-vs-selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Protractor vs Selenium&lt;/a&gt; to make informed decisions for your test automation needs. Gain valuable insights into the key differences, advantages, and ideal use cases of these widely-used testing frameworks.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Auto-Suggestions in Selenium using web-scraping: Google
&lt;/h2&gt;

&lt;p&gt;This approach is similar to the first one, i.e., using the entire auto-suggestions list to compare and select the required search term. It only differs in how we access the list and fetch the values from the auto-suggestion list. This approach uses the technique of Web Scraping.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/scraping-dynamic-web-pages/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Web scraping&lt;/a&gt; is the process of automatically extracting data from websites. It involves writing a program to fetch web pages, parse the HTML content, and extract the desired information. Web scraping enables gathering data for analysis, research, or automation, but it should be done in compliance with website terms and legal regulations.&lt;/p&gt;

&lt;p&gt;Let’s look at the below file to better understand how web scraping can be used for handling auto-suggestions in Selenium.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; package test;


    import java.util.List;


    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.testng.Assert;
    import org.testng.annotations.Test;


    import base.BaseTest;


    public class TestUsingWebScrapping extends BaseTest {


       @Test
       public void testUsingWebScraping_Google() {


           String expectedSearchTerm = "lambdatest careers";


           navigateToGoogleAndSearch();


           // Fetch the list of all suggestions
           System.out.println("Fetching the web element list for all suggestions");
           WebElement autoSuggestionListBox = driver.findElement(By.xpath("//*[@role='listbox']"));
           List&amp;lt;WebElement&amp;gt; autoSuggestionList = autoSuggestionListBox.findElements(By.xpath(".//li"));


           // scrape the autoSuggestionList to get each suggested term for given search
           // term.
           System.out.println("&amp;lt;-------- Started Web scraping for suggestion list --------&amp;gt;");
           for (WebElement listTerm : autoSuggestionList) {
               WebElement term = listTerm.findElement(By.xpath(".//span"));
               System.out.println("Auto Suggestion Value : " + term.getText());
               if (term.getText().equalsIgnoreCase(expectedSearchTerm)) {
                   System.out.println("\nFound required value in auto-suggestion list. Clicking on it now\n");
                   term.click();
                   break;
               }
           }


           // wait for the search results page to load and verify if it has the same term as we
           // expected.
           wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@type='search']")));
           Assert.assertEquals(driver.findElement(By.xpath("//*[@type='search']")).getText(), expectedSearchTerm,
                   "Valid Search result page.");
           System.out.println("Reached to google search result page for : " + expectedSearchTerm);
       }
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Walkthrough
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; Add a new java test file, &lt;em&gt;TestHandleAutoSuggestions_UsingWebScrapping&lt;/em&gt;, and extend &lt;em&gt;BaseTest.java&lt;/em&gt; to it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Aq3Wu5Srjvhlwo8If.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Aq3Wu5Srjvhlwo8If.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Add the first test case, ​​&lt;em&gt;testUsingWebScraping_Google()&lt;/em&gt;, and annotate it with the &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;/em&gt; annotation of TestNG.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AlZ2qAF53mAJydN4E.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AlZ2qAF53mAJydN4E.png" width="800" height="349"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Similar to the steps in previous cases, we define an expected search term and call the &lt;em&gt;navigateToGoogleAndSearch()&lt;/em&gt; method of BaseTest.java to navigate to Google and enter the search term.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ABeb-oRxvPEpzFH1N.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ABeb-oRxvPEpzFH1N.png" width="800" height="306"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; This is the most important step when using the web scraping approach.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.1)&lt;/strong&gt; First, we need to identify the main container web element in which the entire auto-suggestion list is present.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3028%2F0%2AHHMXv5qRS13tDLSL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3028%2F0%2AHHMXv5qRS13tDLSL.png" width="800" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The screenshot below shows it is inside a &lt;em&gt;&amp;lt; ul &amp;gt;&lt;/em&gt; tag with attribute &lt;em&gt;role=”listbox”&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A-qX__iDKLZXgusMS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A-qX__iDKLZXgusMS.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.2)&lt;/strong&gt; Inside this element is the list of all auto-suggestion elements. We need to fetch this using the container element reference from the previous step.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3144%2F0%2A0crsNz_cLxcx2d30.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3144%2F0%2A0crsNz_cLxcx2d30.png" width="800" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; This web element list hosts the relevant auto-suggestion value data. It is present inside the &amp;lt; span &amp;gt; tag of these elements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AmbLw4-dyTxfe1vMT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AmbLw4-dyTxfe1vMT.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.1)&lt;/strong&gt; We start by traversing this list to fetch the required data for each element using a for loop.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2884%2F0%2A5x-bTwR7GLLPjS9v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2884%2F0%2A5x-bTwR7GLLPjS9v.png" width="800" height="221"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.2)&lt;/strong&gt; For each element use the &amp;lt; &lt;em&gt;span&lt;/em&gt; &amp;gt; tag to get the value. Log the auto-suggestion value for better debugging.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2336%2F0%2AnJ2n2gXH8L4R8EOw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2336%2F0%2AnJ2n2gXH8L4R8EOw.png" width="800" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.3)&lt;/strong&gt; Compare it with the expected search term. If it matches, click on it and break the loop, if not continue the process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AuOyX-tjvXx3pJ7-G.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AuOyX-tjvXx3pJ7-G.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6.&lt;/strong&gt; After selecting the search term, wait for the search results page to load and verify if it matches the expected result. This is similar to what we did in previous test scenarios.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AeKiMNbQUEIca5R5s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AeKiMNbQUEIca5R5s.png" width="800" height="162"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Executing the above test case for handling auto-suggestions in Selenium using web scraping would give an output like the below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AIEF66zUAygJ_ng2OERBeVw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AIEF66zUAygJ_ng2OERBeVw.png" width="800" height="526"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Boost Your Mobile App’s Performance! Discover Proven Strategies for &lt;a href="https://www.lambdatest.com/learning-hub/mobile-performance-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Mobile Performance Testing&lt;/a&gt;. Increase User Satisfaction &amp;amp; Rankings. Get Results Fast.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Handling Auto-Suggestions in Selenium using web-scraping: Amazon
&lt;/h2&gt;

&lt;p&gt;Like the last test scenario, this one also uses web scraping to handle the auto-suggestions list and select the required term. In this one, we take the example of a popular eCommerce website, Amazon, to understand the working of auto-suggestions on an eCommerce platform.&lt;/p&gt;

&lt;p&gt;A similar strategy can be added to your website’s test automation scripts. You should be mindful that the website’s design or the products’ availability may change. In such circumstances, you just need to change the search term, and the code will continue functioning correctly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;For this, add a new test case to the same file, *TestUsingWebScrapping.java*, and name it *testUsingWebScraping_Amazon()*.

    @Test
    public void testUsingWebScraping_Amazon()
    {
       String expectedSearchTerm = "samsung galaxy s22";

       // to navigate to the website and enter search term
       System.out.println("Navigating to the website");
       driver.get("https://www.amazon.com");


       System.out.println("Entering search term as : samsung galaxy s");
       driver.findElement(By.id("twotabsearchtextbox")).sendKeys("samsung galaxy s");


       // explicit wait to wait for the auto suggestions list to be present
       wait = new WebDriverWait(driver, Duration.ofSeconds(10));
       wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@class='left-pane-results-container']")));
       System.out.println("Auto-suggestions list found");

       // Fetch the list of all suggestions
       System.out.println("Fetching the web element list for all suggestions");
       WebElement autoSuggestionListBox = driver.findElement(By.xpath("//*[@class='left-pane-results-container']"));
       List&amp;lt;WebElement&amp;gt; autoSuggestionList = autoSuggestionListBox.findElements(By.xpath(".//div[@class='s-suggestion-container']"));

       // scrape the autoSuggestionList to get each suggested term for a given search term.
       System.out.println("&amp;lt;-------- Started Web scraping for suggestion list --------&amp;gt;");
       for (WebElement listTerm : autoSuggestionList) {
           WebElement term = listTerm.findElement(By.xpath(".//div"));
           System.out.println("Auto Suggestion Value : " + term.getText());
           if (term.getText().equalsIgnoreCase(expectedSearchTerm)) {
               System.out.println("\nFound required value in auto suggestion list. Clicking on it now\n");
               term.click();
               break;
           }
       }
       wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[contains(@cel_widget_id,'MAIN-SEARCH_RESULTS')]")));
       //scraping the search results page to get the price of required product
       List&amp;lt;WebElement&amp;gt; productDetails = driver.findElements(By.xpath("//*[contains(@cel_widget_id,'MAIN-SEARCH_RESULTS')]"));
       for(WebElement product : productDetails) {
           WebElement productName = product.findElement(By.xpath(".//h2"));
           WebElement productPrice = product.findElement(By.xpath(".//*[@class='a-price']"));
           System.out.println("Product Name: " + productName.getText());
           if(productName.getText().contains("Ultra 5G S908U")) {
               System.out.println("\nFound Required Product");
               System.out.println("Required Product Price : " + productPrice.getText());
               break;
           }
       }
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Walkthrough
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; Add a new test case &lt;em&gt;testUsingWebScraping_Amazon()&lt;/em&gt;, and annotate it with &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;/em&gt; annotation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ABPbXSAV_GTHfxLr5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ABPbXSAV_GTHfxLr5.png" width="800" height="349"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Store the expected search term to a variable that will be used to compare and select from the auto-suggestion list at a later stage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AdjBO3BNeEna_OeU-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AdjBO3BNeEna_OeU-.png" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Navigate to Amazon.com and enter the search term as &lt;em&gt;samsung galaxy s&lt;/em&gt; and wait for the auto-suggestions list to load.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AmYSuauAqr3N6npZG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AmYSuauAqr3N6npZG.png" width="800" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To verify that the auto-suggestion dropdown list has loaded, you can wait for the web element for the same to be present.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ApVz--8T5_XLZOF4G.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ApVz--8T5_XLZOF4G.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; Again using the same locator, we store the web element reference to a variable. This refers to the main container web element, which has the auto-suggestion list in child tags.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AR8UxtK19Bn6DzaTK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AR8UxtK19Bn6DzaTK.png" width="800" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; Use the container web element to fetch the list of all auto-suggestions-related data that is present inside the &lt;em&gt;&amp;lt; div &amp;gt;&lt;/em&gt; tag in this element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AhPdYVrX_8TBEWpdD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AhPdYVrX_8TBEWpdD.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are using the &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/?utm_source=devto&amp;amp;utm_medium=group&amp;amp;utm_campaign=aug_09&amp;amp;utm_term=bw&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;XPath locator&lt;/a&gt; here for tag along with its class attribute.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AlSMVJVjl7BhB4y1k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AlSMVJVjl7BhB4y1k.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6.&lt;/strong&gt; Use the web scraping approach to fetch relevant auto-suggestion values from the list of web elements fetched in the last step. For this, locate the child &amp;lt; *div *&amp;gt; tag again, which holds this information, and print it for each element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2452%2F0%2AFSjo7HXVS8hsRB9e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2452%2F0%2AFSjo7HXVS8hsRB9e.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6.1)&lt;/strong&gt; Inside the loop only, compare the auto-suggestion value with the expected product name. If it matches, select the same to see a detailed list of all related products.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A1p5TqkL-pYTuEUGb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A1p5TqkL-pYTuEUGb.png" width="800" height="233"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should see a page like the one below on selecting the product.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AXadUjAm4rOiGoMzY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AXadUjAm4rOiGoMzY.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7.&lt;/strong&gt; Use the explicit wait object again to wait for the page load to complete using ExpectedConditions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AW7WrM5-d8c3acllD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AW7WrM5-d8c3acllD.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8.&lt;/strong&gt; Implement the web scraping approach again on this page. This time, we scrape the entire page to get the name and price for the required product.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8.1)&lt;/strong&gt; Fetch the web element locator, which contains all this information, and store it in a list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AcN1MVv5IfZxUgaZi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AcN1MVv5IfZxUgaZi.png" width="800" height="130"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AHDYCh8zLUVVNTsZG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AHDYCh8zLUVVNTsZG.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8.2)&lt;/strong&gt; Scrape the information inside this element to get the product name and price as per requirement. The product name is present inside &amp;lt; &lt;em&gt;h2&lt;/em&gt; &amp;gt; tag and price inside a tag with a class attribute having value &lt;em&gt;class = ‘a-price’.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A7AIFatwqrKUakWEy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A7AIFatwqrKUakWEy.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2972%2F0%2A26qTbAga0KD_oLNN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2972%2F0%2A26qTbAga0KD_oLNN.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8.3)&lt;/strong&gt; Use an if condition to check if the product name contains the required model number. If yes, print the price, if not continue to the next element until all product elements are processed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2712%2F0%2ALfk2YS7gtobEGt9o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2712%2F0%2ALfk2YS7gtobEGt9o.png" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Executing this automation script to handle auto-suggestions on Amazon for a product search and get its price for a matching product using web scraping, you will get an output like the one below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A-kivW9jGrxoyEBiw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A-kivW9jGrxoyEBiw.png" width="800" height="550"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;This guide focuses on &lt;a href="https://www.lambdatest.com/learning-hub/test-automation-metrics?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test automation metrics&lt;/a&gt;, its significance, strategies, challenges, and key test automation metrics for measuring success.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This brings us to the conclusion of our blog on handling auto-suggestions in Selenium Java automation to create robust and reliable automation. We learned details on auto-suggestions, the challenges they might pose, and how to handle them. Auto-suggestions can be successfully handled in automation by using explicit waits, validating suggestions, handling suggestions load time, etc. It is now time for you to begin handling the auto-suggestions on your test website using Selenium automation.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>selenium</category>
      <category>java</category>
      <category>testing</category>
    </item>
    <item>
      <title>How To Handle NoSuchWindowException In Selenium</title>
      <dc:creator>Vipulgupta9090</dc:creator>
      <pubDate>Wed, 14 Jun 2023 06:23:00 +0000</pubDate>
      <link>https://dev.to/testmuai/how-to-handle-nosuchwindowexception-in-selenium-30ke</link>
      <guid>https://dev.to/testmuai/how-to-handle-nosuchwindowexception-in-selenium-30ke</guid>
      <description>&lt;p&gt;In today’s digital landscape, websites are brimming with dynamic content, captivating users with interactive experiences. A notable feature is the frequent emergence of new windows and tabs triggered by specific actions taken by users. These dynamic elements play a vital role in enhancing user engagement and providing seamless navigation.&lt;/p&gt;

&lt;p&gt;As an automation tester, encountering scenarios where multiple windows or tabs need to be handled within an end-to-end flow is a common occurrence. Successfully managing this dynamic behavior becomes essential to ensure the accuracy and reliability of your automated tests.&lt;/p&gt;

&lt;p&gt;However, this becomes highly challenging as automated tests tend to fail and make your test scripts less reliable. Your &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automation testing&lt;/a&gt; script should be able to manage many windows for better testing. In such situations, switching focus to perform any action on the necessary window or tab becomes an unsaid requirement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; offers various methods to work with multiple windows. But any slightest mistake, like using an invalid window handle, switching to a closed window, etc., can lead to &lt;em&gt;NoSuchWindowException&lt;/em&gt; in the automation script. We will discuss more about these in the next sections.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NoSuchWindowException&lt;/em&gt; occurs when the &lt;a href="https://www.lambdatest.com/learning-hub/webdriver?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;WebDriver&lt;/a&gt; tries to interact with a window that is not available at that moment. This could be due to a closed window or the window in focus being different.&lt;/p&gt;

&lt;p&gt;In this &lt;a href="https://www.lambdatest.com/learning-hub/selenium-java?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium Java tutorial&lt;/a&gt;, we will discuss how to handle &lt;em&gt;NoSuchWindowException *in Selenium. We will discuss the most common reasons for the same and the best practices to handle it. The practical implementation of these practices using Selenium Java will help you to learn how to handle *NoSuchWindowException&lt;/em&gt; in your automation scripts to make tests run smoothly and reliably.&lt;/p&gt;

&lt;p&gt;So, Let’s get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a window and window handle?
&lt;/h2&gt;

&lt;p&gt;In Selenium, a window refers to a graphical user interface (GUI) window or a web browser window/tab, in particular while working with &lt;a href="https://www.lambdatest.com/learning-hub/web-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;web automation&lt;/a&gt;. When Selenium WebDriver launches a web browser, it opens a new window in which the webpage gets loaded.&lt;/p&gt;

&lt;p&gt;Each window is represented by a unique ID called a window handle. It allows the WebDriver to interact with the window and its elements. These handles are also used to switch between different windows, manipulate the size and position of the window, and perform other actions on the window and its contents.&lt;/p&gt;

&lt;p&gt;If you have multiple browser windows open, you can use this method to retrieve a set of all the window handles. In this blog, &lt;a href="https://www.lambdatest.com/blog/selenium-with-java/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium with Java&lt;/a&gt; language has been used for demonstration and to provide code samples.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.getWindowHandles()


    Returns a Set&amp;lt;String&amp;gt; containing handles of all the windows
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get the current window handle:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.getWindowHandle()



Returns the handle of the current window as a String
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To switch to a specific window by passing in its handle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.switchTo().window("&amp;lt;random_window_handle&amp;gt;")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To switch to the main window back:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    driver.switchTo().defaultContent()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To understand these methods better, consider the following test scenario.&lt;/p&gt;

&lt;p&gt;We want to reach the Twitter page and click on the Login button. This button is available on the page loaded in a new window. This new window, Window2 is loaded by clicking the Follow button on the previous/main window, Window1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2Ardc4JHGpFFDv5M1wP_QVeg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2Ardc4JHGpFFDv5M1wP_QVeg.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, if we want the test scenario to pass, the driver focus must be switched first to Window2 once it is loaded. If you try to switch driver focus before the load is completed, it will raise a NoSuchWindowException.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Which are the most wanted &lt;a href="https://www.lambdatest.com/blog/automation-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automation testing tools&lt;/a&gt; in 2023 that have climbed the top of the ladder so far? Let’s take a look!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a NoSuchWindowException in Selenium?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;NoSuchWindowException&lt;/em&gt; is one of the most frequently encountered &lt;a href="https://www.lambdatest.com/blog/49-common-selenium-exceptions-automation-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;exceptions in Selenium WebDriver&lt;/a&gt; while working with multiple windows/tabs. The &lt;em&gt;NoSuchWindowException&lt;/em&gt; is raised when the WebDriver cannot find the desired browser window or tab that it needs to interact with.&lt;/p&gt;

&lt;p&gt;This exception would occur if there are multiple open windows and WebDriver is looking for one with incorrect window handles. Another reason could be that the required window is closed already and is no longer available.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;NoSuchWindowException&lt;/em&gt; can also occur if there is a problem with the browser itself, like it has crashed due to some previous actions or has become unresponsive.&lt;/p&gt;

&lt;p&gt;An important thing to note here is that &lt;a href="https://www.lambdatest.com/blog/switch-iframes-in-selenium-java/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;handling multiple frames in Selenium&lt;/a&gt; is slightly different than &lt;a href="https://www.lambdatest.com/blog/how-to-handle-multiple-windows-in-selenium-webdriver-using-java/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;handling multiple windows in Selenium&lt;/a&gt;. As for frames, the methods provided to switch are different, and similarly, the expectation raised in case a frame is not found is &lt;em&gt;NoSuchFrameException&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/32eIE4PAbJk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Perform browser &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automation testing platform&lt;/a&gt; on the most powerful cloud infrastructure. Leverage LambdaTest automation testing for faster, reliable and scalable experience on cloud.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reasons for NoSuchWindowException in Selenium
&lt;/h2&gt;

&lt;p&gt;This exception can occur for many reasons while working with multiple windows. A few of the most common ones are.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Closure of window&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Driver focuses on another window&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Invalid window handle&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Window load timing issue&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Demonstration: How to fix NoSuchWindowException in Selenium?
&lt;/h2&gt;

&lt;p&gt;To demonstrate the best practices for handling &lt;em&gt;NoSuchWindowException&lt;/em&gt; in Selenium, we will create a Java project with Maven and &lt;a href="https://www.lambdatest.com/learning-hub/testng?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;TestNG framework&lt;/a&gt; using &lt;a href="https://www.lambdatest.com/learning-hub/selenium-4?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium 4&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Scenario — 1 (Demonstrate &lt;em&gt;NoSuchWindowException&lt;/em&gt;)&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to &lt;a href="https://www.lambdatest.com/selenium-playground/window-popup-modal-demo" rel="noopener noreferrer"&gt;https://www.lambdatest.com/selenium-playground/window-popup-modal-demo&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click &lt;strong&gt;Follow on Twitter&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Get a list of all window handles and print them on the console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Try to switch to the new window using an invalid window handle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verify &lt;em&gt;NoSuchWindowException&lt;/em&gt; is raised.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Test Scenario — 2 (Handle &lt;em&gt;NoSuchWindowException&lt;/em&gt; using Try-Catch)&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to &lt;a href="https://www.lambdatest.com/selenium-playground/window-popup-modal-demo" rel="noopener noreferrer"&gt;https://www.lambdatest.com/selenium-playground/window-popup-modal-demo&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click &lt;strong&gt;Follow on Twitter&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Get a list of all window handles and print them on the console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start the try block.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Try to switch to the new window using an invalid window handle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verify &lt;em&gt;NoSuchWindowException&lt;/em&gt; is raised and caught inside the catch block.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Test Scenario — 3 (Handle &lt;em&gt;NoSuchWindowException&lt;/em&gt; using Explicit Wait)&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to &lt;a href="https://www.lambdatest.com/selenium-playground/window-popup-modal-demo" rel="noopener noreferrer"&gt;https://www.lambdatest.com/selenium-playground/window-popup-modal-demo&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click &lt;strong&gt;Follow on Twitter&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add Explicit Wait using Expected Conditions to wait until the number of windows is 2.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fetch a list of all window handles and store them in a variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start a loop to switch to each window using the window handle one by one.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Switch to the new window using the window handle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check if this is the required window by verifying its title.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If it is a match, break the loop. If not, repeat Steps 6 and 7 till the required window is found.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the Login button on the new window.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This blog uses the &lt;a href="https://www.lambdatest.com/blog/configure-cucumber-setup-in-eclipse-and-intellij/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Eclipse IDE&lt;/a&gt; for demonstration purposes, but feel free to use any IDE of your preference. The implementation process remains identical until the creation of a Maven project, regardless of the IDE you choose.&lt;/p&gt;

&lt;p&gt;As we are working with Selenium web automation, we would start by adding Selenium dependency along with TestNG, which will be used for &lt;a href="https://www.lambdatest.com/learning-hub/test-execution?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test execution&lt;/a&gt; to the pom.xml of the project.&lt;/p&gt;

&lt;p&gt;After the project is created and dependencies are added, the final pom.xml would be like the one below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;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 https://maven.apache.org/xsd/maven-4.0.0.xsd"&amp;gt;
       &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
       &amp;lt;groupId&amp;gt;NoSuchWindowException&amp;lt;/groupId&amp;gt;
       &amp;lt;artifactId&amp;gt;NoSuchWindowException&amp;lt;/artifactId&amp;gt;
       &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;
       &amp;lt;build&amp;gt;
           &amp;lt;sourceDirectory&amp;gt;src&amp;lt;/sourceDirectory&amp;gt;
           &amp;lt;plugins&amp;gt;
               &amp;lt;plugin&amp;gt;
                   &amp;lt;artifactId&amp;gt;maven-compiler-plugin&amp;lt;/artifactId&amp;gt;
                   &amp;lt;version&amp;gt;3.8.1&amp;lt;/version&amp;gt;
                   &amp;lt;configuration&amp;gt;
                       &amp;lt;release&amp;gt;19&amp;lt;/release&amp;gt;
                   &amp;lt;/configuration&amp;gt;
               &amp;lt;/plugin&amp;gt;
           &amp;lt;/plugins&amp;gt;
       &amp;lt;/build&amp;gt;
       &amp;lt;dependencies&amp;gt;
           &amp;lt;dependency&amp;gt;
               &amp;lt;groupId&amp;gt;org.seleniumhq.selenium&amp;lt;/groupId&amp;gt;
               &amp;lt;artifactId&amp;gt;selenium-java&amp;lt;/artifactId&amp;gt;
               &amp;lt;version&amp;gt;4.8.3&amp;lt;/version&amp;gt;
           &amp;lt;/dependency&amp;gt;
           &amp;lt;dependency&amp;gt;
               &amp;lt;groupId&amp;gt;org.testng&amp;lt;/groupId&amp;gt;
               &amp;lt;artifactId&amp;gt;testng&amp;lt;/artifactId&amp;gt;
               &amp;lt;version&amp;gt;7.7.1&amp;lt;/version&amp;gt;
               &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
           &amp;lt;/dependency&amp;gt;
       &amp;lt;/dependencies&amp;gt;
    &amp;lt;/project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, add a new Java test file &lt;em&gt;TestNoSuchWindowException.java&lt;/em&gt; under &lt;em&gt;src.test&lt;/em&gt; package. Once added the project structure would look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ALMoyvcwDFUfG_HRn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ALMoyvcwDFUfG_HRn.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will add our test cases to this to see how to prevent &lt;em&gt;NoSuchWindowException&lt;/em&gt; from occurring in our &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium automation&lt;/a&gt; scripts.&lt;/p&gt;

&lt;p&gt;In this blog, we will harness the power of a Selenium cloud grid like LambdaTest to perform &lt;a href="https://www.lambdatest.com/selenium-java-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium Java testing&lt;/a&gt;. LambdaTest is a digital experience testing cloud that exponentially increases your &lt;a href="https://www.lambdatest.com/blog/key-browser-statistics/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;browser coverag&lt;/a&gt;e by running your &lt;a href="https://www.lambdatest.com/java-automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Java automation testing&lt;/a&gt; scripts with Selenium on a cloud of 3000+ desktop and mobile environments.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/SqQ8SugRDos"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;A complete &lt;a href="https://www.lambdatest.com/learning-hub/manual-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Manual testing&lt;/a&gt; tutorial covering all aspects of Manual testing, including strategies and best practices.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After adding all the WebDriver configurations and test case examples to demonstrate the fix for &lt;em&gt;NoSuchWindowException&lt;/em&gt;, the completed file should look like the below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; package test;


    import java.net.*;
    import java.time.Duration;
    import java.util.HashMap;
    import java.util.Set;


    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.annotations.*;
    import org.openqa.selenium.By;
    import org.openqa.selenium.NoSuchWindowException;
    import org.openqa.selenium.safari.`Options;
    import org.openqa.selenium.support.ui.*;


    public class TestNoSuchWindowException {


       public RemoteWebDriver driver = null;


       String username = System.getenv("LT_USERNAME") == null ? "&amp;lt;lambdatest_username&amp;gt;" : System.getenv("LT_USERNAME");
       String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "&amp;lt;lambdatest_accesskey&amp;gt;" : System.getenv("LT_ACCESS_KEY");


       @BeforeTest
       public void setup()
       {
           try {
               SafariOptions safariOptions = new SafariOptions();
               safariOptions.setPlatformName("MacOS Big sur");
               safariOptions.setBrowserVersion("14.0");

               HashMap&amp;lt;String, Object&amp;gt; ltOptions = new HashMap&amp;lt;String, Object&amp;gt;();
               ltOptions.put("build", "NoSuchWindowException in Selenium");
               ltOptions.put("name", "Handling NoSuchWindowException");
               ltOptions.put("w3c", true);
               safariOptions.setCapability("LT:Options", ltOptions);

               driver = new RemoteWebDriver(
                       new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), safariOptions);
           } catch (MalformedURLException e) {
               e.printStackTrace();
           }
            driver.get("https://www.lambdatest.com/selenium-playground/window-popup-modal-demo");
       }


       @AfterTest
       public void tearDown() {
           driver.quit();
       }


       @Test
       public void testNoSuchWindowException()
       {  
           System.out.println("Clicking on Follow button");
           driver.findElement(By.xpath("//*[@title='Follow @Lambdatesting on Twitter']")).click();

           Set&amp;lt;String&amp;gt; handles = driver.getWindowHandles();
           for(String handle : handles)
           {
               System.out.println("Valid handle value : " + handle);
               //adding -updated to valid handle value to demonstrate exception
               //due to invalid handle value
               driver.switchTo().window(handle + "-updated");
           }
       }


       @Test
       public void testNoSuchWindowException_fix()
       {  
           System.out.println("Clicking on Follow button");
           driver.findElement(By.xpath("//*[@title='Follow @Lambdatesting on Twitter']")).click();

           WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
           wait.until(ExpectedConditions.numberOfWindowsToBe(2));
           System.out.println("Verified total windows are 2");

           System.out.println("Fetching window handles of all windows");
           Set&amp;lt;String&amp;gt; handles = driver.getWindowHandles();


           System.out.println("Switching to each window one by one to verify if it is required one");
           for(String handle : handles)
           {
               System.out.println("Switching to window with handle : " + handle);
               driver.switchTo().window(handle);
               //using window title to reach to required window
               if(driver.getTitle().equals("Profile / Twitter"))
               {
                   System.out.println("Reached required window");
                   break;
               }
           }
           driver.findElement(By.xpath("//*[@data-testid='login']")).click();
           System.out.println("Clicked Login button successfully");
       }

       @Test
       public void testNoSuchWindowException_fix_tryCatch()
       {
           System.out.println("Clicking on Follow button");
           driver.findElement(By.xpath("//*[@title='Follow @Lambdatesting on Twitter']")).click();
           try
           {
               Set&amp;lt;String&amp;gt; handles = driver.getWindowHandles();
               for(String handle : handles)
               {
                   System.out.println("Valid handle value : " + handle);
                   //adding -updated to valid handle value to get exception
                   //due to invalid handle value
                   System.out.println("Invalid handle value used : " + handle + "-updated");
                   driver.switchTo().window(handle + "-updated");
               }
           } catch (NoSuchWindowException ex) {
               System.out.println("We are inside catch block");
               System.out.println("NoSuchWindowException has been caught.");
           }
       }
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;CodeWalkthrough&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;. We start by creating an instance of &lt;a href="https://www.lambdatest.com/blog/selenium-remotewebdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;*RemoteWebDriver&lt;/a&gt;* as we will execute the code on the cloud Selenium Grid.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AIlzTGxz06Kh3niRG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AIlzTGxz06Kh3niRG.png" width="778" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;. Navigate to the &lt;a href="https://accounts.lambdatest.com/details/profile/" rel="noopener noreferrer"&gt;LambdaTest Profile&lt;/a&gt; section once you create your account to fetch the username and access key. These will be used to make a connection to their cloud grid.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AwNxcVYWDwcbAxxpW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AwNxcVYWDwcbAxxpW.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also configure the username and access key as environment variables using the following commands and directly fetch them in code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For macOS and Linux:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export LT_USERNAME=LT_USERNAME
    export LT_ACCESS_KEY=LT_ACCESS_KEY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;For Windows:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set LT_USERNAME=LT_USERNAME
    set LT_ACCESS_KEY=LT_ACCESS_KEY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also fetch the required browser capabilities to use the LambdaTest platform by navigating to their &lt;a href="https://www.lambdatest.com/capabilities-generator/" rel="noopener noreferrer"&gt;Automation Capabilities Generator&lt;/a&gt;. This helps by offering ready-to-use code for setting up browser capabilities that can be used in execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AZ9M7L3hD0zdmZ90A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AZ9M7L3hD0zdmZ90A.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;. Create a base method and name it as &lt;em&gt;setup()&lt;/em&gt; and annotate with &lt;em&gt;@BeforeTest&lt;/em&gt; &lt;a href="https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;annotation in TestNG&lt;/a&gt;. This method will be used for driver setup and opening the webpage before each test.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AzltolmfWHB0DExj9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AzltolmfWHB0DExj9.png" width="518" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this blog, we have used Selenium 4, which supports &lt;em&gt;SafariOptions&lt;/em&gt; class. In this function, an object of the same is created to define browser properties like browser version, OS version, etc. It also accepts the variables required for LambdaTest execution as a capability.&lt;/p&gt;

&lt;p&gt;These variables are used to set values like build, name, or any other browser property, which will help to identify the run on the &lt;a href="https://accounts.lambdatest.com/dashboard/" rel="noopener noreferrer"&gt;LambdaTest Dashboard&lt;/a&gt; or modify behavior.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AMVYR7fRh6y_gLZ8W.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AMVYR7fRh6y_gLZ8W.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; Finally, an instance of &lt;em&gt;RemoteWebDriver&lt;/em&gt; is created to connect to the LambdaTest Cloud grid using the &lt;em&gt;safariOptions&lt;/em&gt;, and your Lambdatest username and the access key.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AXIyGew7X4VGe7Pgz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AXIyGew7X4VGe7Pgz.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; Finally, navigate to the sample website under test, which will have a new window open on clicking a button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F0%2AAIKnJGQYfrvO2ccE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F0%2AAIKnJGQYfrvO2ccE.png" width="800" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6.&lt;/strong&gt; Add a method as &lt;em&gt;tearDown()&lt;/em&gt; and annotate it with &lt;em&gt;@AfterTest&lt;/em&gt; annotation. This will be used to close the session after each test case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Ao4W6aV55_JU9ZAax.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Ao4W6aV55_JU9ZAax.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Need a great solution for &lt;a href="https://www.lambdatest.com/safari-browser-for-windows?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Safari browser testing&lt;/a&gt; on Windows? Forget about emulators or simulators — use real online browsers. Try LambdaTest for free!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test Scenario: NoSuchWindowException in Selenium
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; Add the first test case and name it as &lt;em&gt;testNoSuchWindowException()&lt;/em&gt;. In this test, we try to simulate the &lt;em&gt;NoSuchWindowException&lt;/em&gt; in Selenium by using an invalid window handle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Au8GM3uuhlbuSsrTO.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Au8GM3uuhlbuSsrTO.png" width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; We start by clicking the Follow button, which will open a new window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3000%2F0%2A4bBr24BTus_AAEP1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3000%2F0%2A4bBr24BTus_AAEP1.png" width="800" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For this, we use the &lt;a href="https://www.lambdatest.com/blog/findelement-and-findelements-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;driver.findElement() method in Selenium&lt;/a&gt; and use &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;XPath&lt;/a&gt; as a web element locator.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Ab-caJHgeRGbvWJRz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Ab-caJHgeRGbvWJRz.png" width="800" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;. Next, fetch the list of all window handles using &lt;em&gt;driver.getWindowHandles()&lt;/em&gt; and store them in a variable. This traverses through the window handles list and prints valid handle values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2132%2F0%2AiYD4oLZU_jOX9oFC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2132%2F0%2AiYD4oLZU_jOX9oFC.png" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; Lastly, our attempt to switch to the newly opened window involves passing an invalid window handle. For this, the valid handle value from the previous step is manipulated to demonstrate the exception.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2364%2F0%2AQWjn7orlcJATrskU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2364%2F0%2AQWjn7orlcJATrskU.png" width="800" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; On executing this, you can observe that the test case fails with &lt;em&gt;NoSuchWindowException&lt;/em&gt; in Selenium as the window handle passed is invalid, and Selenium cannot locate a window using the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AoFGzV6XtzqzR17AC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AoFGzV6XtzqzR17AC.png" width="800" height="612"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the following sections, we will see the implementation of some of the best practices to prevent this &lt;em&gt;NoSuchWindowException&lt;/em&gt; in Selenium from happening.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Elevate Your &lt;a href="https://www.lambdatest.com/mobile-app-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Mobile App Testing&lt;/a&gt; Quality: Make the most of your mobile application testing &amp;amp; Test on LambdaTest Cloud for Comprehensive Testing, Bug Detection &amp;amp; More.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixing NoSuchWindowException in Selenium using Try-Catch
&lt;/h2&gt;

&lt;p&gt;As discussed in the best practices sections, one of the most used ways to handle &lt;em&gt;NoSuchWindowException&lt;/em&gt; in Selenium is using the try-catch block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; For this, add another test case to the Java file and name it &lt;em&gt;testNoSuchWindowException_fix_tryCatch()&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2104%2F0%2AkceUptwkrQd45OIr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2104%2F0%2AkceUptwkrQd45OIr.png" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Click the Follow button, which is expected to open a new window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3000%2F0%2AW1XHTZHU68olRsPt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3000%2F0%2AW1XHTZHU68olRsPt.png" width="800" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; In this approach, code that would lead to an exception on switching to a new window, like the previous scenario, is used.&lt;/p&gt;

&lt;p&gt;The only difference is that we place this code inside the try block. This will help to catch the exception and prevent abrupt behavior from the automation script.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2824%2F0%2AJZvrpYi_5L7TlqsD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2824%2F0%2AJZvrpYi_5L7TlqsD.png" width="800" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; This is followed by a catch block. This helps to catch the exception if it occurs and perform any subsequent actions. It also helps to terminate the code gracefully in such cases.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2564%2F0%2AOUnwDuRoFCqQ3bEm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2564%2F0%2AOUnwDuRoFCqQ3bEm.png" width="800" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; Executing the above code, you will get the following output. In this, you can see that &lt;em&gt;NoSuchWindowException&lt;/em&gt; is raised but is handled gracefully inside the catch block.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3084%2F0%2AjDgVB2ypix_KZnFC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3084%2F0%2AjDgVB2ypix_KZnFC.png" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But it is very important to note that even if the exception is handled, you cannot perform any action on the new window. This is because the exception was raised, which means either the window handle is incorrect or the window does not exist.&lt;/p&gt;

&lt;p&gt;You can always add a retry in such a case to try again the previous action, which should launch the window or check if the handle is correct and try to switch again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Run your &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium Testing&lt;/a&gt; scripts on the LambdaTest cloud grid. Test on 3000+ desktop &amp;amp; mobile environments. Try it for free!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixing NoSuchWindowException in Selenium using Window Handles and Waits
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; Next, add a new test case and name it testNoSuchWindowException_fix(). We will use this to demonstrate how using Waits and Window Handles properly helps us to prevent &lt;em&gt;NoSuchWindowException&lt;/em&gt; from happening.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ATk2ha57zMTG_yZ8Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ATk2ha57zMTG_yZ8Q.png" width="800" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; We start by clicking on the button, which is expected to open a new window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3000%2F0%2AND0DjsfzvNZVL0jo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3000%2F0%2AND0DjsfzvNZVL0jo.png" width="800" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; As we click on the button, the expectation here is that now the total number of windows should be 2 like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A4Yku6-Yq73g9Da5R.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A4Yku6-Yq73g9Da5R.png" width="800" height="558"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To handle the exception, we use &lt;a href="https://www.lambdatest.com/blog/types-of-waits-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=june14_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;explicit waits in Selenium&lt;/a&gt; to wait until the number of windows is equal to 2 and then move forward with the flow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; For this, create an object of WebDriverWait class and pass the driver object and the duration for which we want to wait for the maximum time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2508%2F0%2ArnHD_YNue-JYvwBc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2508%2F0%2ArnHD_YNue-JYvwBc.png" width="800" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using this object with the &lt;em&gt;until()&lt;/em&gt; function, we add an &lt;em&gt;ExpectedConditions&lt;/em&gt; to wait for the number of windows to be 2. The driver waits for a maximum of 5 seconds for this condition to happen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2044%2F0%2AJEjlogkhtKGJTHm4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2044%2F0%2AJEjlogkhtKGJTHm4.png" width="800" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the number of windows is validated, we move forward else the driver throws an error to show that condition is not fulfilled.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; Next, fetch the window handle for all the open windows. We will be storing this in a Set.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2248%2F0%2AlcboEI7RFFjxC1tO.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2248%2F0%2AlcboEI7RFFjxC1tO.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6.&lt;/strong&gt; We will traverse through this list of handles and switch to each window one by one until the required window is found.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3084%2F0%2A4YM_FFhIk_qmPiCP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3084%2F0%2A4YM_FFhIk_qmPiCP.png" width="800" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7.&lt;/strong&gt; To check if the current window is required or not, we check by matching the title of the required window with the current window. If it’s a match, break the loop and continue. If not, switch to the next window using the handle and continue the same flow.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AruqTHCtENjPNp8iY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AruqTHCtENjPNp8iY.png" width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8.&lt;/strong&gt; When the required window is found in WebDriver focus, click on the login button, and the success message is printed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AZMon0v29SjLO8vBQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2392%2F0%2AZMon0v29SjLO8vBQ.png" width="800" height="243"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Executing the above test case, we get an output like the one below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AI8RYROEBsyzMXcQn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AI8RYROEBsyzMXcQn.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the output screenshot, you can see that we had successfully verified that 2 windows are present using waits. Then we switch driver focus to the required new window and can successfully click on the Login button without encountering an exception.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best practices to handle NoSuchWindowException in Selenium
&lt;/h2&gt;

&lt;p&gt;From the demonstration section, it is quite clear that &lt;em&gt;NoSuchWindowException&lt;/em&gt; in Selenium can be easily handled in your automation script by following some of the best practices.&lt;/p&gt;

&lt;p&gt;Let us have a look at these in a bit more detail.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Using explicit waits&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using valid window handles&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Switching to the correct window&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using a try-catch block&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keeping track of window handles&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, &lt;em&gt;NoSuchWindowException&lt;/em&gt; is a common exception in Selenium that can occur when working with multiple windows or tabs. However, with the right knowledge of best practices to handle the same, we can make automation tests run smoothly and reliably. In this blog, we discussed the common reasons that can lead to &lt;em&gt;NoSuchWindowException&lt;/em&gt; in Selenium. We also learned the best practices to handle the same and did some hands-on practice on how to add these approaches to your automation scripts. So, now it is for you to get started and, with a little attention to the powers of Selenium, prevent &lt;em&gt;NoSuchWindowException&lt;/em&gt; in your scripts.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>testing</category>
      <category>selenium</category>
      <category>html</category>
    </item>
    <item>
      <title>How To Handle SSL Certificates In Selenium WebDriver?</title>
      <dc:creator>Vipulgupta9090</dc:creator>
      <pubDate>Fri, 28 Apr 2023 08:31:26 +0000</pubDate>
      <link>https://dev.to/testmuai/how-to-handle-ssl-certificates-in-selenium-webdriver-3p00</link>
      <guid>https://dev.to/testmuai/how-to-handle-ssl-certificates-in-selenium-webdriver-3p00</guid>
      <description>&lt;p&gt;We often access a website on our browser, which works fine. But when you try to access the same while automating it using &lt;a href="https://www.lambdatest.com/learning-hub/webdriver?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium WebDriver&lt;/a&gt;, the website is not loaded, and the browser shows a security error like “&lt;em&gt;The connection is not secure&lt;/em&gt;” or “&lt;em&gt;This connection is Untrusted&lt;/em&gt;.”&lt;/p&gt;

&lt;p&gt;The reason for this is the missing SSL certificates used to add a layer of security to the website.&lt;/p&gt;

&lt;p&gt;While working on internal staging test environments, it is sometimes fine not to have SSL available as the infrastructure is local and is not accessible to the outside world and thus can be bypassed. But in a production environment, any good website cannot do well without these certificates.&lt;/p&gt;

&lt;p&gt;While manually accessing a URL, the browser imports all these required SSL certificates and stores them to load the website. But in the case of automation, every script is executed with a new profile; hence, the certificates are missing, leading to an error. SSL certificates must be handled in Selenium WebDriver to load the website correctly.&lt;/p&gt;

&lt;p&gt;This blog on how to handle SSL certificates in &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; will help you to understand more about SSL certificates and their importance in production and testing, their types, and how to handle SSL certificates in Selenium WebDriver on various browsers.&lt;/p&gt;

&lt;p&gt;So let’s get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an SSL Certificate?
&lt;/h2&gt;

&lt;p&gt;SSL or Secure Sockets Layer is a standard security protocol that helps to establish a secure connection between the client (browser) and the server. SSL certificates help to ensure that all the data transmission between a client and server is encrypted using a strong encryption standard like Advanced Encryption Standard (AES), RSA, TLS, etc.&lt;/p&gt;

&lt;p&gt;This helps validate the client’s identity to the server and thus keeps hackers at bay from spying on any information exchange. SSL encryption thus facilitates the exchange of sensitive information, like card details, usernames, passwords, etc., between the client and server at both ends and during the data transmission phase using encryptions.&lt;/p&gt;

&lt;p&gt;An SSL-secured website URL begins with &lt;em&gt;[https://&lt;/em&gt;,](https://,) and you can see a lock symbol or green color address bar if the connection is secure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_w7XY7gcagPVgJDk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_w7XY7gcagPVgJDk.png" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;In this tutorial, learn what is &lt;a href="https://www.lambdatest.com/learning-hub/regression-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Regression testing&lt;/a&gt;, its importance, types, and how to perform it.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of using SSL Certificates
&lt;/h2&gt;

&lt;p&gt;Encrypting data for transmission: SSL certificates encrypt data sent between a web server and a user’s browser, thus rendering it unreadable for any hacker. This helps safeguard data when dealing with sensitive information like credit card numbers, login credentials, and personal information.&lt;/p&gt;

&lt;p&gt;In this blog section on how to handle SSL certificates in Selenium WebDriver, let’s look at the benefits of using SSL certificates.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Authentication Support&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Higher SEO rankings&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Building user trust&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Compliance Friendly&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Protection against phishing attacks&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How does the SSL Certificate work?
&lt;/h2&gt;

&lt;p&gt;In this blog section on how to handle SSL certificates in Selenium WebDriver, we’ll focus on the workings of SSL certificates.&lt;/p&gt;

&lt;p&gt;SSL establishes a secure encrypted communication channel between the client and the server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AX6t6tRvQpSMK65vW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AX6t6tRvQpSMK65vW.png" width="613" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Detailed steps are mentioned below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The browser tries to make a connection to the server by sending a request to validate if it is secure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server then sends a copy of its SSL certificate to the browser to identify itself as secure and trustworthy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The browser then verifies if the certificate is valid, unexpired, and unrevoked using its Trusted Certificate Authority(CA) list. The browser sends a message to the server if the certificate is valid.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server then responds with a digitally signed acknowledgment, and a secure SSL encrypted session is established between the server and browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server and the browser can now send encrypted messages to each other.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Get started with this complete &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium automation&lt;/a&gt; testing tutorial. Learn what Selenium is, its architecture, advantages and more for automated cross browser testing:.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of SSL Certificates
&lt;/h2&gt;

&lt;p&gt;In this blog section on how to handle SSL certificates in Selenium WebDriver, let’s look at the types of using SSL certificates.&lt;/p&gt;

&lt;p&gt;When a website requests an SSL certificate from the Certificate Authority (CA), it gets a zip file containing different types of certificates as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Root Certificate&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Intermediary Certificate&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Server Certificate&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is an Untrusted SSL Certificate?
&lt;/h2&gt;

&lt;p&gt;Whenever you try to access a website, the authenticity and trust of the website are determined by the SSL certificate. If the certificate fails to validate itself, a secure encrypted connection is not established, and the browser throws an Untrusted Connection exception, resulting in a browser error. This is what you can understand as an Untrusted SSL Certificate.&lt;/p&gt;

&lt;p&gt;On loading a website with an untrusted certificate, you can see that on the web URL bar of the browser, it shows it as Not Secure like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AsMd1anyJhe_C7tL-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AsMd1anyJhe_C7tL-.png" width="800" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The type of certificate error you experience depends on your browser. In the demonstration section, we will see how to handle SSL certificates in Selenium WebDriver for different browsers. But before moving to that, let us understand the different types of SSL certificate errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;It’s crucial to debug websites for Safari before pushing them live. In this article, we look at how to debug websites using &lt;a href="https://www.youtube.com/watch?v=KZstMSOHIvQ&amp;amp;t=19006s?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Safari Developer tools&lt;/a&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of SSL Certificate Errors
&lt;/h2&gt;

&lt;p&gt;SSL certificate error handling can differ slightly across various browsers due to the varying error messages that appear in the case of an untrusted certificate. Before looking into the code to handle these, let us first look at different browser errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chrome&lt;/strong&gt; — &lt;em&gt;Your connection is not private&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ArhxkN5kUsFMC-I0R.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ArhxkN5kUsFMC-I0R.png" width="800" height="397"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Firefox&lt;/strong&gt; &lt;em&gt;— Warning: Potential Security Risk Ahead&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A2-f7Zfio0LUPNcXW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A2-f7Zfio0LUPNcXW.png" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safari&lt;/strong&gt; —&lt;em&gt;This Connection Is Not Private&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AuFRRqNJm5sgyzINB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AuFRRqNJm5sgyzINB.png" width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edge&lt;/strong&gt; — &lt;em&gt;Your connection isn’t private&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AB94MbcSRba3_4fs8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AB94MbcSRba3_4fs8.png" width="800" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You would notice that each browser has a different way of showing the error to the user. But the reason for the error is internally the same for a website server in all cases. A few of the standard error codes and reasons are&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ERROR CODE&lt;/th&gt;
&lt;th&gt;REASON&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ERR_CERT_DATE_INVALID&lt;/td&gt;
&lt;td&gt;Expired Certificate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ERR_CERT_REVOKED&lt;/td&gt;
&lt;td&gt;Revoked Certificate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ERR_CERT_AUTHORITY_INVALID&lt;/td&gt;
&lt;td&gt;Certificate signed by an untrusted source&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your website or web app online for iOS browser compatibility. Perform seamless cross browser testing on the latest &lt;a href="https://www.lambdatest.com/test-on-iphone-simulator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;iPhone Simulator&lt;/a&gt;. Try for free.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: How to handle SSL Certificates in Selenium WebDriver?
&lt;/h2&gt;

&lt;p&gt;So far, we have discussed and understood SSL certificates and the errors we can encounter on different browsers due to invalid certificates. Let us now move to practical implementation to understand better how to handle SSL certificates in Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;For demonstration purposes, we will be writing an automation script for the below scenario for all browsers and seeing the results on cloud execution.&lt;/p&gt;

&lt;p&gt;For this blog on how to handle SSL certificates in Selenium WebDriver, we are using the LambdaTest Cloud Grid. LambdaTest is a highly scalable, reliable, and easy-to-use digital experience testing platform that provides support for &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automation testing&lt;/a&gt; on over 3000+ browsers and OS to support execution on &lt;a href="https://www.lambdatest.com/selenium-grid-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;online Selenium Grid&lt;/a&gt;. This would be very useful for us as we want to execute the code on several browsers to handle SSL certificates in Selenium.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/SqQ8SugRDos"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Perform browser &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automation testing&lt;/a&gt; on the most powerful cloud infrastructure. Leverage LambdaTest automation testing for faster, reliable and scalable experience on cloud.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Scenario&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Consider the following two websites:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Having valid SSL certificates: &lt;a href="https://www.lambdatest.com/selenium-grid-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Having an expired SSL certificate: &lt;a href="https://expired.badssl.com/" rel="noopener noreferrer"&gt;Expired cert&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Set accepting Insecure SSL certificates to True and navigate to both individually.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set accepting Insecure SSL certificates to False and navigate to both one by one.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;

&lt;p&gt;For this blog, we will use a &lt;a href="https://www.lambdatest.com/blog/getting-started-with-maven-for-selenium-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Maven project using Java&lt;/a&gt; in &lt;a href="https://www.lambdatest.com/blog/create-testng-project-in-eclipse-run-selenium-test-script/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Eclipse IDE&lt;/a&gt;. If you are not a fan of Eclipse, use any IDE of your choice and perform the same steps. It will use Selenium WebDriver and TestNG dependencies to automate WebDriver flows and &lt;a href="https://www.lambdatest.com/learning-hub/test-execution?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;test execution&lt;/a&gt;. It is highly recommended to use the latest stable versions for better results.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/QFHMzWqDPiA"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Launch Eclipse or the IDE of your choice.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new Maven project and name it as &lt;em&gt;HandlingSslCertificates&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Inside the src folder, add a test package, and inside it, add three Java files and name them as&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;BaseClass.java:&lt;/em&gt; this will hold the base setup code to configure the browser properties, initiate driver instances.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Test_Accept_InsecureSslCerts.java:&lt;/em&gt; this will have &lt;a href="https://www.lambdatest.com/learning-hub/test-case?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;test cases&lt;/a&gt; for each browser where we accept the insecure SSL certificates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*Test_Reject_InsecureSslCerts.java: *this will have test cases for each browser where we do not accept insecure SSL certificates.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Update the &lt;em&gt;pom.xml&lt;/em&gt; file to have the latest dependencies for T&lt;a href="https://www.lambdatest.com/blog/testng-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;estNG in Selenium&lt;/a&gt;. It would be like below:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;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 https://maven.apache.org/xsd/maven-4.0.0.xsd"&amp;gt;
     &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
     &amp;lt;groupId&amp;gt;HandlingSslCertificates&amp;lt;/groupId&amp;gt;
     &amp;lt;artifactId&amp;gt;HandlingSslCertificates&amp;lt;/artifactId&amp;gt;
     &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;
     &amp;lt;build&amp;gt;
       &amp;lt;sourceDirectory&amp;gt;src&amp;lt;/sourceDirectory&amp;gt;
       &amp;lt;plugins&amp;gt;
         &amp;lt;plugin&amp;gt;
           &amp;lt;artifactId&amp;gt;maven-compiler-plugin&amp;lt;/artifactId&amp;gt;
           &amp;lt;version&amp;gt;3.8.1&amp;lt;/version&amp;gt;
           &amp;lt;configuration&amp;gt;
             &amp;lt;release&amp;gt;16&amp;lt;/release&amp;gt;
           &amp;lt;/configuration&amp;gt;
         &amp;lt;/plugin&amp;gt;
       &amp;lt;/plugins&amp;gt;
     &amp;lt;/build&amp;gt;
     &amp;lt;dependencies&amp;gt;
       &amp;lt;dependency&amp;gt;
           &amp;lt;groupId&amp;gt;org.seleniumhq.selenium&amp;lt;/groupId&amp;gt;
           &amp;lt;artifactId&amp;gt;selenium-java&amp;lt;/artifactId&amp;gt;
           &amp;lt;version&amp;gt;4.1.4&amp;lt;/version&amp;gt;
       &amp;lt;/dependency&amp;gt;
       &amp;lt;dependency&amp;gt;
           &amp;lt;groupId&amp;gt;org.testng&amp;lt;/groupId&amp;gt;
           &amp;lt;artifactId&amp;gt;testng&amp;lt;/artifactId&amp;gt;
           &amp;lt;version&amp;gt;7.7.1&amp;lt;/version&amp;gt;
           &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
       &amp;lt;/dependency&amp;gt;
     &amp;lt;/dependencies&amp;gt;
    &amp;lt;/project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Next, move to &lt;em&gt;BaseClass.java&lt;/em&gt; and add the following code to create a WebDriver instance, depending on the web browser we want to use.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; package test;


    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.HashMap;


    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.edge.EdgeOptions;
    import org.openqa.selenium.firefox.FirefoxOptions;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.openqa.selenium.safari.SafariOptions;


    public class BaseClass {


       public RemoteWebDriver driver = null;
       String username = System.getenv("LT_USERNAME") == null ? "&amp;lt;lambdatest_username&amp;gt;" : System.getenv("LT_USERNAME");
       String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "&amp;lt;lambdatest_accesskey&amp;gt;": System.getenv("LT_ACCESS_KEY");


       public void setup(String browserName, boolean acceptInsecureSSL) {


           HashMap&amp;lt;String, Object&amp;gt; ltOptions = new HashMap&amp;lt;String, Object&amp;gt;();


           try {


               switch (browserName) {
               case "chrome":
                   ChromeOptions chromeOptions = new ChromeOptions();
                   chromeOptions.setPlatformName("Windows 10");
                   chromeOptions.setBrowserVersion("110.0");
                   ltOptions.put("build", "SSL certificates Using Selenium WebDriver");
                   ltOptions.put("name", "Handling on Chrome");
                   chromeOptions.setAcceptInsecureCerts(acceptInsecureSSL);
                   chromeOptions.setCapability("LT:Options", ltOptions);
                   driver = new RemoteWebDriver(
                           new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), chromeOptions);
                   break;
               case "firefox":
                   FirefoxOptions firefoxOptions = new FirefoxOptions();
                   firefoxOptions.setPlatformName("Windows 10");
                   firefoxOptions.setBrowserVersion("109.0");
                   ltOptions.put("build", "SSL certificates Using Selenium WebDriver");
                   ltOptions.put("name", "Handling on Firefox");
                   firefoxOptions.setAcceptInsecureCerts(acceptInsecureSSL);
                   firefoxOptions.setCapability("LT:Options", ltOptions);
                   driver = new RemoteWebDriver(
                           new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"),
                           firefoxOptions);
                   break;
               case "edge":
                   EdgeOptions edgeOptions = new EdgeOptions();
                   edgeOptions.setPlatformName("Windows 10");
                   edgeOptions.setBrowserVersion("108.0");
                   ltOptions.put("build", "SSL certificates Using Selenium WebDriver");
                   ltOptions.put("name", "Handling on Edge");
                   edgeOptions.setAcceptInsecureCerts(acceptInsecureSSL);
                   edgeOptions.setCapability("LT:Options", ltOptions);
                   driver = new RemoteWebDriver(
                           new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), edgeOptions);
                   break;
               case "safari":
                   SafariOptions safariOptions = new SafariOptions();
                   safariOptions.setPlatformName("MacOS Ventura");
                   safariOptions.setBrowserVersion("16.0");
                   ltOptions.put("build", "SSL certificates Using Selenium WebDriver");
                   ltOptions.put("name", "Handling on Safari");
                   safariOptions.setAcceptInsecureCerts(acceptInsecureSSL);
                   safariOptions.setCapability("LT:Options", ltOptions);
                   driver = new RemoteWebDriver(
                           new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), safariOptions);
                   break;
               }


           } catch (MalformedURLException e) {
               System.out.println("Invalid grid URL");
           }
       }


    public void navigateToUrls() {
           System.out.println("Navigating to Lambdatest URL");
           driver.get("https://www.lambdatest.com/");
           System.out.println("The page title is : " +driver.getTitle());

           System.out.println("Navigating to BadSSL URL");
           driver.get("https://expired.badssl.com/");
           System.out.println("The page title is : " +driver.getTitle());

           driver.quit();
       }
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;In the two test files, add the below code to run the following test cases:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Accepting the insecure SSL certificates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Not accepting insecure SSL certificates.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;

    import org.testng.annotations.Test;

    public class Test_Accept_InsecureSslCerts extends BaseClass{

       @Test
       public void testSslCertificatesHandlingChrome()
       {
           setup("chrome", true);
           navigateToUrls();
       }

       @Test
       public void testSslCertificatesHandlingFirefox()
       {
           setup("firefox", true);
           navigateToUrls();
       }

       @Test
       public void testSslCertificatesHandlingEdge()
       {
           setup("edge", true);
           navigateToUrls();
       }

       @Test
       public void testSslCertificatesHandlingSafari()
       {
           setup("safari", true);
           navigateToUrls();
       }
    }

    package test;

    import org.testng.annotations.Test;

    public class Test_Reject_InsecureSslCerts extends BaseClass{

       @Test
       public void testSslCertificatesHandlingChrome()
       {
           setup("chrome", false);
           navigateToUrls();
       }

       @Test
       public void testSslCertificatesHandlingFirefox()
       {
           setup("firefox", false);
           navigateToUrls();
       }

       @Test
       public void testSslCertificatesHandlingEdge()
       {
           setup("edge", false);
           navigateToUrls();
       }

       @Test
       public void testSslCertificatesHandlingSafari()
       {
           setup("safari", false);
           navigateToUrls();
       }
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Walkthrough: *BaseClass.java&lt;/strong&gt;*&lt;/p&gt;

&lt;p&gt;This Java file is the base test class for creating the web driver instance as per the mentioned browser and to have a common function to navigate to both the URLs as per the defined test scenario above.&lt;/p&gt;

&lt;p&gt;Both the functions in this class will be called in each test case on both classes.&lt;/p&gt;

&lt;p&gt;Let’s understand both these functions in detail now.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; The first step is to create an instance of &lt;em&gt;RemoteWebDriver&lt;/em&gt;. We are using a &lt;a href="https://www.lambdatest.com/blog/selenium-remotewebdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Remote WebDriver&lt;/a&gt; as this helps to support execution on the cloud Selenium Grid. This provides increased scalability and the required speed for&lt;a href="https://www.lambdatest.com/learning-hub/automated-test-execution?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt; automated test execution&lt;/a&gt; and, at the same time, also allows users to perform &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;cross browser testing&lt;/a&gt; across several OS and browser combinations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AOPbXXx1AujHtxA8U.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AOPbXXx1AujHtxA8U.png" width="800" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; As mentioned in the previous step, since we are using the LambdaTest platform, we need to provide the username and access key for our user to connect with the cloud hub. You can find these details under &lt;a href="https://accounts.lambdatest.com/detail/profile?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Profile Section&lt;/a&gt; after your account is created.&lt;/p&gt;

&lt;p&gt;Once you have your username and access key, these can be configured as environment variables on your system and fetched from there to use in the automation script.&lt;/p&gt;

&lt;p&gt;You can configure these environment variables using export or set commands based on the operating system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For macOS and Linux:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export LT_USERNAME=LT_USERNAME&lt;br&gt;
export LT_ACCESS_KEY=LT_ACCESS_KEY&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For Windows:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;set LT_USERNAME=LT_USERNAME&lt;br&gt;
set LT_ACCESS_KEY=LT_ACCESS_KEY&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AbpEYayRoX2P9M7sGVyyNIw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AbpEYayRoX2P9M7sGVyyNIw.png" width="800" height="146"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Create a method and name it &lt;em&gt;setup()&lt;/em&gt;. This method will take two parameters, namely &lt;em&gt;browserName&lt;/em&gt;, and &lt;em&gt;acceptInsecureSSL&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2444%2F0%2ARbMYfuODCXxLzaoG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2444%2F0%2ARbMYfuODCXxLzaoG.png" width="800" height="214"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;browserName&lt;/em&gt; — helps to identify which browser we want to execute our code and create a driver instance with the capabilities of that particular browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;acceptInsecureSSL&lt;/em&gt; — a boolean parameter used to set the browser capabilities to identify if we need to accept or reject websites with untrusted SSL certificates. &lt;em&gt;True&lt;/em&gt; means allowing all websites irrespective of SSL certificate status, while False means allowing only those websites with a valid SSL certificate.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; Create a HashMap variable and name it as &lt;em&gt;ltOptions&lt;/em&gt;. This will set various properties for the LambdaTest cloud grid execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2412%2F0%2A-WPAt6Fxxszc1xOR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2412%2F0%2A-WPAt6Fxxszc1xOR.png" width="800" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; This is the most important part of the demonstration, as here we add a switch case to decide which browser capabilities to set, including accepting insecure certificates and creating an instance of Remote WebDriver, depending on that.&lt;/p&gt;

&lt;p&gt;This case is specific to the Chrome browser. Similar cases will be created for each browser in the next steps.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2008%2F0%2AnEvI5ypfFJClm1_B.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2008%2F0%2AnEvI5ypfFJClm1_B.png" width="800" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We start by creating an object in the &lt;em&gt;ChromeOptions&lt;/em&gt; class. This class is used to manipulate various properties of the Chrome driver, which we will use for execution.&lt;/p&gt;

&lt;p&gt;Here you can notice the &lt;em&gt;setAcceptInsecureCerts()&lt;/em&gt; property. This is the property with which we are most concerned as part of this demonstration for this blog on how to handle SSL certificates in Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2180%2F0%2Ax5MpCHrqKyWXRuUa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2180%2F0%2Ax5MpCHrqKyWXRuUa.png" width="800" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This takes a boolean argument that tells the browser driver whether it should allow websites with untrusted SSL certificates to load. &lt;em&gt;True&lt;/em&gt; means allowing all websites irrespective of SSL certificate status, while &lt;em&gt;False&lt;/em&gt; means allowing only those websites with a valid SSL certificate. This property is common to all browsers and has similar behavior.&lt;/p&gt;

&lt;p&gt;We will also use the same object to set some of the properties to display cloud execution results better on the &lt;a href="https://accounts.lambdatest.com/dashboard?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Dashboard&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2528%2F0%2AR76k9GjI9L-1Bvtd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2528%2F0%2AR76k9GjI9L-1Bvtd.png" width="800" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the end of each browser case, we write the code to connect with &lt;em&gt;RemoteWebDriver&lt;/em&gt; on the LambdaTest platform using the properties defined for the browser, like &lt;em&gt;chromeOptions&lt;/em&gt; in this case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AQ7j_b10BuN3roTVK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AQ7j_b10BuN3roTVK.png" width="800" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An added advantage of using the LambdaTest platform is that you can directly select the browser and version details, and your capabilities are automatically generated using &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Capabilities Generator&lt;/a&gt;, which is ready to be added to the code directly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Af90o0-GSoMMahjoY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Af90o0-GSoMMahjoY.png" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Like the Chrome browser, Firefox, Edge, and Safari cases would look like the below using &lt;em&gt;FirefoxOptions&lt;/em&gt;, &lt;em&gt;EdgeOptions&lt;/em&gt;, and &lt;em&gt;SafariOptions&lt;/em&gt;, respectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6.&lt;/strong&gt; The last step is to add a function &lt;em&gt;navigateToUrls()&lt;/em&gt;, a common function used to navigate to URLs for both websites and fetch the page title. This same function is used to close the browser and quit the WebDriver instance at the end of each test case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2388%2F0%2ARxwl54EOOwTWLCLm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2388%2F0%2ARxwl54EOOwTWLCLm.png" width="800" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough: **Test_Accept_InsecureSslCerts.java&lt;/strong&gt;*&lt;/p&gt;

&lt;p&gt;In this Java test file, we have written four different test cases, one for each browser, for easy understanding and differentiation.&lt;/p&gt;

&lt;p&gt;In each test function, we have two common steps:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; Call the &lt;em&gt;setup()&lt;/em&gt; function of &lt;em&gt;BaseClass.java&lt;/em&gt; and pass the browser name on which we want to execute. Along with this, pass the boolean parameter &lt;em&gt;acceptInsecureSSL&lt;/em&gt; as true.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AQQcCDgAq7DsSdVYz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AQQcCDgAq7DsSdVYz.png" width="702" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Call the &lt;em&gt;navigateToUrls()&lt;/em&gt; function to navigate to both URLs one by one. In this case, both websites should load, which is #2 of our established test scenarios.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A7v9V5YBiq9foFQHy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A7v9V5YBiq9foFQHy.png" width="528" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough: **Test_Reject_InsecureSslCerts.java&lt;/strong&gt;*&lt;/p&gt;

&lt;p&gt;Similar to the previous one, this Java test file has four different test cases, one for each browser.&lt;/p&gt;

&lt;p&gt;The only difference is the boolean parameter &lt;em&gt;acceptInsecureSSL&lt;/em&gt; is false, which means #3 of our test scenarios.&lt;/p&gt;

&lt;p&gt;On running the cases, the LambdaTest website would get loaded but the other one would fail to load and throw an error on each browser as we are not accepting websites with insecure SSL certificates in this case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A9ulLdt2P89yharCj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A9ulLdt2P89yharCj.png" width="716" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Run your &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium Automation&lt;/a&gt; Testing scripts on the LambdaTest cloud grid. Test on 3000+ desktop &amp;amp; mobile environments. Try it for free.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test Execution
&lt;/h2&gt;

&lt;p&gt;Having developed a basic understanding and writing the automation script to handle SSL certificates in Selenium WebDriver, it is now time to execute the code and see the results of execution locally and on the LambdaTest Dashboard.&lt;/p&gt;

&lt;p&gt;The test case would be executed using the &lt;a href="https://www.lambdatest.com/learning-hub/testng?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr28_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;TestNG framework&lt;/a&gt;. To execute, follow the below-mentioned steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Right-click the test case name in Eclipse IDE.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to the Run Test and select TestNG Test to begin the execution.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;First, we will execute the cases one by one for all browsers from &lt;em&gt;Test_Accept_InsecureSslCerts&lt;/em&gt; and observe that all cases pass and both websites are loaded successfully.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AQJo4bQLPBtRya8Ji.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AQJo4bQLPBtRya8Ji.png" width="590" height="141"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can observe from the above screenshot of local execution that we have titles of both websites, which means both are loaded successfully. Executing on all four browsers would give similar results in this case.&lt;/p&gt;

&lt;p&gt;Before moving forward, let us observe the results of this execution on the LambdaTest Dashboard as well.&lt;/p&gt;

&lt;p&gt;For this, log in to your account, move to &lt;strong&gt;Automation -&amp;gt; Build&lt;/strong&gt; section, and select your build.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A5nAqfNhVkEFm-Tme.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A5nAqfNhVkEFm-Tme.png" width="800" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An added advantage of using the LambdaTest grid here is that we can select each test case and verify the configuration passed to the test case is correct, i.e., &lt;em&gt;acceptInsecureCerts&lt;/em&gt; = true on all browsers for this execution, from the &lt;strong&gt;Input Config&lt;/strong&gt; section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chrome Browser&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Ao0v6z19Q6z68J2iG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Ao0v6z19Q6z68J2iG.png" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Firefox Browser&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AoPNX3_4DlN8gc0tq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AoPNX3_4DlN8gc0tq.png" width="800" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safari Browser&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A3Bwt8a2HDu78G5oQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A3Bwt8a2HDu78G5oQ.png" width="800" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edge Browser&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AxMkUmk3JiBKoM5rj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AxMkUmk3JiBKoM5rj.png" width="800" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, we try to execute the test cases from &lt;em&gt;Test_Reject_InsecureSslCerts&lt;/em&gt; on which LambdaTest should get loaded, and BadSSL should raise a Privacy error due to an insecure SSL certificate since on this, we are not accepting insecure SSL certificates.&lt;/p&gt;

&lt;p&gt;On local execution, since the website would not get loaded, the title would not be the same and, instead, signify the same on each browser.&lt;/p&gt;

&lt;p&gt;For Safari, it would be &lt;strong&gt;&lt;em&gt;This Connection Is Not Private for Safari&lt;/em&gt;&lt;/strong&gt;, for Chrome and Edge &lt;strong&gt;&lt;em&gt;Privacy error&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;WebDriverException&lt;/em&gt;&lt;/strong&gt; for Firefox since we are using Remote WebDriver.&lt;/p&gt;

&lt;p&gt;You can learn more about them through this blog on &lt;a href="https://www.lambdatest.com/blog/49-common-selenium-exceptions-automation-testing/" rel="noopener noreferrer"&gt;common Selenium exceptions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AaGF4Z1DN9gCpd_V_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AaGF4Z1DN9gCpd_V_.png" width="583" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AVACR1W9AhBurN5QU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AVACR1W9AhBurN5QU.png" width="588" height="139"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, we can check the results on the LambdaTest Dashboard. You would observe that for these executions on each browser, unlike previous cases, the Input config for all cases would have &lt;em&gt;acceptInsecureCerts&lt;/em&gt; as false, like below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chrome Browser&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A-tovG923hIpulrHM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A-tovG923hIpulrHM.png" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Firefox Browser&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AFFwqqMxMduvZLPjk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AFFwqqMxMduvZLPjk.png" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Safari Browser&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AAoOAg3BLB72LouvW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AAoOAg3BLB72LouvW.png" width="800" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edge Browser&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A9gE_ARkZ5O0qkDc4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A9gE_ARkZ5O0qkDc4.png" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With this, we have reached the end of this blog on how to handle SSL certificates in Selenium WebDriver. In this blog, we learned about SSL certificates, how it works, and what an insecure certificate means. On demonstration, we also understood that Selenium provides us with functions to both accept and reject such insecure SSL certificates in our automation on each browser. So, now it is time for you to explore SSL more and implement it in your code.&lt;/p&gt;

&lt;p&gt;Happy testing.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>selenium</category>
      <category>webdriver</category>
      <category>certification</category>
    </item>
    <item>
      <title>A Complete Guide To Exception Tests In TestNG [With Examples]</title>
      <dc:creator>Vipulgupta9090</dc:creator>
      <pubDate>Mon, 24 Apr 2023 08:36:10 +0000</pubDate>
      <link>https://dev.to/testmuai/a-complete-guide-to-exception-tests-in-testng-with-examples-3mmo</link>
      <guid>https://dev.to/testmuai/a-complete-guide-to-exception-tests-in-testng-with-examples-3mmo</guid>
      <description>&lt;p&gt;While automating critical applications like banking apps, it’s important to implement error handling and validation checks to ensure that the application is secure and reliable for the bank’s customers. Additionally, clear error messages and notifications should be provided to assist users in troubleshooting the issue.&lt;/p&gt;

&lt;p&gt;Unforeseen circumstances or anomalies can occur during software development, and it is crucial to handle them efficiently to maintain program stability. To achieve this, &lt;a href="https://www.lambdatest.com/blog/49-common-selenium-exceptions-automation-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;exception handling&lt;/a&gt; can be implemented in the code.&lt;/p&gt;

&lt;p&gt;Since these are an inherent part of any software, an exception test is a critical and mandatory requirement. By writing exception tests, you can ensure that your application behaves as expected without impacting customers when faced with such exceptional scenarios. This is also critical for ensuring business continuity because it reduces risk and costs and, in some cases, helps to meet compliance and regulatory testing requirements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/learning-hub/testng?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;TestNG&lt;/a&gt; is the framework that is most frequently used to run exception tests.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Y6M1bz8jtT8"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;This blog post will explore some of the common exceptions encountered during &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automation testing&lt;/a&gt;, demonstrate how to handle them by writing exception tests in TestNG, and verify that your code performs as intended.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do you use a Mac and want to run the test in Internet Explorer? This article explores how to test &lt;a href="https://www.lambdatest.com/blog/test-internet-explorer-for-mac/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Internet Explorer for Mac&lt;/a&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Exceptions?
&lt;/h2&gt;

&lt;p&gt;Any event that interrupts the normal flow of a program’s execution is called an Exception. These are typically caused by errors or unusual conditions, such as invalid input, a file that cannot be found, or divide-by-zero or element locator/interaction-related issues while working with &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When an exception occurs, the program will typically halt execution or simply crash or produce incorrect results.&lt;/p&gt;

&lt;p&gt;Exception handling allows programs to recover from such errors, provide more informative error messages to users, and ensure that critical resources are correctly released even when an error occurs.&lt;/p&gt;

&lt;p&gt;Let us take this example flow to understand how exceptions and exception handling work.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Consider you are working to automate a web page. For this, you would need to use the object of a &lt;a href="https://www.lambdatest.com/learning-hub/webdriver?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;WebDriver&lt;/a&gt; to interact with the page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As a practice, the following steps would be to perform desired actions on the webpage and then close the browser and terminate the driver instance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But imagine a scenario where for some reason, the browser gets closed between execution, or you do so manually.&lt;br&gt;
Then, the code tries to continue and interact with some web element that is supposed to be present on the webpage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Such a scenario would lead to an exception as the browser is already closed. The output, in this case, would show you a &lt;em&gt;SessionNotFound&lt;/em&gt; or &lt;em&gt;WebDriver&lt;/em&gt; exception as the browser session is terminated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To handle such exceptions, we can use exception handling and maybe relaunch the browser in such a scenario to continue execution and get desired results.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Commonly encountered Exceptions
&lt;/h2&gt;

&lt;p&gt;Here are a few examples of exceptions that we may encounter when writing automation scripts with Selenium for &lt;a href="https://www.lambdatest.com/learning-hub/web-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;web automation&lt;/a&gt; or when writing code in general.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;NullPointerException&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When trying to refer to some object or variable that has no reference or a null reference.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ArrayIndexOutOfBoundsException&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Occurs while working with lists or arrays when a program tries to access an element that does not exist and is beyond array length.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;IllegalArgumentException&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When an illegal or corrupt argument is passed to a function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;IOException&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Occurs due to issues on input/output flows, like accessing a file or opening/closing it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;NoSuchElementException&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Occurs when the element the code is trying to access is not present in the DOM.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ElementNotVisibleException&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Occurs when the element is present on &lt;a href="https://www.lambdatest.com/blog/document-object-model/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;DOM&lt;/a&gt; but is not actually visible on the webpage for interaction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ElementNotInteractableException&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Occurs when we try to interact with a disabled or inactive web element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;StaleElementReferenceException&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Occurs when we try to access some element with an older reference that is no longer valid due to some actions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;TimeoutException&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Occurs when the required action takes too long to be completed, like loading a page, waiting for some web element, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;NoSuchWindowException&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Occurs when working with multiple windows when the required window is not available.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;InvalidSelectorException&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Occurs when the web element selector is not valid. This can be due to syntax errors or invalid selector values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;WebDriverException&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Generic exception when working with WebDriver. This can occur due to WebDriver or browser-related problems in the automation script.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exception tests in TestNG
&lt;/h2&gt;

&lt;p&gt;An exception test is a &lt;a href="https://www.lambdatest.com/learning-hub/test-case?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test case&lt;/a&gt; that confirms a particular exception is raised whenever a certain code block is executed. Exception testing is an important part of &lt;a href="https://www.lambdatest.com/learning-hub/software-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;software testing&lt;/a&gt;, as it helps to validate that the application behaves as expected when faced with exceptional scenarios. It also helps to improve code and product quality by identifying such issues. This, in turn, also increases &lt;a href="https://www.lambdatest.com/learning-hub/test-coverage?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test coverage&lt;/a&gt; and provides better results.&lt;/p&gt;

&lt;p&gt;To write such a test, the TestNG testing framework is used.&lt;/p&gt;

&lt;p&gt;TestNG is an open-source &lt;a href="https://www.lambdatest.com/blog/automation-testing-frameworks/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automation testing framework&lt;/a&gt; for Java built on the capabilities of JUnit and NUnit. It helps to write test cases in a well-structured, easy-to-maintain way using &lt;a href="https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;TestNG annotations&lt;/a&gt;, attributes, and other features.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;This tutorial dive deep into &lt;a href="https://www.lambdatest.com/learning-hub/web-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;web testing&lt;/a&gt; to help you understand its life cycle, elements, angles, the role of automation, and more.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  expectedExceptions
&lt;/h2&gt;

&lt;p&gt;Using the &lt;em&gt;expectedExceptions&lt;/em&gt; attribute of the &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;/em&gt; annotation, we can write exception tests in TestNG. This attribute accepts a single exception class or a list of exception classes that are expected to be thrown from the test case. The test will succeed if any of the predefined exceptions are raised. The test will fail if a different exception or no exception is thrown. Using the &lt;em&gt;expectedExceptionsMessageRegExp&lt;/em&gt; attribute, we can also confirm the exception message.&lt;/p&gt;

&lt;p&gt;For example, a TestNG exception test for the scenario mentioned above would look like this below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Test(expectedExceptions = ElementNotInteractableException.class)
    public void testButtonNotInteractable() {
     //code to fill transfer details and
     //try to click on submit button
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It verifies that an &lt;em&gt;ElementNotInteractableException&lt;/em&gt; is thrown when a user tries to click on the transfer button. If no exception or any other exception is thrown, it will fail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of using Exception tests in TestNG
&lt;/h2&gt;

&lt;p&gt;There are several advantages of using exception tests in TestNG, a few of them are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Improved quality&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Exception tests help verify that the code behaves as expected when faced with unexpected scenarios and can handle them gracefully. This helps to improve application quality and stability.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Early issue detection&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With exception testing, ROI improves significantly as issues can be detected early on. This saves time, money, and effort to fix any such issue if they reach production.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Increased confidence&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Having exception tests in place also gives testers more confidence while testing any new changes as it ensures nothing on existing functionality breaks and all unwanted situations are catered to.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Easy to maintain and write cases&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TestNG makes writing the cases very easy and well-structured using annotations. We can simply write one by providing the &lt;em&gt;expectedExceptions&lt;/em&gt; attribute of the &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;/em&gt; annotation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Better insight into the code&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Exception tests provide an understanding of how the application would behave in exceptional scenarios. This can be used to verify assumptions around expected behavior and thus improve overall understanding.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to write Exception tests in TestNG?
&lt;/h2&gt;

&lt;p&gt;TestNG helps to validate the exceptions thrown by the test using the &lt;em&gt;expectedExceptions&lt;/em&gt; attribute within &lt;a href="http://twitter.com/Test" rel="noopener noreferrer"&gt;@Test&lt;/a&gt; annotation.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;expectedExceptions&lt;/em&gt; can take a single exception or a list of exceptions to validate from. If the exception raised from the test case is one of those mentioned above, then the test case passes; else, it fails.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verify exception class
&lt;/h2&gt;

&lt;p&gt;There are two ways to define a single exception to be validated. Both these are just different ways to represent the same thing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pass the exception class name directly.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   @Test(expectedExceptions = ElementNotInteractableException.class)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Pass the exception class name within curly brackets.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  @Test(expectedExceptions = {ElementNotInteractableException.class})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There might be scenarios where the same code could lead to different exceptions under different circumstances. In such a case, we can also pass the list of all such expected exceptions by enclosing them within curly brackets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   @Test(expectedExceptions = {ElementNotInteractableException.class, NoSuchElementException.class})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this, the test case passes if the raised exception is one amongst the list and fails if it is not present in the list or no exception is thrown at all from the automation code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Upload your app for testing within seconds using the LambdaTest cloud and perform mobile app testing right away. Find bugs early on, improve performance, quality, user experience and make the most of &lt;a href="https://www.lambdatest.com/mobile-app-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;mobile appl testing&lt;/a&gt; on LambdaTest.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Verify exception message
&lt;/h2&gt;

&lt;p&gt;TestNG with &lt;a href="http://twitter.com/Test" rel="noopener noreferrer"&gt;@Test&lt;/a&gt; annotation also provides support to assert the exception message using the expectedExceptionsMessageRegExp attribute. We can have this message in the code from where the exception is thrown.&lt;/p&gt;

&lt;p&gt;Asserting the exception message enables us to confirm that the exception is raised for the correct reasons, which we expected.&lt;/p&gt;

&lt;p&gt;To understand this better, consider insufficient funds in the source account part of the bank application example we took in the introduction. In this case, an InsufficientFundsException can be raised for two reasons.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The source has no funds, or&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The source has insufficient funds to complete the transfer.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In both cases, the exception raised would be the same, and the exception test would pass. Asserting exception messages enables us to be more specific and helps in verifying the corner cases that the message raised is as expected or not. Thus confirming what exactly leads to failure in this case, the message could specify the same — no funds or insufficient funds, although the exception class remains the same.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; @Test(expectedExceptions = { InsufficientFundsException.class }, expectedExceptionsMessageRegExp = "Source has fewer than requested funds")
    public void testFewerFunds() {}


    @Test(expectedExceptions = { InsufficientFundsException.class }, expectedExceptionsMessageRegExp = "Source has zero funds")
    public void testZeroFunds() {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also verify the exception messages with the help of Regular Expressions using .*., depending upon the position of the regular expression we can use it to make pattern matching such as starts-with, contains, and ends-with while verifying the exception message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; @Test(expectedExceptions = { InsufficientFundsException.class }, expectedExceptionsMessageRegExp = "Source has .*")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means the exception message should have the word Source followed by certain other words.&lt;/p&gt;

&lt;p&gt;Let us now move forward and see how the implementation of exception tests in TestNG works by taking examples.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Run your &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium Automation Testing&lt;/a&gt; scripts on the LambdaTest cloud grid. Test on 3000+ desktop &amp;amp; mobile environments. Try it for free!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Exception tests in TestNG
&lt;/h2&gt;

&lt;p&gt;We will be creating a Maven project. It will use Selenium WebDriver and TestNG dependencies to automate web page flows and test case execution for exception tests, respectively. It is highly recommended to use the latest versions of the dependencies for the best results.&lt;/p&gt;

&lt;p&gt;After creating the project and adding said dependencies, the pom.xml should look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;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 https://maven.apache.org/xsd/maven-4.0.0.xsd"&amp;gt;
           &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
           &amp;lt;groupId&amp;gt;ExceptionTest&amp;lt;/groupId&amp;gt;
           &amp;lt;artifactId&amp;gt;ExceptionTest&amp;lt;/artifactId&amp;gt;
           &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;
           &amp;lt;build&amp;gt;
                   &amp;lt;sourceDirectory&amp;gt;src&amp;lt;/sourceDirectory&amp;gt;
                   &amp;lt;plugins&amp;gt;
                           &amp;lt;plugin&amp;gt;
                                   &amp;lt;artifactId&amp;gt;maven-compiler-plugin&amp;lt;/artifactId&amp;gt;
                                   &amp;lt;version&amp;gt;3.8.1&amp;lt;/version&amp;gt;
                                   &amp;lt;configuration&amp;gt;
                                           &amp;lt;release&amp;gt;16&amp;lt;/release&amp;gt;
                                   &amp;lt;/configuration&amp;gt;
                           &amp;lt;/plugin&amp;gt;
                   &amp;lt;/plugins&amp;gt;
           &amp;lt;/build&amp;gt;
           &amp;lt;dependencies&amp;gt;
                   &amp;lt;dependency&amp;gt;
                           &amp;lt;groupId&amp;gt;org.seleniumhq.selenium&amp;lt;/groupId&amp;gt;
                           &amp;lt;artifactId&amp;gt;selenium-java&amp;lt;/artifactId&amp;gt;
                           &amp;lt;version&amp;gt;4.8.0&amp;lt;/version&amp;gt;
                   &amp;lt;/dependency&amp;gt;
                   &amp;lt;dependency&amp;gt;
                           &amp;lt;groupId&amp;gt;org.testng&amp;lt;/groupId&amp;gt;
                           &amp;lt;artifactId&amp;gt;testng&amp;lt;/artifactId&amp;gt;
                           &amp;lt;version&amp;gt;7.7.1&amp;lt;/version&amp;gt;
                           &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
                   &amp;lt;/dependency&amp;gt;
           &amp;lt;/dependencies&amp;gt;
    &amp;lt;/project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, add a Java test file and name it &lt;em&gt;ExceptionTest.java.&lt;/em&gt; We will add different test cases to this to understand exception tests with different examples.&lt;/p&gt;

&lt;p&gt;In this blog on exception tests in TestNG, we will mostly take examples of exceptions faced during &lt;a href="https://www.lambdatest.com/selenium-automation-testing-with-testng?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium automation with TestNG&lt;/a&gt;. Here, we are using the LambdaTest Cloud Grid to run our tests.&lt;/p&gt;

&lt;p&gt;LambdaTest is a digital experience testing cloud, a highly scalable, reliable, and easy-to-use platform that provides support for over 3000+ browsers and OS to perform &lt;a href="https://www.lambdatest.com/testng-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;TestNG testing&lt;/a&gt; on &lt;a href="https://www.lambdatest.com/selenium-grid-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium Cloud Grid&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To kick start Selenium automation testing with TestNG, refer to our documentation &lt;a href="https://www.lambdatest.com/support/docs/testng-with-selenium-running-java-automation-scripts-on-lambdatest-selenium-grid/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;TestNG with Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/tzXoNpof8vc"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Automate &lt;a href="https://www.lambdatest.com/cypress-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Cypress testing&lt;/a&gt; and perform browser automation testing with LambdaTest. Our cloud infrastructure has 3000+ desktop &amp;amp; mobile environments. Try for free!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To stay up-to-date on tutorials related to &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium testing&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/cypress-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Cypress testing&lt;/a&gt;, and more, subscribe to the &lt;a href="https://www.youtube.com/c/LambdaTest?sub_confirmation=1?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest YouTube Channel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Using the &lt;a href="https://www.lambdatest.com/blog/selenium-remotewebdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;RemoteWebDriver&lt;/a&gt; configuration to access a webpage on the cloud platform and after adding different test cases to demonstrate exception testing, the final *ExceptionTest.java *would look like the below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; package test;


    import java.net.*;
    import java.util.HashMap;
    import java.util.concurrent.TimeUnit;


    import org.openqa.selenium.*;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.annotations.*;


    public class ExceptionTest {


       public RemoteWebDriver driver = null;


       String username = System.getenv("LT_USERNAME") == null ? "&amp;lt;lambdatest_username&amp;gt;" : System.getenv("LT_USERNAME");
       String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "&amp;lt;lambdatest_accesskey&amp;gt;" : System.getenv("LT_ACCESS_KEY");


       @BeforeTest
       public void setup() {
           try {
               ChromeOptions chromeOptions = new ChromeOptions();
               chromeOptions.setPlatformName("Windows 10");
               chromeOptions.setBrowserVersion("110.0");


               HashMap&amp;lt;String, Object&amp;gt; ltOptions = new HashMap&amp;lt;String, Object&amp;gt;();
               ltOptions.put("build", "Exception Test in TestNG");
               chromeOptions.setCapability("LT:Options", ltOptions);


               driver = new RemoteWebDriver(
                       new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), chromeOptions);
           } catch (MalformedURLException e) {
               e.printStackTrace();
           }


           driver.get("https://www.lambdatest.com/selenium-playground/");
       }


       @AfterTest
       public void tearDown() {
           driver.quit();
       }


       // This case will pass as same exception is thrown
       // due to invalid web element selector
       @Test(expectedExceptions = TimeoutException.class)
       public void testSingleException_passed() {
           driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.MILLISECONDS);
           driver.get("https://ecommerce-playground.lambdatest.io/");
       }


       // This case will fail as web element selector is valid
       // hence no exception is raised
       @Test(expectedExceptions = TimeoutException.class)
       public void testSingleException_failed() {
           driver.get("https://ecommerce-playground.lambdatest.io/");
       }


       // This case will pass as exception thrown
       // is one of the expected list i.e. NoSuchWindowException
       @Test(expectedExceptions = { NoSuchElementException.class, NoSuchWindowException.class })
       public void testMultipleException_NoSuchWindowException_passed() {
           driver.switchTo().window("new_window");
       }


       // This case will pass as exception thrown
       // is one of the expected list i.e. NoSuchElementException
       @Test(expectedExceptions = { NoSuchElementException.class, NoSuchWindowException.class })
       public void testMultipleException_NoSuchElementException_passed() {
           driver.findElement(By.xpath("//*[@class='st_heading_1']"));
       }


       // This case will fail as exception raised is not on the list
       // i.e. TimeoutException
       @Test(expectedExceptions = { NoSuchElementException.class, NoSuchWindowException.class })
       public void testMultipleException_failed_1() {
           driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.MILLISECONDS);
           driver.get("https://ecommerce-playground.lambdatest.io/");
       }


       // This case will fail as none of the expected exception is raised
       @Test(expectedExceptions = { NoSuchElementException.class, NoSuchWindowException.class })
       public void testMultipleException_failed_2() {
           driver.findElement(By.xpath("//*[@class='st_heading']"));
       }


       // This case will pass as expected exception message is same.
       @Test(expectedExceptions = NoSuchElementException.class, expectedExceptionsMessageRegExp = "(?s).*no such element.*")
       public void testExceptionMessage_passed() {
           driver.findElement(By.xpath("//*[@class='st_heading_1']"));
       }


       // This case will fail as expected exception message is not same.
       @Test(expectedExceptions = NoSuchWindowException.class, expectedExceptionsMessageRegExp = "(?s).*no such element.*")
       public void testExceptionMessage_failed() {
           driver.switchTo().window("new_window");
       }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s start to understand the code step by step now.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Walkthrough:
&lt;/h2&gt;

&lt;p&gt;We will first do a walkthrough for the common code used to set up and launch the browser and close it at the end of the &lt;a href="https://www.lambdatest.com/learning-hub/test-execution?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test execution&lt;/a&gt;. This will be followed by different test case examples to understand what we have learned so far in this blog about exception tests in TestNG.&lt;/p&gt;

&lt;p&gt;**Step 1: **Create an instance of RemoteWebDriver. This is required as we will be executing the code on the cloud Selenium Grid.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A-nkaCgG0CnERor1Ev_ES6A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A-nkaCgG0CnERor1Ev_ES6A.png" width="800" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Since we are using the LambdaTest platform, we need to provide the username and access key for our user to connect with the cloud grid. You can find these details under &lt;a href="https://accounts.lambdatest.com/detail/profile?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Profile Section&lt;/a&gt; after your account is created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AR-K9BVjQUKkG7scFrEx6tw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AR-K9BVjQUKkG7scFrEx6tw.png" width="800" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can configure the username and access key as environment variables on your system and fetch them in the automation script.&lt;/p&gt;

&lt;p&gt;To configure these following commands can be used&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For macOS and Linux:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; export LT_USERNAME=LT_USERNAME
    export LT_ACCESS_KEY=LT_ACCESS_KEY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;For Windows:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set LT_USERNAME=LT_USERNAME
    set LT_ACCESS_KEY=LT_ACCESS_KEY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Step 3: **Create a method named *setup()&lt;/em&gt; and annotate it with &lt;em&gt;@BeforeTest&lt;/em&gt; TestNG annotation to be able to execute it before each test case for driver setup and opening the webpage&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AcdvQVXNbFg_f_RfFs_bULw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AcdvQVXNbFg_f_RfFs_bULw.png" width="584" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this function, we start by creating an object of *ChromeOptions *class, which is used to define browser properties like browser version, OS version, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2036%2F1%2An_sbzWm8tjzHF9KkkP61mw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2036%2F1%2An_sbzWm8tjzHF9KkkP61mw.png" width="800" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is followed by steps to define some variables for LambdaTest execution. These variables are used to set values like build, name, or any other browser property, which will help to identify the run on the dashboard or modify behavior. This is done with the help of a &lt;em&gt;HashMap *variable, which is then passed to *chromeOptions&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2472%2F1%2A4Rr7RWcDCVe0-qMeoiYEBg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2472%2F1%2A4Rr7RWcDCVe0-qMeoiYEBg.png" width="800" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An additional benefit of using LambdaTest is that all of these browser properties can be easily retrieved using the LambdaTest &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Automation Capabilities Generator&lt;/a&gt;. You only need to select the required operating system and browser combination and versions, and the rest is done for you by this utility, and you have the code ready to use. This reduces the possibility of error and provides more valid combinations and data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AL0fEgM3313n1He_vE4O6VQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AL0fEgM3313n1He_vE4O6VQ.png" width="800" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, using all these properties, we create an instance of RemoteWebDriver for connecting to the LambdaTest cloud grid to start execution. Your LambdaTest username and the access key will be used in this step.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2A__ouQXQdF0oOOhzUxUOqrQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2A__ouQXQdF0oOOhzUxUOqrQ.png" width="800" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, navigate to the required URL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2356%2F1%2AUXOghchnZsTIqRncMIT_NA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2356%2F1%2AUXOghchnZsTIqRncMIT_NA.png" width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Add a new method as &lt;em&gt;tearDown()&lt;/em&gt; and annotate it with &lt;em&gt;@AfterTest&lt;/em&gt; annotation. This function will be used to close the browser and quit the driver session after each test case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AHA4Db-JJw5nVpeqp8nzt3Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AHA4Db-JJw5nVpeqp8nzt3Q.png" width="700" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This completes the basic code setup. Let’s start to see some working examples now.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;a href="https://www.lambdatest.com/learning-hub/black-box-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Black Box testing&lt;/a&gt;? Don’t worry; we will be covering what is Black box testing, merits, demerits, types &amp;amp; techniques.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Exception test for TimeoutException
&lt;/h2&gt;

&lt;p&gt;Add the first test case and name it &lt;em&gt;testSingleException_passed()&lt;/em&gt;. In this test case, we will try to simulate &lt;em&gt;TimeoutException&lt;/em&gt; by setting the driver timeout to a very low value. As a result, when we try to navigate to a new URL, the driver throws an exception as the page load is not completed. To make the test case pass, the value for the &lt;em&gt;expectedExceptions&lt;/em&gt; attribute is set as &lt;em&gt;TimeoutException&lt;/em&gt;.class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F1%2AE9z5R0qcVwTz2IJPPC_RHA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F1%2AE9z5R0qcVwTz2IJPPC_RHA.png" width="800" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we have the &lt;em&gt;pageLoadTimeout(&lt;/em&gt;) to 10 milliseconds and then try to get a new URL. This is expected to throw a *Timeout *exception as the website would not be loaded in this time frame.&lt;/p&gt;

&lt;p&gt;Now try to execute the test case and see if it fails or passes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A7XtnPdarR3taV1rKHd1FNQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A7XtnPdarR3taV1rKHd1FNQ.png" width="778" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can notice that this case is passed since the expected exception matches the one thrown by the code.&lt;/p&gt;

&lt;p&gt;To understand this better, add another test case, &lt;em&gt;testSingleException_failed()&lt;/em&gt;. This case is exactly similar to the previous one with just one difference: the &lt;em&gt;pageLoadTimeout()&lt;/em&gt; is not updated, unlike earlier, and hence the webpage is loaded successfully in the expected time. Hence, no exception is raised from the test case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2384%2F1%2AXl-uNsoT_8-2P2ByYG6Nbw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2384%2F1%2AXl-uNsoT_8-2P2ByYG6Nbw.png" width="800" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, you can see we are still expecting a &lt;em&gt;TimeoutException&lt;/em&gt;. This should lead to test failure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3132%2F1%2AxlBuqXXrg17ReB69uKhR6g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3132%2F1%2AxlBuqXXrg17ReB69uKhR6g.png" width="800" height="796"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the test execution result above, you can see that the test case failed as expected to get an exception, but none was thrown.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your &lt;a href="https://www.lambdatest.com/puppeteer-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Puppeteer test&lt;/a&gt; scripts online. Deploy a quality build faster by running automated checks across over 3000+ browsers and OS combinations with the LambdaTest cloud. Try for free!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Exception test for NoSuchElementException and NoSuchWindowException
&lt;/h2&gt;

&lt;p&gt;In this section, we take examples where multiple exceptions are passed as a list to the &lt;em&gt;expectedExceptions&lt;/em&gt; attribute. We will take two passed test case scenarios to understand that the same test case with the same list passes when any of the given exceptions are raised from the code.&lt;/p&gt;

&lt;p&gt;Add a test case &lt;em&gt;testMultipleException_NoSuchWindowException_passed()&lt;/em&gt;. This test will showcase how we can pass multiple exceptions as a list and test cases getting passed when the thrown exception is one amongst the list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3164%2F1%2AYkFeq3PyoVJGwKF05fT3rg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3164%2F1%2AYkFeq3PyoVJGwKF05fT3rg.png" width="800" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this test case, you can see we are expecting to get either &lt;em&gt;NoSuchElementException&lt;/em&gt; or &lt;em&gt;NoSuchWindowException&lt;/em&gt; when trying to switch the window.&lt;/p&gt;

&lt;p&gt;Executing this test would result in success as the window name given is invalid and would make the code throw &lt;em&gt;NoSuchWindowException&lt;/em&gt;, which is on the *expectedExceptions *list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A8B8FJNoK5eZo5kXQJ29SJQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A8B8FJNoK5eZo5kXQJ29SJQ.png" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, we can add another test case as &lt;em&gt;testMultipleException_NoSuchElementException_passed()&lt;/em&gt;, keep the &lt;em&gt;expectedExceptions&lt;/em&gt; list the same, and just update the code to raise &lt;em&gt;NoSuchElementException&lt;/em&gt; due to an invalid web element locator and the test case is passed this time as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3164%2F1%2A-i1ZeeurjMDpDiEIBkRWMQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3164%2F1%2A-i1ZeeurjMDpDiEIBkRWMQ.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A49SKiMgcCMs9OjkSjH8nNA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A49SKiMgcCMs9OjkSjH8nNA.png" width="800" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While working with multiple exception tests in TestNG, the test case could fail due to two reasons related to expected exceptions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;No exception is thrown at all from the test case.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The exception raised is not on the expected list.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s take a look at the implementation of these.&lt;/p&gt;

&lt;p&gt;In this test case, the &lt;em&gt;expectedExceptions&lt;/em&gt; list is still the same as the previous one, but the code is updated to throw a *Timeout *exception.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3164%2F1%2AfwpqRAWukkXWBI5SMXNaGw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3164%2F1%2AfwpqRAWukkXWBI5SMXNaGw.png" width="800" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Executing this would lead to failure because the thrown exception is not one of the given lists, and the output would look like the one below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2A7r6YYPJfaHIRJCe2ljDVmA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2A7r6YYPJfaHIRJCe2ljDVmA.png" width="800" height="651"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another reason for failure in this implementation could be no exception thrown at all. For this, we keep the list the same and update the code to fetch a valid web element from the webpage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3164%2F1%2AW8RNNvM_OnCVcs5urDeqIg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3164%2F1%2AW8RNNvM_OnCVcs5urDeqIg.png" width="800" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Executing this would give results like the one below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2528%2F1%2AUiMqEMc_CfGyw6t9V4mksQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2528%2F1%2AUiMqEMc_CfGyw6t9V4mksQ.png" width="800" height="536"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see from the highlighted error message that the test case failed as it was expecting an exception from the list, but none was thrown.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Which are the most wanted automation testing tools in 2023 that have climbed the top of the ladder so far? Let’s take a look on top &lt;a href="https://www.lambdatest.com/blog/automation-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;test automation tools&lt;/a&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Exception test with Exception Messages
&lt;/h2&gt;

&lt;p&gt;In this section, we take examples of test cases to understand how exception messages can be asserted in exception tests in TestNG.&lt;/p&gt;

&lt;p&gt;In this case, we have added the code which should throw a &lt;em&gt;NoSuchElementException&lt;/em&gt; with an exception message containing keywords no such element and the same is set to be verified as the expected message for the exception using the &lt;em&gt;expectedExceptionsMessageRegExp&lt;/em&gt; attribute of *&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; *annotation. Since this exception message is dynamic depending on the web element locator passed, we use regex to pass the expected message.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2A44v4Jmh9lUAPfapDDqGVbw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2A44v4Jmh9lUAPfapDDqGVbw.png" width="800" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On executing the above case, you can see that the test output is passed since the expected and actual exception message is the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AAaJp9p5OI0LfauCijohW8A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AAaJp9p5OI0LfauCijohW8A.png" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To demonstrate the failure due to &lt;a href="https://www.lambdatest.com/blog/asserts-in-testng/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;TestNG assertion&lt;/a&gt; in the message, we reuse the same test case but update the execution code inside it to make it raise a different exception. A different exception would lead to another message and hence showcase test case failure in this example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AujUGEl4oPSsh1Jn6-fMnoA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AujUGEl4oPSsh1Jn6-fMnoA.png" width="800" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above code, we have updated the expected exception to be a valid one as there is no such window with the name &lt;em&gt;new window&lt;/em&gt;, but the exception message is not updated. Let’s run it to see the output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AKIHndF8FsUa_sSWFQUVSpw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AKIHndF8FsUa_sSWFQUVSpw.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the output error, it is evident that it failed due to the difference in the expected and actual exception messages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Execution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since we are using the LambdaTest cloud grid for the execution, results can be viewed on the &lt;a href="https://accounts.lambdatest.com/dashboard?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Dashboard&lt;/a&gt; as well&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2702%2F1%2AZmSTscHFDU51jVUzz10cag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2702%2F1%2AZmSTscHFDU51jVUzz10cag.png" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also view details of all the test cases executed by navigating to the &lt;strong&gt;Build **section under **Automation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2294%2F1%2AQXO8rSWlasuu-5M3tUDWDw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2294%2F1%2AQXO8rSWlasuu-5M3tUDWDw.png" width="800" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to test your knowledge around TestNG concepts, its configuration, annotations, and more, you can take &lt;a href="https://www.lambdatest.com/certifications/testng?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;TestNG certification&lt;/a&gt; by LambdaTest. LambdaTest TestNG certification is an industry-recognized certification that validates an individual’s proficiency in using the TestNG testing framework for automated testing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Run your &lt;a href="https://www.lambdatest.com/jest?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Jest&lt;/a&gt; automation tests in massive parallel across multiple browser and OS combinations with LambdaTest.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That concludes our blog on exception tests in TestNG. Throughout this post, we have gained knowledge about exception tests in TestNG, their purpose, and how to implement them with practical examples. By applying these concepts in your automation testing with &lt;a href="https://www.lambdatest.com/blog/testng-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr24_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;TestNG in Selenium&lt;/a&gt;, you can achieve optimal results when encountering unforeseen circumstances. It’s time for you to dive in and start testing some exceptions!&lt;/p&gt;

</description>
      <category>testing</category>
      <category>testng</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How To Perform Local Website Testing Using Selenium And Java</title>
      <dc:creator>Vipulgupta9090</dc:creator>
      <pubDate>Mon, 06 Mar 2023 06:31:27 +0000</pubDate>
      <link>https://dev.to/testmuai/how-to-perform-local-website-testing-using-selenium-and-java-2h5j</link>
      <guid>https://dev.to/testmuai/how-to-perform-local-website-testing-using-selenium-and-java-2h5j</guid>
      <description>&lt;p&gt;Users expect new features and websites to be seamless and user-friendly when they go live. End-to-end &lt;a href="https://www.lambdatest.com/blog/website-testing/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;website testing&lt;/a&gt; in local infrastructure becomes an unspoken critical requirement for this. However, if this test is performed later or after the entire website or app has been developed, the possibility of bugs and code issues increases. Such issues can do more damage than we can ever think of.&lt;/p&gt;

&lt;p&gt;According to a report, 88% of users are less likely to return to the website after a bad user experience. As much as $2.6 Billion is lost each year due to slow-loading websites and images on them if it takes more than an average of 2 seconds. Also, up to 8 out of 10 users stop visiting a website if it is incompatible with their device.&lt;/p&gt;

&lt;p&gt;A mere look at these numbers is terrifying due to the cost and effort involved in fixing these at a later stage, in addition to the customer base lost due to bad impressions and first experiences.&lt;/p&gt;

&lt;p&gt;In such situations, testing the websites beforehand becomes imperative on such platforms where this cost can be reduced to a minimum.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/cloud-testing-tutorial/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Cloud testing&lt;/a&gt; platforms like LambdaTest help test such websites on various local environments by providing remote access to real browsers and operating systems. This allows you to verify the functionality and compatibility of your website across different configurations without having to set up a complex &lt;a href="https://www.lambdatest.com/learning-hub/test-infrastructure?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test infrastructure&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Testing a website, especially in a production environment, can be time-consuming and resource-intensive. This can slow development and make it difficult to detect bugs and issues early on, delaying developer feedback.&lt;/p&gt;

&lt;p&gt;Local website testing tests a website on a developer’s machine using automated functional tests. These test scripts can be designed to be integrated with the CI/CD pipeline and executed for each local deployment. This saves time and resources by identifying issues early, shortening the feedback cycle, and thus increasing the ROI on development and testing.&lt;/p&gt;

&lt;p&gt;Automated local website testing enables developers to speed up and streamline the testing process. Effective &lt;a href="https://www.lambdatest.com/learning-hub/test-management?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test case management&lt;/a&gt; is crucial in this scenario, as it allows for testing on various browser configurations and OS versions to cater to the diverse systems used by end users. A well-designed &lt;a href="https://www.lambdatest.com/blog/automation-testing-frameworks/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;test automation framework&lt;/a&gt; is essential for performing local testing efficiently.&lt;/p&gt;

&lt;p&gt;Because we will be discussing website testing in this blog, &lt;a href="https://www.lambdatest.com/selenium?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; is the best choice. Furthermore, because the website will be hosted locally, we will require a platform that allows for local website testing without interfering with the local infrastructure or the developer’s machine.&lt;/p&gt;

&lt;p&gt;In this blog on local website testing, we will learn more about &lt;a href="https://www.lambdatest.com/local-page-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;local page testing&lt;/a&gt; and its advantages in software development and the testing cycle. It follows how we can write an automation test script for a locally hosted website and execute the same in an organized manner so as not to block local infrastructure but still get faster feedback.&lt;/p&gt;

&lt;p&gt;So, let us get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Local Website Testing?
&lt;/h2&gt;

&lt;p&gt;Local website testing allows developers to host and test the website on their computers or local infrastructure. Once the developer is confident, the website can be moved to a live testing server before making it live in production.&lt;/p&gt;

&lt;p&gt;This website is a copy that behaves like the real one and provides a place to test it with the least threat. This includes checking &lt;a href="https://www.lambdatest.com/learning-hub/cross-browser-compatibility?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;cross browser compatibility&lt;/a&gt;, user interactions, different links or images on the page, etc.&lt;/p&gt;

&lt;p&gt;This configuration is ideally different from a staging or pre-prod &lt;a href="https://www.lambdatest.com/blog/what-is-test-environment/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;test environment&lt;/a&gt; where any app or website is usually tested in a testing cycle by the QA team before it is made available on production. This is because in a staging or pre-prod environment, more stable services are running, or features are being tested at a later stage of development and require more &lt;a href="https://www.lambdatest.com/learning-hub/regression-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;regression testing&lt;/a&gt; and/or &lt;a href="https://www.lambdatest.com/learning-hub/integration-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;integration testing&lt;/a&gt; with external resources. As a result, we don’t want to risk breaking such an environment with early-stage changes that are more prone to bugs. Local hosting and testing websites become extremely important and useful in such cases.&lt;/p&gt;

&lt;p&gt;There are a few different ways to set up local website testing. One common method is to use a local development server, such as XAMPP, which can be installed on a computer and configured to run a website. In this blog on local website testing, we will use the same to access the website on localhost.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Need a great solution for cross browser testing on Safari? Forget about emulators or simulators — use real online browsers. Try LambdaTest to test on &lt;a href="https://www.lambdatest.com/test-on-safari-browsers?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;safari browser online&lt;/a&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of Local Website Testing
&lt;/h2&gt;

&lt;p&gt;There are several advantages to using local website testing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Accelerated developer feedback:&lt;/strong&gt; Local website testing greatly improves the feedback cycle as developers can quickly make changes to the code and check the results. This improves the development, leading to a better user experience and a more refined final product. Overall it highly improves the efficiency and effectiveness of the development process and allows the delivery of a high-quality website in a shorter time by reducing the risk of any major issues after launch.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Speed of Execution:&lt;/strong&gt; It allows developers to quickly test their changes without waiting for the code to be deployed on &lt;a href="https://www.lambdatest.com/blog/what-is-test-environment/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;testing environments&lt;/a&gt;. This saves a lot of time and helps them to iterate faster during the development cycle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cost-effective:&lt;/strong&gt; Testing a website locally is highly cost-effective as it lessens or even eliminates the time required for testing on a live server, saving hosting and associated services costs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Greater Control and Ease to Debug:&lt;/strong&gt; A developer has better control over the environmental configurations when performing local website testing. Also, they have access to various debugging tools on their computers, like the developer’s console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integration with CI/CD pipeline:&lt;/strong&gt; Local website testing can be used in conjunction with &lt;a href="https://www.lambdatest.com/blog/what-is-continuous-integration-and-continuous-delivery/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Continuous Integration and Continuous Development (CI/CD)&lt;/a&gt; pipeline to ensure that changes to the website are thoroughly tested before they are deployed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Great fit for Agile:&lt;/strong&gt; Local website testing can be a valuable tool in an &lt;a href="https://www.lambdatest.com/blog/agile-development/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Agile development&lt;/a&gt; environment because it allows developers to test changes to their website and receive feedback quickly.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It adds the advantage of allowing devs and QAs to work in parallel and provide better results.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring Tunnel for Local Website Testing
&lt;/h2&gt;

&lt;p&gt;Having understood the basics and advantages of local website testing, let us now move to the implementation part and see how we can perform it on our local computer.&lt;/p&gt;

&lt;p&gt;In this blog, we will use the LambdaTest platform to test the locally hosted website.&lt;/p&gt;

&lt;p&gt;LambdaTest &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;cloud Selenium Grid&lt;/a&gt; supports testing of the local websites using the LambdaTest tunnel app. This tunnel allows you to test the website on over 3000+ real browser and OS combinations by connecting the local systems to LambdaTest servers via SSH-based integration.&lt;/p&gt;

&lt;p&gt;%[&lt;a href="https://youtu.be/KcqY1w_-LLE" rel="noopener noreferrer"&gt;https://youtu.be/KcqY1w_-LLE&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your native, hybrid, and web apps across all legacy and latest mobile operating systems on the most powerful &lt;a href="https://www.lambdatest.com/android-emulator-online?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Android emulator online&lt;/a&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Subscribe to the &lt;a href="https://www.youtube.com/c/LambdaTest?sub_confirmation=1" rel="noopener noreferrer"&gt;LambdaTest YouTube Channel&lt;/a&gt; and stay updated with the latest tutorial around &lt;a href="https://www.lambdatest.com/automated-browser-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automated browser testing&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/cypress-e2e-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Cypress E2E testing&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/learning-hub/mobile-app-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Mobile App Testing&lt;/a&gt;, and more.&lt;/p&gt;

&lt;p&gt;This tunnel helps you test plain HTML, CSS, PHP, Python, or similar web files saved on your local system over combinations of operating systems, browsers, and screen resolutions available on LambdaTest. This tunnel follows various protocols, such as Web Socket, HTTPS, SSH(Secure Shell), etc., to help you establish a secure and unique tunnel connection through corporate firewalls between your system and LambdaTest cloud servers.&lt;/p&gt;

&lt;p&gt;Before setting up the tunnel and seeing how it works, we must host a local website or webpage. In this blog, we are referring to this CodePen project.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to host a local website?
&lt;/h2&gt;

&lt;p&gt;To set up and verify it by launching the webpage, follow these steps&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Open the link mentioned above, export the project, and unzip the downloaded *.zip.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Make sure to turn on XAMPP or any other web hosting tool you use. If you are using XAMPP, start the &lt;strong&gt;Apache&lt;/strong&gt; service under &lt;strong&gt;Actions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Copy and Paste the content from the unzipped folder in Step 1, inside the XAMPP &lt;strong&gt;htdocs&lt;/strong&gt; folder, to access the website on this URL: &lt;a href="http://localhost." rel="noopener noreferrer"&gt;http://localhost.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2674%2F0%2Ag4w7OURZpvFEZ7kI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2674%2F0%2Ag4w7OURZpvFEZ7kI.png" width="800" height="306"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this, the setup for the local website is done. Next, we move to the Tunnel configuration and see how we can use the same to perform &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automated testing&lt;/a&gt; of the local website.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Now, test your native, hybrid, and web apps across all legacy and latest mobile operating systems on the most powerful &lt;a href="https://www.lambdatest.com/android-emulator-online?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;online emulator Android&lt;/a&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to configure the tunnel?
&lt;/h2&gt;

&lt;p&gt;This blog covers configuring the LamdbaTest tunnel connection and &lt;a href="https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;testing locally hosted web pages&lt;/a&gt; from a macOS (Big Sur) perspective. The configuration remains the same for all previous versions.&lt;/p&gt;

&lt;p&gt;LambdaTest also supports tunnel configuration and testing of local websites on &lt;a href="https://www.lambdatest.com/support/docs/local-testing-windows/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Windows&lt;/a&gt; and &lt;a href="https://www.lambdatest.com/support/docs/local-testing-linux/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Linux&lt;/a&gt; machines.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: &lt;a href="https://accounts.lambdatest.com/login?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Create your account&lt;/a&gt; on the LambdaTest platform and login into the dashboard.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Click &lt;strong&gt;Configure Tunnel&lt;/strong&gt; on the top right and select &lt;strong&gt;COMMAND LINE&lt;/strong&gt;. Then, download the binary file by clicking on &lt;strong&gt;Download Link&lt;/strong&gt;. This binary helps establish a secure tunnel connection to the LambdaTest cloud servers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2668%2F0%2AUGu6Ut-xsWBBPrhE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2668%2F0%2AUGu6Ut-xsWBBPrhE.png" width="800" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Navigate to your downloads folder and extract the downloaded zip.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AmuKvCPRSgNjAsUB3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AmuKvCPRSgNjAsUB3.png" width="800" height="109"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Copy the command to execute the downloaded binary from the dashboard. The command would look like this. You can mention an optional tunnelName in the command to identify which tunnel you want to execute your test case in case of multiple tunnels.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    LT --user {user's login email} --key {user's access key} --tunnelName {user's tunnel name}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Execute the command to start the tunnel and make the connection. On successful tunnel connection, you will see a prompt “You can start testing now”. Please note that the tunnel has been named as LambdaTest.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ArdmIMRjrINJ0wY_m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ArdmIMRjrINJ0wY_m.png" width="800" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; After this, move back to the &lt;a href="https://accounts.lambdatest.com/dashboard?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Dashboard&lt;/a&gt; to verify the tunnel before we write automation code for local website testing using &lt;a href="https://www.lambdatest.com/blog/selenium-with-java/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium with Java&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; Navigate to &lt;strong&gt;Real Time Testing&lt;/strong&gt;, select &lt;strong&gt;Browser Testing&lt;/strong&gt;, enter the &lt;strong&gt;localhost URL&lt;/strong&gt; you want to test, and select the tunnel name. You can select the test configuration of your choice from various major browsers &amp;amp; their versions to perform a test session. After selecting the configuration, click on the &lt;strong&gt;START&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2228%2F0%2At6KlTeNknQsgBISa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2228%2F0%2At6KlTeNknQsgBISa.png" width="800" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8:&lt;/strong&gt; At this point, you should get navigated to your localhost URL. This shows that the setup is verified, and we can write the automation code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2378%2F0%2AEZzom8E-ipcdFBCr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2378%2F0%2AEZzom8E-ipcdFBCr.png" width="800" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Are you using &lt;a href="https://www.lambdatest.com/playwright-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Playwright&lt;/a&gt; for automation testing? Run your Playwright test scripts instantly on 50+ browser/OS combinations using the LambdaTest cloud. Sign up for free.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Local Website Testing using Selenium and Java
&lt;/h2&gt;

&lt;p&gt;Having completed the tunnel setup, let us now try to implement an automated test script for the same local website using Selenium and Java.&lt;/p&gt;

&lt;p&gt;We would execute the script on the Lambda Test Selenium cloud grid through the tunnel we have already configured.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test scenario for demonstration&lt;/strong&gt; — navigate to &lt;strong&gt;localhost&lt;/strong&gt; using the tunnel and click on the first toggle button.&lt;/p&gt;

&lt;p&gt;A sample test script for local website testing using Selenium with Java would be like the one below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  package test.java;


    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.HashMap;


    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;


    public class TestLocalWebsiteUsingTunnel {


       WebDriver driver = null;
       String user_name = System.getenv("LT_USERNAME") == null ? "LT_USERNAME" : System.getenv("LT_USERNAME");
       String access_key = System.getenv("LT_ACCESS_KEY") == null ? "LT_ACCESS_KEY"
               : System.getenv("LT_ACCESS_KEY");


       @BeforeTest
       public void testSetUp() throws Exception {
           ChromeOptions browserOptions = new ChromeOptions();
           browserOptions.setPlatformName("Windows 10");
           browserOptions.setBrowserVersion("108.0");
           HashMap&amp;lt;String, Object&amp;gt; ltOptions = new HashMap&amp;lt;String, Object&amp;gt;();
           ltOptions.put("username", user_name);
           ltOptions.put("accessKey", access_key);
           ltOptions.put("project", "Local Website Testing using Selenium JAVA");
           ltOptions.put("build", "Local Website Testing");
           ltOptions.put("tunnel", true);
           ltOptions.put("selenium_version", "4.0.0");
           ltOptions.put("w3c", true);
           browserOptions.setCapability("LT:Options", ltOptions);


           try {
               driver = new RemoteWebDriver(
                       new URL("https://" + user_name + ":" + access_key + "@hub.LambdaTest.com/wd/hub"), browserOptions);
           } catch (MalformedURLException exc) {
               exc.printStackTrace();
           }
       }


       @Test(description = "Demonstration of Automated Local Website Testing using LambdaTest Tunnel")
       public void testLocalWebsite() throws InterruptedException {
           driver.get("https://localhost");
           driver.findElement(By.cssSelector("[for='cb1']")).click();
       }


       @AfterTest
       public void tearDown() {
           if (driver != null) {
               driver.quit();
           }
       }
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Code Walkthrough
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; The first step would be to create an instance of the &lt;a href="https://www.lambdatest.com/blog/selenium-remotewebdriver/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;RemoteWebDriver&lt;/a&gt; as we will be executing the code on the Selenium cloud grid.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Ao-5BDqPjwB2D_27H.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Ao-5BDqPjwB2D_27H.png" width="800" height="229"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; As already mentioned, since we are using LambdaTest cloud grid and tunnel for local website testing, we would need to add credentials of the same in the environment variables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ARtVMR8O8YKONdCOy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ARtVMR8O8YKONdCOy.png" width="800" height="179"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you do not have your credentials, navigate to the LambdaTest Dashboard, click on the Profile icon in the top-right corner of your screen, then click on &lt;strong&gt;Profile&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A6Wyre-b3rPsDsaxn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A6Wyre-b3rPsDsaxn.png" width="800" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will find your username and access key here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ATIbL9YRl203ov-EY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ATIbL9YRl203ov-EY.png" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Next, we add a function as &lt;strong&gt;testSetUp()&lt;/strong&gt; to set the initial browser capabilities that will be passed on to the LambdaTest grid to define the browser and OS configurations. This method will be annotated with &lt;strong&gt;@BeforeTest&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;annotation in TestNG&lt;/a&gt; as we want to execute it before each test case run.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2764%2F0%2AeX8wy_qEu9w-HKq-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2764%2F0%2AeX8wy_qEu9w-HKq-.png" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The most important thing to note here is the following code to set the &lt;strong&gt;tunnel&lt;/strong&gt; as &lt;strong&gt;true&lt;/strong&gt;. This code tells the LambdaTest grid that this automation script is part of localhost website testing and that tunnel configuration is to be used for execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AsrLMmeq0cwUuo8EY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AsrLMmeq0cwUuo8EY.png" width="800" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; After setting the initial capabilities and tunnel configuration, we add the test case function &lt;strong&gt;testLocalWebsite()&lt;/strong&gt; inside, telling the driver to navigate to &lt;strong&gt;localhost&lt;/strong&gt; and click on the first toggle button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ARZJQqVldSNg5ulBN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ARZJQqVldSNg5ulBN.png" width="800" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For this click, we are using the &lt;a href="https://www.lambdatest.com/blog/how-pro-testers-use-css-selectors-in-selenium-automation-scripts/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;CSS Selector&lt;/a&gt; of the web element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ASz79FHYmkhyaKJdm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ASz79FHYmkhyaKJdm.png" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; After executing every test script, we must close the browser. To perform the same, another function as tearDown() is added and annotated with &lt;strong&gt;@AfterTest&lt;/strong&gt; to execute it after every test execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A5czEmUXWCSWuI4-o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A5czEmUXWCSWuI4-o.png" width="658" height="508"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;A comprehensive &lt;a href="https://www.lambdatest.com/learning-hub/end-to-end-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;end to end Testing&lt;/a&gt; tutorial that covers what E2E Testing is, its importance, benefits, and how to perform it with real-time examples.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test Execution
&lt;/h2&gt;

&lt;p&gt;So far, we have understood local website testing and how to make relevant configurations to perform the same on the LambdaTest platform using the tunnel. Now, we will execute the test script and see what the &lt;a href="https://www.lambdatest.com/learning-hub/test-execution?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test execution&lt;/a&gt; looks like on the LambdaTest Dashboard.&lt;/p&gt;

&lt;p&gt;Since we have used &lt;a href="https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;TestNG annotations&lt;/a&gt;, the test script can be executed as TestNG run. Upon execution, you will see results like below on Dashboard.&lt;/p&gt;

&lt;p&gt;Navigate to &lt;strong&gt;Automation&lt;/strong&gt; &amp;gt; &lt;strong&gt;Build&lt;/strong&gt; to see the execution results.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Av3cftGKAiBOwITpp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Av3cftGKAiBOwITpp.png" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To view the details of execution, click on the session name on the right side. You can note that the URL to test navigated is &lt;strong&gt;localhost&lt;/strong&gt;, and &lt;strong&gt;Tunnel ID&lt;/strong&gt; is the same as the tunnelName, i.e., &lt;strong&gt;LambdaTest&lt;/strong&gt;, which we mentioned while starting the tunnel in the configuration section.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AFwJkqzQ3pnbhZ-34.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AFwJkqzQ3pnbhZ-34.png" width="800" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;A comprehensive &lt;a href="https://www.lambdatest.com/learning-hub/ui-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar06_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;UI testing&lt;/a&gt; tutorial that covers what UI testing is, its importance, benefits, and how to perform it with real-time examples.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this blog on how to perform local website testing using Selenium and Java, we have learned about local website testing and why it is so important in the software development world and implemented the same on the LambdaTest platform using an automation test script.&lt;br&gt;
Overall, local website testing is an effective solution for developers who want to ensure that their website is thoroughly tested and free of bugs and issues before it goes to production.&lt;/p&gt;

&lt;p&gt;Happy Local Testing!!&lt;/p&gt;

</description>
      <category>testing</category>
      <category>selenium</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>A Complete Guide To Selenium Page Load Strategy</title>
      <dc:creator>Vipulgupta9090</dc:creator>
      <pubDate>Fri, 03 Mar 2023 17:07:06 +0000</pubDate>
      <link>https://dev.to/testmuai/a-complete-guide-to-selenium-page-load-strategy-26f0</link>
      <guid>https://dev.to/testmuai/a-complete-guide-to-selenium-page-load-strategy-26f0</guid>
      <description>&lt;p&gt;Webpage automation has been growing extensively, and most of us have worked on it. It is something on which we work almost regularly, automating several scenarios and navigating through many web pages.&lt;/p&gt;

&lt;p&gt;But do you ever wonder what goes behind the scenes when the webpage is loaded in &lt;a href="https://www.lambdatest.com/learning-hub/webdriver?utm_source=medium&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium WebDriver&lt;/a&gt;? We know of a few WebDriver functions used to navigate to a webpage, like a &lt;em&gt;get() *and *navigate()&lt;/em&gt;, but do they define how a page is loaded, or is there some other functionality at play in the backend of things?&lt;/p&gt;

&lt;p&gt;The answer to this is YES! It is defined by something called the Page Load Strategy in Selenium WebDriver. The WebDriver uses Page Load Strategy to determine how the webpage will be loaded before you proceed with further interactions; to execute the next actions.&lt;/p&gt;

&lt;p&gt;In this blog on Selenium Page Load Strategy, we will take a deeper look at this strategy and try to understand its different types and how they can be implemented in automation code.&lt;/p&gt;

&lt;p&gt;So let us get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Page Load Strategy?
&lt;/h2&gt;

&lt;p&gt;Page Load Strategy defines when the page would be considered loaded for the current session whenever the automation code launches a browser and uses get() or navigate().to() method. By default, Selenium WebDriver uses the standard Page Load Strategy, which is NORMAL. It means waiting until the entire webpage and its components, like CSS, images, frames, etc., are loaded.&lt;/p&gt;

&lt;p&gt;If the page takes longer to load due to the components mentioned above (or throttling network conditions) and is not important to your script, you can change it to EAGER or NONE to speed up the execution. This value applies to the entire session, so wisely choose it for your project else it might lead to test flakiness.&lt;/p&gt;

&lt;p&gt;The usage of Page Load Strategy has been introduced as a part of &lt;a href="https://www.lambdatest.com/learning-hub/selenium-4?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium 4&lt;/a&gt; features. Unlike past versions, it removes the dependency on using default page load timeouts.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Mmp_FiNIYtQ"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Need a great solution for &lt;a href="https://www.lambdatest.com/safari-browser-for-windows?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Safari for Windows&lt;/a&gt;? Forget about emulators or simulators — use real online browsers. Try LambdaTest for free.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Page Load Strategy work?
&lt;/h2&gt;

&lt;p&gt;To determine whether the Page load is completed or not, &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; makes use of the document.readyState property. The document.readyState describes the loading state of the document. A document here refers to any webpage that is loaded in the browser.&lt;/p&gt;

&lt;p&gt;The Ready State method of Document is supported by all browsers, making it a reliable option for use with Selenium’s PageLoadStrategy.&lt;/p&gt;

&lt;p&gt;Just like we have default PageLoadStrategy, default document.readyState for which Selenium WebDriver waits is Complete.&lt;/p&gt;

&lt;p&gt;The readyState property of a browser’s document object represents the current state of the page loading process. The property can have one of the following values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;loading&lt;/strong&gt; — The page is still loading.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;interactive&lt;/strong&gt; — The page has finished loading, but sub-resources such as images, stylesheets, and frames may still be loading.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;complete&lt;/strong&gt; — The page and all sub-resources have finished loading.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can use the readyState property to determine when a page has fully loaded and is ready to be manipulated by JavaScript or other means. For example, in Selenium, you can use the readyState property to determine when a page has loaded, and the elements on the page can be interacted with using Selenium commands.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fivr0gpb84w26oy9a811z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fivr0gpb84w26oy9a811z.png" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before moving forward, let’s understand a bit more about the readyState of the document with a short exercise.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to &lt;a href="https://ecommerce-playground.lambdatest.io/" rel="noopener noreferrer"&gt;https://ecommerce-playground.lambdatest.io/&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the site is loaded, right-click -&amp;gt; click on Inspect -&amp;gt; open the console and execute the command document.readyState.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As a result of this command, you would see the output as complete, which is the document’s current state. This means that the entire page is loaded with the necessary resources (i.e. images, CSS, etc.).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, refresh the page and hit enter again on the above command. You will be able to see the loading state of the document at this moment, like below.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpople3l5k3rkeh82cyct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpople3l5k3rkeh82cyct.png" width="800" height="703"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you cannot see the loading state after 4–5 refreshes, try throttling the Internet conditions by setting Network to slower options. You can learn more about it through this blog on &lt;a href="https://www.lambdatest.com/blog/test-mobile-websites-on-different-network-conditions/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;testing websites on different network conditions&lt;/a&gt;. We will be making use of the same approach in the demonstration as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq34l8e18gjzkcaak5hgx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq34l8e18gjzkcaak5hgx.png" width="800" height="590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this shows how document.readyState works in practice. You can also check the website’s performance under varying network conditions using &lt;a href="https://www.lambdatest.com/lt-browser?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LT Browser&lt;/a&gt;, a &lt;a href="https://www.lambdatest.com/mobile-friendly-tester?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;mobile-friendly tester&lt;/a&gt; tool. LT Browser is a complementary tool offered by LambdaTest, which allows you to test your website’s responsiveness on over 50 different devices, including smartphones, tablets, and laptops. It also offers the ability to create custom resolutions, simulate different network conditions, and generate performance reports using Google Lighthouse.&lt;/p&gt;

&lt;p&gt;Let’s move forward to understand these values and how they are mapped with PageLoadStrategy values.&lt;/p&gt;

&lt;h2&gt;
  
  
  PageLoadStrategy &amp;lt;&amp;gt; document.readyState Mapping
&lt;/h2&gt;

&lt;p&gt;The readyState of the document can be one of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;loading&lt;/strong&gt;: Document load is under progress.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;interactive&lt;/strong&gt;: Document has been loaded and parsed, but the sub-resource like scripts, images, CSS, and frames are still loading. At this point, the DOMContentLoaded event is fired.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;complete&lt;/strong&gt;: Document and all sub-resources have finished loading. This indicates the firing of the &lt;strong&gt;load&lt;/strong&gt; event.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each PageLoadStrategy is mapped to a particular document.readyState value based on which the WebDriver will complete the navigation method and continue executing the next steps.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;PageLoadStrategy&lt;/th&gt;
&lt;th&gt;document.readyState&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Normal&lt;/td&gt;
&lt;td&gt;complete&lt;/td&gt;
&lt;td&gt;Used by default by browser and Selenium WebDriver. Waits for all the resources to be downloaded.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Eager&lt;/td&gt;
&lt;td&gt;interactive&lt;/td&gt;
&lt;td&gt;Resources like images and CSS might still be loading, but DOM is accessible and ready to interact.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;any&lt;/td&gt;
&lt;td&gt;WebDriver is not blocked at all. Execution continues without any wait as soon as the initial page is loaded.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Types of Page Load Strategies in Selenium
&lt;/h2&gt;

&lt;p&gt;There are typically three types of Selenium Page Load Strategies that can be used in &lt;a href="https://www.lambdatest.com/learning-hub/web-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;web automation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  NORMAL (Default, if not specified)
&lt;/h2&gt;

&lt;p&gt;This Selenium Page Load Strategy makes the WebDriver wait until the page load is complete, i.e., the load event is fired.&lt;/p&gt;

&lt;p&gt;The load event is fired when the page has loaded, including the dependent resources such as CSS, JavaScript, iFrames, and images.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ebo4lbvtexbtpo99jty.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ebo4lbvtexbtpo99jty.png" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It waits for the HTML content to be downloaded and parsed along with all the subresources.&lt;/p&gt;

&lt;h2&gt;
  
  
  EAGER
&lt;/h2&gt;

&lt;p&gt;With this Selenium Page Load Strategy, the WebDriver only waits until the initial page is loaded and the DOMContentLoaded event is fired.&lt;/p&gt;

&lt;p&gt;Unlike the load event, the DOMContentLoaded event is fired as soon as the DOM has been loaded without waiting for additional resources like CSS, JavaScript, iFrames, and images to be loaded.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh0xe0tmtw468npmkdyvq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh0xe0tmtw468npmkdyvq.png" width="800" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It waits only for the HTML content to be downloaded and parsed.&lt;/p&gt;

&lt;h2&gt;
  
  
  NONE
&lt;/h2&gt;

&lt;p&gt;In this Selenium Page Load Strategy, the WebDriver is not blocked and continues with the &lt;a href="https://www.lambdatest.com/learning-hub/test-execution?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test execution&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It waits only for the HTML content to be downloaded.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6bypowjkbyjtopsyaze5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6bypowjkbyjtopsyaze5.png" width="800" height="285"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can learn more about waits through this blog on &lt;a href="https://www.lambdatest.com/blog/types-of-waits-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Waits in Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do you use a Mac and want to run the test in Internet Explorer? This article explores how to test &lt;a href="https://www.lambdatest.com/blog/test-internet-explorer-for-mac/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Internet Explorer on Mac&lt;/a&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: WebDriver setup with different Selenium Page Load Strategies
&lt;/h2&gt;

&lt;p&gt;Now that we have developed a basic understanding of different Selenium Page Load Strategies, let’s look at code examples to understand how it is implemented.&lt;/p&gt;

&lt;p&gt;To do the same, we will be writing an &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automation testing&lt;/a&gt; script for the following scenario.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create an instance of RemoteWebDriver using cloud-based Selenium Grid. For the same, we will be using the LambdaTest platform.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set WebDriver capabilities to specific PageLoadStrategy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, use the driver to launch the browser and navigate to &lt;a href="https://ecommerce-playground.lambdatest.io/" rel="noopener noreferrer"&gt;https://ecommerce-playground.lambdatest.io/&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For this, we will note the time duration in which page load is completed to see how it varies for different Selenium Page Load Strategies under the same conditions. Differences in page load time will help get more clarity on all strategies.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;

&lt;p&gt;For this Selenium Page Load Strategy blog, the Maven project using &lt;a href="https://www.lambdatest.com/blog/how-to-install-testng-in-eclipse-step-by-step-guide/?utm_source=hashnode&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Java in Eclipse IDE&lt;/a&gt; will be used. If you are not a fan of Eclipse, use any IDE of your choice and perform the same steps. It will use Selenium WebDriver and TestNG dependencies to automate interaction with the page elements and test case execution. It is highly recommended to use the latest stable versions for better results.&lt;/p&gt;

&lt;p&gt;Let’s get started now.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Launch Eclipse or the IDE of your choice.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a new Maven project and name it PageLoadStrategy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside the src folder, add a test package and inside it a Test class and name it as TestPageLoadStrategy. This class will hold the entire code for this demonstration.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5p6sgi1eb5kmgycf2vm1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5p6sgi1eb5kmgycf2vm1.png" width="329" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Update the pom.xml file to have the latest dependencies for using TestNG in Selenium, as shown below, before writing the actual test case.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;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 https://maven.apache.org/xsd/maven-4.0.0.xsd"&amp;gt;
     &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
     &amp;lt;groupId&amp;gt;PageLoadStrategy&amp;lt;/groupId&amp;gt;
     &amp;lt;artifactId&amp;gt;PageLoadStrategy&amp;lt;/artifactId&amp;gt;
     &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;
     &amp;lt;build&amp;gt;
       &amp;lt;sourceDirectory&amp;gt;src&amp;lt;/sourceDirectory&amp;gt;
       &amp;lt;plugins&amp;gt;
         &amp;lt;plugin&amp;gt;
           &amp;lt;artifactId&amp;gt;maven-compiler-plugin&amp;lt;/artifactId&amp;gt;
           &amp;lt;version&amp;gt;3.8.1&amp;lt;/version&amp;gt;
           &amp;lt;configuration&amp;gt;
             &amp;lt;release&amp;gt;16&amp;lt;/release&amp;gt;
           &amp;lt;/configuration&amp;gt;
         &amp;lt;/plugin&amp;gt;
       &amp;lt;/plugins&amp;gt;
     &amp;lt;/build&amp;gt;
     &amp;lt;dependencies&amp;gt;
       &amp;lt;dependency&amp;gt;
           &amp;lt;groupId&amp;gt;org.seleniumhq.selenium&amp;lt;/groupId&amp;gt;
           &amp;lt;artifactId&amp;gt;selenium-java&amp;lt;/artifactId&amp;gt;
           &amp;lt;version&amp;gt;4.6.0&amp;lt;/version&amp;gt;
       &amp;lt;/dependency&amp;gt;
       &amp;lt;dependency&amp;gt;
           &amp;lt;groupId&amp;gt;org.testng&amp;lt;/groupId&amp;gt;
           &amp;lt;artifactId&amp;gt;testng&amp;lt;/artifactId&amp;gt;
           &amp;lt;version&amp;gt;7.7.0&amp;lt;/version&amp;gt;
           &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
       &amp;lt;/dependency&amp;gt;
     &amp;lt;/dependencies&amp;gt;
    &amp;lt;/project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Automate &lt;a href="https://www.lambdatest.com/cypress-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Cypress testing&lt;/a&gt; and perform browser automation testing with LambdaTest. Our cloud infrastructure has 3000+ desktop &amp;amp; mobile environments. Try for free.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Once the pom.xml is updated, add the following code to the test case file, TestPageLoadStrategy.java. This file will contain three tests demonstrating the usage of all three types of Selenium Page Load Strategies.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.*;
import java.util.HashMap;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;

public class TestPageLoadStrategy {

   public RemoteWebDriver driver = null;
   String username = "&amp;lt;lambdtest_username&amp;gt;";
   String accessKey = "&amp;lt;lambdtest_accesskey&amp;gt;";

  ChromeOptions chromeOptions = new ChromeOptions();
   HashMap&amp;lt;String, Object&amp;gt; ltOptions = new HashMap&amp;lt;String, Object&amp;gt;();

   private void setup(String buildName) {

       chromeOptions.setPlatformName("Windows 10");
       chromeOptions.setBrowserVersion("108.0");

       ltOptions.put("project", "Page Load Strategy");
       ltOptions.put("selenium_version", "4.0.0");
       ltOptions.put("w3c", true);
       ltOptions.put("networkThrottling", "Regular 2G");
       ltOptions.put("build", buildName);
   }

   private void checkPageLoad() {
       try {
           driver = new RemoteWebDriver(
                   new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), chromeOptions);

           Instant startTime = Instant.now();
           System.out.println("Page load started at : " + startTime.toString());

           System.out.println("Navigating to the URL");
           driver.get("https://ecommerce-playground.lambdatest.io/");

           Instant endTime = Instant.now();
           System.out.println("Page load ended at : " + endTime.toString());

           Duration duration = Duration.between(startTime, endTime);
           System.out.println("Total PageLoad time : " + duration.toMillis() + " milli seconds");
       } catch (MalformedURLException e) {
           System.out.println("Invalid grid URL");
       } finally {
           driver.quit();
       }
   }

   @Test
   public void testNormalStrategy() {
       // To set PageLoadStrategy = Normal
       setup("Page Load Strategy - Normal");

       chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);
       chromeOptions.setCapability("LT:Options", ltOptions);
       checkPageLoad();
   }

   @Test
   public void testEagerStrategy() {
       // To set PageLoadStrategy = Eager
       setup("Page Load Strategy - Eager");
       chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);
       chromeOptions.setCapability("LT:Options", ltOptions);
       checkPageLoad();
   }

   @Test
   public void testNoneStrategy() {
       // To set PageLoadStrategy = None
       setup("Page Load Strategy - None");
       chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
       chromeOptions.setCapability("LT:Options", ltOptions);
       checkPageLoad();
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Code Walkthrough: TestPageLoadStrategy.java
&lt;/h2&gt;

&lt;p&gt;This Java file is the main test class for demonstrating the implementation of all strategies using different functions.&lt;/p&gt;

&lt;p&gt;Let’s understand each step in detail.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; The first step would be to create an instance of &lt;a href="https://www.lambdatest.com/blog/selenium-remotewebdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;RemoteWebDriver&lt;/a&gt;. This is required to be able to support execution on &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;cloud Selenium Grid.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjfuay7ktbyh056ztivyt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjfuay7ktbyh056ztivyt.png" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using cloud &lt;a href="https://www.lambdatest.com/blog/selenium-grid-setup-tutorial/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium Grid &lt;/a&gt;provides increased scalability and speed for &lt;a href="https://www.lambdatest.com/java-automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Java automation testing&lt;/a&gt; and allows users to perform extensive parallel and &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;cross browser testing&lt;/a&gt; across several OS and browser combinations.&lt;/p&gt;

&lt;p&gt;For this blog demonstration, we are using the LambdaTest Cloud Grid. LambdaTest is a highly scalable, reliable, and easy-to-use platform that provides support of over 3000+ browsers and OS to support execution on Selenium Cloud Grid.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/WZlsHlReRww"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Subscribe to the &lt;a href="https://www.youtube.com/c/LambdaTest?sub_confirmation=1" rel="noopener noreferrer"&gt;LambdaTest YouTube Channel&lt;/a&gt; and stay updated with the latest tutorials around &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium testing&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/cypress-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Cypress testing&lt;/a&gt;, and more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Also, Online &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium Grid&lt;/a&gt; to run your browser automation testing scripts on cloud infrastructure containing 3000+ desktop and mobile browser environments. Perform Selenium Testing on a cloud automation testing grid that scales along with your tests.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; As mentioned in the previous step, since we are using the LambdaTest platform, we need to provide the username and access key for our user to connect with the cloud hub. You can find these details under &lt;a href="https://accounts.lambdatest.com/detail/profile?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Profile Section&lt;/a&gt; after your account is created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj0bz4syl3z972tk6qneg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj0bz4syl3z972tk6qneg.png" width="800" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4tc3yorssfdglsnm1n3f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4tc3yorssfdglsnm1n3f.png" width="800" height="317"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Create an object of the ChromeOptions class. This class is used to manipulate various properties of the Chrome driver, which we will use for execution. This chromeOptions object will be used in each test case to set the PageLoadStrategy and then passed to WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3eg6z4qpsbfqop885477.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3eg6z4qpsbfqop885477.png" width="800" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4.&lt;/strong&gt; Create a HashMap variable and name it as ltOptions. This will set various properties for the LambdaTest cloud grid execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuaprob98jkicg6b8rbtv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuaprob98jkicg6b8rbtv.png" width="800" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5.&lt;/strong&gt; Next is the &lt;strong&gt;setup()&lt;/strong&gt; method, used to set a few basic properties in chromeOptions and ltOptions. These are the common properties of all three PageLoadStrategy cases.&lt;/p&gt;

&lt;p&gt;Also, note that we are using the networkThrottling property of LambdaTest to set the network to Regular 2G with 250 Kbps download speed and 50 Kbps upload speed. This will help demonstrate the load time better in all scenarios, as discussed in the previous section.&lt;/p&gt;

&lt;p&gt;Using the LambdaTest platform provides this advantage where you can directly select the browser and version details, and your capabilities are automatically generated using &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Capabilities Generator&lt;/a&gt;. You just need to add them to the code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fanhxlpa42tlf3r9d00cc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fanhxlpa42tlf3r9d00cc.png" width="800" height="306"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This method takes one parameter, buildName. This parameter is used to display the build name on the LambdaTest Dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb97k5dmhrxt6pks78fc0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb97k5dmhrxt6pks78fc0.png" width="800" height="510"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6.&lt;/strong&gt; Add a new function, checkPageLoad(). In the try block, we write the code to connect with RemoteWebDriver on the LambdaTest platform using the properties defined in chromeOptions.&lt;/p&gt;

&lt;p&gt;After the connection is established, an object of the Instant class of java.time package is created to store and log the start time when we are about to launch the browser and start to navigate. This is followed by a driver called to the browser and the launch of the test website.&lt;/p&gt;

&lt;p&gt;As soon as the website is considered loaded depending upon the set PageLoadStrategy, the driver passes the control to the next command, which is again an object of Instant class, to get the current time again, the end time.&lt;/p&gt;

&lt;p&gt;By now, the page is loaded as per our requested strategy, and we have the start and end times. The time taken to load a webpage in milliseconds can be determined by finding the difference between two points. The Duration class, which is part of the same package, is used to accomplish this.&lt;/p&gt;

&lt;p&gt;You will notice that this load time differs in all our test cases for different strategies.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7imcb6a3pd7a2x6i9is.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7imcb6a3pd7a2x6i9is.png" width="800" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A catch block is added next for the try block to handle the exception if it occurs. MalformedURLException is to catch the exception in case our RemoteWebDriver URL has invalid syntax. Once everything is done, we finally close the driver instance. To learn more about it, you can go through this blog on the &lt;a href="https://www.lambdatest.com/blog/49-common-selenium-exceptions-automation-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;most common exceptions in Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  testNormalStrategy()
&lt;/h3&gt;

&lt;p&gt;This is the test case to demonstrate the Normal strategy.&lt;/p&gt;

&lt;p&gt;Firstly, the setup() method defines basic properties for the browser and LambdaTest platform. Next, the value for PageLoadStrategy is set as NORMAL here in the chromeOptions, followed by setting other capabilities.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz3ex1ylixqixran67o1q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz3ex1ylixqixran67o1q.png" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, the checkPageLoad() method is called to load the webpage and to log the load time, which we will compare for each strategy in the execution section.&lt;/p&gt;

&lt;h3&gt;
  
  
  testEagerStrategy()
&lt;/h3&gt;

&lt;p&gt;This is the test case to demonstrate the Eager strategy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fppe16vdgx2vdda5ack2r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fppe16vdgx2vdda5ack2r.png" width="800" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All the steps, in this case, are similar to the previous one. The only difference being the value for PageLoadStrategy is set as EAGER here in the chromeOptions, followed by calling of the checkPageLoad() method.&lt;/p&gt;

&lt;h3&gt;
  
  
  testNoneStrategy()
&lt;/h3&gt;

&lt;p&gt;This is the test case to demonstrate the None strategy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flwfc2l7g3w65404ug54n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flwfc2l7g3w65404ug54n.png" width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Value for PageLoadStrategy is set as NONE here in the chromeOptions, and the rest of the steps are similar to previously discussed test cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Run your Selenium &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Automation Testing&lt;/a&gt; scripts on the LambdaTest cloud grid. Test on 3000+ desktop &amp;amp; mobile environments. Try it for free!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test Execution
&lt;/h2&gt;

&lt;p&gt;Having understood the working and implementation of all the PageLoadStrategy, let us now execute the code and see the results of execution locally and on the LambdaTest Dashboard.&lt;/p&gt;

&lt;p&gt;The test case would be executed using TestNG. To execute, follow the below mentioned steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Right-click the test case name in Eclipse IDE.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to Run Test, and select TestNG Test to begin the execution.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let us, execute the cases one by one and see the load time varies for each strategy&lt;/p&gt;

&lt;h3&gt;
  
  
  NORMAL Strategy
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff4mqmk4qv2k6t6ii4k7g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff4mqmk4qv2k6t6ii4k7g.png" width="614" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can notice the TotalPageLoad time, in this case, is 70852 milliseconds as we are using a network with Regular 2G speed. It has been established that the highest value should be among the three cases. As established so far, it should be the highest among all three. We can verify this by running the cases for Eager and None and comparing them.&lt;/p&gt;

&lt;h3&gt;
  
  
  EAGER Strategy
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbhij0y54cpxplkthqa85.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbhij0y54cpxplkthqa85.png" width="603" height="345"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As expected, the PageLoadTime, i.e., 70231 milliseconds, for the Eager strategy in the same scenario is comparatively less than Normal.&lt;/p&gt;

&lt;h3&gt;
  
  
  NONE Strategy
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg3e6mku2v6wuup1n85c8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg3e6mku2v6wuup1n85c8.png" width="604" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can note here that the PageLoadTime for None strategy is significantly less than the other two as it does not wait for any resources to be downloaded.&lt;/p&gt;

&lt;p&gt;Here is a quick summary of the page load times in different strategies&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;PageLoadStrategy&lt;/th&gt;
&lt;th&gt;PageLoadTime (in milliseconds)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Normal&lt;/td&gt;
&lt;td&gt;70852&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Eager&lt;/td&gt;
&lt;td&gt;70231&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;28908&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can also view the execution results by navigating to the LambdaTest Dashboard as shown below for each PageLoadStrategy:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bykdiwpikj37tlz5zf9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bykdiwpikj37tlz5zf9.png" width="800" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can navigate to the Builds section under Automation to view details of your automation build.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Build details for PageLoadStrategy.NORMAL:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvnss1y66jhb1zuf786os.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvnss1y66jhb1zuf786os.png" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Build details for PageLoadStrategy.EAGER:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Facal5wxnfa7xrlxb3rt0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Facal5wxnfa7xrlxb3rt0.png" width="800" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Build details for PageLoadStrategy.NONE:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9sxjr2u4xk2fnxm70s4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9sxjr2u4xk2fnxm70s4e.png" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are proficient in the TestNG framework and want to showcase your expertise as a &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;test automation&lt;/a&gt; expert, you can obtain the TestNG certification from LambdaTest at no cost.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Now, run your &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium Automation Testing&lt;/a&gt; scripts on the LambdaTest cloud grid. Test on 3000+ desktop &amp;amp; mobile environments. Try it for free.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With this, we have reached the end of this &lt;a href="https://www.lambdatest.com/learning-hub/testng?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=mar03_kj&amp;amp;utm_term=kj&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium TestNG tutorial&lt;/a&gt; on different Page Load Strategies in Selenium WebDriver. In this, we have learned about further usage and working of these Selenium Page Load Strategies and how we can integrate them in automation script and execute on Selenium Cloud Grid.&lt;/p&gt;

&lt;p&gt;I hope now you can implement these in your use cases more efficiently.&lt;/p&gt;

&lt;p&gt;Happy Loading!!&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>How To Automate Using TestNG In Selenium? [TestNG Tutorial]</title>
      <dc:creator>Vipulgupta9090</dc:creator>
      <pubDate>Fri, 09 Sep 2022 08:44:09 +0000</pubDate>
      <link>https://dev.to/testmuai/how-to-automate-using-testng-in-selenium-testng-tutorial-1a0g</link>
      <guid>https://dev.to/testmuai/how-to-automate-using-testng-in-selenium-testng-tutorial-1a0g</guid>
      <description>&lt;p&gt;Automation testing is a fast-growing industry, and every tester tends to opt for tools and frameworks that are self-sufficient and offer useful features out of the box. Though there are a number of test automation frameworks like &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt;, Cypress, etc; I still prefer using Selenium.&lt;/p&gt;

&lt;p&gt;The reason for choosing Selenium is very simple — Selenium supports the Java programming language.&lt;/p&gt;

&lt;p&gt;The features offered by Selenium multiply when it is used in conjunction with Java-based test automation frameworks like TestNG, JUnit, etc. An ideal framework in such a case would be robust enough and complement the &lt;a href="https://www.lambdatest.com/blog/13-reasons-why-selenium-webdriver-should-be-your-first-choice-for-automation-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;advantages of Selenium WebDriver &amp;amp; Java&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Of all the candidates available to serve these needs, the one that stands out is TestNG. TestNG is not only easy to implement and learn but also supports reporting of execution and logs. It makes &lt;a href="https://www.lambdatest.com/blog/what-is-parallel-testing-and-why-to-adopt-it/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;parallel testing&lt;/a&gt; such an easy task! Ask any Selenium TestNG user to validate this claim, a majority of them will answer in affirmation. As per the State of open source testing survey, TestNG is one of the best &lt;a href="https://www.lambdatest.com/blog/9-of-the-best-java-testing-frameworks-for-2021/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Java testing frameworks&lt;/a&gt; preferred by developers. Selenium is the only test automation framework that is ahead of TestNG. Close to 50% of the organizations prefer the TestNG framework.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2028%2F0%2At75g_tyhioiG0KQT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2028%2F0%2At75g_tyhioiG0KQT.png" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TestNG&lt;/strong&gt;, where &lt;strong&gt;NG&lt;/strong&gt; stands for &lt;strong&gt;Next Generation&lt;/strong&gt;, is a testing framework developed by Cedric Beust. In this &lt;a href="https://www.lambdatest.com/learning-hub/testng?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium TestNG tutorial&lt;/a&gt;, I will deep dive into how to install TestNG and cover the essentials on writing your first automation script with TestNG for &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; automation testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is TestNG?
&lt;/h2&gt;

&lt;p&gt;TestNG is an open-source advanced testing framework built on the capabilities of JUnit and NUnit. The framework is not only robust but also provides awesome features like &lt;a href="https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;TestNG annotations&lt;/a&gt;, grouping, parameterizations, etc., that help keep tests well-structured and easy to maintain.&lt;/p&gt;

&lt;p&gt;At the time of writing this blog, the latest version of TestNG is 7.5. However, the preferred version is 6.14.3. I will be using the said version for demonstrating the features of TestNG Selenium in this &lt;a href="https://www.lambdatest.com/learning-hub/testng?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium TestNG tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The TestNG framework is designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).&lt;/p&gt;

&lt;p&gt;Salient features of TestNG:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Provides &lt;strong&gt;HTML Reports&lt;/strong&gt; in an efficient easy to read format, thus catering to WebDriver’s limitation to generate such reports&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Support to &lt;strong&gt;Group&lt;/strong&gt; multiple test cases to be executed together whenever that particular group is invoked&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ability to set &lt;strong&gt;Priority&lt;/strong&gt; among test cases which help to decide which case has to be executed first&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provides the ability to execute only the failed cases with the help of &lt;strong&gt;testng-failed.xml&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Support for &lt;a href="https://www.lambdatest.com/cross-browser-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Cross Browser testing&lt;/a&gt;, &lt;strong&gt;Data Parameterization&lt;/strong&gt;, and &lt;strong&gt;Parallel testing&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easy to understand &lt;strong&gt;Annotations&lt;/strong&gt;, which helps control the sequence of execution in automation script without a static main method&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Handles Uncaught Exceptions&lt;/strong&gt; inherently, thus preventing sudden test termination.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;This &lt;a href="https://www.lambdatest.com/blog/cypress-test-automation-framework/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Cypress test automation&lt;/a&gt; tutorial will help you learn the benefits of Cypress automation, and how to install Cypress.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation and Setup of TestNG in Selenium
&lt;/h2&gt;

&lt;p&gt;Having understood the features of TestNG, the next would be to know how to set it up on the system before I deep dive into the practical demonstration. In this tutorial on TestNG in Selenium, I will cover the installation process for 2 of the most used IDEs -Eclipse and IntelliJ.&lt;/p&gt;

&lt;p&gt;Watch this video to learn how TestNG has become one of the most robust test automation frameworks and all you need to know to get started with TestNG in Selenium.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/90F7dePW1vo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  How to install TestNG in Eclipse
&lt;/h2&gt;

&lt;p&gt;To &lt;a href="https://www.lambdatest.com/blog/how-to-install-testng-in-eclipse-step-by-step-guide/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;install TestNG in Eclipse&lt;/a&gt;, download and install Eclipse IDE on your system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Launch &lt;strong&gt;Eclipse&lt;/strong&gt;, click on &lt;strong&gt;Help&lt;/strong&gt;, and select &lt;strong&gt;Eclipse Marketplace&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AHmocWYMwypz0gud1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AHmocWYMwypz0gud1.png" width="800" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Inside the Eclipse Marketplace window, go to the &lt;strong&gt;Search&lt;/strong&gt; tab and find TestNG. If it is already installed, you will see the &lt;strong&gt;Installed&lt;/strong&gt; button, and no need to continue with these steps; otherwise, it would say &lt;strong&gt;Install&lt;/strong&gt;. Click Install in such a case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2380%2F0%2A-uta9bHu5YUjzOit.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2380%2F0%2A-uta9bHu5YUjzOit.png" width="800" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: On the consecutive window, make sure the &lt;strong&gt;TestNG **checkbox is checked and click **Confirm&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2580%2F0%2ADzfOuLof8qzwITB0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2580%2F0%2ADzfOuLof8qzwITB0.png" width="800" height="452"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: It is recommended to restart Eclipse after the installation is completed for changes to take effect correctly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: After restarting, verify the installation by right-clicking on any project and checking if the TestNG menu is visible to create a &lt;strong&gt;TestNG class&lt;/strong&gt; or not.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2212%2F0%2AkRB_I0j4pe1csoEj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2212%2F0%2AkRB_I0j4pe1csoEj.png" width="800" height="1157"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to install TestNG in IntelliJ
&lt;/h2&gt;

&lt;p&gt;In this section of the blog on TestNG in Selenium, I will cover how to install TestNG in IntelliJ. For IntelliJ, we will run tests on Java where we will download the JAR from the Maven Repositories website and add it.&lt;/p&gt;

&lt;p&gt;But before proceeding to add the JAR, make sure you already have IntelliJ installed on the system. If not, you can download IntelliJ by navigating to the JetBrains website.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Visit Maven Repositories, search for &lt;strong&gt;TestNG&lt;/strong&gt; and select the latest most used version (i.e. 7.1.0). In this tutorial, I will be using TestNG version 6.14.3. Hence, the POM file that I will be using for demonstration will be having traces of TestNG 6.14.3.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A41-LVFWgVZesiXJC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A41-LVFWgVZesiXJC.png" width="800" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AAWJXBx92piRv2i9X.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AAWJXBx92piRv2i9X.png" width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Select and download the jar file. We will be adding this jar to IntelliJ.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Al0bEB230PEoho4XY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Al0bEB230PEoho4XY.png" width="800" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Launch IntelliJ, open the &lt;strong&gt;File&lt;/strong&gt; menu, and select &lt;strong&gt;Project Structure&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2132%2F0%2ASepy4--J83PV4G8v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2132%2F0%2ASepy4--J83PV4G8v.png" width="800" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Open the &lt;strong&gt;Modules&lt;/strong&gt; panel, go to the &lt;strong&gt;Dependencies&lt;/strong&gt; tab, click on + sign, and select &lt;strong&gt;JARs or directories&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AKIOYyR3f6KoBulh3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AKIOYyR3f6KoBulh3.png" width="800" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: Navigate to the path where we have the downloaded jar from previous steps and select it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt;: It would appear on the list of dependencies. Just check it and click the Apply button followed by the &lt;strong&gt;OK button&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AcEKow_BJAPkmreAq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AcEKow_BJAPkmreAq.png" width="800" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And with this, we are done with installation on both IntelliJ and Eclipse.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;This &lt;a href="https://www.lambdatest.com/playwright-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Playwright&lt;/a&gt; automation testing tutorial will guide you through the setup of the Playwright framework, which will enable you to write end-to-end tests for your future projects.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Setup in Eclipse
&lt;/h2&gt;

&lt;p&gt;For this TestNG in Selenium tutorial, we would be going ahead with Eclipse IDE. In this section, we will learn &lt;a href="https://www.lambdatest.com/blog/create-testng-project-in-eclipse-run-selenium-test-script/?utm_source=medium&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;how to create a TestNG project in Eclipse&lt;/a&gt;. However, if you are more comfortable using IntelliJ, then please go ahead with it. The steps mentioned would be the same irrespective of the IDE used. So let us create the project structure and add a test class to verify if it is being executed as TestNG Run or not.&lt;/p&gt;

&lt;p&gt;In this entire tutorial, we will be using the same project structure as discussed above and add a new class file each time we want to practice something by executing and analyzing the output.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create a Maven project in Eclipse and name it &lt;strong&gt;testng_selenium&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Update the &lt;strong&gt;pom.xml&lt;/strong&gt; to have &lt;strong&gt;Selenium&lt;/strong&gt; and &lt;strong&gt;TestNG&lt;/strong&gt; dependencies.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;POM&lt;/em&gt;&lt;/strong&gt;, short for Project Object model, is an XML file that makes the basic fundamental unit of Maven. It stores the information about the project like version, description, etc., and also contains configuration details like dependencies, which Maven uses to build the project and make our scripts work.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;project xmlns="[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0)" xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)" xsi:schemaLocation="[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0) [https://maven.apache.org/xsd/maven-4.0.0.xsd](https://maven.apache.org/xsd/maven-4.0.0.xsd)"&amp;gt;
  &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
  &amp;lt;groupId&amp;gt;testng_selenium&amp;lt;/groupId&amp;gt;
  &amp;lt;artifactId&amp;gt;testng_selenium&amp;lt;/artifactId&amp;gt;
  &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;
  &amp;lt;build&amp;gt;
    &amp;lt;sourceDirectory&amp;gt;src&amp;lt;/sourceDirectory&amp;gt;
    &amp;lt;plugins&amp;gt;
      &amp;lt;plugin&amp;gt;
        &amp;lt;artifactId&amp;gt;maven-compiler-plugin&amp;lt;/artifactId&amp;gt;
        &amp;lt;version&amp;gt;3.8.1&amp;lt;/version&amp;gt;
        &amp;lt;configuration&amp;gt;
          &amp;lt;release&amp;gt;16&amp;lt;/release&amp;gt;
        &amp;lt;/configuration&amp;gt;
      &amp;lt;/plugin&amp;gt;
    &amp;lt;/plugins&amp;gt;
  &amp;lt;/build&amp;gt;
   &amp;lt;dependencies&amp;gt;
   &amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;org.seleniumhq.selenium&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;selenium-java&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;3.141.59&amp;lt;/version&amp;gt;
   &amp;lt;/dependency&amp;gt;
   &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.testng&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;testng&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;6.14.3&amp;lt;/version&amp;gt;
            &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
        &amp;lt;/dependency&amp;gt;
  &amp;lt;/dependencies&amp;gt;
&amp;lt;/project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Create a package and name it &lt;strong&gt;test&lt;/strong&gt;. This package will contain all our test class files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AXytyxtMAkHoH7FWX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AXytyxtMAkHoH7FWX.png" width="546" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Inside this package, add a new class &lt;strong&gt;VerifySetup&lt;/strong&gt;, add the following code, and save it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;

import org.testng.annotations.Test;

public class VerifySetup {

[@Test](http://twitter.com/Test)
 public void verifySetup()
 {
  System.out.println("TestNG setup is working fine");
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Now execute this as a TestNG test to verify the setup. For this select the class name, right-click on it, select &lt;strong&gt;Run As&lt;/strong&gt; and then click on &lt;strong&gt;TestNG Test&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_F_Ul-LZ45ge2b3D.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_F_Ul-LZ45ge2b3D.png" width="800" height="576"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Upon successful execution, the output should look like the below. This verifies our TestNG setup and the same step can be done to execute more classes that we add in this tutorial.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2548%2F0%2A7d8nT0VKRXsHFqjt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2548%2F0%2A7d8nT0VKRXsHFqjt.png" width="800" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TestNG Annotations and Attributes
&lt;/h2&gt;

&lt;p&gt;Annotation means a note or a comment in any document that is used to provide it certain meaning. TestNG annotations also serve a similar purpose. These are used to provide meaning to any function in the test script and describe its behavior.&lt;/p&gt;

&lt;p&gt;Annotations in TestNG are some predefined keywords that help to control the execution of the automation script in a proper flow. One must note, any test which is not annotated in TestNG is ignored while running tests. So it is mandatory to add required annotations for the test cases. Here is one example to understand this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;

import org.testng.annotations.Test;

public class UnAnnotatedTestExample {

[@Test](http://twitter.com/Test)
 public void testWithAnnotation() 
 {
  System.out.println("This test is annotated");
 }

public void testWithoutAnnotation() 
 {
  System.out.println("This test is not annotated");
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you can see, in the above example, we have added two test cases, one with &lt;code&gt;@ Test&lt;/code&gt; annotation and one without it. So the expectation here is only the one with annotation is executed and another one is skipped. Let’s execute the class and verify this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2420%2F0%2APIESweT6yxXqptCp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2420%2F0%2APIESweT6yxXqptCp.png" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is clear from the output, that only the case with annotation got executed. One more thing to note here is that the Test run on the output also shows the total number of test cases as 1 since TestNG reads the number of test cases from &lt;code&gt;@ Test&lt;/code&gt; annotation and we had only one such case in the file.&lt;/p&gt;

&lt;p&gt;TestNG annotations also provide relevant information about the method, class, or test suite they are associated with. These are represented by adding @ prefix. Let us have a look at all these annotations one by one, which we will use in this Selenium TestNG tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Annotations
&lt;/h2&gt;

&lt;p&gt;TestNG currently supports ten types of annotations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;@ Test&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the most important annotation as it covers the actual test case logic and executes the automation run. There are some attributes associated with this annotation that helps to serve various use cases. We will learn these attributes in the following sections.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Test](http://twitter.com/Test)
public void testAnnotation()
{
//Write your test case logic here.
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;@BeforeTest&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This annotation is executed before running any test with the &lt;code&gt;@ Test&lt;/code&gt; annotation in a class. One example of using this can be to maximize the browser for test cases in a class.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@BeforeTest](http://twitter.com/BeforeTest)
public void browserSetup()
{
//Code to maximize the browser
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;@AfterTest&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This annotation is executed after all the test cases in a class are executed. One use case of this annotation would be to write the method to compile a test run report after all tests are run.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@AfterTest](http://twitter.com/AfterTest)
public void compileReport()
{
//code to compile a report
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;@BeforeMethod&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This annotation is executed before any and every test method, i.e., before every &lt;code&gt;@ Test&lt;/code&gt; annotation method in the class. For example, we have cases to test some dashboard that comes up after login. So instead of writing login steps in each test case, @BeforeMethod annotation can be used to write those steps once in a method to be called before every test case.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@BeforeMethod](http://twitter.com/BeforeMethod)
public void doLogin()
{
//code to do login
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;@AfterMethod&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This annotation is executed after any and every test method, i.e., after every &lt;code&gt;@ Test&lt;/code&gt; annotation method in the class. In continuation to the above example, let’s say we need to log out after every test case. In such a scenario, this annotation would help.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@AfterMethod](http://twitter.com/AfterMethod)
public void doLogout()
{
//code to do logout
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;@BeforeClass&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This annotation is executed before the first test method with &lt;code&gt;@ Test&lt;/code&gt; annotation in the class is executed. For example, this can be used to navigate to the URL before executing the test.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@BeforeClass](http://twitter.com/BeforeClass)
public void navigateUrl()
{
//code to navigate to URL
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;@AfterClass&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This annotation is executed after the last &lt;code&gt;@ Test&lt;/code&gt; method is executed. The method with this annotation can be used to close the driver after the automation run.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@AfterClass](http://twitter.com/AfterClass)
public void closeDriver()
{
//code to close the driver
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;@BeforeSuite&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This annotation marks the entry point of any automation script execution on TestNG in Selenium. Any method with this annotation is executed before any test in all the classes in that suite. This can be used to do generic setup steps for the execution, like initializing WebDriver for execution.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@BeforeSuite](http://twitter.com/BeforeSuite)
public void initializeDriver()
{
//code to initialize the driver
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;@AfterSuite&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This annotation in TestNG is the last to be executed after all test methods of all the classes within the suite have been executed. A method with this annotation is mostly used for cleanup activities like closing the active driver sessions.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@AfterSuite](http://twitter.com/AfterSuite)
public void cleanUp()
{
//code to close all sessions
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;@BeforeGroups&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TestNG provides the capability to group the tests with similar functionalities in a group with the help of the group attribute of &lt;code&gt;@ Test&lt;/code&gt; annotation. This helps when we want to execute only a particular type of test case. The method with @BeforeGroup annotation is executed before the first test case for the given group is executed. We can mention the group name inside brackets next to the annotation.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@BeforeGroups](http://twitter.com/BeforeGroups)("groupName")
public void startGroupExecution()
{
//code to execute before particular group test cases.
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;@AfterGroups&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This annotation in TestNG is executed after all the test cases of a given group are executed.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@AfterGroups](http://twitter.com/AfterGroups)("groupName")
public void stopGroupExecution()
{
//code to execute after particular group test cases.
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;With this, we have understood all the annotations used in TestNG. These annotations are highly usable as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Very easy to learn as there are no rules on which annotation to use with which method. The tester can use them as per free will and understanding.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ease of parametrization, which helps in creating groups and other tasks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Strongly typed, which saves time by highlighting errors while implementing only.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Saves the time and effort to extend classes to define the order of execution, unlike JUnit&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Watch this video to learn about the TestNG Annotations and how they help provide better structure and readability to the code.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/zI7Xbaj4RIs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Hierarchy of Annotations
&lt;/h2&gt;

&lt;p&gt;TestNG annotations have a predefined hierarchy of execution which helps in executing the source code. These are executed in the below order :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@BeforeSuite](http://twitter.com/BeforeSuite) -&amp;gt; [@BeforeTest](http://twitter.com/BeforeTest) -&amp;gt; [@BeforeClass](http://twitter.com/BeforeClass) -&amp;gt; [@BeforeMethod](http://twitter.com/BeforeMethod) -&amp;gt; [@Test](http://twitter.com/Test) -&amp;gt; [@AfterMethod](http://twitter.com/AfterMethod) -&amp;gt; [@AfterClass](http://twitter.com/AfterClass) -&amp;gt; [@AfterTest](http://twitter.com/AfterTest) -&amp;gt; [@AfterSuite](http://twitter.com/AfterSuite)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let us have a look at the following code example to demonstrate the hierarchy better.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;TestNGAnnotationsHierarchy.java&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class TestNGAnnotationsHierarchy {
 [@Test](http://twitter.com/Test)
 public void testCase1() {
   System.out.println("This is Test Case 1"); 
 }

 [@Test](http://twitter.com/Test)
 public void testCase2() {
   System.out.println("This is Test Case 2"); 
 }

[@BeforeMethod](http://twitter.com/BeforeMethod)
 public void beforeMethod() {
   System.out.println("This will execute before every Test Method");
 }

 [@AfterMethod](http://twitter.com/AfterMethod)
 public void afterMethod() {
   System.out.println("This will execute after every Test Method");
 }

 [@BeforeClass](http://twitter.com/BeforeClass)
 public void beforeClass() {
   System.out.println("This will execute before the Class");
 }

 [@AfterClass](http://twitter.com/AfterClass)
 public void afterClass() {
   System.out.println("This will execute after the Class");
 }

 [@BeforeTest](http://twitter.com/BeforeTest)
 public void beforeTest() {
   System.out.println("This will execute before the First Test");
 }

 [@AfterTest](http://twitter.com/AfterTest)
 public void afterTest() {
   System.out.println("This will execute after the Last Test");
 }

 [@BeforeSuite](http://twitter.com/BeforeSuite)
 public void beforeSuite() {
   System.out.println("This will execute before the Test Suite");
 }

 [@AfterSuite](http://twitter.com/AfterSuite)
 public void afterSuite() {
   System.out.println("This will execute after the Test Suite");
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Upon execution, this is how the output for this code snippet would be where all the methods get executed as per the hierarchy of the annotations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AMhJcJ8jzOoSGhfCI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AMhJcJ8jzOoSGhfCI.png" width="800" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looking at this code example and the output, it is evident that TestNG religiously follows the order of execution of annotations.&lt;/p&gt;

&lt;p&gt;One might wonder when you see this example is that since every annotation has a particular sequence defined, then how do the two &lt;code&gt;@ Test&lt;/code&gt; methods in this example decide which one to execute when. This is something that is handled using &lt;a href="https://www.lambdatest.com/blog/prioritizing-tests-in-testng-with-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;test case priority in Selenium TestNG&lt;/a&gt;. The same will be covered in the upcoming sections of the blog.&lt;/p&gt;

&lt;h2&gt;
  
  
  TestNG Attributes
&lt;/h2&gt;

&lt;p&gt;Just like a method in Java, TestNG annotations have attributes that help in making our tests more defined and provide more information about the test script while performing &lt;a href="https://www.lambdatest.com/selenium-automation-testing-with-testng?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium automation testing with TestNG&lt;/a&gt;. These attributes are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;description&lt;/strong&gt;: It helps to define the purpose of the test&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Test](http://twitter.com/Test)(description = "this is the definition of the test")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;groups&lt;/strong&gt;: This attribute helps to group test cases of the same functionality under a common group by giving it a group name. It is very useful when the user wants to execute cases of a particular group or wants to exclude cases of a particular group from execution.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Test](http://twitter.com/Test)(groups = "DashboardCases")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;dependsOnMethods&lt;/strong&gt;: The test having this attribute is only executed if the dependent test method passes. If it fails or is not executed, then the test with this attribute is skipped from execution.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Test](http://twitter.com/Test)(dependsOnMethods = "LoginFunctionality")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;alwaysRun&lt;/strong&gt;: It ensures that this test method always runs and is not dependent on the result of the methods that it depends upon.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Test](http://twitter.com/Test)(alwaysRun = true)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;dataProvider&lt;/strong&gt;: This attribute is used when we want to run a test case on a particular data set (e.g. running the same test case on different browsers). This attribute provides the data to the test case for which it is used with the help of another method annotated with @DataProvider annotation of TestNG.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Test](http://twitter.com/Test)(dataProvider = "CrossBrowserTesting")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We will learn more about DataProviders in a further section of this blog.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;enabled&lt;/strong&gt;: This attribute is helpful when we want to skip executing a particular test case. It helps to do so by setting the value to false.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://twitter.com/Test" rel="noopener noreferrer"&gt;@Test&lt;/a&gt;(enabled = false)&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;timeOut&lt;/strong&gt;: This attribute is used to define the max execution time for a test case. In other words, the test case should be completed in the given time, which is given in milliseconds, else the test execution would be terminated, and the case would be marked as failed with timeout exception (org.testng.internal.thread.ThreadTimeoutException)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Test](http://twitter.com/Test)(timeOut = 5000)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;invocationCount&lt;/strong&gt;: This attribute is used when we want to execute the test case in a loop for a given number of times.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Test](http://twitter.com/Test)(invocationCount = 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Would execute the test case 3 times&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;invocationTimeOut&lt;/strong&gt;: This attribute is used in combination with the invocationCount attribute. This attribute defines the timeframe within which the test should execute for a given number of times.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; [@Test](http://twitter.com/Test)(invocationCount = 3, invocationTimeOut = 20)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Means execute the case 3 times in 20 milliseconds.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;expectedExceptions&lt;/strong&gt;: This attribute helps to handle the exceptions that the test method is expected to throw. If the test method throws the exception that is set on the attribute, then the test case passes, and for any other unhandled exception, the test method fails.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    [@Test](http://twitter.com/Test)(expectedExceptions = {ArithmeticException.class})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;priority&lt;/strong&gt;: This attribute is used to define the order of execution by assigning priorities to the test cases. A test method with lower priority is always executed first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Test](http://twitter.com/Test)(priority=1), [@Test](http://twitter.com/Test)(priority=2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In this case, the test with priority 1 is executed first, followed by the test with priority 2. Move to the next section to know more about priorities.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider the below code example to understand attributes implementation.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;

import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class TestNGAttributesExample {

[@Test](http://twitter.com/Test)(enabled = false, description = "This test will be skipped as enabled is set to false")
 public void disabledTest() {
  System.out.println("This method will be skipped from the test run using the attribute enabled=false");
 }

[@Test](http://twitter.com/Test)(invocationCount = 5, invocationTimeOut = 20, groups = "testngAttributes")
 public void invocationCountTest() {
  System.out.println("This method will be executed by 5 times");
 }

[@Test](http://twitter.com/Test)(timeOut = 500, groups = "testngAttributes")
 public void baseTest() {
  System.out.println("This is base test method");
 }

[@Test](http://twitter.com/Test)(dependsOnMethods = "baseTest", groups = "testngAttributes")
 public void firstDependentTest() {
  System.out.println("This is dependent method 1 and will only execute if baseTest passes");
 }

[@Test](http://twitter.com/Test)(alwaysRun = true, dependsOnMethods = "baseTest", groups = "testngAttributes")
 public void secondDependentTest() {
  System.out.println(
    "This is dependent method 2 and will execute irrespective of baseTest result because of alwaysRun");
 }

[@BeforeGroups](http://twitter.com/BeforeGroups)("testngAttributes")
 public void beforeGroupMethod() {
  System.out.println("This method is executed before testngAttributes group test cases");
 }

[@AfterGroups](http://twitter.com/AfterGroups)("testngAttributes")
 public void afterGroupMethod() {
  System.out.println("This method is executed after testngAttributes group test cases");
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Running the above code would give the following output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2936%2F0%2Ab0ZHZXKpk8ug-K0V.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2936%2F0%2Ab0ZHZXKpk8ug-K0V.png" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TestNG Priorities
&lt;/h2&gt;

&lt;p&gt;As mentioned before in this TestNG in Selenium tutorial, test case execution in TestNG is governed by the priority of test cases. But what if no priority is assigned to test cases. Then what decides the order of execution.&lt;/p&gt;

&lt;p&gt;If there are multiple test cases, then TestNG executes them in alphabetical order. So for the following code, aTest is executed before bTest.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Test](http://twitter.com/Test)
public void aTest()
{}

[@Test](http://twitter.com/Test) 
public void bTest()
{}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, let us assume a scenario where we want to execute bTest first. In such scenarios, a priority attribute is used. We can achieve this by assigning lower priority to the test we want to execute first. Priorities in TestNG can be assigned starting from 0, where lower priority cases are executed first.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Test](http://twitter.com/Test)(priority = 2)
public void aTest()
{}

[@Test](http://twitter.com/Test)(priority = 1)
public void bTest()
{}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this case, bTest will be executed before aTest.&lt;/p&gt;

&lt;p&gt;The next question that comes up is, what if the same priority is assigned to both test cases.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Test](http://twitter.com/Test)(priority = 1)
public void aTest()
{}

[@Test](http://twitter.com/Test)(priority = 1)
public void bTest()
{}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In such a case, TestNG runs the cases in alphabetical order, i.e., aTest will be executed before bTest.&lt;/p&gt;

&lt;p&gt;Another scenario could be that the test class file is a combination of test cases with and without priority like below&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;

import org.testng.annotations.Test;

public class TestNGPrioritiesExample {
 [@Test](http://twitter.com/Test)(priority = 1)
 public void aTest() {
  System.out.println("This is test a.");
 }

[@Test](http://twitter.com/Test)(priority = 2)
 public void bTest() {
  System.out.println("This is test b.");
 }

[@Test](http://twitter.com/Test)
 public void cTest() {
  System.out.println("This is test c.");
 }

[@Test](http://twitter.com/Test)(priority = 1)
 public void dTest() {
  System.out.println("This is test d.");
 }

[@Test](http://twitter.com/Test)
 public void eTest() {
  System.out.println("This is test e.");
 }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In such a case, TestNG, by default, assigns a priority = 0 to such cases that do not have the attribute. Those are executed first in alphabetical order, followed by other cases as per priorities logic. So the output for the above code would be&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A6UXUwNC6UwkTculo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A6UXUwNC6UwkTculo.png" width="800" height="304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is all about priorities, and these would be highly beneficial to use in any automation script while working with TestNG in Selenium. To learn more about the test case priority, you can go through our earlier blog on &lt;a href="https://www.lambdatest.com/blog/prioritizing-tests-in-testng-with-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;how to set test case priority in TestNG with Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  DataProviders in TestNG
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/how-to-use-dataproviders-in-testng-with-examples/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;DataProviders in TestNG&lt;/a&gt; is a part of the inbuilt TestNG data-driven testing approach and is used to pass different values to the same TestNG test class through &lt;strong&gt;@DataProvider&lt;/strong&gt; annotation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TestNG DataProvider sample:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@DataProvider](http://twitter.com/DataProvider)(name = "dataProvider_name")
public Object[][] dataProviderMethod() {
return new Object[][] {&amp;lt;values&amp;gt;};
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This annotation consists of only one attribute- name and it is always of type &lt;strong&gt;String&lt;/strong&gt;. If the name is not provided then the name of the method becomes the data provider name by default, &lt;strong&gt;dataProviderMethod&lt;/strong&gt; in this case.&lt;/p&gt;

&lt;p&gt;DataProvider always returns a 2-D list of objects, on which the test method performs the execution using a data-driven approach on each passed argument.&lt;/p&gt;

&lt;p&gt;Let’s take an example of a simple cross browser code where we want to navigate to a website on both Chrome and Firefox browsers.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderExample {

[@DataProvider](http://twitter.com/DataProvider)(name = "browserName")
 public Object[][] browserName() 
 {
  return new Object[][] { { "chrome" }, { "firefox" } };
 }

[@Test](http://twitter.com/Test)(dataProvider = "browserName")
 public void crossBrowserTest(String browser) 
 {
  WebDriver driver = null;

  System.out.println("Launching browser : " + browser);

  switch(browser)
  {
   case "chrome":
    System.setProperty("webdriver.chrome.driver", "&amp;lt;Path_to_your_chrome_driver&amp;gt;");
    driver = new ChromeDriver();
    break;
   case "firefox":
    System.setProperty("webdriver.firefox.driver", "&amp;lt;Path_to_your_firefox_driver&amp;gt;");
    driver = new FirefoxDriver();
    break;
   default:
    System.out.println("Invalid browser name passed");
  }

  driver.navigate().to("[http://www.lambdatest.com/](http://www.lambdatest.com/)");
  System.out.println("Navigated Successfully to the website");
 }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above code, we have implemented DataProvider having a name as &lt;strong&gt;browserName&lt;/strong&gt;, which passes 2 values &lt;strong&gt;chrome&lt;/strong&gt; and &lt;strong&gt;firefox&lt;/strong&gt;. These values are passed to the test method &lt;strong&gt;crossBrowserTest&lt;/strong&gt; using the parameter value and the test case is executed one by one on both browsers.&lt;/p&gt;

&lt;p&gt;This is a classic example of cross-browser testing code implementation where we want to execute the same script on multiple browsers.&lt;/p&gt;

&lt;p&gt;Executing the above code would give an output like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2AlpVHfBZNgMBx1MqP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2AlpVHfBZNgMBx1MqP.png" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One thing to note here is that instead of having only 1 &lt;code&gt;@ Test&lt;/code&gt; annotated test case, the execution shows two test cases are executed. This is because we passed 2 different values to the test case and hence TestNG treats it as 2 separate test cases since the test data is different.&lt;/p&gt;

&lt;h2&gt;
  
  
  TestNG Assertions
&lt;/h2&gt;

&lt;p&gt;Assertions provide the means to verify if the actual and expected results of the test case execution are a match or not thus deciding the outcome of the test case whether it is a pass or fail. These are widely used to validate the results in any script of TestNG in Selenium.&lt;/p&gt;

&lt;p&gt;An example of &lt;a href="https://www.lambdatest.com/blog/asserts-in-testng/?utm_source=devto[](url)&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;TestNG Assertions&lt;/a&gt; would be to validate the username on the dashboard after a user logs in to a website.&lt;/p&gt;

&lt;p&gt;The syntax for TestNG Assert:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Assert.Method(actual, expected, message)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;actual&lt;/strong&gt;: the actual value which is fetched from the test case as a part of the execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;expected&lt;/strong&gt;: the expected value with which we want to compare the actual value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;message&lt;/strong&gt;: string message to be displayed in case of assert failure.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Want to perform browser &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;test automation cloud&lt;/a&gt; on the most powerful cloud infrastructure? Leverage LambdaTest automation testing for faster, reliable, and scalable experience on cloud.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of TestNG Asserts
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Hard Assert&lt;/strong&gt;: These are the default type of asserts in TestNG. These assert stop the test case execution when any assert fails and subsequent statements are not executed/validated. These are used in cases when subsequent steps in the test cases are not to be executed when a particular step assertion fails.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Soft Assert&lt;/strong&gt;: These are the opposite of Hard Asserts. In the case of soft assert, the test case execution of subsequent steps continues even if some assert fails in between. These are used when we want to continue the execution and see the results for all steps even if a few fails in between. To use soft asserts, &lt;strong&gt;org.testng.asserts.SoftAssert&lt;/strong&gt; package needs to be included.&lt;/p&gt;

&lt;p&gt;The most commonly used TestNG asserts are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;assertTrue&lt;/strong&gt;&lt;br&gt;
This assertion verifies whether the given condition is true. If false, it will fail the test case.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Assert.assertTrue(condition, message);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;assertFalse&lt;/strong&gt;&lt;br&gt;
This assertion verifies whether the given condition is false. If true, it will fail the test case.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Assert.assertFalse(condition, message);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;assertEquals&lt;/strong&gt;&lt;br&gt;
This assertion verifies if the actual and expected are a match. If they match, it passes the test case otherwise fails.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Assert.assertEquals(actual, expected, message);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;assertNotEquals&lt;/strong&gt;&lt;br&gt;
This assertion verifies if the actual and expected are not a match. If they match, it fails the test case otherwise passes.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Assert.assertNotEquals(actual, expected, message);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  TestNG Selenium Demonstration
&lt;/h2&gt;

&lt;p&gt;By now, this TestNG in Selenium tutorial has covered almost all the main features of the TestNG framework that you would need to write your first Java automation script. In the following code example, we will combine all the annotations, attributes, priorities, data providers, and assertions that we have learned so far.&lt;/p&gt;

&lt;p&gt;The following test scenario will be executed by this code (To print and verify the sum of 2 numbers)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a dataProvider that passes values as &lt;strong&gt;firstValue, secondValue, expectedSum&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Navigate to &lt;a href="https://www.lambdatest.com/selenium-playground/simple-form-demo?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;https://www.lambdatest.com/selenium-playground/simple-form-demo&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run Test 1 with the first set of values, {1, 4, 5}. This test should pass as the expected sum is correct.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Clear the old values from the form.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run Test 2 with the second set of values, {2, 4, 7}. This test should fail as the expected sum is not correct. We are intentionally passing incorrect expected values to see the differences in output and test reports for passed and failed cases. Always remember that ideally the expected should always be the correct value. This is done just for the tutorial purpose here.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Execute the code and analyze the reports.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In addition to this test case, we will add two more test cases with priority to showcase the usage, along with one of them having invocationCount attribute to show case getting executed multiple times as per priority.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.*;
import org.testng.Assert;
import org.testng.annotations.*;

public class SeleniumTestNGExample {

public RemoteWebDriver driver = null;
 String username = "&amp;lt;lambdatest_username&amp;gt;";
 String accessKey = "&amp;lt;lambdatest_accesskey&amp;gt;";

[@BeforeSuite](http://twitter.com/BeforeSuite)
 public void setUp() {
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("browserName", "Chrome");
  capabilities.setCapability("version", "92.0");
  capabilities.setCapability("platform", "Windows 10");
  capabilities.setCapability("resolution", "1024x768");
  capabilities.setCapability("build", "TestNG Selenium Tutorial");
  capabilities.setCapability("name", "TestNG Selenium JAVA");

try {
   driver = new RemoteWebDriver(
     new URL("https://" + username + ":" + accessKey + "[@hub](http://twitter.com/hub).lambdatest.com/wd/hub"), capabilities);
  } catch (MalformedURLException e) {
   System.out.println("Invalid grid URL");
  }
  System.out.println("The driver setup process is completed using BeforeSuite");
 }

[@BeforeTest](http://twitter.com/BeforeTest)
 public void browserProfile() {
  driver.manage().window().maximize();
  System.out.println("The browser profile is updated using BeforeTest");

}

[@BeforeClass](http://twitter.com/BeforeClass)
 public void navigateToUrl() {
  driver.get("[https://www.lambdatest.com/selenium-playground/simple-form-demo](https://www.lambdatest.com/selenium-playground/simple-form-demo)");
  System.out.println("Navigated to URL using BeforeClass");
 }

[@Test](http://twitter.com/Test)(description = "Test case with priority", priority = 1)
 public void testPriotity()
 {
  System.out.println("This case has priority 1");
 }

 [@Test](http://twitter.com/Test)(description = "Test case with priority and invocation count", priority = 2, invocationCount = 2)
 public void testInvocationCount()
 {
  System.out.println("This case has priority 2 and invocation count");
 }

[@DataProvider](http://twitter.com/DataProvider)(name = "SanityTestData")
 public Object[][] sanityTestDataProvider() {
  String[][] testData = { { "1", "4", "5" }, { "2", "4", "7" } };
  return testData;
 }

[@Test](http://twitter.com/Test)(dataProvider = "SanityTestData", description = "Test case with group and data provider but without priority
", alwaysRun = true, groups = { "sanity" })
 public void testSumOfTwoValues(String firstValue, String secondValue, String expectedSum) {
  // to enter data and submit
  driver.findElement(By.id("sum1")).sendKeys(firstValue);
  driver.findElement(By.id("sum2")).sendKeys(secondValue);
  driver.findElement(By.xpath("//button[text()='Get values']")).click();

// to fetch actual result
  String actualSum = driver.findElement(By.id("addmessage")).getText();

// to assert actual and expected result
  Assert.assertEquals(actualSum, expectedSum,
    "Expected and actual results do not match. Expected : " + expectedSum + " and Actual : " + actualSum);

}

[@AfterMethod](http://twitter.com/AfterMethod)()
 public void clearOldData() {
  driver.findElement(By.id("sum1")).clear();
  driver.findElement(By.id("sum2")).clear();
 }

[@AfterClass](http://twitter.com/AfterClass)
 public void closeDriver() {
  driver.quit();
  System.out.println("Quit the driver using AfterClass");
 }

[@AfterTest](http://twitter.com/AfterTest)
 public void testReporting() {
  System.out.println("Test Report generated using AfterTest");
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;You can see that this code uses a number of annotations, dataProviders, assertions, priorities and attributes which we have learnt and gives an idea of how these can be used together while working with automation scripts on TestNG in Selenium to use the features at full extent.&lt;/p&gt;

&lt;p&gt;In this TestNG in Selenium tutorial, I have created an instance of &lt;a href="https://www.lambdatest.com/blog/selenium-remotewebdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium RemoteWebDriver&lt;/a&gt; as the TestNG framework is best utilized with a Cloud &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; Grid. The reason being, along with the &lt;a href="https://www.lambdatest.com/blog/why-selenium-grid-is-ideal-for-automated-browser-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;advantages of Selenium Grid&lt;/a&gt; like executing multiple cases together across a variety of browsers and OS, a Cloud Grid also offers the speed and scalability which makes executions faster and more reliable with improved debugging and troubleshooting support.&lt;/p&gt;

&lt;p&gt;So, in this code example, we will be executing our code on LambdaTest, which is one such Selenium Cloud Grid for performing &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;cross browser testing&lt;/a&gt; at scale. It helps to execute testing across 3000+ real &lt;a href="https://www.lambdatest.com/virtual-browsers" rel="noopener noreferrer"&gt;online browsers&lt;/a&gt; and OS without the hassle of maintaining in-house Selenium Grid. &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; testing tools like LambdaTest provides interactive reporting dashboards for results analysis, a great variety of logs, and integrations with &lt;a href="https://www.lambdatest.com/blog/31-best-ci-cd-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;top CI/CD tools&lt;/a&gt;, project management tools, etc.&lt;/p&gt;

&lt;p&gt;Next, we add the LambdaTest username and access key from the &lt;a href="https://accounts.lambdatest.com/detail/profile?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest profile section&lt;/a&gt; to be used to run the script.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AEwPVcf71_BGH7UTd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AEwPVcf71_BGH7UTd.png" width="800" height="306"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Inside the &lt;strong&gt;setUp()&lt;/strong&gt; method, browser capabilities are being set, and a remote grid is launched, which is to be used for executing the run.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2888%2F0%2AKzFd1Kf_97_ZPNoq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2888%2F0%2AKzFd1Kf_97_ZPNoq.png" width="800" height="263"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;browserProfile()&lt;/strong&gt; method will help to execute the step to maximize the browser window once it is launched.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3136%2F0%2ASq-dTjErXmyDq69d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3136%2F0%2ASq-dTjErXmyDq69d.png" width="800" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have used the &lt;strong&gt;navigateToUrl()&lt;/strong&gt; method to open the website on which the test would be executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AB1AhAnDdmeonPnXJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AB1AhAnDdmeonPnXJ.png" width="800" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have added two test cases that will run on local only to showcase the usage of priority and invocationCount usage, namely &lt;strong&gt;testPriotity()&lt;/strong&gt; and &lt;strong&gt;testInvocationCount()&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AcZpcaq1N_c-j8bZg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AcZpcaq1N_c-j8bZg.png" width="800" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sanityTestDataProvider()&lt;/strong&gt; provides two sets of test data on which the test method would be executed of which one should pass and the other should fail, as already stated above.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A3ALJ-Wtik9-x_z3a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A3ALJ-Wtik9-x_z3a.png" width="800" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;testSumOfTwoValues()&lt;/strong&gt; is the actual test method where we have added the steps to enter the values and fetch the results to be compared using assertion. This method is executed once for each type of test data and helps to decide whether it is a pass or fail by means of assert.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2612%2F0%2A87O357C1SNlT7uUc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2612%2F0%2A87O357C1SNlT7uUc.png" width="800" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next to fetch the result of execution, we use the &lt;strong&gt;getText()&lt;/strong&gt; method with the identifier and store it as a String. Post this, we add Assertion to help validate the result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AqsYY6vd2g9oeNY1i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AqsYY6vd2g9oeNY1i.png" width="800" height="119"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this code, we have implemented assertEquals which will compare the values and print a message if there is a mismatch.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2024%2F0%2AfkcLmzGHku_h7n44.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2024%2F0%2AfkcLmzGHku_h7n44.png" width="800" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;clearOldData()&lt;/strong&gt; function helps to clear the data filled in the input text boxes as a part of previous test execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2424%2F0%2AQQ3MoXFD1NOrWhkN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2424%2F0%2AQQ3MoXFD1NOrWhkN.png" width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After every execution, it is a good practice to close any driver instances that we have created during the course of execution. The same has been implemented by the &lt;strong&gt;closeDriver()&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2580%2F0%2A36NY8J2L9nG-TIhn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2580%2F0%2A36NY8J2L9nG-TIhn.png" width="800" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to signify the completion of the executed test run and provide the tester with a message that the test report is ready to be analyzed, we have used the &lt;strong&gt;testReporting()&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;The two most important parts of the sample code are the dataProvider method and the main test method.&lt;/p&gt;

&lt;p&gt;The above helps to find the web elements and enter data in the input boxes and then it by clicking the button. To fetch web element locators, we navigate to the page and right-click, and then do Inspect to find the locators as desired by any strategy like id, XPath, CSS, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AKu2g8TkauMV5Ao36.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AKu2g8TkauMV5Ao36.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This Selenium testing tutorial for beginners will help you interact with web elements in &lt;a href="https://www.lambdatest.com/blog/selenium-webdriver-tutorial-with-examples/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium WebDriver&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/VeV_sup5S8E"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You can follow the &lt;a href="https://www.youtube.com/channel/UCCymWVaTozpEng_ep0mdUyw?sub_confirmation=1?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest YouTube Channel&lt;/a&gt; and stay updated with the latest tutorials around &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; testing, &lt;a href="https://www.lambdatest.com/cypress-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Cypress testing&lt;/a&gt;, CI/CD, and more.&lt;/p&gt;

&lt;p&gt;Having understood the sample code, now execute the class as &lt;strong&gt;TestNG Test&lt;/strong&gt; to get output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A5Cv0lMsGAXyImQSU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A5Cv0lMsGAXyImQSU.png" width="800" height="699"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this screenshot, we can see the test case gets executed as per priority, and the one with invocation count as 2 gets executed two times.&lt;/p&gt;

&lt;p&gt;You can see that &lt;strong&gt;Default test&lt;/strong&gt; numbers show total &lt;strong&gt;Tests run: 3&lt;/strong&gt; since we have three &lt;strong&gt;&lt;code&gt;@ Test&lt;/code&gt;&lt;/strong&gt; annotated test cases in the class of which 1 has failures and hence &lt;strong&gt;Failures: 1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;However, the &lt;strong&gt;Default suite&lt;/strong&gt; section shows the actual number of test cases along with the actual passed and failed number of cases.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Total tests run: 5 *&lt;/em&gt;-&amp;gt; testPriotity() executed 1 time + testInvocationCount() executed 2 times + testSumOfTwoValues() executed once for each data set (total 2 times) -&amp;gt; 1 + 2 + 2 = 5&lt;/p&gt;

&lt;p&gt;For the failed test case, the log shows the message we added on the assert to appear in case of failure: &lt;strong&gt;Expected and actual results do not match. Expected: 7 and Actual: 6&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Also, we can log in to the &lt;a href="https://accounts.lambdatest.com/dashboard?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Dashboard&lt;/a&gt; to check the execution results for the automation script we have executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AWUSr7xxV4JbTZ9UU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AWUSr7xxV4JbTZ9UU.png" width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see more detailed logs and reporting for this run by clicking on the recent test run and navigating to the &lt;a href="https://automation.lambdatest.com/logs?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Automation Logs&lt;/a&gt; section, as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AKBK0IeWLbPsSrS8g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AKBK0IeWLbPsSrS8g.png" width="800" height="458"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In addition to the Automation Logs and related info with it, the LambdaTest Dashboard now provides users with more detailed views and data about test case execution and associated trends in graphical representation by means of &lt;a href="https://analytics.lambdatest.com/test-overview?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Analytics Dashboard&lt;/a&gt; available under the Automation Tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AbuUUi1sIly84yHyG.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AbuUUi1sIly84yHyG.gif" width="600" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Analytics also provides you the ability to analyze the test trends and resource utilization by means of multiple filters like users, OS, browsers, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AwGqZXUVRAv-ub3hp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AwGqZXUVRAv-ub3hp.png" width="800" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TestNG Reports Generation in Selenium
&lt;/h2&gt;

&lt;p&gt;The same results can be analyzed in a more formatted way with the help of HTML reports generated by TestNG. To access the same, refresh your project by right-clicking on the project name. This is advised to update the report to the latest run.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A-r-JEPcQCKdfdLrK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A-r-JEPcQCKdfdLrK.png" width="800" height="711"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, navigate to the &lt;strong&gt;test-output&lt;/strong&gt; folder and from it open the &lt;strong&gt;index.html&lt;/strong&gt; file. This is the default format report for the TestNG run.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Aur-YLDMF6xu7tWiq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Aur-YLDMF6xu7tWiq.png" width="696" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you select the &lt;strong&gt;test&lt;/strong&gt; from the Info section, it shows the number of classes executed and the number of failed tests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AjAFuvTX2N1OQWaMB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AjAFuvTX2N1OQWaMB.png" width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Selecting the group section shows the name of the &lt;strong&gt;group&lt;/strong&gt; and test case methods executed as a part of it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Aw_pFkHXAeq9Ri4v0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Aw_pFkHXAeq9Ri4v0.png" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chronological View&lt;/strong&gt; shows the order in which all the methods in the class were executed, along with the time taken by each function on each step.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AyiNDWx8VA0XOUqvu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AyiNDWx8VA0XOUqvu.png" width="800" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Last, and most important is the &lt;strong&gt;Results&lt;/strong&gt; section, which shows the complete detail of the executed cases with proper exception/asserts failure in case of failed test cases. We can hide/show only passed or only failed test cases as well using this section as per the requirement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A9bEsYyYOSb-q69YE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A9bEsYyYOSb-q69YE.png" width="800" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A-vwgpzf7Fp1B3CB8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A-vwgpzf7Fp1B3CB8.png" width="800" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are a &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;test automation&lt;/a&gt; expert and want to demonstrate your proficiency in the TestNG framework, you can earn this TestNG certification offered by LambdaTest for FREE&lt;/p&gt;

&lt;p&gt;With TestNG certification, you can challenge your skills in performing automated testing with TestNG and take your career to the next level.&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the TestNG certification from LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/dzXX2hJhuCY"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With this Selenium TestNG tutorial, we can easily conclude that using TestNG in Selenium makes the tests more structured and easy to maintain. Moreover, with the support of its additional features like reporting, dataProviders, more robust annotations, etc., over its predecessors, TestNG makes the automation testing experience user-friendly. Having said that, it is time for you to get started with your first TestNG in Selenium automation project, and remember to use the annotations wisely as they are to make life easier.&lt;/p&gt;

&lt;p&gt;Happy Testing!!&lt;/p&gt;

</description>
      <category>selenium</category>
      <category>tutorial</category>
      <category>testing</category>
    </item>
    <item>
      <title>How To Handle Modal Dialog Box In Selenium WebDriver Java</title>
      <dc:creator>Vipulgupta9090</dc:creator>
      <pubDate>Thu, 08 Sep 2022 10:21:07 +0000</pubDate>
      <link>https://dev.to/testmuai/how-to-handle-modal-dialog-box-in-selenium-webdriver-java-3gke</link>
      <guid>https://dev.to/testmuai/how-to-handle-modal-dialog-box-in-selenium-webdriver-java-3gke</guid>
      <description>&lt;p&gt;Websites nowadays are frequently implementing the usage of pop-ups, alerts, or modal dialog boxes for various use cases like accepting cookies, asking for permissions, entering data, warning alerts, etc. Although there are a few valid use cases for these, unnecessary popups might look annoying and need to be handled.&lt;/p&gt;

&lt;p&gt;You must have encountered these in your day-to-day web interactions on different websites like Amazon or Walmart on the first visit prompting for SignUp, on various food websites like McD, Swiggy to provide the location, or a payment gateway modal to complete payment.&lt;/p&gt;

&lt;p&gt;With this increased usage, it is now expected of an automation tester that they are aware of their handling via automation. The main reason is that most of these popups, which at times are multiple, appear on the homepage for the website under test and need to be handled/closed till the parent page is accessible to interact with and the actual test flow can begin.&lt;/p&gt;

&lt;p&gt;So if you have not got a chance to work on these or automate them so far, this blog is the right one for you. In this blog, we will learn how to handle modal dialog box in &lt;a href="https://www.lambdatest.com/blog/selenium-webdriver-tutorial-with-examples/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium WebDriver&lt;/a&gt; Java with practical examples.&lt;/p&gt;

&lt;p&gt;This blog would cover handling Bootstrap dialog boxes, pop-up windows/tabs, and alerts. I will be starting with the basics of pop-ups, post which I will demonstrate how to handle modal dialog box in Selenium WebDriver Java. So let’s get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to handle Modal Dialog Box in Selenium WebDriver Java?
&lt;/h2&gt;

&lt;p&gt;In this &lt;a href="https://www.lambdatest.com/learning-hub/selenium-java?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium Java tutorial&lt;/a&gt;, we will learn what is a modal dialog box and demonstrate the handling of a modal dialog box in &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; Java.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Modal Dialog Box?
&lt;/h2&gt;

&lt;p&gt;A Modal Dialog Box (also referred to as Bootstrap Modal Window) is built in Bootstrap Framework, due to which it gets displayed on top of your current page. Due to this, modal boxes need to be interacted with first before moving to the current webpage.&lt;/p&gt;

&lt;p&gt;For starters, The &lt;strong&gt;Bootstrap Framework&lt;/strong&gt; is a very popular, free, and open-source &lt;a href="https://www.lambdatest.com/blog/best-css-frameworks-2021/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;CSS framework&lt;/a&gt; that is highly used in web page development, especially for responsive and &lt;a href="https://www.lambdatest.com/blog/mobile-first-web-design/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;mobile-first web pages&lt;/a&gt;. It contains HTML, CSS, and JavaScript. To learn more about the Bootstrap framework, you can deep dive into &lt;a href="https://www.lambdatest.com/blog/css-grid-vs-bootstrap/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;CSS Grid vs. Bootstrap&lt;/a&gt; comparison.&lt;/p&gt;

&lt;p&gt;A modal dialog box written with Bootstrap is formed with three key components, namely,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Trigger element&lt;/strong&gt;, which triggers the modal window to be shown on the webpage. It can be any link on the webpage or any button, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modal&lt;/strong&gt;, which is like a holding area for the actual modal window code that will come up when the trigger element is interacted with.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modal content&lt;/strong&gt;, is the actual code and the content with which the window is formed and displayed as per the implementation.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So while automating any modal window, we first fetch the modal using our web driver and then identify the modal content inside it to perform actions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do you wish to perform browser &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automated testing&lt;/a&gt; on the most powerful cloud infrastructure. Leverage LambdaTest automation testing now.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Handling Modal Dialog Box in Selenium WebDriver Java
&lt;/h2&gt;

&lt;p&gt;Having developed a basic understanding of the modal dialog box, it’s time to implement it and see how it works in real-time.&lt;/p&gt;

&lt;p&gt;Please note that for all the examples in this blog, we will be using this same project setup for all the examples in this blog and keep adding more test class files as we proceed. So do the setup carefully on your machine for implementation purposes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;. Choose the IDE on which you want to do the setup. In this blog, Eclipse IDE is being used. You can follow the same steps if any other IDE has opted. In case you are using IntelliJ, you can go through the article on &lt;a href="https://www.lambdatest.com/blog/build-and-execute-selenium-projects/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;building and executing Selenium Projects&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;. Create a Maven Project in Eclipse and name it as &lt;strong&gt;Dialog_Boxes_With_Selenium_Java&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;. Update the &lt;strong&gt;pom.xml&lt;/strong&gt; file to have &lt;strong&gt;Selenium&lt;/strong&gt; (as it will be a web automation project using Selenium WebDriver) and &lt;strong&gt;TestNG&lt;/strong&gt; (to create test flow and execute test cases using Annotations) dependencies.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;project xmlns="[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0)" xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)" xsi:schemaLocation="[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0) [https://maven.apache.org/xsd/maven-4.0.0.xsd](https://maven.apache.org/xsd/maven-4.0.0.xsd)"&amp;gt;
  &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
  &amp;lt;groupId&amp;gt;Dialog_Boxes_With_Selenium_Java&amp;lt;/groupId&amp;gt;
  &amp;lt;artifactId&amp;gt;Dialog_Boxes_With_Selenium_Java&amp;lt;/artifactId&amp;gt;
  &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;
  &amp;lt;build&amp;gt;
 &amp;lt;sourceDirectory&amp;gt;src&amp;lt;/sourceDirectory&amp;gt;
 &amp;lt;plugins&amp;gt;
   &amp;lt;plugin&amp;gt;
     &amp;lt;artifactId&amp;gt;maven-compiler-plugin&amp;lt;/artifactId&amp;gt;
     &amp;lt;version&amp;gt;3.8.1&amp;lt;/version&amp;gt;
     &amp;lt;configuration&amp;gt;
       &amp;lt;release&amp;gt;16&amp;lt;/release&amp;gt;
     &amp;lt;/configuration&amp;gt;
   &amp;lt;/plugin&amp;gt;
 &amp;lt;/plugins&amp;gt;
  &amp;lt;/build&amp;gt;
  &amp;lt;dependencies&amp;gt;
     &amp;lt;dependency&amp;gt;
       &amp;lt;groupId&amp;gt;org.seleniumhq.selenium&amp;lt;/groupId&amp;gt;
       &amp;lt;artifactId&amp;gt;selenium-java&amp;lt;/artifactId&amp;gt;
       &amp;lt;version&amp;gt;3.141.59&amp;lt;/version&amp;gt;
     &amp;lt;/dependency&amp;gt;
     &amp;lt;dependency&amp;gt;
       &amp;lt;groupId&amp;gt;org.testng&amp;lt;/groupId&amp;gt;
       &amp;lt;artifactId&amp;gt;testng&amp;lt;/artifactId&amp;gt;
       &amp;lt;version&amp;gt;7.5&amp;lt;/version&amp;gt;
       &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
     &amp;lt;/dependency&amp;gt;
  &amp;lt;/dependencies&amp;gt;
&amp;lt;/project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;. Now inside the &lt;strong&gt;src&lt;/strong&gt; package, add a package and name it as &lt;strong&gt;test&lt;/strong&gt;. This will contain all the test classes which will be created in this blog.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;. Inside the &lt;strong&gt;test&lt;/strong&gt; package add a Java class file and name it as &lt;strong&gt;BaseClass.java&lt;/strong&gt;. This file will have the basic driver functions like, initialize driver, quit driver, etc.&lt;/p&gt;

&lt;p&gt;In this blog on how to handle modal dialog box in Selenium WebDriver Java, we will be using the &lt;a href="https://www.lambdatest.com/testng-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;TestNG testing&lt;/a&gt; framework for test execution. Reason being, it is widely supported with &lt;a href="https://www.lambdatest.com/selenium-java-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium Java testing&lt;/a&gt; and provides an easy way to write and manage test automation scripts with the usage of annotations.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class BaseClass {

  public RemoteWebDriver driver = null;
  String username = "&amp;lt;lambdatest_username&amp;gt;";
  String accessKey = "&amp;lt;lambdatest_accesskey&amp;gt;";

  [@BeforeMethod](http://twitter.com/BeforeMethod)
  public void setUp() {
       DesiredCapabilities capabilities = new DesiredCapabilities();
       capabilities.setCapability("browserName", "Chrome");
       capabilities.setCapability("version", "92.0");
       capabilities.setCapability("platform", "Windows 10");
       capabilities.setCapability("resolution", "1024x768");
       capabilities.setCapability("build", "DialogBoxes And Pop-ups Selenium JAVA");
       capabilities.setCapability("name", "DialogBoxes And Pop-ups Selenium JAVA");

       try {
            driver = new RemoteWebDriver(
                       new URL("https://" + username + ":" + accessKey + "[@hub](http://twitter.com/hub).lambdatest.com/wd/hub"), capabilities);
       } catch (MalformedURLException e) {
            System.out.println("Invalid grid URL");
       }
  }

  [@AfterMethod](http://twitter.com/AfterMethod)
  public void closeDriver() {
       driver.quit();
  }
}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We have created &lt;strong&gt;BaseClass.java&lt;/strong&gt; as the first file, which will have the code to initiate the browser session for &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium automation&lt;/a&gt; and handle the code for browser setup and closing it after test completion. This will be inherited to our test classes, thus preventing redundancy for such code and providing an abstraction of driver code from every other class user.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;. Inside the &lt;strong&gt;BaseClass&lt;/strong&gt;, we will be adding two functions, one to initialize the driver and create an instance of it and another to close it once the test cases are executed.&lt;/p&gt;

&lt;p&gt;We will be executing the code on a &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium Cloud Grid&lt;/a&gt; using an instance of &lt;a href="https://www.lambdatest.com/blog/selenium-remotewebdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium RemoteWebDriver&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The reason is, along with the advantages of Selenium Grid, like executing multiple cases across various browsers and OS, a Cloud Grid also offers speed and scalability, which makes executions faster and more reliable.&lt;/p&gt;

&lt;p&gt;An example of one such Selenium Cloud Grid is LambdaTest, which facilitates &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;cross browser testing&lt;/a&gt; across an &lt;a href="https://www.lambdatest.com/online-browser-farm?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;online browser farm&lt;/a&gt; of 3000+ real browsers and operating systems on the cloud. &lt;a href="https://www.lambdatest.com/automation-testing-tool?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Automation testing tools&lt;/a&gt; like LambdaTest provide an interactive reporting dashboard for results analysis along with all the &lt;a href="https://www.lambdatest.com/blog/benefits-of-cloud-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;benefits of cloud testing&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here’s a glimpse of LambdaTest cloud Selenium Grid:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/WZlsHlReRww"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You can also Subscribe to the &lt;a href="https://www.youtube.com/channel/UCCymWVaTozpEng_ep0mdUyw?sub_confirmation=1?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest YouTube Channel&lt;/a&gt; and stay updated with the latest tutorials around &lt;a href="https://www.lambdatest.com/automated-browser-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automated browser testing&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/cypress-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Cypress testing&lt;/a&gt;, CI/CD, and more.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public RemoteWebDriver driver = null;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;. Next would be to add the LambdaTest username and access key for your account to be used to run the script. You can find these details in the &lt;a href="https://accounts.lambdatest.com/detail/profile?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Profile Section&lt;/a&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String username = "&amp;lt;lambdatest_username&amp;gt;";
String accessKey = "&amp;lt;lambdatest_accesskey&amp;gt;";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;. After that, add the first method in this file as &lt;strong&gt;setUp()&lt;/strong&gt;, which would be used to set initial capabilities for the browser and launch the grid with the given credentials.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ATz8dgc-CIsSTPsB6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ATz8dgc-CIsSTPsB6.png" width="612" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see we have set some capabilities for the Chrome browser to be used for our test case. As we are using the LambdaTest platform here for remote execution, we can take advantage of its &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium Desired Capabilities Generator&lt;/a&gt; to generate such values for a number of browser and operating systems combinations by selecting them as required.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2876%2F0%2AZuMX2wEL9T60NCmf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2876%2F0%2AZuMX2wEL9T60NCmf.png" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;. Just like the &lt;strong&gt;setUp()&lt;/strong&gt; method would create the driver instance to be used for executing the script, it is a good practice to quit the current browser instance as soon as the case is completed. To facilitate the same, add the next method as &lt;strong&gt;closeDriver()&lt;/strong&gt; to quit the driver instance. This would have &lt;strong&gt;@AfterMethod&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;annotation in TestNG&lt;/a&gt; as the method with this annotation will be called after every test method has completed execution.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@AfterMethod](http://twitter.com/AfterMethod)
public void closeDriver() {
 driver.quit();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This completes the setup for our base implementation. Let’s move to the next section to implement and see how modal dialog box automation works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do you wanna know which are the most wanted &lt;a href="https://www.lambdatest.com/blog/automation-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automated testing tools&lt;/a&gt; that have climbed the top of the ladder so far? Let’s take a look.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Modal Dialog Box Automation Example
&lt;/h2&gt;

&lt;p&gt;For this blog purpose, we will be automating the following flow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to &lt;a href="https://www.lambdatest.com/selenium-playground/bootstrap-modal-demo?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;https://www.lambdatest.com/selenium-playground/bootstrap-modal-demo&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Launch Model&lt;/strong&gt; under &lt;strong&gt;Single Modal Example&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fetch the modal container web element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fetch the modal content web elements using the container element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verify and interact with the modal dialog window.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2AS0CP_Hm9IRPMEAJg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2AS0CP_Hm9IRPMEAJg.png" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F1%2AQWAJeF701rmFcakLzbfolw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F1%2AQWAJeF701rmFcakLzbfolw.png" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestModalDialogBox extends BaseClass {

[@Test](http://twitter.com/Test)(description = "test to verify modal dialog box")
 public void verifyModalDialogBox() throws InterruptedException {
  // to navigate to the website
  System.out.println("Navigating to the website");
  driver.get("[https://www.lambdatest.com/selenium-playground/bootstrap-modal-demo](https://www.lambdatest.com/selenium-playground/bootstrap-modal-demo)");

// to click Launch model button - Trigger element
  System.out.println("Clicking launch modal button");
  driver.findElement(By.xpath("//button[[@data](http://twitter.com/data)-target='#myModal']")).click();

// wait to let the modal box be visible
  WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(ExpectedConditions.visibilityOfElementLocated(
                                     By.className("modal-dialog")));

// to fetch the web element of the modal container
  System.out.println("Fetching the web element for modal container");
  WebElement modalContainer = driver.findElement(
                       By.className("modal-dialog"));

// to fetch the web elements of the modal content and interact with them
  // code to fetch content of modal body and verify it
  System.out.println("Fetching modal body content and asserting it");
  WebElement modalContentBody = modalContainer.findElement(By.xpath(".//div[[@class](http://twitter.com/class)='modal-body']"));
  Assert.assertEquals(modalContentBody.getText(),
    "This is the place where the content for the modal dialog displays", "Verify modal body message");

// code to click on accept modal button
  System.out.println("Clicking modal accept button");
  WebElement modalAcceptButton = modalContainer
    .findElement(By.xpath(".//button[contains(text(),'Save Changes')]"));
  modalAcceptButton.click();
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;. First lines of code in the testcase are to navigate to the URL on which we will have our modal dialog box to automate.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("[https://www.lambdatest.com/selenium-playground/bootstrap-modal-demo](https://www.lambdatest.com/selenium-playground/bootstrap-modal-demo)");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;. After the page has loaded, we click on the button to launch the dialog box. As we learnt, this is the trigger element which makes the dialog box appear.&lt;/p&gt;

&lt;p&gt;For this we use the &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;XPath element locator&lt;/a&gt; to fetch the element using its tag and attribute key-value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2AsERFYi8fI3bI5tnA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2AsERFYi8fI3bI5tnA.png" width="800" height="486"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(By.xpath("//button[[@data](http://twitter.com/data)-target='#myModal']")).click();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;. After clicking the button, we wait for the modal box to be visible. For this, we have used explicit waits, as per which the &lt;a href="https://www.lambdatest.com/learning-hub/webdriver?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;WebDriver&lt;/a&gt; waits for a certain condition to occur before proceeding to execute the further code. To know more about explicit waits, you can go through this article on &lt;a href="https://www.lambdatest.com/blog/types-of-waits-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;types of Waits in Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To use these waits in your test automation script, the following imports are done:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;. After this, an object of &lt;strong&gt;WebDriverWait&lt;/strong&gt; class is instantiated using the &lt;strong&gt;WebDriver&lt;/strong&gt; reference and maximum time to wait for before raising an exception. The time is in seconds.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WebDriverWait wait = new WebDriverWait(driver, 30);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Using this wait object, we implement some “ExpectedConditions” function to be fulfilled. There are certain predefined conditions for it in Selenium, of which we have used &lt;strong&gt;visibilityOfElementLocated()&lt;/strong&gt;, which means to wait until the given WebElement is visible.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("modal-dialog")));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;. Once the modal dialog box opens up, we fetch the WebElement of the modal container as we will be interacting with the elements inside the modal dialog box container.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WebElement modalContainer = driver.findElement(By.className("modal-dialog"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3152%2F1%2AZG_rSo-Z-ZErX_vn9HVU4g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3152%2F1%2AZG_rSo-Z-ZErX_vn9HVU4g.png" width="800" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt;. Next, we fetch the modal dialog box WebElements to perform actions on their content. One important thing to note here is the way how the WebElement for the model content is fetched.&lt;/p&gt;

&lt;p&gt;For this, in place of the driver, the WebElement identifier for the modal container is used, and the identifying XPath for the element starts with “&lt;strong&gt;.//,&lt;/strong&gt;” which signifies that the corresponding element to be found is inside this element, i.e., inside the modal dialog box.&lt;/p&gt;

&lt;p&gt;The following code showcases this implementation of how elements would be identified and how we can assert on it or click the button. To learn more about assertions you can go through our earlier blog on &lt;a href="https://www.lambdatest.com/blog/asserts-in-testng/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;How To Use Assertions In TestNG With Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WebElement modalContentBody = modalContainer.findElement(By.xpath(".//div[[@class](http://twitter.com/class)='modal-body']"));
Assert.assertEquals(modalContentBody.getText(), "This is the place where the content for the modal dialog displays", "Verify modal body message");

// code to click on accept modal button
WebElement modalAcceptButton = modalContainer .findElement (By.xpath (".//button[contains(text(),'Save Changes')]"));
modalAcceptButton.click();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Test Execution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Having understood the code implementation, lets execute the code using TestNG. Once successfully executed, you can see the following output on the IDE.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AQ8PLmdRlp2cUKFiV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AQ8PLmdRlp2cUKFiV.png" width="425" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since we executed the code on a remote grid using LambdaTest, execution logs are also available on the &lt;a href="https://accounts.lambdatest.com/dashboard?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Dashboard&lt;/a&gt;, including video, steps of the automation script, and network and console logs.&lt;/p&gt;

&lt;p&gt;You can see your executed test cases under &lt;strong&gt;Recent Tests&lt;/strong&gt; on the &lt;strong&gt;Dashboard&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2ACr5xe0-16NxJlm8o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2ACr5xe0-16NxJlm8o.png" width="800" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To see the detailed execution for your case, click on the test case, and you can see the details like the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2AiEO8hNTNzyvlYL6u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2AiEO8hNTNzyvlYL6u.png" width="800" height="510"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also navigate to the &lt;a href="https://analytics.lambdatest.com/test-overview?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;LambdaTest Analytics Dashboard&lt;/a&gt; to see test performance metrics. From the Test Summary section, you will see the total number of tests passed or failed, including completed and pending tests. You can also see the snapshot of consistent tests from the Test Overview section.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AD-mwRfy-PJpjcBqAubAXGg.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AD-mwRfy-PJpjcBqAubAXGg.gif" width="600" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to handle Alerts using Selenium Java?
&lt;/h2&gt;

&lt;p&gt;In this section of the blog on how to handle modal box in Selenium WebDriver Java, we will learn what are alerts and how to handle alerts in Windows using Selenium Java&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Alerts?
&lt;/h2&gt;

&lt;p&gt;Alerts in web development usually refer to a message or notification that lets the user know about information, namely:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Asking for access permissions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Highlighting mandatory fields on a form.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Display warning messages or help information.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike modal dialog boxes, Selenium offers some special functions to deal with alerts. This is because it is a very tricky and time-consuming task for testers to automate alerts using regular methods. A few of these predefined methods are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Click Accept Button of the alert Dismiss/Cancel an Alert Box&lt;/p&gt;

&lt;p&gt;driver.switchTo().alert().dismiss();&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Click Accept on Alert Box&lt;/p&gt;

&lt;p&gt;driver.switchTo().alert().accept();&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Fetch static data from Alert Box&lt;/p&gt;

&lt;p&gt;driver.switchTo().alert().getText();&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Send data to input fields on Alert Box&lt;/p&gt;

&lt;p&gt;driver.switchTo().alert().sendKeys("Text");&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;sendKeys()&lt;/strong&gt; function is used to enter data to the input fields on any web page in Selenium automation. &lt;a href="https://www.lambdatest.com/blog/how-to-use-selenium-sendkeys/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;SendKeys in Selenium&lt;/a&gt; performs the same action here by entering data to any input field on the alert.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Handling Alerts Window using Selenium Java
&lt;/h2&gt;

&lt;p&gt;For this blog on how to handle modal dialog box in Selenium Java, we will be automating the following flow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to &lt;a href="https://www.lambdatest.com/selenium-playground/javascript-alert-box-demo?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;https://www.lambdatest.com/selenium-playground/javascript-alert-box-demo&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Click Me&lt;/strong&gt; button under &lt;strong&gt;Java Script Alert Box&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verify the alert body content as &lt;strong&gt;Please Enter Your Name&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter some data in the alert input box.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click OK to accept the alert.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2Ay9I9N8C1nj7WEyVr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2Ay9I9N8C1nj7WEyVr.png" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;

import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestAlerts extends BaseClass {

[@Test](http://twitter.com/Test)(description = "test to verify alerts")
 public void verifyAlerts() throws InterruptedException {
  // to navigate to the website
  System.out.println("Navigating to the website");
  driver.get("[https://www.lambdatest.com/selenium-playground/javascript-alert-box-demo](https://www.lambdatest.com/selenium-playground/javascript-alert-box-demo)");

// to click the button to get demo alert
  System.out.println("Clicking launch alert button");
  driver.findElement(By.xpath("(//button[contains(text(),'Click Me')])[3]")).click();

// to let the alert be visible
  WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(ExpectedConditions.alertIsPresent());

// to fetch the alert body content and verify it
  System.out.println("Fetching the alert body content and asserting it");
  String alertBodyText = driver.switchTo().alert().getText();
  Assert.assertEquals(alertBodyText, "Please enter your name", "Verify alert body content");

// to enter data as required by the alert
  System.out.println("Entering date in the alert input box");
  driver.switchTo().alert().sendKeys("LambdaTest");

// to accept the alert
  System.out.println("Accepting the alert");
  driver.switchTo().alert().accept();
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;. This code also starts with the first step as navigating to the target page where the actual automation script would be executed.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("[https://www.lambdatest.com/selenium-playground/javascript-alert-box-demo](https://www.lambdatest.com/selenium-playground/javascript-alert-box-demo)");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;. Once the page is loaded, we click the button to get the alert on which alert functions can be implemented and verified.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(By.xpath("(//button[contains(text(),'Click Me')])[3]")).click();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;. Just like for the modal dialog box, for alert as well, explicit wait is being implemented. Imports and object creation remain same for this case as well with only difference being on &lt;strong&gt;ExpectedConditions&lt;/strong&gt;. For supporting alerts, just like there are predefined functions in Selenium, &lt;strong&gt;alertIsPresent()&lt;/strong&gt; is used in this case.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wait.until(ExpectedConditions.alertIsPresent());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;. After the alert is visible, we start with implementing &lt;strong&gt;alert().getText()&lt;/strong&gt; to fetch the content on the alert and asserting it.&lt;/p&gt;

&lt;p&gt;But before we can do so, we need to make sure that the driver instance focus has been switched to the alert from the main window. For this purpose, &lt;strong&gt;the switchTo()&lt;/strong&gt; function is used in &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium automation testing&lt;/a&gt;. This function is used whenever we are working with multiple windows like new tab, alerts, pop ups, etc. in web automation.&lt;/p&gt;

&lt;p&gt;Since in this test script, we need to switch to alert in this test script to interact with it, &lt;strong&gt;switchTo().alert()&lt;/strong&gt; is used. We will learn more about how to switch between parent/child windows in the popups section.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String alertBodyText = driver.switchTo().alert().getText();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;. Here we are using the &lt;strong&gt;Assert&lt;/strong&gt; class of &lt;strong&gt;TestNG&lt;/strong&gt; to compare the content using the &lt;strong&gt;assertEquals()&lt;/strong&gt; function. In this function, we will pass the expected content, which is the string and the actual content, which is the body content of the alert that is fetched on the previous step. If both matches, assert is passed; else, it fails with an error that is expected and actual data do not equal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Assert.assertEquals(alertBodyText, "Please enter your name","Verify alert body content");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt;. Next, we verify the &lt;strong&gt;alert().sendKeys()&lt;/strong&gt; function by entering some data in the input field.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.switchTo().alert().sendKeys("LambdaTest");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 7&lt;/strong&gt;. The last step of this test scenario is to accept the alert for which the predefined method that Selenium provides is being used.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.switchTo().alert().accept();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Depending upon the use case, you can also implement the function to dismiss/cancel the alert. For this following method Selenium method can be used&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.switchTo().alert().dismiss();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Test Execution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As mentioned for the modal dialog box in the previous section, we can similarly execute the alerts test case from IDE and see the output like below on successful execution on the IDE and the LambdaTest dashboard for the automation cases.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AdAYfjJ9K7mbyXwUl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AdAYfjJ9K7mbyXwUl.png" width="404" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AxaCGKRUeaeqePlrAC1qhcA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F1%2AxaCGKRUeaeqePlrAC1qhcA.png" width="800" height="527"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to handle Popups using Selenium Java?
&lt;/h2&gt;

&lt;p&gt;In this section of the blog on how to handle modal box in Selenium WebDriver Java, we will learn what are popups and how to handle popups using Selenium Java.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Popups?
&lt;/h2&gt;

&lt;p&gt;Pop-up refers to a new window or tab that pops up or gets opened as a result of some interaction on the webpage. It can come up in a number of conditions and as per implementation.&lt;/p&gt;

&lt;p&gt;In Selenium automation, the driver always focuses on the currently active window and interacts with it. So whenever we are working with pop-ups in Selenium, some additional methods are used to shift the driver’s focus to the newly opened window and then back to the main window.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Get handles of all the windows that are open at that instance.&lt;/p&gt;

&lt;p&gt;driver.getWindowHandles();&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Get the current window handle on which the driver is focused.&lt;/p&gt;

&lt;p&gt;driver.getWindowHandle();&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Switch to the main parent window.&lt;/p&gt;

&lt;p&gt;driver.switchTo().defaultContent();&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Switch to any specific window using its handle.&lt;/p&gt;

&lt;p&gt;driver.switchTo().window(handleId);&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;handleId&lt;/strong&gt; here refers to the ids of the window which we can fetch using the &lt;strong&gt;getWindowHandle()&lt;/strong&gt; or &lt;strong&gt;getWindowHandles()&lt;/strong&gt; function of the &lt;strong&gt;WebDriver&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Check out this blog for most wanted &lt;a href="https://www.lambdatest.com/blog/automation-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;testing tools for automation&lt;/a&gt; that have climbed the top of the ladder so far.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Demonstration: Handling Popups using Selenium Java
&lt;/h2&gt;

&lt;p&gt;For this blog purpose, we will be automating the following flow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to &lt;a href="https://www.lambdatest.com/selenium-playground/window-popup-modal-demo?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;https://www.lambdatest.com/selenium-playground/window-popup-modal-demo&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Follow On Twitter&lt;/strong&gt; under &lt;strong&gt;Single Window Popup&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Switch to a new popup window.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Assert &lt;strong&gt;Follow&lt;/strong&gt; button on it is present.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Switch back to the main window.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Assert &lt;strong&gt;Follow On Twitter&lt;/strong&gt; is displayed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2AZEOwgQNtedAKAOaZ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2AZEOwgQNtedAKAOaZ.png" width="800" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package test;

import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestPopups extends BaseClass {

[@Test](http://twitter.com/Test)(description = "test to verify pop ups")
 public void verifyPopups() throws InterruptedException {
  // to navigate to the website
  System.out.println("Navigating to the website");
  driver.get("[https://www.lambdatest.com/selenium-playground/window-popup-modal-demo](https://www.lambdatest.com/selenium-playground/window-popup-modal-demo)");
  driver.manage().window().maximize();

// to fetch and save the handle of current window
  System.out.println("storing the main window handle");
  String mainWindowHandle = driver.getWindowHandle();

// to click the button to get a popup (new tab in this case)
  System.out.println("Clicking launch popup button");
  WebElement followButtonOnMainWindow = driver.findElement(By.xpath("//a[contains([@title](http://twitter.com/title),'Twitter')]"));
  followButtonOnMainWindow.click();

// to get the list of all window handles after the new tab
  // should have length 2 since 1 new tab opens up
  System.out.println("Fetching the list of all window handles and asserting them");
  Set&amp;lt;String&amp;gt; windowHandles = driver.getWindowHandles();
  Assert.assertEquals(windowHandles.size(), 2, "Verify the total number of handles");

// switch to new opened tab
  System.out.println("Switching to the new window handle");
  Iterator&amp;lt;String&amp;gt; itr = windowHandles.iterator();
  while (itr.hasNext()) {
   String childWindowHandle = itr.next();
   // to skip the handle of our main window and switch to new one
   if (!mainWindowHandle.equalsIgnoreCase(childWindowHandle))
    driver.switchTo().window(childWindowHandle);

}

WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(ExpectedConditions.visibilityOfElementLocated(
                                     By.xpath("//span[(text()='Follow')]")));
  // to verify that driver focus is shifted to popup window
  System.out.println("Asserting some element on the new popup window to confirm switch");
  WebElement twitterFollowButton = driver.findElement(By.xpath("//span[(text()='Follow')]"));
  Assert.assertTrue(twitterFollowButton.isDisplayed(), "Verify twitter follow button is displayed");

// shift driver back to main window and verify
  System.out.println("Switching back to main window and asserting same");
  driver.switchTo().window(mainWindowHandle);
            wait.until(ExpectedConditions.visibilityOfElementLocated(
                                     By.xpath("//a[contains([@title](http://twitter.com/title),'Twitter')]")));
  Assert.assertTrue(followButtonOnMainWindow.isDisplayed(), "Verify focus is shifted to main window");
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Code Walkthrough&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can find two special imports in this test class, namely&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.Iterator;
import java.util.Set;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;These are done to support the usage of &lt;strong&gt;Set&lt;/strong&gt; type Data Structure and &lt;strong&gt;Iterator&lt;/strong&gt;, the function to traverse through the Set. More details about these are with the corresponding code where these are used.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;. For writing the automation test case for handling pop-ups, after navigating to the required webpage, first, we get and save the handle of the main window. This is done as once there is a new popup window, we need to have some way to differentiate between the handle of the current window and the newly opened window.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String mainWindowHandle = driver.getWindowHandle();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;. After storing this, the next step would be to click on the link text or button, in this case, on the current/main window, to get the new popup window.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WebElement followButtonOnMainWindow = driver.findElement(By.xpath ("//a[contains([@title](http://twitter.com/title),'Twitter')]"));
followButtonOnMainWindow.click();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;. Now, as per the test scenario expectation, we should have two windows on the screen. To verify the same, use the &lt;strong&gt;getWindowHandles()&lt;/strong&gt; method to get identifiers (or window handles) of the respective Windows.&lt;/p&gt;

&lt;p&gt;This method returns the list of handles of all the Windows, which to understand are the ids of different Windows that can be used to identify them. This entire list is stored in a Set type of data structure for easy accessibility.&lt;/p&gt;

&lt;p&gt;As the number of window handles is stored in the &lt;strong&gt;Set&lt;/strong&gt; type variable, we can use the &lt;strong&gt;size()&lt;/strong&gt; method to fetch the total number of handles in that and assert as per expectation which is 2 here.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set&amp;lt;String&amp;gt; windowHandles = driver.getWindowHandles();  Assert.assertEquals(windowHandles.size(), 2, "Verify the total number of handles");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;. Next step would be to switch to the new pop up window. For this, we traverse through the list of window handles and compare it with the main window handle value, which we stored in the first step.&lt;/p&gt;

&lt;p&gt;For traversal through the &lt;strong&gt;Set&lt;/strong&gt; containing all window handles, we are using &lt;strong&gt;Iterator&lt;/strong&gt;. As the name suggests, it means to visit repeatedly until some point. So here, we keep on iterating the set for the next values till we reach the last value.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Iterator&amp;lt;String&amp;gt; itr = windowHandles.iterator();
while (itr.hasNext()) {
 String childWindowHandle = itr.next();
 // to skip the handle of our main window and switch to new one
 if (!mainWindowHandle.equalsIgnoreCase(childWindowHandle))
  driver.switchTo().window(childWindowHandle);

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;. Once we get a non-matching value, we switch to the new popped-up window and use the Explicit Wait like discussed before to wait for the new popped-up window using the &lt;strong&gt;visibilityOfElementLocated()&lt;/strong&gt; function and raise an Assert to verify the switch is successful.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// to verify that driver focus is shifted to popup window
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[(text()='Follow')]")));
WebElement twitterFollowButton = driver.findElement(By.xpath ("//span[(text()='Follow')]"));
Assert.assertTrue(twitterFollowButton.isDisplayed(), "Verify twitter follow button is displayed");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt;. The last step is to switch back to the main window using its already stored handle and assert some element on it to verify that the driver focus has been shifted again to the main window.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.switchTo().window(mainWindowHandle);
Assert.assertTrue(followButtonOnMainWindow.isDisplayed(), "Verify focus is shifted to main window");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Test Execution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a similar way as discussed above, after execution, we will get the result on IDE as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AcjBxeuOYHGjFFRab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AcjBxeuOYHGjFFRab.png" width="490" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And we can see the detailed execution logs on LamdaTest Dashboard by navigating to the test details and can see the data like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2AZJ8Pa1yCGEyP4Leb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3998%2F0%2AZJ8Pa1yCGEyP4Leb.png" width="800" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you’re a Java developer looking to improve your skills, the following &lt;a href="https://www.lambdatest.com/certifications/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;certification from LambdaTest&lt;/a&gt; can help:&lt;/p&gt;

&lt;p&gt;This Selenium Java 101 certification is for beginning to intermediate level Java developers who want to refresh their knowledge of the programming language and learn more about the Selenium automation testing.&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the Selenium Java 101 certification from LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/vUxfvuAI7kE"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;By now you must have got a fair understanding about popups and alerts and how you can handle modal dialog box in Selenium WebDriver Java over a cloud grid like LambdaTest. However, if you want to leverage the best features of the cloud setup with faster execution (as good as the speed of a local setup), you can try &lt;a href="https://www.lambdatest.com/hyperexecute?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;HyperExecute&lt;/a&gt; — a blazing fast next gen testing cloud, which is upto 70% faster than any traditional Selenium cloud setup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With this blog on how to handle modal dialog box in Selenium WebDriver Java, we have learned the implementation and working for all of these in detail covering all the aspects and functions that you might encounter and would need while working with these. I hope, by now, you would be confident enough to automate any kind of modal dialog box, alerts, or popups that come up in your automation scripts. So go ahead and &lt;a href="https://www.lambdatest.com/blog/selenium-tutorial/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep08_kj&amp;amp;utm_term=kj&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;get started with Selenium Java automation testing&lt;/a&gt; to handle these with ease.&lt;/p&gt;

&lt;p&gt;Happy Automating !!&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>selenium</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
