<?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>Forem: HaritaLT</title>
    <description>The latest articles on Forem by HaritaLT (@haritalt).</description>
    <link>https://forem.com/haritalt</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%2F843009%2F3cf0abca-4975-48d0-99bc-d0bb6a84baf0.png</url>
      <title>Forem: HaritaLT</title>
      <link>https://forem.com/haritalt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/haritalt"/>
    <language>en</language>
    <item>
      <title>How To Download Files Using JavaScript and Selenium</title>
      <dc:creator>HaritaLT</dc:creator>
      <pubDate>Fri, 30 Sep 2022 08:10:26 +0000</pubDate>
      <link>https://forem.com/testmuai/how-to-download-files-using-javascript-and-selenium-2183</link>
      <guid>https://forem.com/testmuai/how-to-download-files-using-javascript-and-selenium-2183</guid>
      <description>&lt;p&gt;Downloading files is one of the most popular features utilized by Internet users worldwide. Almost all web applications provide a feature that allows users to download a file from the web page. It has many use cases, from downloading your favorite music or video files to invoices and bank statements.&lt;/p&gt;

&lt;p&gt;If you are an automation tester fixated on &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium automation testing&lt;/a&gt;, the chances are that you might run into a requirement of testing a feature around downloading files. While being a powerful tool for performing &lt;a href="https://www.lambdatest.com/automated-browser-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automated browser testing&lt;/a&gt;, Selenium natively doesn’t support download functionality. However, we can easily use Selenium to deal with file download, and this blog explores the exact scenario!&lt;/p&gt;

&lt;p&gt;In this &lt;a href="https://www.lambdatest.com/learning-hub/selenium-javascript?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium JavaScript tutorial&lt;/a&gt;, you will learn how to download files using JavaScript and &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=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium WebDriver.&lt;/a&gt;&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Challenges and Solutions in downloading files with Selenium
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; is a popular open-source tool that is used to automate web browsers. Selenium WebDriver comes with an exhaustive list of native functions that allows automation testers to effortlessly execute a wide variety of user interactions on the web application like click, type, drag and drop, etc. But when it comes to automating a feature like downloading files, things are not quite straightforward.&lt;/p&gt;

&lt;p&gt;One obstacle that testers face while downloading files with Selenium is that every web browser offers a different UI when downloading a file. Even though there is no easy way to control the browser’s native dialogs by JavaScript, there is a workaround for it.&lt;/p&gt;

&lt;p&gt;The solution is to trick the browser profile. By leveraging the browser profile setting, you can download files in Selenium WebDriver without interacting with the download window pop-up.&lt;/p&gt;

&lt;p&gt;In this blog on how to download files using JavaScript, let us learn how to write automation test code to download a file using JavaScript and Selenium.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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_kMX1PUkS564zeK8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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_kMX1PUkS564zeK8.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to download files using JavaScript and Selenium?
&lt;/h2&gt;

&lt;p&gt;We will be automating the following test scenario to download files using JavaScript and Selenium:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Go to the website File Download Demo for Automation.&lt;br&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; Enter data in the textarea.&lt;br&gt;
&lt;strong&gt;Step 3:&lt;/strong&gt; Click the “&lt;strong&gt;Generate File&lt;/strong&gt;” button to generate the text file.&lt;br&gt;
&lt;strong&gt;Step 4:&lt;/strong&gt; Click the “&lt;strong&gt;Download&lt;/strong&gt;” link to download the file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AiYKATN7ImInWKjZJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AiYKATN7ImInWKjZJ.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites to download files using JavaScript
&lt;/h2&gt;

&lt;p&gt;Before getting started with coding, ensure that you have the following prerequisites installed in your system to download files using JavaScript and Selenium:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Install Node.js and NPM.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&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=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Install Eclipse and IntelliJ &lt;/a&gt;(or any IDE of your choice).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Installation and basic project setup
&lt;/h2&gt;

&lt;p&gt;In this section of this article on how to download files using JavaScript, I will be explaining briefly how to do a basic project setup for &lt;a href="https://www.lambdatest.com/blog/automation-testing-with-selenium-javascript/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automation testing with Selenium JavaScript&lt;/a&gt;. Feel free to skip this section if you already have a working project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a project folder in the desired location in your system. Open the folder in your IDE.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Initialize the project in this folder. Run the following command.&lt;/p&gt;

&lt;p&gt;npm init -y&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Install the project dependencies. We need to install Selenium WebDriver and browser drivers. Here, I am installing the driver for the Chrome browser.&lt;/p&gt;

&lt;p&gt;npm install --save selenium-webdriver chromedriver&lt;/p&gt;

&lt;p&gt;With this, the project setup is now completed, and the &lt;strong&gt;package.json&lt;/strong&gt; file is generated in the root folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; To maintain the test cases, create a new folder called &lt;strong&gt;tests&lt;/strong&gt;. We will be storing our test files inside this folder.&lt;/p&gt;

&lt;p&gt;My project folder structure now 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%2F2000%2F0%2AXh7DjmBFoo4keKYo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AXh7DjmBFoo4keKYo.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Accelerate JavaScript Automation with Parallel testing. &lt;a href="https://accounts.lambdatest.com/register?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=register" rel="noopener noreferrer"&gt;Try LambdaTest Now!&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Writing test case to download files using JavaScript
&lt;/h2&gt;

&lt;p&gt;Now, it is time to write the code! For downloading a file using Selenium, we will be making use of the &lt;strong&gt;click()&lt;/strong&gt; method. The automation steps are detailed below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a test file testOne.js inside the tests folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Inside &lt;strong&gt;testOne.js&lt;/strong&gt;, pull all the required functions from &lt;strong&gt;node_modules&lt;/strong&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { By, Key, Builder } = require("selenium-webdriver");
require("chromedriver");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Create a function example(), which will be containing your test script. Feel free to provide any function name of your choice.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function example() {

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

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Inside the function, let us write our test script using Selenium and JavaScript.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function example() {
  //To wait for browser to build and launch properly
  let driver = await new Builder().forBrowser("chrome").build();

  try {
    //To go to the test website from the browser with our code.
    await driver.get("[http://demo.automationtesting.in/FileDownload.html](http://demo.automationtesting.in/FileDownload.html)");

    //To enter data inside the text area
    await driver
      .findElement(By.id("textbox"))
      .sendKeys("I love testing!", Key.RETURN);

    //To click on "Generate File" button
    await driver.findElement(By.id("createTxt")).click();

    //To click on "Download" link
    await driver.findElement(By.id("link-to-download")).click();

    //Wait for 5s till download is completed
    await driver.sleep(5000);
  } catch (e) {
    console.log("Error Occured:", e.name);
  }
  //It is always a safe practice to quit the browser after execution
  await driver.quit();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;To locate the Web Elements, I have made use of the Chrome Developer 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%2Aq498QSPot4cgnlhK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2Aq498QSPot4cgnlhK.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Now simply add the function call. The full implementation is given below&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { By, Key, Builder } = require("selenium-webdriver");
require("chromedriver");

async function example() {
  //To wait for browser to build and launch properly
  let driver = await new Builder().forBrowser("chrome").build();

  try {
    //To go to the test website from the browser with our code.
    await driver.get("http://demo.automationtesting.in/FileDownload.html");

    //To enter data inside the text area
    await driver
      .findElement(By.id("textbox"))
      .sendKeys("I love testing!", Key.RETURN);

    //To click on "Generate File" button
    await driver.findElement(By.id("createTxt")).click();

    //To click on "Download" link
    await driver.findElement(By.id("link-to-download")).click();

    //Wait for 5s till download is completed
    await driver.sleep(5000);
  } catch (e) {
    console.log("Error Occured:", e.name);
  }
  //It is always a safe practice to quit the browser after execution
  await driver.quit();
}

example();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; Run the code. Make sure that the IDE terminal is pointed towards the tests folder location and execute the following command in the terminal.&lt;/p&gt;

&lt;p&gt;node testOne.js&lt;/p&gt;

&lt;p&gt;The test starts running and an instance of Chrome browser is instantiated.&lt;/p&gt;

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

&lt;p&gt;The test gets executed successfully and the file is downloaded to your default Downloads folder location.&lt;/p&gt;

&lt;p&gt;Pace-up your releases and ship quality products faster. &lt;a href="https://accounts.lambdatest.com/register?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=register" rel="noopener noreferrer"&gt;Try LambdaTest Now!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do you want to learn more about the exciting world of &lt;a href="https://www.lambdatest.com/javascript-automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;JavaScript automation testing&lt;/a&gt;? Deep dive into how Selenium WebDriver with JavaScript can be used for &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automated testing&lt;/a&gt; of web applications with this video tutorial:&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Check out this blog, &lt;a href="https://www.lambdatest.com/blog/emulator-vs-simulator-vs-real-device/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Emulator vs Simulator&lt;/a&gt; vs Real Device Testing: Key Differences&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to download files to a specific folder using JavaScript and Selenium?
&lt;/h2&gt;

&lt;p&gt;Rather than downloading into the default Downloads folder, sometimes you might need to download the files to a specific folder location for storing and verifying all the downloads. In this section of this article on how to download files using JavaScript, let us learn how to configure the code to set the browser file Download directory for Chrome driver using Selenium and JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Make the following two additional imports in our previous code.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const chromeDriver = require("selenium-webdriver/chrome");
const webdriver = require("selenium-webdriver");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Define Chrome options. Inside setUserPreferences, set your custom download path.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var options = new chromeDriver.Options();
  options.setUserPreferences({
    "download.default_directory": "your\download\folder\path",
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For example,&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"download.default_directory": "D:\\Test",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Redefine your driver instance.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var driver = new webdriver.Builder()
    .withCapabilities(webdriver.Capabilities.chrome())
    .forBrowser("chrome")
    .setChromeOptions(options)
    .build();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The full implementation is as shown below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { By, Key, Builder } = require("selenium-webdriver");
require("chromedriver");

const chromeDriver = require("selenium-webdriver/chrome");
const webdriver = require("selenium-webdriver");

async function example() {
  //To set chromeOptions
  var options = new chromeDriver.Options();
  options.setUserPreferences({
    "download.default_directory": "D:\\Test",
  });

  //To wait for browser to build and launch properly
  var driver = new webdriver.Builder()
    .withCapabilities(webdriver.Capabilities.chrome())
    .forBrowser("chrome")
    .setChromeOptions(options)
    .build();

  try {
    //To go to the test website from the browser with our code.
    await driver.get("http://demo.automationtesting.in/FileDownload.html");

    //To enter data inside the text area
    await driver
      .findElement(By.id("textbox"))
      .sendKeys("I love testing!", Key.RETURN);

    //To click on "Generate File" button
    await driver.findElement(By.id("createTxt")).click();

    //To click on "Download" link

    await driver.findElement(By.id("link-to-download")).click();

    //Wait for 5s till download is completed
    await driver.sleep(5000);
  } catch (e) {
    console.log("Error Occured:", e.name);
  }
  //It is always a safe practice to quit the browser after execution
  await driver.quit();
}

example();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Run the code.&lt;/p&gt;

&lt;p&gt;node testTwo.js&lt;/p&gt;

&lt;p&gt;The test gets executed successfully, and the file is downloaded to the specific path mentioned.&lt;/p&gt;

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

&lt;p&gt;If you are a JavaScript developer, this certification will certify your competence as an in-depth, hands-on JavaScript automation pro.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AJesdFjFM5a_0eWpn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AJesdFjFM5a_0eWpn.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the Selenium JavaScript 101 certification from LambdaTest:&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Check this out: &lt;a href="https://www.lambdatest.com/blog/debug-websites-using-safari-developer-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Dev Tools for Safari&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to download files using JavaScript and cloud Selenium Grid?
&lt;/h2&gt;

&lt;p&gt;In the above demo test scenarios, we learned how to download files using JavaScript and Selenium WebDriver to our local systems and execute our tests in the Chrome browser.&lt;/p&gt;

&lt;p&gt;However, most &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=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium testing projects&lt;/a&gt; use &lt;a href="https://www.lambdatest.com/selenium-grid-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;online Selenium Grid&lt;/a&gt; to achieve cross browser testing and better test coverage to ensure that the test is working fine in all major browser-OS-device combinations.&lt;/p&gt;

&lt;p&gt;In this section of this article on how to download files using JavaScript, let us learn how to execute our code in the cloud-based Selenium Grid provided by LambdaTest.&lt;/p&gt;

&lt;p&gt;LambdaTest is a cross browser testing platform that provides an online browser farm of 3000+ browser and OS combinations and an online Selenium Grid that you can integrate with your current test project via minimum code changes. LambdaTest scalable cloud infrastructure helps you perform &lt;a href="https://www.lambdatest.com/web-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;web testing&lt;/a&gt; and &lt;a href="https://www.lambdatest.com/web-application-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;web application testing&lt;/a&gt; across real desktop and mobile browsers. You can execute your JavaScript automation tests instantly on LambdaTest cloud, without worrying about building or maintaining an in-house device lab.&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?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=youtube" rel="noopener noreferrer"&gt;LambdaTest YouTube channel&lt;/a&gt; when you want to catch up with the latest news on &lt;a href="https://www.lambdatest.com/automated-browser-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automated browser testing&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/automated-ui-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automated UI testing&lt;/a&gt;, etc.&lt;/p&gt;

&lt;p&gt;Follow along to learn how!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a free account on LambdaTest and secure your unique username and access key.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Inside the test file, define all the required variables for connecting with the Remote Grid like your unique username, access key, grid host, remote grid URL etc.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const USERNAME = "YOUR_USERNAME"; //replace with your username
const KEY = "YOUR_ACCESSKEY"; //replace with your accesskey

const GRID_HOST = "hub.lambdatest.com/wd/hub";

const gridUrl = "https://" + USERNAME + ":" + KEY + "@" + GRID_HOST;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Pass the browser capabilities. You can generate your custom desired capabilities using &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Capabilities Generator.&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%2AA6EXlkEsT-mMtpMv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AA6EXlkEsT-mMtpMv.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var capabilities = {
    "LT:Options": {
      user: USERNAME,
      accessKey: KEY,
      build: "JavaScript and Selenium Testing",
      name: "Download File",
      platformName: "Windows 11",
      selenium_version: "4.1.0",
      driver_version: "99.0",
    },
    browserName: "Chrome",
    browserVersion: "99.0",
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, I am configuring my test to run in Chrome Browser version 99.0 with Windows 11 OS using the latest Selenium 4.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; The configuration is completed. Our final code now looks like this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const USERNAME = "YOUR_USERNAME"; //replace with your username
const KEY = "YOUR_ACCESSKEY"; //replace with your accesskey

const GRID_HOST = "hub.lambdatest.com/wd/hub";

const { By, Key, Builder } = require("selenium-webdriver");

async function example() {

  var capabilities = {
    "LT:Options": {
      user: USERNAME,
      accessKey: KEY,
      build: "JavaScript and Selenium Testing",
      name: "Download File",
      platformName: "Windows 11",
      selenium_version: "4.1.0",
      driver_version: "99.0",
    },
    browserName: "Chrome",
    browserVersion: "99.0",
  };

  const gridUrl = "https://" + USERNAME + ":" + KEY + "@" + GRID_HOST;

  //To wait for browser to build and launch properly
  let driver = await new Builder()
    .usingServer(gridUrl)
    .withCapabilities(capabilities)
    .build();

  try {
    //To go to the test website from the browser with our code.
    await driver.get("http://demo.automationtesting.in/FileDownload.html");

    //To enter data inside the text area
    await driver
      .findElement(By.id("textbox"))
      .sendKeys("I love testing!", Key.RETURN);

    //To click on "Generate File" button
    await driver.findElement(By.id("createTxt")).click();

    //To click on "Download" link

    await driver.findElement(By.id("link-to-download")).click();

    //Wait for 5s till download is completed
    await driver.sleep(5000);
  } catch (e) {
    console.log("Error Occured:", e.name);
  }
  //It is always a safe practice to quit the browser after execution
  await driver.quit();
}

example();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Run the code.&lt;/p&gt;

&lt;p&gt;node testThree.js&lt;/p&gt;

&lt;p&gt;The test starts running. Go to the &lt;a href="https://accounts.lambdatest.com/login?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=login" rel="noopener noreferrer"&gt;LambdaTest Dashboard&lt;/a&gt; to observe the live running status.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AqWcadnLjFltk16uX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AqWcadnLjFltk16uX.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our test got executed successfully. You can analyze the detailed report containing video and logs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2A8pwD0TmDzn59NkHh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2A8pwD0TmDzn59NkHh.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Moreover, you can navigate to the &lt;a href="https://accounts.lambdatest.com/login?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=login" rel="noopener noreferrer"&gt;LambdaTest Analytics Dashboard&lt;/a&gt; to see test performance metrics. You can see the total number of tests passed or failed from the Test Summary section. 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%2F3200%2F0%2A6Bv_4HnOpO8v4nq1.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%2F3200%2F0%2A6Bv_4HnOpO8v4nq1.gif"&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%2F2560%2F0%2AzuFO2WD7t41uac9Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2560%2F0%2AzuFO2WD7t41uac9Q.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Check this out: &lt;a href="https://www.lambdatest.com/blog/debug-websites-using-safari-developer-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Developer Tools for Safari&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;File Download is a common feature offered by most web applications. In this tutorial on how to download files using JavaScript and Selenium, we explored how to write an automation test case for downloading a file using JavaScript and Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;Additionally, we explored edge cases like how to download files using JavaScript to a specific folder and how to run our tests using an online Selenium Grid for better &lt;a href="https://www.lambdatest.com/blog/code-coverage-vs-test-coverage/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;test coverage&lt;/a&gt; and cross browser testing. I hope this tutorial turned out to be beneficial!&lt;/p&gt;

&lt;p&gt;Happy Testing!&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>testing</category>
    </item>
    <item>
      <title>Running Your First Test With NightWatchJS</title>
      <dc:creator>HaritaLT</dc:creator>
      <pubDate>Tue, 07 Jun 2022 17:52:21 +0000</pubDate>
      <link>https://forem.com/testmuai/running-your-first-test-with-nightwatchjs-20jn</link>
      <guid>https://forem.com/testmuai/running-your-first-test-with-nightwatchjs-20jn</guid>
      <description>&lt;p&gt;In the world of automation testing, Selenium continues to be the most sought after tool. With shorter development cycles and faster releases backed by Agile and DevOps, companies are keen on adopting the right automation testing strategy on par with the development and ensure a high-quality end product. Speeding up automation testing means choosing a plan that aids in handling repetitive work and optimizing tasks with minimal maintenance and effort. And herein lies the importance of implementing the right test automation framework.&lt;/p&gt;

&lt;p&gt;Various test automation frameworks are available in the market to choose from. Nightwatch.js is one such well-favored JavaScript-based framework since its release in 2014. This blog will be your guide to everything you need to know to kickstart your journey in &lt;a href="https://www.lambdatest.com/selenium-automation-testing-with-nightwatchjs-framework?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Nightwatch.js for Selenium automation testing.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Starting with a brief overview, our primary focus in this Nightwatch.js tutorial would be setting up and configuration of Nightwatch.js into your system. And by the end, you will be able to write your first test in Nightwatch.js Selenium.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  What Is Nightwatch.js?
&lt;/h2&gt;

&lt;p&gt;Nightwatch.js is an open-source automated testing framework for browser-based web applications and websites. It is written in Node.js runtime and uses the W3C WebDriver API (formerly Selenium WebDriver) for interacting with various browsers to perform commands and assertions on DOM elements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Is Nightwatch.js So Popular?
&lt;/h2&gt;

&lt;p&gt;The reason behind Nightwatch.js popularity is its out of the box features. Some of the noteworthy features that make Nightwatch.js so popular are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easy To Use&lt;/strong&gt;— with a clean and simple syntax, code can be written swiftly and efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;In-Built Test Runner&lt;/strong&gt;— comes with an inbuilt command-line test runner suite with Grunt support for executing Selenium automated tests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Page Object Model&lt;/strong&gt;— supports CSS and XPath locators making browser automation more manageable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexibility&lt;/strong&gt;— enables unit, integration as well as end-to-end testing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Selenium Server Support&lt;/strong&gt;— it can automatically control the standalone Selenium server or various browser web drivers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integrations&lt;/strong&gt;— provides a single integrated solution for application testing, eliminating the dependency on third-party software.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Support For Cloud Services&lt;/strong&gt;— compatible with cloud-based testing platforms like LambdaTest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Continuous Integration&lt;/strong&gt;— supports software like Jenkins, TeamCity, etc., assisting developers in continuously building and testing software.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Nightwatch.js Installation &amp;amp; Local Set Up
&lt;/h2&gt;

&lt;p&gt;It is time to proceed with the installation and environment set-up for Nightwatch.js automation. We will be going step by step from the beginning. But, before installing Nightwatch.js, make sure your system is pre-equipped with the following.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;a) Java&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Check whether Java is already installed in your system using the following command:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AlNonXrYzKC0W7hkG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AlNonXrYzKC0W7hkG.png" width="190" height="28"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If not, install the latest JDK appropriate to your machine from &lt;a href="https://www.java.com/en/download/help/download_options.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b) Node.js &amp;amp; NPM&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Check whether Node.js is already installed in your system using the following command:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2A6OoPea-uOtLRwGZJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2A6OoPea-uOtLRwGZJ.png" width="109" height="31"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If not, install the latest LTS version from &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; NPM will be installed along with Node.js. So no need for separate installation.&lt;br&gt;
After installation, verify the version of Node.js and npm using these 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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A6G87eNpvXWPm19MV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2A6G87eNpvXWPm19MV.png" width="393" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;c) Visual Studio Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here, we are using Visual Studio Code as the IDE. You can choose any other IDE of your choice.&lt;/p&gt;

&lt;p&gt;If not already installed, you may download VSC from &lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Installing Nightwatch.js &amp;amp; Project Setup
&lt;/h2&gt;

&lt;p&gt;Now let’s proceed with Nightwatch.js installation and basic project configuration. As promised, we will go step by step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a folder for your project in the desired location in your system. Open the folder in your IDE.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Now, let us initialize our project with the &lt;strong&gt;package.json&lt;/strong&gt; file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open the terminal in Visual Studio Code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verify that it is pointing to the project folder location.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Give the command:&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;npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;You will be asked to fill in project details. You may fill them or leave them blank as of now.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://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%2A4vdkESFXppQw1709.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2A4vdkESFXppQw1709.png" width="475" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You will also notice the package.json file is created in your project folder, containing all the project configuration details.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Save some time by using the following command in order to generate a default package.json file without any questions being asked.&lt;/p&gt;

&lt;p&gt;npm init -y&lt;/p&gt;

&lt;p&gt;Here y stands for “yes”.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have successfully created the package.json file.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Let’s install Nightwatch.js to our project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev nightwatch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will add the “node_modules” folder in your directory and download Nightwatch.js to save it as devDependency in &lt;strong&gt;package.json&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You will also notice that the “package-lock.json” file has been created as well. This file is used to lock all the dependencies with the version number so that it will be easier to set up the code in another system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; You may also install Nightwatch.js globally so that it is accessible for all the projects located anywhere within the system.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev nightwatch -g
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here g stands for “global”.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Now let us install our browser web driver.&lt;/p&gt;

&lt;p&gt;a) ChromeDriver&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev chromedriver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;b) GeckoDriver (Firefox)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install — save-dev geckodriver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Or why not install them all in one go?&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm i nightwatch geckodriver chromedriver — save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Alternatively, we can combine steps 4 and 5 into a single step. Use the command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install — save-dev nightwatch chromedriver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It is to be noted that, while the Selenium Server was required with older Nightwatch versions (v0.9 and prior), starting with version 1.0 Selenium is no longer necessary.&lt;/p&gt;

&lt;p&gt;The setup is now completed! Let’s run some demo tests now!&lt;/p&gt;

&lt;h2&gt;
  
  
  Running A Demo Test
&lt;/h2&gt;

&lt;p&gt;Do you know Nightwatch.js comes with some sample tests? Shall we try running that? That’s what this section of our Nightwatch.js tutorial is all about.&lt;/p&gt;

&lt;p&gt;Let us begin-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Go to the folder “&lt;strong&gt;node_modules/nightwatch/examples/tests/&lt;/strong&gt;”.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Under the ‘tests’ folder, you can find some sample 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%2F2000%2F0%2Atm37V2-by63zim2w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2Atm37V2-by63zim2w.png" width="366" height="730"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Let us run the first sample test “&lt;strong&gt;ecosia.js&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%2F5ruc3eewtiemb0ya35xz.png" class="article-body-image-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%2F5ruc3eewtiemb0ya35xz.png" width="800" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a simple, straightforward program which-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Launches the default browser&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Opens the search engine “Ecosia”&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inputs “nightwatch” in search field&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verifies the search result.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s go for it!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; In the terminal run the command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./node_modules/.bin/nightwatch node_modules/nightwatch/examples/tests/ecosia.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, “./node_modules/.bin/nightwatch” represents the path of nightwatch.js executable.&lt;/p&gt;

&lt;p&gt;“node_modules/nightwatch/examples/tests/ecosia.js” gives the path of the test file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; The code gets executed, and the test starts running. Default browser will get launched, and the test gets executed 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%2F3200%2F0%2AHcu79F37u2f2YDEu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AHcu79F37u2f2YDEu.png" width="800" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; Observe the test run result in your terminal.&lt;/p&gt;

&lt;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%2Fj60kckg2702rr9qncs6f.png" class="article-body-image-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%2Fj60kckg2702rr9qncs6f.png" width="800" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congrats! Nightwatch is now successfully configured in your system.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/nightwatch-js-tutorial-for-test-automation-beginners/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Nightwatch.js Tutorial For Test Automation Beginners — With Examples&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing Your First Nightwatch.js Automation Test Script
&lt;/h2&gt;

&lt;p&gt;In the previous section, we were able to run a demo test successfully. But we need to write our own scripts, don’t we?&lt;/p&gt;

&lt;p&gt;Now that we have got a basic hand-on, it’s time to get our hands dirty. In this session, we will be writing and running our very first test file using Nightwatch.js automation. Let’s check out the configurations that need to be done.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Configurations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our goal is to run our test files using the “&lt;strong&gt;npm test&lt;/strong&gt;” command from the project’s base directory. Also, we are setting Chrome as our default browser in the example. But feel free to use the browser of your choice.&lt;/p&gt;

&lt;p&gt;Now let’s start step by step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Let us create two folders in our root directory — “tests” for keeping test files and “reports” where reports will get stored after each test run.&lt;/p&gt;

&lt;p&gt;(You can provide any folder name of your choice. However, it should be properly mapped in the nightwatch.json file about which we will discuss in step 3.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; In your package.json file, replace “scripts — test” to “&lt;strong&gt;nightwatch&lt;/strong&gt;”.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;“scripts”: {
  “test”: “nightwatch”
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; In root, create a “nightwatch.json” file. Paste the following snippet.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "src_folders" : "tests",
    "output_folder" : "reports",

    "webdriver" : {
        "start_process": true,
        "server_path": "node_modules/chromedriver/lib/chromedriver/chromedriver.exe",
        "host": "localhost",
        "port": 4444
      },
      "test_settings" : {
        "default" : {
            "desiredCapabilities" : {
                "browserName" : "chrome"            
              }           
          }
      }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The “&lt;em&gt;nightwatch.json&lt;/em&gt;” is the configuration file which the nightwatch test runner binary expects.&lt;/p&gt;

&lt;p&gt;Let’s have a close look into the above code structure of “&lt;em&gt;nightwatch.json&lt;/em&gt;”.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;**src_folders:&lt;/em&gt;* indicates the location that contains the test suites. You can also give an array of folders. If not specified, we should pass the test location inline as the second argument to the &lt;a href="https://nightwatchjs.org/guide/running-tests/#nightwatch-runner" rel="noopener noreferrer"&gt;test runner&lt;/a&gt;, as we did in our demo test.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;**output_folder:&lt;/em&gt;* indicates the location where test reports get saved, i.e., JUnit report files, XML reports, test logs, selenium log, screenshots, video logs, network logs, etc.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;**webdriver:&lt;/em&gt;* an object containing WebDriver related configuration options.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;**start_process:&lt;/em&gt;* a boolean value that indicates that, once we start our test, the nightwatch will automatically start/stop the webdriver instance in the background as a child process.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;**server_path:&lt;/em&gt;* Here, we are giving the path of the chrome driver executable. Only useful if start_process is enabled.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;**host:&lt;/em&gt;* We are using localhost for running our test. Mainly needed when the WebDriver service is running on a different machine.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;**port:&lt;/em&gt;* The port number on which the WebDriver service will listen and/or on which Nightwatch will attempt to connect.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;**test-settings:&lt;/em&gt;* Defines the browser we want to use.&lt;/p&gt;

&lt;p&gt;You may also create “&lt;strong&gt;nightwatch.conf.js&lt;/strong&gt;” for configuration. If both these files are present in the project, the nightwatch.conf.js file will be given more precedence over nightwatch.json.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Configurations are done. Now let us write our first test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing Nightwatch.js Automation Tests?
&lt;/h2&gt;

&lt;p&gt;We have already created a “&lt;strong&gt;tests&lt;/strong&gt;” folder. Now we will be creating test files inside this folder. Each file inside it will be loaded as a test suite by the Nightwatch test runner. Let’s start!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a test file and name it as — &lt;strong&gt;testfile1.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Let’s create the test structure.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {

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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Inside each file, the test runner will look for different keys that are exported. The key is basically the test case name. Each key is mapped to a function in which the browser instance will be passed as an argument.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
"Get URL and title" : function(browser) {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Inside this function, we write our test code. Let’s see the snippet below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
    "Get URL and title" : function(browser) {
     browser
     .url("[http://www.google.com](http://www.google.com)")
     .assert.urlContains("google")
     .assert.title("Google")
     .pause(3000);
       }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here we are trying to launch Google and verify that the title and URL of the output page match the expected result. Simple and straightforward.&lt;/p&gt;

&lt;p&gt;Let’s understand the code now.&lt;/p&gt;

&lt;p&gt;**&lt;em&gt;url() *&lt;/em&gt;— navigate to a specific URL.&lt;/p&gt;

&lt;p&gt;**&lt;em&gt;urlContains() *&lt;/em&gt;— checks if the current URL contains the given value.&lt;/p&gt;

&lt;p&gt;**&lt;em&gt;title() *&lt;/em&gt;— checks if the page title equals the given value.&lt;/p&gt;

&lt;p&gt;**&lt;em&gt;pause() *&lt;/em&gt;— suspends the test for the given time in milliseconds.&lt;/p&gt;

&lt;p&gt;**&lt;em&gt;assert *&lt;/em&gt;— when an assertion fails, the test ends, skipping all other assertions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Now it’s time to run the code. Go to the terminal. Verify you are in the project base directory. Give the following command:&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;The test will start running, and the browser will be 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%2F3020%2F0%2Aq0xSboQnUFkfcaLm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3020%2F0%2Aq0xSboQnUFkfcaLm.png" width="800" height="451"&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%2AahX4J1OXClzXk7Iy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AahX4J1OXClzXk7Iy.png" width="642" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All assertions are passed. Congrats, you have successfully written your first test code!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; Check out the reports generated in your “reports” folder.&lt;/p&gt;

&lt;p&gt;In this sample test case, we have explored only a few commands. Feel free to explore more by visiting the &lt;a href="https://nightwatchjs.org/api/" rel="noopener noreferrer"&gt;official API documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Parallel Testing Using Nightwatch.js
&lt;/h2&gt;

&lt;p&gt;We can enable parallel testing in Nightwatch.js using a quick and simple configuration in our “nightwatch.json” file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"test_workers": {
        "enabled": true,
        "workers": "auto"
      },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;By enabling “test_workers” to be true, we are enabling parallel testing. Let’s look into the steps now:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Add the above code in your “nightwatch.json” file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Create an additional test file in the “tests” folder for running in parallel with the first test file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; In the Terminal, give the command:&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Two browser windows get launched, executing the test files in parallel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; After execution, observe the reports generated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do you know &lt;a href="https://www.lambdatest.com/web-technologies/inline-block?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=web_technologies" rel="noopener noreferrer"&gt;CSS inline-block&lt;/a&gt; display a single element as a block while flowing text around it.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Cross Browser Testing &amp;amp; Test Coverage Across Devices
&lt;/h2&gt;

&lt;p&gt;We came to a detailed understanding of Nightwatch.js setup and test execution in our local machine through the previous sections of this Nightwatch.js tutorial. We have also created our first test file, did parallel testing, and generated reports.&lt;/p&gt;

&lt;p&gt;But to stay ahead in a highly competitive market, it is our duty as testers to ensure the quality of the application in all scenarios, i.e, the app should behave as expected across all platforms, browsers, and devices without crashing. This is where the terms test coverage and cross browser testing comes into the picture.&lt;/p&gt;

&lt;p&gt;To leverage the test coverage, cloud-based solutions are widely adopted by companies due to various merits like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Low investment cost compared to buying the infrastructure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provides access to hundreds of device and platform combinations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Extends supports to old and outdated browser versions and OS devices&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No set-up or installation, or maintenance needs to be done locally.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Scalability.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When it comes to distributed test execution, Selenium Grid is always a good call. It has the capability to run test cases on different versions of browsers and different operating systems simultaneously.&lt;/p&gt;

&lt;p&gt;Hence, using a combination of Selenium Grid with the right cloud-based solution provider for cross &lt;a href="https://www.lambdatest.com/feature?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;browser compatibility testing&lt;/a&gt; is the optimum strategy that you can deploy to assure quality. Now let us see how we can perform automated browser testing using a remote Selenium Grid provided by LambdaTest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Check this out- &lt;a href="https://www.lambdatest.com/web-technologies/minmaxwh" rel="noopener noreferrer"&gt;CSS min/max-width/height&lt;/a&gt;: Use min-width, max-width, min-height, and max-height to set a minimum or maximum width, or minimum or maximum height for an element.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test Execution With Selenium Grid Cloud
&lt;/h2&gt;

&lt;p&gt;LambdaTest is trusted by 100,000 companies worldwide as a reliable online Selenium Grid provider to fulfill their cross browser testing needs. LambdaTest provides you access to 2000+ browsers for both mobile and desktop to help you gain the maximum test coverage during the automated browser testing.&lt;/p&gt;

&lt;p&gt;You can even leverage parallel testing with Selenium automation testing, along with our open Selenium API to help you extract test reports of your Selenium script execution over LambdaTest, effortlessly.&lt;/p&gt;

&lt;p&gt;Now, let us try and execute the above test case using the remote WebDriver for LambdaTest’s &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium Grid cloud&lt;/a&gt;. You can achieve this using a few simple and easy steps with few changes in your existing script for local setup. Let’s look into it step by step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; We need to pass browser and environment details to the LambdaTest Selenium Grid via the desired capabilities class. Unlike in local setup, now we have the flexibility to choose the precise settings we want.&lt;/p&gt;

&lt;p&gt;There is a helpful tool provided by LambdaTest to generate desired capabilities based on the combination we want. Visit &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Desired Capabilities&lt;/a&gt; page to generate them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AiSWCsSizQKv7VKhx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AiSWCsSizQKv7VKhx.png" width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, the desired capabilities class in nightwatch.json will look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"desiredCapabilities": {
       "build":"Nightwatch-Selenium--Test",
       "platform" : "Windows 10",
       "browserName" : "Chrome",
       "version" : "87.0",
       "selenium_version" : "3.13.0",
        "geoLocation" : "IN",
        "chrome.driver" : "87.0",
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Now you need to fetch your unique Access token and username.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Log in to LambdaTest or create your &lt;a href="https://accounts.lambdatest.com/register?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=its_sign_up" rel="noopener noreferrer"&gt;account&lt;/a&gt;. A unique access token will be created for you.&lt;br&gt;
This token is used as a secret key to link your local scripts with the LambdaTest platform.&lt;/p&gt;

&lt;p&gt;There are 3 ways to find it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If already logged in, from the Capabilities generator page, you can fetch it.&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%2F2000%2F0%2Ap7qGy2bPGHFueIH1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2Ap7qGy2bPGHFueIH1.png" width="624" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2- You can also fetch the details from the &lt;a href="https://accounts.lambdatest.com/login?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=its_sign_up" rel="noopener noreferrer"&gt;Profile section&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%2F2066%2F0%2ArWdRduThBsXf2ZPC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2066%2F0%2ArWdRduThBsXf2ZPC.png" width="800" height="119"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3- Or from the Automation dashboard page, by clicking the key button in the top right corner.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AZVPAn2YOr9o4jAdo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AZVPAn2YOr9o4jAdo.png" width="512" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Paste the following snippet in your nightwatch.conf.js file. (If not created already, you can create this file in your root)&lt;/p&gt;

&lt;p&gt;Here we are declaring the user configuration for an access key, username, host, and port for the test.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = (function(settings) {
    console.log(settings["test_settings"]["default"]["username"])
    if (process.env.LT_USERNAME) {
      settings["test_settings"]["default"]["username"] = process.env.LT_USERNAME;
    }
    if (process.env.LT_ACCESS_KEY) {
      settings["test_settings"]["default"]["access_key"] = process.env.LT_ACCESS_KEY;
    }
    if (process.env.SELENIUM_HOST) {
      settings.selenium.host = process.env.SELENIUM_HOST;
    }
    if (process.env.SELENIUM_PORT) {
      settings.selenium.host = process.env.SELENIUM_PORT;
    }
    return settings;
  })(require('./nightwatch.json'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Configure your nightwatch.json file. Since we are using LambdaTest, why don’t we go for cross browser testing across multiple platforms?&lt;/p&gt;

&lt;p&gt;In this code, we are configuring for the script to run across 3 browsers — Chrome, Firefox, and Safari on 2 different platforms — Windows and macOS.&lt;/p&gt;

&lt;p&gt;Check out the code below and let’s look into the required changes and additions to be made on your existing file:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "src_folders" : "tests",
  "output_folder" : "reports",

  "test_workers": {
    "enabled": true,
    "workers": "auto"
  },
  "selenium" : {
    "start_process" : false,
    "server_path" : "",
    "log_path" : "",
    "host" : "hub.lambdatest.com",
    "port" : 80,
    "cli_args" : {
    "webdriver.chrome.driver" : "",
    "webdriver.ie.driver" : "",
    "webdriver.firefox.profile" : ""
    }
  },
  "test_settings" : {
    "default" : {
    "launch_url" : "[http://google.com](http://google.com)",
    "selenium_port"  : 80,
    "selenium_host"  : "[https://lambdatest.com](https://lambdatest.com)",
    "silent": false,
    "screenshots" : {
        "enabled" : true,
        "path" : ""
    },
    "username" : "haritagr16",
    "access_key" : "123456789",

    "skip_testcases_on_fail": false,

    "desiredCapabilities": {
       "build":"Nightwatch-Selenium--Test",
       "platform" : "Windows 10",
       "browserName" : "Chrome",
       "version" : "87.0",
       "selenium_version" : "3.13.0",
       "visual":true,
        "video":true,
        "console":true,
        "geoLocation" : "IN",
        "chrome.driver" : "87.0",
        "network":true
    }
    },
    "chrome": {
    "desiredCapabilities": {
        "platform": "Windows 10",
        "browserName": "chrome",
        "version": "87.0"
    }
    },
    "safari" : {
    "desiredCapabilities": {
        "platform": "macos 10.13",
        "browserName": "safari",
        "version": "11.0"
    }
    },
    "firefox" : {
    "desiredCapabilities": {
        "platform": "win10",
        "browserName": "firefox",
        "version": "60"
    }
    },
    "edge" : {
    "desiredCapabilities": {
        "platform": "Windows 10",
        "browserName": "MicrosoftEdge",
        "version": "17.0"
    }

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

&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Pass your Access Token and User details.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provide Desired Capabilities for all the browsers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set the Hostname corresponding to LambdaTest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optionally we have also enabled report formats like video recording, console, network logs, etc., to be true.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A more detailed explanation is given below but feel free to skip to Step 6.&lt;/p&gt;

&lt;p&gt;We have used additional parameters to configure “nightwatch.json” for cloud-based cross browser testing. They are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;selenium *&lt;/em&gt;— an object containing Selenium Server related configuration options. If “&lt;/strong&gt;selenium*&lt;em&gt;” is not used, “&lt;/em&gt;&lt;em&gt;webdriver&lt;/em&gt;*” options should be set instead (as we did in our local set up). Starting with Nightwatch 1.0, Selenium is only required when testing against a grid setup or a cloud testing service such as LambdaTest.&lt;/p&gt;

&lt;p&gt;**&lt;em&gt;cli_args *&lt;/em&gt;— list of CLI args to be passed to Selenium process, basically browser drivers.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;**test_settings:&lt;/em&gt;*&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;silent *&lt;/em&gt;— whether to show the extended HTTP traffic command logs from the WebDriver or Selenium server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;desiredCapabilities *&lt;/em&gt;— an object to specify browser name for instance along with other capabilities.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;username and access_key *&lt;/em&gt;— Usually only needed for cloud testing Selenium services. In case the server requires credentials these values will be used to compute the Authorization header.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; In package.json, change the “ scripts — test” argument value.&lt;/p&gt;

&lt;p&gt;This is important as it is here where we are specifying the browsers where we need to run our test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
        "test": "./node_modules/.bin/nightwatch -e firefox,safari,chrome tests"
      }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; Execute the test.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;The code starts running and logs can be observed in the terminal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AIMNOnHpPnvcaHma8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AIMNOnHpPnvcaHma8.png" width="800" height="110"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To observe live running status go to your Automation dashboard in 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%2AGS4IBKAsbRKzEHH0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AGS4IBKAsbRKzEHH0.png" width="800" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can analyze individual test run reports from Automation Logs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2Ax7rJ_kGDGpyuUjWE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2Ax7rJ_kGDGpyuUjWE.png" width="800" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have successfully run our test cases using an automated cross browser testing solution provided by LambdaTest through a hassle-free experience!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do you know &lt;a href="https://www.lambdatest.com/web-technologies/justify-content-space-evenly?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=web_technologies" rel="noopener noreferrer"&gt;CSS justify-content: space-evenly&lt;/a&gt; value spreads the space between items evenly. It is similar to space-around, however it gives equal room on the edges rather than half-sized space. It can be used in both CSS flexbox and CSS grid.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;Nightwatch.js is one of the most in-demand Selenium Javascript-based automation testing frameworks known for its simplicity and integrated features. In this blog, we have looked into Nightwatch.js installation and setup, along with a detailed walkthrough on configurations. Additionally, we have also leveraged our automation testing by adopting test coverage concepts and cross browser testing.&lt;/p&gt;

&lt;p&gt;Hopefully, this Nightwatch.js tutorial will enable you to kickstart your journey in Nightwatch.js automation. If you wish to optimize your web application quality by unlocking coverage in a wider variety of platforms or browsers or versions, feel free to explore the LambdaTest platform.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy testing!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>testing</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Angular Testing With Jasmine And Karma Using Selenium [Tutorial]</title>
      <dc:creator>HaritaLT</dc:creator>
      <pubDate>Mon, 06 Jun 2022 17:10:11 +0000</pubDate>
      <link>https://forem.com/testmuai/angular-testing-with-jasmine-and-karma-using-selenium-tutorial-3cc1</link>
      <guid>https://forem.com/testmuai/angular-testing-with-jasmine-and-karma-using-selenium-tutorial-3cc1</guid>
      <description>&lt;p&gt;Software testing is the best way to prevent software defects and plays an important role in the Software Development Life Cycle (SDLC). It is a critical step in software development, and can be achieved using various testing methods. Different testing approaches like Selenium automation testing, performance testing, and automated Unit testing can be chosen based on your application’s testing requirements.&lt;/p&gt;

&lt;p&gt;Software testing is often considered to be a task of the QA team. However, developers can also write tests for their code to ensure the individual components or “units” of the software are working correctly and this is called Unit testing. As shown below, Angular is considered as the preferred test framework for automation testing. In this blog, we deep dive into how to perform Angular testing with Jasmine and Karma.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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_c_9IgoT_dIFxVWu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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_c_9IgoT_dIFxVWu.png" alt="[*Source](https://www.statista.com/statistics/1124699/worldwide-developer-survey-most-used-frameworks-web/)*" width="557" height="571"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Various Unit testing frameworks for Selenium automation like Angular are available making it easy for developers to write their own Unit tests cases. Angular is considered to be one of the best web development frameworks. It comes with in-built Unit testing tools like Jasmine and Karma for automated Unit testing.&lt;/p&gt;

&lt;p&gt;In this Angular testing tutorial, we discuss in-depth the benefits of Unit testing and how to perform Angular testing with Jasmine and Karma using Selenium.&lt;/p&gt;

&lt;p&gt;With the assumption that you are comfortable with Typescript and Angular applications, let’s get started!&lt;/p&gt;

&lt;p&gt;Now perform live interactive manual testing of your Angular websites on LambdaTest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do you know &lt;a href="https://www.lambdatest.com/web-technologies/css3-cursors-grab?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun06_sd&amp;amp;utm_term=sd&amp;amp;utm_content=web_technologies" rel="noopener noreferrer"&gt;CSS grab &amp;amp; grabbing cursors &lt;/a&gt;adds support for the css 3 cursor property. It is useful to indicate that something can be grabbed (dragged to be moved).&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Unit Testing
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://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%2ASvaQHPUXMdJVAoUq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2ASvaQHPUXMdJVAoUq.png" width="512" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Unit testing is a type of software technique where individual units or components of an application are tested to ascertain they are working as expected. Unit tests are typically automation tests written and run by the software developer during the coding phase to test standalone modules of the application for its functional correctness. A unit can be an individual function, method, procedure, module, or object.&lt;/p&gt;

&lt;p&gt;The main goal of Unit testing is to break the application source code into isolated units to identify, analyze and fix the defects arising from that particular unit. Testing a unit in isolation means testing without external resources like API endpoint or database. Instead, substitutes such as stub methods, mock objects or fakes will be used to harness a fake response and test code will be written based on that.&lt;/p&gt;

&lt;p&gt;Example: A library named Moxios is used to mock API calls using Axios.*&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AND9gsP-v5KnJ7Wbk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AND9gsP-v5KnJ7Wbk.png" width="512" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Developers and product owners often overlook Unit testing by pointing out limited development time and resources as reasons, making them proceed without it. And yes, this seldom works out. But the companies that are serious about the quality delivery of their product understands the significance of Unit testing and is diligent in enforcing it.&lt;/p&gt;

&lt;p&gt;Since we are talking about Unit testing, it is also worth mentioning the most popular approaches that are used in combination with it — Test-Driven Development (TDD) and Behaviour-Driven Development (BDD).&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Unit testing
&lt;/h2&gt;

&lt;p&gt;Unit testing is becoming more popular in the web development world. According to the reports presented by the fourth annual &lt;strong&gt;Developer Ecosystem Survey&lt;/strong&gt; (2020) conducted by JetBrains, 77% of the developers stated that testing is an integral part of the development process in their companies or teams. Furthermore, 68% voted for Unit testing as the most popular approach.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2862%2F0%2AvYV8AApx0kWACB1C.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2862%2F0%2AvYV8AApx0kWACB1C.png" alt="[*Source](https://www.jetbrains.com/lp/devecosystem-2020/testing/)*" width="800" height="299"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Larger or smaller, every project can enjoy the benefits of Unit testing. A few significant advantages are listed below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Helps identify and fix bugs early in the development cycle and save costs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Forces developers to improve the design of implementations as it is difficult to write unit tests for poorly written code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ensures the developer that the new feature added is not breaking the existing code hence giving confidence in code quality.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Simplified debugging fails; only the latest code needs to be if the test fails debugged.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Aids in overall faster development.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Allows better refactoring of code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Good Unit testing can even serve as project documentation. Developers, especially those who newly joined the team, can look at the unit tests to learn about the functionality provided by the unit, how to use it etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Limitations of Unit testing
&lt;/h2&gt;

&lt;p&gt;It is equally important to understand the limitations of Unit testing. A few are listed below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It is impossible to catch every error in the program using Unit testing as it cannot evaluate every execution path. It cannot catch integration errors, complex broad system errors or non-functional attributes like usability, scalability, performance etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The difficulty of setting up realistic and relevant unit tests. If they are not set correctly, it can diminish the value and accuracy of test results.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the Unit test involves just passing input and verifying the output for a method, it tends to be easy to create. However, it is not so simple if it includes something more complex like interaction with an external resource.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do you know &lt;a href="https://www.lambdatest.com/web-technologies/css3-colors?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun06_sd&amp;amp;utm_term=sd&amp;amp;utm_content=web_technologies" rel="noopener noreferrer"&gt;CSS3 Colors&lt;/a&gt; is a small, lightweight and modular color library that allows you to describe RGB and HSLA with arrays, as well as set transparent colors via rgba() and hsla().&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Unit testing With Angular Framework
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://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%2AsbpQMK9o_gohELBh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AsbpQMK9o_gohELBh.png" width="296" height="221"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since Unit testing has become an integral part of development, all the major development frameworks now come with support for Unit testing. Numerous testing libraries and frameworks are available to reduce the amount of time taken to write tests.&lt;/p&gt;

&lt;p&gt;In the further section of Angular testing with Jasmine and Karma tutorial, you will learn how to test Angular applications with Karma Testrunner and Jasmine framework using Selenium.&lt;/p&gt;

&lt;p&gt;But before that, let us know and understand why we are using them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Karma?
&lt;/h2&gt;

&lt;p&gt;Karma is the default test runner for applications created using Angular CLI. Being a direct product of the AngularJS team, Karma is very well suited for testing Angular or any other JavaScript-based project. Running on Node.js, Karma test runner can be seamlessly integrated with testing frameworks like Jasmine, Mocha, QUnit and integration services like Jenkins, CircleCI, TravisCI etc. In addition, it allows you to run your automated tests on various browsers and devices.&lt;/p&gt;

&lt;p&gt;Karma uses a configuration file to set the startup file, the testing framework, the reporters, the browser and other things. It aims to bring a productive testing environment to developers by spending less time on setting up the configurations but more time on coding. Additionally, there is also good online documentation.&lt;/p&gt;

&lt;p&gt;The NPM package download trends show that even in 2021 Karma is the most popular Test Runner for Angular Testing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AsbFx7OctFZlT5OAc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AsbFx7OctFZlT5OAc.png" alt="[*Source](https://www.npmtrends.com/ava-vs-karma-vs-protractor)*" width="800" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Jasmine?
&lt;/h2&gt;

&lt;p&gt;Jasmine is a testing framework for JavaScript that supports the &lt;strong&gt;Behavior-Driven Development&lt;/strong&gt; (BDD) practice. It plays very well with Karma and is part of the default setup with Angular CLI. Jasmine is known to be feature-rich and provides easy integration with reporting. Also, it provides additional functions such as fakes, spies and pass-throughs. We will be performing Angular Testing with Jasmine.&lt;/p&gt;

&lt;p&gt;Behaviour-Driven Development (BDD) is a testing practice that focuses on the application’s behavior. BDD allows you to write tests in human-readable (simple English) format so that even non-technical people can easily understand what is being tested. This plain English format used to write BDD test scenarios is known as the **Gherkin **language.&lt;/p&gt;

&lt;p&gt;The BDD tests can act as living test documentation that is easy to maintain and can be consumed by all team members including product owners, business analysts, developers, testers, 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%2F2000%2F0%2AgrNZ8E92aNTEAhTn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AgrNZ8E92aNTEAhTn.png" width="512" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will be writing our unit tests using Jasmine following the BDD approach. Then using Karma test runner, we can trigger the test execution from the CLI. Karma also has a watch mode that can automatically detect changes in development files and re-runs the test. Don’t worry! You will reach a better understanding once you start writing the unit tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Selenium?
&lt;/h2&gt;

&lt;p&gt;Although Selenium is a tool that needs no introduction among software testers, because this article is primarily aimed at developers, let us first specify &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun06_sd&amp;amp;utm_term=sd&amp;amp;utm_content=" rel="noopener noreferrer"&gt;what is Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Selenium is a free open-source test automation framework that is used to automate web browsers. It allows you to validate websites and web apps across multiple devices, browsers and OS to ensure it is working properly as expected. Selenium Webdriver and Selenium Grid are the integral components of the Selenium automation framework.&lt;/p&gt;

&lt;p&gt;If you are looking for ways to scale by running tests distributedly, Selenium Grid is the best choice. Selenium Grid allows you to run tests parallely on multiple machines (environments) against various browsers, OS and device combinations hence boosting the speed of the testing cycle without compromising on the test coverage.&lt;/p&gt;

&lt;p&gt;In this Angular testing with Jasmine and Karma tutorial, we will be configuring the Jasmine framework with the online Selenium Grid provided by LambdaTest platform. LambdaTest serves you with a cloud-based Grid infrastructure through which you can access 3000+ desktop and mobile browser environments.&lt;/p&gt;

&lt;p&gt;Now that you got a gist of the above discussed tools, let us move on to writing Unit test cases in an Angular application.&lt;/p&gt;

&lt;p&gt;But before that let us set up a dummy project upon which we will be writing meaningful Unit tests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Check this out: &lt;a href="https://www.lambdatest.com/web-technologies/css3-attr?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun06_sd&amp;amp;utm_term=sd&amp;amp;utm_content=web_technologies" rel="noopener noreferrer"&gt;CSS3 attr() function for all properties&lt;/a&gt;- With CSS Values and Units Level 3, the “attr()” function can be used on any CSS property, plus with non-string values (e.g., numbers and colors).&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up an example project using Angular, Karma and Jasmine
&lt;/h2&gt;

&lt;p&gt;You can find all the code samples used in this Angular testing tutorial from the &lt;a href="https://github.com/harita-R/angular-unit-test" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;. Let us begin by creating a new Angular app.&lt;/p&gt;

&lt;p&gt;Following are my choice of tools.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Angular CLI&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Visual Studio Code as IDE&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can run the below command to install Angular CLI globally.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g [@angular/cli](http://twitter.com/angular/cli)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Create a new Angular project using Angular CLI. This will take care of Jasmine and Karma configuration for us out-of-the-box.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Open the IDE, create a new project folder and run the following command in the terminal. Here, I am naming the project as “angular-unit-test”.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ng new angular-unit-test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The project is set up successfully with all the dependencies installed. The root folder structure now 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%2F2000%2F0%2AoIJXSYrHz_0OnNGU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AoIJXSYrHz_0OnNGU.png" width="267" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To run the test, pass the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ng test
&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%2F2888%2F1%2AskfDowa4yXuQwmmZlQJuiQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2F1%2AskfDowa4yXuQwmmZlQJuiQ.png" width="800" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The test execution starts and a new browser instance gets opened. Observe the browser report and terminal logs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AKMHZaI1Lr5NQvVEA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AKMHZaI1Lr5NQvVEA.png" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Let us start building our app by creating new components. For the demo, I will be creating a simple Angular app using Bootstrap containing the following components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Header&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Footer&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Login Form&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Signup Form&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://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%2AR8qeaLhaI1RYB45F.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AR8qeaLhaI1RYB45F.png" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run the following command to generate new components using Angular CLI.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ng g c folder_name/component_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;All my components have been placed inside a folder named “components” in src/app. To create a component named “header”, run the following command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ng g c components/header
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The header component is created successfully. Notice that 4 files are auto-generated for every component — html,css, ts and spec.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AmYW5UeWLR8JC2NVk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AmYW5UeWLR8JC2NVk.png" width="340" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will be writing our Unit test inside the spec.ts file. By default, the file will 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%2F2652%2F0%2AWAzs_p1mETU9RrC4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2652%2F0%2AWAzs_p1mETU9RrC4.png" width="800" height="739"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since explaining basic Angular things is out of this tutorial’s scope, I won’t be covering the app creation. Refer to the steps below to clone and set up my completed sample app.&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/harita-gr/MyStay_app.git](https://github.com/harita-gr/MyStay_app.git)

npm install

npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Configure testing in your Angular Project.&lt;/p&gt;

&lt;p&gt;When we run the ng test, Angular takes the test configuration from the angular.json file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AVVfTegW7e7dmjKSX.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AVVfTegW7e7dmjKSX.jpg" width="800" height="619"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The test-related configuration mainly lies on the shoulder of these 2 files.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;test.ts&lt;/strong&gt; — also known as test entry file, this file is used to load all spec.ts files from the project for testing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;karma.conf.js&lt;/strong&gt; — here we specify configurations like browser, port, report, online grid etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let us take a look at our &lt;strong&gt;test.ts&lt;/strong&gt; file. This file is the entry point of tests for the application. You don’t have to make any changes to the &lt;strong&gt;test.ts&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;Let us quickly grasp what is going on here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;At the beginning of the file, we are initializing our Angular test environment using all the imports.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After this, we find all the test files by matching their name against the regular expression (filename ending with spec.ts ) and load them.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;**TestBed **is also initialized in this file. If you are wondering what TestBed is, it is a powerful Unit testing tool provided by Angular. It creates a dynamically constructed test module that emulates an Angular NgModule.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2A3dPPZB4TsK-FhDZ_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2A3dPPZB4TsK-FhDZ_.png" width="800" height="559"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let us move on to the &lt;strong&gt;karma.conf.js&lt;/strong&gt;  file. But as you can notice, most of them are self-explanatory. So let us quickly go through some of them.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;frameworks *&lt;/em&gt;— Here we set our test automation framework which is Jasmine by default. But you can go on and change it as per your project requirement.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;reporters *&lt;/em&gt;— This is where we set our reporters. You can modify or add new ones.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;autoWatch *&lt;/em&gt;— If this is set to true, the test will run in watch mode. When you change any file and save it, tests are re-build and re-run.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;port *&lt;/em&gt;— This is where you can set your port.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;browsers *&lt;/em&gt;-Here you can set the browsers on which the tests should run. By default it is Chrome. But you can install and add new browser launchers.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are running your tests on a local machine, this configuration is good enough. But since we will be using an online grid for running our tests, we need to make some additional configurations which are as follows.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Set the webdriver configuration for running in the LambdaTest host.We need to pass a grid hostname, port, and user and access key. You can fetch your unique username and access key by &lt;a href="https://accounts.lambdatest.com/register?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun06_sd&amp;amp;utm_term=sd&amp;amp;utm_content=register" rel="noopener noreferrer"&gt;creating a free LambdaTest&lt;/a&gt; account and navigating to your profile page.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Lambdatest grid hostname and port
      var webdriverConfig = {
        hostname: "hub.lambdatest.com",
        port: 80,
        user: process.env.LT_USERNAME || 'YOUR_USERNAME',
        accessKey: process.env.LT_ACCESS_KEY || 'YOUR_ACCESSKEY',
      };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2- As our tests will be run on a cloud grid, we no longer need the browser launcher set up. Instead, install the webdriver launcher and add it as a plugin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; npm install -D karma-webdriver-launcher
&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;plugins: [
          require("karma-jasmine"),
          require("karma-webdriver-launcher"),
          require("karma-jasmine-html-reporter"),
          require("karma-coverage"),
          require("[@angular](http://twitter.com/angular)-devkit/build-angular/plugins/karma"),
        ],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3- Now, we need to define our custom launchers. LambdaTest provides you access to 3000+ browser/device/OS configurations. So you no longer need to restrict yourself by testing only on one device/browser/OS like when you test locally. I am configuring the tests to run in Chrome, Firefox and Edge in a custom browser version and OS.The configuration needs to be done similar to the object given below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;customLaunchers: {
          chrome: {
            base: "WebDriver",
            config: webdriverConfig,
            browserName: "chrome",
            platform: "windows 8.1",
            version: "71.0",
            build:'Unit testing in Angular', //Build name
            name: "Unit test", // Test name
            pseudoActivityInterval: 5000, // 5000 ms heartbeat
          },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can generate Selenium Capabilities using &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun06_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Desired Capability&lt;/a&gt; Generator to find your custom combination.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AsU2XCvIbQFn_6-Sf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AsU2XCvIbQFn_6-Sf.png" width="800" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4- Also update the browser name w.r.t the custom launcher.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;browsers: ["chrome"],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s all! The final karma.conf.js file now looks 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;// Karma configuration file, see link for more information
    // [https://karma-runner.github.io/1.0/config/configuration-file.html](https://karma-runner.github.io/1.0/config/configuration-file.html)

    module.exports = function (config) {
      // Lambdatest grid hostname and port
      var webdriverConfig = {
        hostname: "hub.lambdatest.com",
        port: 80,
        user: process.env.LT_USERNAME || 'YOUR_USERNAME',
        accessKey: process.env.LT_ACCESS_KEY || 'YOUR_ACCESSKEY',
      };

      config.set({
        basePath: "",
        // hostname: 'localhost', // hostname, where karma web server will run
        frameworks: ["jasmine", "[@angular](http://twitter.com/angular)-devkit/build-angular"],
        plugins: [
          require("karma-jasmine"),
          require("karma-webdriver-launcher"),
          require("karma-jasmine-html-reporter"),
          require("karma-coverage"),
          require("[@angular](http://twitter.com/angular)-devkit/build-angular/plugins/karma"),
        ],
        client: {
          jasmine: {},
          clearContext: false, // leave Jasmine Spec Runner output visible in browser
        },
        jasmineHtmlReporter: {
          suppressAll: true, // removes the duplicated traces
        },
        coverageReporter: {
          dir: require("path").join(__dirname, "./coverage/angular-unit-test"),
          subdir: ".",
          reporters: [{ type: "html" }, { type: "text-summary" }],
        },
        reporters: ["progress", "kjhtml"],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ["chrome","firefox","edge"],
        singleRun: false,
        restartOnFileChange: true,

        customLaunchers: {
          chrome: {
            base: "WebDriver",
            config: webdriverConfig,
            browserName: "chrome",
            platform: "windows 8.1",
            version: "71.0",
            build:'Unit testing in Angular', //Build name
            name: "Unit test", // Test name
            pseudoActivityInterval: 5000, // 5000 ms heartbeat
          },
          firefox: {
            base: "WebDriver",
            config: webdriverConfig,
            browserName: "Firefox",
            platform: "windows 10",
            version: "91.0",
            build:'Unit testing in Angular',
            name: "Unit test",
            pseudoActivityInterval: 5000, // 5000 ms heartbeat
          },
          edge: {
            base: "WebDriver",
            config: webdriverConfig,
            browserName: "MicrosoftEdge",
            platform: "windows 10",
            version: "93.0",
            build:'Unit testing in Angular',
            name: "Unit test",
            pseudoActivityInterval: 5000, // 5000 ms heartbeat
          }
        },
      });
    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5- Let us test our configuration by triggering our test runner.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Our test will start running on a 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%2AXl4qHrgh0g-eaw9W.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AXl4qHrgh0g-eaw9W.png" width="800" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go to the LambdaTest Automation Dashboard to access the results of your 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%2F3200%2F0%2ArU8HXyWmY7RKhiwc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2ArU8HXyWmY7RKhiwc.png" width="800" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This &lt;a href="https://www.youtube.com/watch?v=BQ-9e13kJ58&amp;amp;list=PLZMWkkQEwOPl0udc9Dap2NbEAkwkdOTV3?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun06_sd&amp;amp;utm_term=sd&amp;amp;utm_content=tutorial_video" rel="noopener noreferrer"&gt;Selenium JavaScript testing tutorial&lt;/a&gt; for beginners and professionals will help you learn everything you need to know to work with JavaScript for Selenium automation testing.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Writing your first Angular Unit Test using Jasmine
&lt;/h2&gt;

&lt;p&gt;We have successfully completed our test configuration.In this section of Angular testing with Jasmine, we will write unit test cases for Angular using Jasmine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Test Run flow
&lt;/h2&gt;

&lt;p&gt;As mentioned earlier, associated with each component, a spec.ts file will be generated inside which we will be writing our test case. The default spec file generated for the App component app.component.spec.ts is 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%2A6A0mERSrx3eOWzcz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2A6A0mERSrx3eOWzcz.png" width="800" height="628"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us understand the test run flow.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Import all the tools we will be going to use like TestBed.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Import all of the dependencies for this component.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To start our test block, we use the “&lt;strong&gt;describe&lt;/strong&gt;” function, the title of which will be matching the component name under test.describe(string, function)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://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%2AhQJ7HiwIvwCJMNKB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AhQJ7HiwIvwCJMNKB.png" width="788" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Then we have the “it” function, which is used to define test cases. Meaningful names can be given to the test case.it(string, function)&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Additionally, we also have setup and teardown functions which will be run before or after each “it” function. Here, “&lt;strong&gt;beforeEach&lt;/strong&gt;” is defined which contains async code. The purpose of this is to await till all the async code is finished before continuing. This function is called before each test specification (it function) is run.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://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%2AJo2twv9q2Dt9RnPm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AJo2twv9q2Dt9RnPm.png" width="800" height="575"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have three Unit tests over here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First one verifies that the app is created successfully.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Second one verifies the app title is equal to the expected value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Third one verifies that the title is rendered.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The overall run flow can be visualized as 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%2F2000%2F0%2A_nTkhxQEX_2Sgdo2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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_nTkhxQEX_2Sgdo2.png" width="789" height="789"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing Unit test cases for Components
&lt;/h2&gt;

&lt;p&gt;Now that you have an idea about how the test run flows, let us start by writing Unit test cases for the header component. To write meaningful tests, it is crucial to identify what you need to test.&lt;/p&gt;

&lt;p&gt;For the header component, I want to verify the following cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It should display a logo containing the text “My Stay”.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It should have six nav links.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It should navigate to the register page by clicking the signup/login button.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The code for testing the header component 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;//header.component.spec.ts

    import { ComponentFixture, TestBed, tick } from '@angular/core/testing';
    import { By } from '@angular/platform-browser';
    import { Location } from "@angular/common";

    import { HeaderComponent } from './header.component';

    describe('HeaderComponent', () =&amp;gt; {
      let component: HeaderComponent;
      let fixture: ComponentFixture&amp;lt;HeaderComponent&amp;gt;;

      beforeEach(async () =&amp;gt; {
        await TestBed.configureTestingModule({
          declarations: [ HeaderComponent ]
        })
        .compileComponents();
      });

      beforeEach(() =&amp;gt; {
        fixture = TestBed.createComponent(HeaderComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });

      it('should create', () =&amp;gt; {
        expect(component).toBeTruthy();
      });

      it('should display logo containing text "My Stay"', () =&amp;gt; {
         const logoText =fixture.debugElement.query(By.css('.navbar-brand')).nativeElement;
         expect(logoText.textContent).toBe('My Stay ');
      });

      it('should contain 6 nav links', () =&amp;gt; {
        const navLinks = fixture.debugElement.queryAll(By.css('li.nav-item'));
        expect(navLinks.length).toEqual(6);
      });

      it('navigates to register page on clicking signup/signin button', () =&amp;gt; {
        const location: Location = TestBed.inject(Location);
        const button =fixture.debugElement.query(By.css('button.btn')).nativeElement;
        button.click();
        expect(location.path()).toBe('/user-register');
      });
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now I want to test my footer component. Following are the test cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It should contain the copyright text&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It should contain three social media contact buttons&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The code for testing footer component 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;//footer.component.spec.ts

    import { ComponentFixture, TestBed } from '@angular/core/testing';
    import { By } from '@angular/platform-browser';

    import { FooterComponent } from './footer.component';

    describe('FooterComponent', () =&amp;gt; {
      let component: FooterComponent;
      let fixture: ComponentFixture&amp;lt;FooterComponent&amp;gt;;

      beforeEach(async () =&amp;gt; {
        await TestBed.configureTestingModule({
          declarations: [ FooterComponent ]
        })
        .compileComponents();
      });

      beforeEach(() =&amp;gt; {
        fixture = TestBed.createComponent(FooterComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });

      it('should create', () =&amp;gt; {
        expect(component).toBeTruthy();
      });

      it('should contain copyright text', () =&amp;gt; {
        const text =fixture.debugElement.query(By.css('.copyright')).nativeElement;
        expect(text.textContent).toContain('Copyright');
      });

      it('should contain 3 social media contact buttons ', () =&amp;gt; {
        const btns = fixture.debugElement.queryAll(By.css('a.btn'));
        expect(btns.length).toEqual(3);
      });
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, I want to test my Login Component. Following are the test cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It should be able to login by entering an email and password and submitting form&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It should route to register page by clicking the “Sign Up here” link&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The code for testing the login component 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;//login.component.spec.ts

    import { ComponentFixture, TestBed } from '@angular/core/testing';
    import { FormsModule } from '@angular/forms';
    import { By } from '@angular/platform-browser';
    import { Location } from "@angular/common";

    import { LoginComponent } from './login.component';

    describe('LoginComponent', () =&amp;gt; {
      let component: LoginComponent;
      let fixture: ComponentFixture&amp;lt;LoginComponent&amp;gt;;

      beforeEach(async () =&amp;gt; {
        await TestBed.configureTestingModule({
          imports: [FormsModule ],
          declarations: [LoginComponent],
        }).compileComponents();
      });

      beforeEach(() =&amp;gt; {
        fixture = TestBed.createComponent(LoginComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });

      it('should create', () =&amp;gt; {
        expect(component).toBeTruthy();
      });

      it('should be able to login by entering email and password and submitting form', () =&amp;gt; {
        component.User['email'] = 'test@test.com';
        component.User['password'] = '123456789';
        const loginBtn = fixture.debugElement.query(By.css('button.login-btn')).nativeElement;
        loginBtn.click();
        expect(component.submitted).toBeTrue;
      });


      it('should route to register page by clicking “Sign Up here” link', () =&amp;gt; {
        const location: Location = TestBed.inject(Location);
        const link =fixture.debugElement.query(By.css('a')).nativeElement;
        link.click();
        expect(location.path()).toBe('/user-register');
      });
    });

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

&lt;/div&gt;



&lt;p&gt;Now it is time to run the test cases. Execute the following command.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Go to the LambdaTest Automation Dashboard. You can notice the test starts running on a cloud-based 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%2F3200%2F0%2AXY1Q95S5v_Nnnqlb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AXY1Q95S5v_Nnnqlb.png" width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go to Automation Logs to see the detailed report of your 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%2Am1ofcw7wpbf-IMLg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2Am1ofcw7wpbf-IMLg.png" width="800" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have successfully executed our Unit test cases in Angular.&lt;/p&gt;

&lt;p&gt;Navigate to the &lt;a href="https://accounts.lambdatest.com/login?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun06_sd&amp;amp;utm_term=sd&amp;amp;utm_content=" rel="noopener noreferrer"&gt;LambdaTest Analytics Dashboard&lt;/a&gt; to view test performance metrics of your Unit tests. The Test Overview will provide a screenshot of tests with consistent behavior. Meanwhile, the Test Summary will show the total number of passed or failed tests, and any completed or queued 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%2AvqxjU-OOIsRxKodH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AvqxjU-OOIsRxKodH.png" width="800" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices to Improve Unit testing
&lt;/h2&gt;

&lt;p&gt;While performing Unit testing, here are some best practices one should keep in mind.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Follow a consistent naming convention and test one code at a time&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Make sure that there is a corresponding unit test case to every module&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Before moving on to the next phase, all the bugs should be fixed. So you can end up with a clean and reliable code for committing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Continuously refactor test code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, focus more on the tests that affect the behavior of the system.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s a short glimpse of the Selenium JavaScript 101 certification from LambdaTest:&lt;/p&gt;

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

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

&lt;p&gt;In this Selenium JavaScript tutorial on Angular testing with Jasmine and Karma, we learnt how to perform Unit testing in Angular framework with Jasmine and Karma test runner using Selenium. We also covered practical implementation.&lt;/p&gt;

&lt;p&gt;As the code base keeps on growing it becomes time-consuming to execute the unit tests locally in our system in addition to the limited scope test coverage. So, we utilized the online Selenium Grid provided by LambdaTest platform to leverage our test execution by running the unit tests parallelly across multiple custom-configured environments.&lt;/p&gt;

&lt;p&gt;I hope you found this Angular testing tutorial useful!&lt;/p&gt;

&lt;p&gt;Happy Testing!&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>testing</category>
      <category>webdev</category>
      <category>techtalks</category>
    </item>
    <item>
      <title>How To Speed Up JavaScript Testing With Selenium and WebDriverIO?</title>
      <dc:creator>HaritaLT</dc:creator>
      <pubDate>Fri, 03 Jun 2022 18:58:30 +0000</pubDate>
      <link>https://forem.com/testmuai/how-to-speed-up-javascript-testing-with-selenium-and-webdriverio-4ceb</link>
      <guid>https://forem.com/testmuai/how-to-speed-up-javascript-testing-with-selenium-and-webdriverio-4ceb</guid>
      <description>&lt;p&gt;Automation testing has become an integral part of the Agile way of software development. As the application grows, it becomes crucial to have a reliable testing framework to cope with the development velocity, detect the bugs at an early stage, and ensure quality. However, to achieve greater speed, scalability, wider test coverage, and a better ROI, it becomes important to adopt various testing strategies. &lt;a href="https://www.lambdatest.com/learning-hub/webdriverio?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun03_sd&amp;amp;utm_term=sd&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;WebdriverIO&lt;/a&gt; is a JavaScript testing framework extensively adopted by top-companies worldwide to simply and concisely fulfill all their testing needs quickly and efficiently.&lt;/p&gt;

&lt;p&gt;Built on top of NodeJS and Selenium Webdriver (W3C WebDriver) API, WebdriverIO supports both web and mobile application testing. Since you are taking your time to read this article, it means you might fall into any of the below categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You might be just starting with WebdriverIO and want to lay the foundation right to build a robust framework for JavaScript testing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You are already using WebdriverIO and seeking out how to optimize your framework and speed up the JavaScript testing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You might be keen on getting some idea on how to level up your JavaScript with Selenium automated testing game and extract more out of the framework.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we will be trying to cater to each of these requirements and see how to speed up JavaScript testing. With the expectation that at least a few of the tips mentioned here will save your day, let’s get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices to Adopt While Developing WebdriverIO Framework
&lt;/h2&gt;

&lt;p&gt;Honestly, setting up an automated testing framework is not an easy task, especially when you are using JavaScript with Selenium. But once the foundation is laid right, with timely maintenance, the benefits that can be reaped are limitless.&lt;/p&gt;

&lt;p&gt;In this section, we will be discussing some key points that need to be considered, especially if you are just starting up with the WebdriverIO framework setup for JavaScript testing. Let’s look into it!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.WebdriverIO Version&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First and foremost, go for the latest version of WDIO, i.e., currently v6, released in early 2020. According to official documentation, LTS will be provided for v5 till April 2021, and v4 is currently deprecated. Now let’s see some of the desirable features that v6 has to offer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;a) Natively supports Puppeteer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To run a local test case, you are free to switch between Puppeteer and WebdriverIO API as you wish. Also, you won’t need to download the browser driver anymore. With WebdriverIO, you can check if a browser driver is running and accessible at localhost:4444/. In case it doesn’t, check if it uses Puppeteer as a fallback. Being able to access Puppeteer in your tests allows you to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Leverage the Chrome DevTools protocol capabilities, with a much richer set of automation features.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run commands on Puppeteer much faster. Hence it can help speed up local execution.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;b) Performance Improvements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This has been one of the primary design goals of WDIO v6. It has also come with many internal improvements that will speed up test execution and lower CPU and memory usage.&lt;/p&gt;

&lt;p&gt;And there’s a lot more. You can always refer to the &lt;a href="https://webdriver.io/blog/2020/03/26/webdriverio-v6-released.html" rel="noopener noreferrer"&gt;WebdriverIO official documentation&lt;/a&gt; to know more about WebdriverIO v6.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Page Object Model (POM)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;POM is a framework design pattern widely adopted by Agile practitioners for easy test script maintenance and enhanced code reusability. Implementing an approach like POM will help you efficiently manage your locators, reduce duplication, and make your framework more programmer-friendly.&lt;/p&gt;

&lt;p&gt;So even if this directly won’t speed up your execution time, in the long run, the POM approach can save your time spent on creating and maintaining test scripts for JavaScript testing. You can refer to the &lt;a href="https://webdriver.io/docs/pageobjects.html" rel="noopener noreferrer"&gt;WebdriverIO Page Object Model&lt;/a&gt; page to know more about it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Create Helper Methods&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, this is also linked with code reusability and ease of maintenance. Fundamentally, the idea is to wrap the code for commonly performed actions — like logging in, clicking on something, waiting for a result, logging out, etc. into a method so that they can be shared and reused by all scripts.&lt;/p&gt;

&lt;p&gt;Also, you can add parameters to customize these helper methods to suit various function call needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Don’t test the same functionality twice&lt;/strong&gt;&lt;br&gt;
To speed up the testing, we should be strategic in terms of test coverage. Reducing the duplicate and avoidable scenarios directly helps to bring the test case count down.&lt;/p&gt;

&lt;p&gt;Needless to explain more, the lower the test case count, the faster the execution result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Avoid Hardcoding URLs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In WebdriverIO, we have a configuration file wdio.conf.js, which aids us in specifying the location of the tests, frameworks you want to use, reporters, browsers, and all other general configs of the project.&lt;/p&gt;

&lt;p&gt;Another useful option is to configure the baseUrl of the application here so that we don’t need to specify it every time in our test scripts.&lt;/p&gt;

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

&lt;p&gt;But generally, in most cases, we might need to run the application in multiple environments like — development, QA, staging, production, etc., each having different URLs. In that case, we can easily configure the URLs by declaring an environment variable and passing the detail of the environment/URL in which the test should run along with the test run command.&lt;/p&gt;

&lt;p&gt;The configuration steps are briefly explained below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a separate file to maintain the URLs and export them. Here we have created a file named ‘wdio.config.url.js’ (Give any name of choice)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AQsis7egES1bokMQF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AQsis7egES1bokMQF.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; The remaining settings are to be done in the wdio.conf.js file. First of all, require the URL file.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const urls = require(‘./wdio.config.url’)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Create an environment variable to pass the URL values.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ENV = process.env.ENV
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Create a condition to check if the value passed for ENV is valid.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`if (!ENV || !['qa', 'dev', 'live'].includes(ENV)) {
    console.log('Invalid environment! Please select : ENV=qa|dev|live')
    process.exit()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Now, inside exports.config change the baseUrl to match the environment variable.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;baseUrl: url[process.env.ENV],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; That’s pretty much it. Now along with the test run command, pass the value for ENV.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run test ENV=dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Here, the URL for DEV will be mapped, and the test starts running. If any invalid value is passed or the value of ENV is skipped, corresponding logs will be generated as per the check condition we created.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Writing a good test script&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, it all comes to writing a good test script. Following are a few good practices that you can adopt while designing your test script while performing JavaScript testing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Writing only one test case in a test file. This helps in keeping the test cases independent of each other, analyzing failure, and also aids in parallel testing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keeping it short or atomic. Reduce the number of steps to below 20 (there is no golden rule). Make use of helper methods.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can take this certification as proof of expertise in the field of test automation with JavaScript to empower yourself and boost your career.&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the Selenium JavaScript 101 certification from LambdaTest:&lt;/p&gt;

&lt;p&gt;{% youtube DQwSRfCkhSM %}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Have you read about &lt;a href="https://www.lambdatest.com/web-technologies/css-paint-api?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun03_sd&amp;amp;utm_term=sd&amp;amp;utm_content=web_technologies" rel="noopener noreferrer"&gt;CSS Paint API&lt;/a&gt;? Css-paint-api is a Python library that lets you generate images used by CSS using code.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Optimize Your Framework to Speed Up JavaScript Test Execution
&lt;/h2&gt;

&lt;p&gt;So far, we have covered some of the best practices that can be adopted while developing the automated testing framework. Here, we will be discussing how we can optimize the WebdriverIO framework to speed up JavaScript testing while using JavaScript with Selenium.&lt;/p&gt;

&lt;p&gt;Let’s look into some possible factors that can slow down the testing and methods that can be adopted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Parallel Testing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As the application features grow, so does the test count. It becomes more and more vital to enhance the execution speed and reduce overall execution time. One way of achieving this is through parallel testing, i.e., running your test scripts concurrently.&lt;/p&gt;

&lt;p&gt;To enable parallel testing, adjust the maxInstances property in your config file. See the below sample code.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.config = {
            // ...
            // set maxInstance for all browser
            maxInstances: 3,
            // ...
            capabilities: [{
                browserName: 'firefox'
            }, {
                browserName: 'chrome'
            }],
            // ...
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Here the maxInstances is set to 3, and 2 capabilities are mentioned (Chrome, Firefox). The math is simple. The WDIO test runner will spawn 6 processes, i.e., 3 test files will run simultaneously in the 2 browsers. You can adjust the values according to your requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Disabling screenshots&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Screenshots are often our saviors when it comes to debugging. But it also comes with the cost of execution time and slowing down your tests. Hence, the suggestion is to enable screenshots only when test failure occurs instead of taking screenshots for each and every step.&lt;/p&gt;

&lt;p&gt;This &lt;a href="https://webdriver.io/docs/docs/rpii-wdio-html-reporter.html#to-take-a-screenshot-after-any-test-fails" rel="noopener noreferrer"&gt;WebdriverIO official documentation on the screenshot&lt;/a&gt; will guide you on how to implement this using Hooks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Error Logging&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Configuring error logging doesn’t have any direct impact on execution speed up. However, the idea is to enable logs only when some error occurs, hence keeping the log data more relevant, assisting us in quick debugging.&lt;/p&gt;

&lt;p&gt;The level of logging verbosity can be set in the wdio.conf.js file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AVbM2lyNqPnm9k0Wg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AVbM2lyNqPnm9k0Wg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Use the right wait methods&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;WebdriverIO provides us with various methods to assert wait in the program. It’s good to have an idea about which is to be used when and most importantly, which to avoid.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;a) pause()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method pauses execution for a specific amount of time. To avoid flaky test results, it is recommended not to use this command to wait for an element to show up, a page to load, or an action to happen. It is better to use commands like waitUntil, waitForExist, or other waitFor* commands.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;```browser.pause(milliseconds)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;b) waitUntil()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This wait command is your universal weapon if you want to wait on something. It expects a condition and waits until that condition is fulfilled with a truthy value.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;browser.waitUntil(condition, { timeout, timeoutMsg, interval })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;c) waitFor&lt;/strong&gt;*&lt;/p&gt;

&lt;p&gt;WebdriverIO offers many waitFor APIs, which helps to wait until an element is displayed, exists, clickable, etc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;waitForClickable&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;waitForDisplayed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;waitForEnabled&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;waitForExist&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using the waitUntil() or waitFor* methods will make the script performance better. As the elements can be accessed as soon as they are available, and no extra waiting time is required.&lt;/p&gt;

&lt;p&gt;Do check out the official &lt;a href="https://webdriver.io/docs/api.html" rel="noopener noreferrer"&gt;WebdriverIO API documentation&lt;/a&gt; for more information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Use headless browsers and drivers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A headless browser is a browser that does not have a user interface. Headless testing is a way of running your test cases without browser UI, making your script run faster. This is an option you can consider if you don’t need a real browser for testing or using CI/CD.&lt;/p&gt;

&lt;p&gt;And no need to worry! You will still be able to capture screenshots and see how your application looks in headless mode. Debugging can be done as effectively. In WDIO, by adding a few arguments in your config file, you can adopt the headless version of your favorite browser.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;a) For Firefox&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;browserName: 'firefox',
'moz:firefoxOptions': {
    args: ['-headless']
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;b) For Chrome&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;browserName: 'chrome',        
        'goog:chromeOptions': {        
            args: ['--headless', '--disable-gpu']
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Check this out: &lt;a href="https://www.lambdatest.com/web-technologies/css-placeholder-shown?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun03_sd&amp;amp;utm_term=sd&amp;amp;utm_content=web_technologies" rel="noopener noreferrer"&gt;:placeholder-shown CSS pseudo-class&lt;/a&gt;- Form elements sometimes provides a hint to the user on type of input they should give. Example, the HTML5 placeholder attribute. :input:placeholder-shown matches an element that is showing such placeholder text.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Further Enhancements to Your Test Framework
&lt;/h2&gt;

&lt;p&gt;WebdriverIO is an extremely feature-rich framework that provides tight integration with various services. This includes Jenkins, Bamboo, along with Docker for CI/CD implementations, Applitools for Visual Testing, LambdaTest for Cross Browser Testing, etc. In this section, we will talk about a few of the widely executed strategies while using JavaScript with Selenium that will level up your automated JavaScript testing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1) Automation Testing using CI/CD Pipeline&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To achieve speedy delivery with feedback on quality, it is crucial to integrate the automated testing into the CI/CD (Continuous Integration/Continuous Delivery) pipeline. This helps in enabling Continuous Testing.&lt;/p&gt;

&lt;p&gt;WebdriverIO provides tight integration with CI systems like Jenkins. Being open-source, Jenkins is widely preferred in the automation testing world. With some quick and easy steps, Jenkins integration can be achieved in your WebdriverIO framework. Do check out the &lt;a href="https://webdriver.io/docs/jenkins.html" rel="noopener noreferrer"&gt;Jenkins official guide&lt;/a&gt; for Jenkins set-up!&lt;/p&gt;

&lt;p&gt;Another favored strategy is to integrate Jenkins with container technologies like Docker. With the advent of Docker, practically all necessary application dependencies can be containerized. With this &lt;a href="https://webdriver.io/docs/docs/wdio-docker-service.html" rel="noopener noreferrer"&gt;WDIO-Docker service&lt;/a&gt;, you may run your application container in your CI and in complete isolation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2) Cross Browser Testing with Selenium Grid&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To stay ahead in a highly competitive market, it is our duty as testers to ensure the application’s quality in all scenarios, i.e., the app should behave as expected across all platforms, browsers, and devices without crashing. This is where test coverage and cross browser testing comes into the picture.&lt;/p&gt;

&lt;p&gt;When it comes to distributed test execution, &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun03_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Cloud Selenium Grid&lt;/a&gt; is always a good call. It can run test cases on different versions of browsers and different operating systems simultaneously. While using JavaScript with Selenium, you can use Selenium Grid along with the right cloud-based solution provider for cross browser testing. This could be an optimal strategy that you can deploy to assure quality.&lt;/p&gt;

&lt;p&gt;LambdaTest is a popular cloud-based platform that provides you access to 2000+ browsers for both mobile and desktop to help you gain the maximum test coverage during the automated browser testing. Moreover, you can choose from a wide range of Windows and Mac operating systems, along with all legacy and latest browsers.&lt;/p&gt;

&lt;p&gt;WedriverIO provides you service to seamlessly integrate LambdaTest into your framework through a few quick and easy steps. Let’s take a look!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; In your project terminal, give the following command to save LambdaTest service as a devDependency.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i wdio-lambdatest-service --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Create a free account or log in to your &lt;a href="https://accounts.lambdatest.com/login?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun03_sd&amp;amp;utm_term=sd&amp;amp;utm_content=it_sign_up" rel="noopener noreferrer"&gt;LambdaTest account&lt;/a&gt; and fetch your unique username and access key. You can fetch the details from the Profile section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Now let’s make some edits to the config file. In your wdio.conf.js file&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user= process.env.LT_USERNAME || "myusername",
key= process.env.LT_ACCESS_KEY || "1234567890",

exports.config = {

    user,
    key,
    logFile : './logDir/api.log',
    services: [
        ['lambdatest', {
            tunnel: true
        }]
    ],
    hostname: 'hub.lambdatest.com',
    path: '/wd/hub',
    port: 80,
    capabilities: [{
        build: ‘WebdriverIO-RemoteSelenium-Test’
        maxInstances: 5,
        browserName: 'chrome',
        acceptInsecureCerts: true,
        network: true,
        video: true,
        visual: true,
        console: true

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

&lt;/div&gt;
&lt;p&gt;These are the additional configurations to be done:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Pass your access token and user details by creating two variables, &lt;strong&gt;user&lt;/strong&gt; and &lt;strong&gt;key&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set the tunnel as true to enable routing connections from LambdaTest cloud through your computer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set the &lt;strong&gt;hostname&lt;/strong&gt; corresponding to LambdaTest&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optionally, you can also enable report formats like video recording, console, network logs, etc., to be true.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Run your tests. Use the command appropriate to you.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx wdio wdio.conf.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The test starts running, and logs can be observed in the terminal.&lt;/p&gt;

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

&lt;p&gt;To observe live running status, go to your Automation dashboard in 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%2F2048%2F0%2Asb-8J_FkOgNjUDN_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2048%2F0%2Asb-8J_FkOgNjUDN_.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can analyze individual test run reports from Automation Logs.&lt;/p&gt;

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

&lt;p&gt;We have successfully run our test cases using an automated cross browser testing solution provided by LambdaTest through a hassle-free experience!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do you know &lt;a href="https://www.lambdatest.com/web-technologies/css-overscroll-behavior?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun03_sd&amp;amp;utm_term=sd&amp;amp;utm_content=" rel="noopener noreferrer"&gt;CSS overscroll-behavior&lt;/a&gt; lets you control the way the contents of a scroll container scrolls when its position in the browser window is larger than or equal to the scrollport’s size.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Automation was introduced to accelerate the testing process and identify development bugs at the early stage. More and more companies are coming out from the shell of the traditional way of manual testing and adopting a more “shift left” approach focusing on how to leverage the benefits of automated testing in their development process.&lt;/p&gt;

&lt;p&gt;In this article, we have looked into some of the best practices to adopt for building a robust framework using WebdriverIO while using JavaScript with Selenium. We have also discussed various methods to optimize the performance by not only reducing execution time by speeding up the test but also debugging as well as maintenance time.&lt;/p&gt;

&lt;p&gt;Additionally, we have also covered a few of the best strategies to stay ahead, like CI/CD implementation and cross browser testing. Empowered by LambdaTest, we utilized a cloud-based solution for remote Selenium Grid to achieve &lt;a href="https://www.lambdatest.com/blog/what-is-parallel-testing-and-why-to-adopt-it/#:~:text=Parallel%20testing%20in%20Selenium%20is,by%20using%20a%20Selenium%20Grid.?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun03_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;parallel cross browser testing&lt;/a&gt; across multiple platforms.&lt;/p&gt;

&lt;p&gt;You can also checkout how to perform manual browser testing on LambdaTest platform.&lt;/p&gt;

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

&lt;p&gt;Hopefully, this blog will enable you to optimize your framework and speed up testing. If you wish to optimize your web application quality by unlocking coverage in a wider variety of platforms or browsers or versions, please feel free to explore the LambdaTest platform.&lt;/p&gt;

&lt;p&gt;Happy Testing!&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>webdev</category>
      <category>testing</category>
      <category>javascript</category>
    </item>
    <item>
      <title>A Detailed Guide On Selenium With Java [Tutorial]</title>
      <dc:creator>HaritaLT</dc:creator>
      <pubDate>Thu, 02 Jun 2022 18:20:58 +0000</pubDate>
      <link>https://forem.com/testmuai/a-detailed-guide-on-selenium-with-java-tutorial-a50</link>
      <guid>https://forem.com/testmuai/a-detailed-guide-on-selenium-with-java-tutorial-a50</guid>
      <description>&lt;p&gt;While getting started with automation testing, two questions pop up in a tester’s mind.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Which framework should be used for automation testing?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Which programming language should be preferred?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When it comes to the &lt;a href="https://www.lambdatest.com/blog/best-test-automation-frameworks-2021/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automation testing framework&lt;/a&gt;, &lt;strong&gt;Selenium&lt;/strong&gt; is often the first option that comes to our mind. Being open-source with large community support, Selenium has established itself as the favorite choice of automation testers over the last decade with significant traction.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;GitHub Stars&lt;/td&gt;
&lt;td&gt;23.8k&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Forks&lt;/td&gt;
&lt;td&gt;7k&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Downloads&lt;/td&gt;
&lt;td&gt;240k&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;When it comes to selecting the right programming language for writing tests, many factors have to be considered. This includes the language in which the Application Under Test (AUT) is being developed, community support, available test automation frameworks, IDE support, usability, simplicity, and the learning curve for the tester writing the test case.&lt;/p&gt;

&lt;p&gt;Test scenarios in Selenium can be implemented in popular programming languages like C#, Java, JavaScript, Python, PHP, and Ruby. In this Selenium Java tutorial, let us explore the most popular combination in automation testing — Selenium with Java. By the end of this detailed guide, you will be in a position to kick start your journey in Selenium automation testing with Java. I have covered a range of topics like installation and project setup for writing your first test case. I will also cover some of the Selenium best practices for automation testing using Selenium with Java.&lt;/p&gt;

&lt;p&gt;We have a lot to cover 🙂 So, let’s get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started: Selenium Automation Testing with Java
&lt;/h2&gt;

&lt;p&gt;If you’re new to automation or moved from &lt;a href="https://www.lambdatest.com/blog/starting-automation-testing-from-scratch-here-is-what-you-need-to-know/" rel="noopener noreferrer"&gt;manual to automation testing&lt;/a&gt;, then Selenium WebDriver is the best place to start. It has everything that you need to kickstart your Selenium automation journey.&lt;/p&gt;

&lt;p&gt;In this section of this article on Selenium with Java, I will take you through the prerequisites and everything you need to know about Selenium to get started with Selenium automation testing with Java.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Selenium?
&lt;/h2&gt;

&lt;p&gt;Selenium is a widely used automation testing framework used for browser automation of web applications. According to a recent test automation survey, 81% of the respondents used Selenium as their automating testing tool.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2ALfDGw5__WXAMuydq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2ALfDGw5__WXAMuydq.png" alt="[*Source](https://www.tricentis.com/state-of-open-source-2020/)*" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A few features of Selenium that contributed to its popularity are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open-source with a large community.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supports automation across multiple platforms like &lt;strong&gt;Windows, Macintosh, Linux&lt;/strong&gt;, etc.,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supports multiple browsers including &lt;strong&gt;Chrome, Firefox, IE, Safari&lt;/strong&gt;, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supports multiple programming languages like &lt;strong&gt;Java, Python, Ruby, C#&lt;/strong&gt;, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supports multiple frameworks like &lt;strong&gt;JUnit, TestNG&lt;/strong&gt;, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easy to integrate with CI tools like &lt;strong&gt;Jenkins, Docker&lt;/strong&gt;, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easy report generation&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://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%2ALCKwumNEAlMy4RMK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2ALCKwumNEAlMy4RMK.png" width="512" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Selenium is available in four different flavors, with the Selenium WebDriver being the most popular. Selenium WebDriver is an object-oriented API used for automating interactions with web browsers (e.g., Chrome, Firefox, Edge, etc.) using corresponding browser drivers.&lt;/p&gt;

&lt;p&gt;We also have the Selenium Grid, which helps run tests in parallel across different browser and platform combinations. Selenium Grid follows a hub-node concept where we can run the WebDriver scripts on a single machine (hub), but the execution will be done by multiple remote machines (nodes) via routing commands.&lt;/p&gt;

&lt;p&gt;LambdaTest provides you with a cloud-based online Selenium Grid that helps you run your test cases over an online browser farm of 3000+ browsers and browser versions, securing faster execution with maximum test coverage via minimum configurations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AULcr6tFaZXgnVjde.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AULcr6tFaZXgnVjde.png" width="548" height="524"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The latest version, &lt;strong&gt;Selenium 4&lt;/strong&gt;, comes with numerous major revamps, including enhanced architecture and advanced features. You can go through this Selenium 4 tutorial to learn &lt;a href="https://www.lambdatest.com/blog/what-is-deprecated-in-selenium4/" rel="noopener noreferrer"&gt;what is new in Selenium 4&lt;/a&gt;.&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;Do you know &lt;a href="https://www.lambdatest.com/web-technologies/css-logical-props?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=web_technologies" rel="noopener noreferrer"&gt;CSS Logical Properties&lt;/a&gt; and Values make it possible to control layout through logical, rather than physical, direction and dimension mappings. They are the writing-mode equivalent of their corresponding physical equivalents&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Selenium WebDriver work?
&lt;/h2&gt;

&lt;p&gt;The Selenium WebDriver works with a &lt;strong&gt;Client-Server&lt;/strong&gt; architecture model.&lt;/p&gt;

&lt;p&gt;We have the &lt;strong&gt;Selenium Client Library on the client-side&lt;/strong&gt;, which supports multiple client libraries corresponding to the programming language being used, like Java, Ruby, Python, etc. The language binding feature of Selenium allows it to support various programming languages. Based on the programming language of the test scripts, we need to choose the client library. For example, you need to use the Java language bindings to use Selenium with Java. You can download your respective language bindings from &lt;a href="https://www.selenium.dev/downloads/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We have the &lt;strong&gt;Browser Driver on the server-side&lt;/strong&gt;, which accepts the HTTP requests coming from the client-side via the communication protocol. The browser driver then interacts with the &lt;strong&gt;Real browser&lt;/strong&gt; and drives it to execute the actions defined in the automation scripts. Every browser has a browser driver specific to it. For example, to automate the Chrome browser, you need to use the Chrome driver. Shown below are the locations from where you can download the respective browser driver.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;BROWSER&lt;/th&gt;
&lt;th&gt;DOWNLOAD LOCATION&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Opera&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/operasoftware/operachromiumdriver/releases" rel="noopener noreferrer"&gt;https://github.com/operasoftware/operachromiumdriver/releases&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Firefox&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/mozilla/geckodriver/releases" rel="noopener noreferrer"&gt;https://github.com/mozilla/geckodriver/releases&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Chrome&lt;/td&gt;
&lt;td&gt;&lt;a href="http://chromedriver.chromium.org/downloads" rel="noopener noreferrer"&gt;http://chromedriver.chromium.org/downloads&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Internet Explorer&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver" rel="noopener noreferrer"&gt;https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Microsoft Edge&lt;/td&gt;
&lt;td&gt;&lt;a href="https://blogs.windows.com/msedgedev/2015/07/23/bringing-automated-testing-to-microsoft-edge-through-webdriver/" rel="noopener noreferrer"&gt;https://blogs.windows.com/msedgedev/2015/07/23/bringing-automated-testing-to-microsoft-edge-through-webdriver/&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The client-side and server-side interact with each other via a communication protocol.&lt;/p&gt;

&lt;p&gt;For versions earlier than Selenium 4, the &lt;strong&gt;JSON wire protocol&lt;/strong&gt; was used for communicating between the client and the server. It consists of the REST API that sends requests from the client to the server (browser’s native API) via HTTP. But here, since the Selenium Client Libraries (client) uses JSON protocol and the Web browser (server) users W3C protocol, encoding, and decoding of the API requests were required for communication between test scripts and browsers.&lt;/p&gt;

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

&lt;p&gt;From Selenium 4, the JSON wire protocol is replaced by the WebDriver W3C protocol enabling direct communication between the Selenium Client libraries and the target browser. As the WebDriver and Web Browser use the same protocol, encoding and decoding of the API requests are no longer required making the test cases more stable than ever.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2ACHwcmXsKA7AOKJtv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2ACHwcmXsKA7AOKJtv.png" width="800" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why choose Selenium with Java?
&lt;/h2&gt;

&lt;p&gt;According to the &lt;a href="https://insights.stackoverflow.com/survey/2021#technology" rel="noopener noreferrer"&gt;Stack Overflow Developer Survey&lt;/a&gt;, Java is one of the most popular programming languages, preferred by more than 35% of the respondents.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AUpP_GyrueT0mNRz2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AUpP_GyrueT0mNRz2.png" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Choosing to write your Selenium automation tests using Java has multiple benefits. A few of them are listed below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;As Selenium supports Java, detailed documentation is available, making it easy to write test cases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Compared to other languages like Python, programs written in Java are faster.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accessibility to various operating systems like UNIX, Linux, Windows, Mac OS, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Java comes with multiple complete automation frameworks like TestNG, JUnit, etc., that can be leveraged with Selenium for better coding, reporting, and maintenance.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installation and Setup: How to use Selenium with Java?
&lt;/h2&gt;

&lt;p&gt;To start your Selenium automation testing journey, the first step is to install and configure Selenium onto your system. In this section of this article on Selenium with Java, I will guide you through the installation process step by step. You will also be learning how to create a basic Selenium with Java project setup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;The installation process consists of the following steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install JDK.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install Eclipse IDE.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install Selenium WebDriver and Language Bindings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install Browser Drivers (Optional).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Install JDK
&lt;/h3&gt;

&lt;p&gt;Let us start with the Java Development Kit (JDK) installation. The JDK includes JRE (Java Runtime Environment) used to write and run Java code. You can download JDK from any of the below options.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.oracle.com/java/technologies/downloads/" rel="noopener noreferrer"&gt;Oracle JDK&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.openlogic.com/openjdk-downloads" rel="noopener noreferrer"&gt;OpenJDK&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the installation is complete, set the environment variables. The steps are as below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Right-click on Computer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on ‘&lt;strong&gt;Properties&lt;/strong&gt;’.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on ‘&lt;strong&gt;Advanced system settings&lt;/strong&gt;’.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On System Properties, select the ‘&lt;strong&gt;Advanced&lt;/strong&gt;’ Tab.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on ‘&lt;strong&gt;Environment Variables&lt;/strong&gt;.’&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy the path where the JDK is installed. (E.g.: C:\Program Files\Java\jdk1.6.0\bin )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the opened &lt;strong&gt;Environment Variables&lt;/strong&gt; window, select ‘&lt;strong&gt;New&lt;/strong&gt;‘ under ‘&lt;strong&gt;System variables&lt;/strong&gt;’.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set the path. If ‘&lt;strong&gt;Path&lt;/strong&gt;’ is already existing, then edit it.&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%2F2000%2F1%2A6VU5_QNtD84CfUKdV8EWHA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2A6VU5_QNtD84CfUKdV8EWHA.png" width="800" height="228"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;9- We also need to set the CLASSPATH. Create a new environment variable and copy the same JDK path as the value. If ‘CLASSPATH’ is already existing, then edit 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%2AFe2ueZeG9IapRhoQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AFe2ueZeG9IapRhoQ.png" width="800" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The environment variable setup is now complete.&lt;/p&gt;

&lt;p&gt;Open the command prompt and run the following command to verify that Java has been successfully installed in your system.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;java -version
&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%2F2000%2F0%2AVlVP-Rj92c0DILUN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AVlVP-Rj92c0DILUN.png" width="800" height="123"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Eclipse IDE
&lt;/h3&gt;

&lt;p&gt;Next, we need to install an IDE (Integrated Development Environment). An IDE is a platform where developers can write the code and run it. I will be using the &lt;strong&gt;Eclipse IDE for Java Developers&lt;/strong&gt; for this demo. You can download the latest version from &lt;a href="https://www.eclipse.org/downloads/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once the installer file is downloaded, complete the installation process. Alternatively, you can also use any other popular IDE like Jetbrains Webstorm, IntelliJ IDEA, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Selenium WebDriver and Language Bindings
&lt;/h3&gt;

&lt;p&gt;Download the Selenium Client Library for Java from &lt;a href="https://www.selenium.dev/downloads/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. The download comes as a ZIP file. Extract the contents (JAR files) and store them in a directory.&lt;/p&gt;

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

&lt;p&gt;We are downloading client drivers for Selenium 4. Please check out ” &lt;em&gt;Previous Releases&lt;/em&gt; ” for working with Selenium 3 or older 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%2F3840%2F0%2AZdPHag21yjDVDO4P.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3840%2F0%2AZdPHag21yjDVDO4P.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Browser Drivers (Optional)
&lt;/h3&gt;

&lt;p&gt;Finally, if you are running your test scripts on your local system, it is mandatory to have its respective browser driver for automating a browser. You can download the drivers for the browsers of your choice from &lt;a href="https://www.selenium.dev/ecosystem/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;However, if you are utilizing a cloud Selenium Grid like LambdaTest, you can skip 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%2F3656%2F0%2AcG91NNsM5X0VOend.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3656%2F0%2AcG91NNsM5X0VOend.png" width="800" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the installation process complete, let us move on to the project setup.&lt;/p&gt;

&lt;p&gt;Watch this video to know more about test result analysis and reporting in Selenium 4. Also, learn about different selenium reporting tools based on ease of setup, pricing, supported report formats, and more.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  How to create a Selenium Java project in Eclipse?
&lt;/h2&gt;

&lt;p&gt;We need to have a project set up for writing and storing our test cases. You can follow the below-mentioned steps to create a new Selenium with Java project in Eclipse.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Launch Eclipse&lt;br&gt;
&lt;strong&gt;Step 2&lt;/strong&gt;: Create a workspace.&lt;/p&gt;

&lt;p&gt;A workspace will be a place where you will be storing all your projects. You can create as many workspaces 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%2F2000%2F0%2Ao7hujiwu4MLV5uA9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2Ao7hujiwu4MLV5uA9.png" width="800" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Create a new Java project.&lt;/p&gt;

&lt;p&gt;Go to &lt;strong&gt;File &amp;gt; New &amp;gt; Java Project&lt;/strong&gt;. Name the project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2An7Cloot977AzUrAW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2An7Cloot977AzUrAW.png" width="800" height="556"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A new pop-up window will appear. Enter the following details:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Project name&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Location to save the project&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Execution JRE&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once done, click ‘&lt;strong&gt;Finish&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%2Acepp5fBVYt-L6vZS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2Acepp5fBVYt-L6vZS.png" width="800" height="812"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The project is now created.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Add Selenium API JAR files to the Java project by following the below-mentioned steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Right-click on the project and select &lt;strong&gt;Properties&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select &lt;strong&gt;Java Build Path&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Under the libraries section, click on &lt;strong&gt;Add External JARs&lt;/strong&gt;, which gets enabled upon opting for Classpath.&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%2F2756%2F0%2ANOZEdOY8zbpGIvK5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2756%2F0%2ANOZEdOY8zbpGIvK5.png" width="800" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4- Add the Selenium Java Client JARS. Navigate to the folder location where you have downloaded and extracted the JAR 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%2F1%2ATniRhPc2UohPRkRXnN2LfQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2ATniRhPc2UohPRkRXnN2LfQ.png" width="800" height="567"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5- Once all the JAR files are added, click “&lt;strong&gt;Apply and Close&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%2F2756%2F1%2AQ1GEmOEoP7tNgH0BW6DI2A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2756%2F1%2AQ1GEmOEoP7tNgH0BW6DI2A.png" width="800" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With our project configured with Selenium and Java, we are ready to write and run our first test script in Selenium.&lt;/p&gt;

&lt;p&gt;If you’re a developer or a tester and want to take your Selenium with Java skills to the next level, this Selenium Java 101 certification from LambdaTest can help you reach that goal.&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;&lt;strong&gt;&lt;em&gt;Check this out &lt;a href="https://www.lambdatest.com/web-technologies/css-masks?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=web_technologies" rel="noopener noreferrer"&gt;CSS Masks&lt;/a&gt;: you can mask part of an element using another element (or image)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  [Demo] Writing the first test case using Selenium and Java
&lt;/h2&gt;

&lt;p&gt;It is time to hit practicals! In this section of this article on Selenium with Java, you will be learning to write your very first test case using Selenium with Java.&lt;/p&gt;

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

&lt;p&gt;First, let us understand the test scenario for which we will be writing a test script. We will be automating a simple form submission where the steps are as follows.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Launch the website.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter the value for ‘&lt;strong&gt;a&lt;/strong&gt;’.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter the value for ‘&lt;strong&gt;b&lt;/strong&gt;’.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the ‘&lt;strong&gt;Get Total&lt;/strong&gt;’ button to display the sum of two numbers, ‘&lt;strong&gt;a&lt;/strong&gt;’ and ‘&lt;strong&gt;b&lt;/strong&gt;’.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verify the result.&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%2F2000%2F0%2AdfzaXEWs_sRRvXpO.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AdfzaXEWs_sRRvXpO.png" width="800" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Case 1: Running our test case on a local machine
&lt;/h2&gt;

&lt;p&gt;In this section of this article on Selenium with Java, let us learn how to write the automated test case and run it on our local machine. The steps are detailed below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Create a class file inside your Java Project.&lt;br&gt;
Right-click on the &lt;strong&gt;src&lt;/strong&gt; folder inside your Project. Select &lt;strong&gt;New &amp;gt; Class&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%2AQU37aFF9qTZKKBtX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AQU37aFF9qTZKKBtX.png" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am naming it &lt;strong&gt;TestOne.java&lt;/strong&gt;. The test gets created inside the default package.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AbJ10YdTjAUh2R5Jh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AbJ10YdTjAUh2R5Jh.png" width="660" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Write our test case code using Selenium with Java.&lt;/p&gt;

&lt;p&gt;First of all, let us make the required Selenium imports on the top of our test class file. I will be running the test on the Chrome browser for this demo.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import org.openqa.selenium.By;      
import org.openqa.selenium.WebDriver;              
import org.openqa.selenium.chrome.ChromeDriver;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Inside the &lt;strong&gt;main()&lt;/strong&gt; method, let us set properties to define the path of the browser driver, which we have downloaded during the installation steps.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;System.setProperty(“webdriver.chrome.driver”, “C:/location_of_chromedriver.exe”);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;After this, define a new Webdriver instance.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WebDriver driver= new ChromeDriver();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;A glance of all possible methods WebDriver API provides us for browser automation is shown in the below snapshot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2ALRbHeaFTj0krPpWe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2ALRbHeaFTj0krPpWe.png" width="353" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let us write the code for our test scenario.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Maximize window      
        driver.manage().window().maximize();    

        //Launch the Site.      
        driver.get("[https://www.lambdatest.com/selenium-playground/simple-form-demo](https://www.lambdatest.com/selenium-playground/simple-form-demo)");

        //Enter values into form and submit      
        driver.findElement(By.id("sum1")).sendKeys("2");                    
        driver.findElement(By.id("sum2")).sendKeys("5");    
        driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='gettotal']/button")).click();  

        //Verify the result
        String result = driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)=\'addmessage\']")).getText();

        if( Integer.parseInt(result) == 7) {
             System.out.println("Test Success");
        } else {
             System.out.println("Test Failed");
        }

        //Close the browser
        driver.quit();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;I would like to briefly explain the WebDriver commands I have used in the test script.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;get(String arg): void&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;get()&lt;/strong&gt; method takes a string URL as a parameter and opens the specified URL in the current browser window. This method returns nothing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;findElement(By by): WebElement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The findElement() method in Selenium is a WebElement command used to find a WebElement in the DOM for performing an action like clicking, typing, etc. It accepts an input parameter of the type &lt;strong&gt;By&lt;/strong&gt; for locating the element using various locator strategies like id, classname, XPath, etc.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;findElement()&lt;/strong&gt; returns a WebElement if the element is found, else throws a runtime exception — &lt;strong&gt;NoSuchElementException&lt;/strong&gt;. To learn more about exception handling, you can go through this blog on common Selenium exceptions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;quit(): void&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;quit()&lt;/strong&gt; method is used to close all browser windows opened by the Webdriver. It accepts no parameter and returns nothing.&lt;/p&gt;

&lt;p&gt;The WebElement methods used in the test script are:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;WEBELEMENT METHOD&lt;/th&gt;
&lt;th&gt;METHOD USAGE&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;click(): void&lt;/td&gt;
&lt;td&gt;To click the element&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sendKeys(): void&lt;/td&gt;
&lt;td&gt;To simulate typing into an element, which may set its value. It takes a character sequence as input.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;getText(): String&lt;/td&gt;
&lt;td&gt;To get the visible (i.e. not hidden by CSS) text of the element, including sub-elements.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To know more about the WebElement methods, please find the official documentation &lt;a href="https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebElement.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For locating the elements I made use of the browser &lt;strong&gt;Developer Tools&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%2F3838%2F0%2A4gLvsqS-POQomowX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3838%2F0%2A4gLvsqS-POQomowX.png" width="800" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our final code looks like this!&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import org.openqa.selenium.By;      
import org.openqa.selenium.WebDriver;              
import org.openqa.selenium.chrome.ChromeDriver;

public class TestOne {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "E:/Selenium/chromedriver.exe");

        WebDriver driver= new ChromeDriver();          

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

        //Launch the Site.      
        driver.get("https://www.lambdatest.com/selenium-playground/simple-form-demo");

        //Enter values into form and submit      
        driver.findElement(By.id("sum1")).sendKeys("2");                    
        driver.findElement(By.id("sum2")).sendKeys("5");    
        driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='gettotal']/button")).click();  

        //Verify the result
        String result = driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)=\'addmessage\']")).getText();

        if( Integer.parseInt(result) == 7) {
             System.out.println("Test Success");
        } else {
             System.out.println("Test Failed");
        }
               //Close the browser
        driver.quit();              
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Source: &lt;a href="https://github.com/harita-R/selenium-java/blob/master/src/TestOne.java" rel="noopener noreferrer"&gt;*GitHub&lt;/a&gt;*&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Run the test.&lt;/p&gt;

&lt;p&gt;Finally, it is time to run our test. Right-click on the test class file and select.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run As &amp;gt; Java Application&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%2F3832%2F0%2Ak_bQZqyz3na_QXUq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3832%2F0%2Ak_bQZqyz3na_QXUq.png" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The browser gets launched, and the test gets executed successfully on your local machine!&lt;/p&gt;

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

&lt;p&gt;You can also check the console for logs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2300%2F0%2Ahp12bgii1HG1iaT8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2300%2F0%2Ahp12bgii1HG1iaT8.png" width="800" height="274"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Case 2: Running our test case on an online Selenium Grid
&lt;/h2&gt;

&lt;p&gt;In the previous section of this article on Selenium with Java, we have seen how to run our test case on a local machine. Here, let us explore how to run our test case in a remote cloud-based Selenium Grid.&lt;/p&gt;

&lt;p&gt;Running our test cases in an online Selenium Grid helps us tackle the below-mentioned problems that occur during local execution when the project size and test coverage requirement increase.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Scalability&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Better browser coverage&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Infrastructure limitations for cross browser testing etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;LambdaTest is a cross browser testing platform that provides an online browser farm of 3000+ browser and OS combinations and an online Selenium Grid that you can integrate with your current test project via minimum code changes. Follow along to learn how!&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?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=youtube" rel="noopener noreferrer"&gt;LambdaTest YouTube Channel&lt;/a&gt; and stay updated with the latest tutorials around Selenium testing, Cypress E2E testing, CI/CD, and more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Create a free account on LambdaTest and secure your unique username and access key.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Inside the test class, define all the required variables for connecting with the Remote Grid. In addition, you can also define your unique username and access key.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static String username = "YOUR_USERNAME";
   static String accesskey = "YOUR_ACCESS_KEY";
   static RemoteWebDriver driver = null;
   static String gridURL = "[@hub](http://twitter.com/hub).lambdatest.com/wd/hub";
   static boolean status = false;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Pass the browser capabilities. You can generate your custom desired capabilities using LambdaTest Capabilities Generator.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DesiredCapabilities capabilities = new DesiredCapabilities();  
     capabilities.setCapability("browserName", "Chrome");
     capabilities.setCapability("browserVersion", "101.0");
     HashMap&amp;lt;String, Object&amp;gt; ltOptions = new HashMap&amp;lt;String, Object&amp;gt;();
     ltOptions.put("user",username);
     ltOptions.put("accessKey",accesskey);
     ltOptions.put("build", "Selenium Java Test");
     ltOptions.put("name", "TestOne");
     ltOptions.put("platformName", "Windows 11");
     ltOptions.put("selenium_version","4.1.2");
     ltOptions.put("driver_version","101.0");
     capabilities.setCapability("LT:Options", ltOptions);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Define a new instance of RemoteWebDriver()&lt;/p&gt;

&lt;p&gt;Remote WebDriver is an interface which is used to execute the browser automation on a remote computing device or in a distributed environment. It can control the browser in the grid by configuring the node and the hub.&lt;/p&gt;

&lt;p&gt;Note that you no longer need to pass the browser driver.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try {
            driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
        }
        catch (MalformedURLException e) {
            System.out.println("Invalid grid URL");
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: Our final code now looks like this!&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class TestOneWithRemoteGrid {

    static String username = "YOUR_USERNAME";
    static String accesskey = "YOUR_ACCESS_KEY";
    static RemoteWebDriver driver = null;
    static String gridURL = "[@hub](http://twitter.com/hub).lambdatest.com/wd/hub";
    static boolean status = false;

    public static void main(String[] args) {

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("browserName", "Chrome");
        capabilities.setCapability("browserVersion", "101.0");
        HashMap&amp;lt;String, Object&amp;gt; ltOptions = new HashMap&amp;lt;String, Object&amp;gt;();
        ltOptions.put("user",username);
        ltOptions.put("accessKey",accesskey);
        ltOptions.put("build", "Selenium Java Test");
        ltOptions.put("name", "TestOne");
        ltOptions.put("platformName", "Windows 11");
        ltOptions.put("selenium_version","4.1.2");
        ltOptions.put("driver_version","101.0");
        capabilities.setCapability("LT:Options", ltOptions);

        try {
            driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
        } catch (MalformedURLException e) {
            System.out.println("Invalid grid URL");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }  


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

        //Launch the Site.      
        driver.get("[https://www.lambdatest.com/selenium-playground/simple-form-demo](https://www.lambdatest.com/selenium-playground/simple-form-demo)");

        //Enter values into form and submit        
        driver.findElement(By.id("sum1")).sendKeys("2");                    
        driver.findElement(By.id("sum2")).sendKeys("5");    
        driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='gettotal']/button")).click();  

        //Verify the result
        String result = driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)=\'addmessage\']")).getText();

        if( Integer.parseInt(result) == 7) {
             System.out.println("Test Success");
        } else {
             System.out.println("Test Failed - " + result);
        }                

        //Close browser
        driver.quit();                    
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Source: &lt;a href="https://github.com/harita-R/selenium-java/blob/master/src/TestOneWithRemoteGrid.java" rel="noopener noreferrer"&gt;*GitHub&lt;/a&gt;*&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt;: Run the test.&lt;/p&gt;

&lt;p&gt;The test starts running. Go to the LambdaTest Dashboard to observe the live running status.&lt;/p&gt;

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

&lt;p&gt;Our test got executed successfully. You can analyze the detailed report containing video and logs.&lt;/p&gt;

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

&lt;p&gt;Moreover, you can navigate to the LambdaTest Analytics Dashboard to see test performance metrics. You can see the total number of tests passed or failed from the Test Summary section. 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%2F0%2AdRB_WYUv2VLDi3C7.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%2AdRB_WYUv2VLDi3C7.gif" width="600" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do you know &lt;a href="https://www.lambdatest.com/web-technologies/css-math-functions?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=web_technologies" rel="noopener noreferrer"&gt;CSS math functions&lt;/a&gt; min(), max() and clamp() is a polyfill that provides more advanced mathematical expressions in addition to ‘calc()’&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using TestNG framework for parallel test execution
&lt;/h2&gt;

&lt;p&gt;If you are working on an actual project, you will need to write, run and maintain hundreds of test cases. Each testing cycle will demand you to execute your test cases on multiple platforms and devices to ensure that the application behaves as expected under various conditions to assure the product quality before going live. It becomes highly desirable to reduce the overall test execution time, and one way to achieve it is through parallel testing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parallel testing&lt;/strong&gt; is a favored test execution strategy where the tests are run in parallel to reduce overall test execution time. Parallel testing was introduced to replace the traditional approach of sequential testing. As the name suggests, tests are executed sequentially, which is more time-consuming.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AOiBhAsUHm5YExMP-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AOiBhAsUHm5YExMP-.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You have learned how to write and run a test case sequentially using Selenium with Java in the previous section. Now, I will walk you through how to run tests in parallel on a cloud-based Selenium Grid using the &lt;strong&gt;TestNG&lt;/strong&gt; framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to TestNG framework
&lt;/h2&gt;

&lt;p&gt;TestNG is a prominent open-source test automation framework for Java, where NG stands for ‘Next Generation.’ TestNG is widely adopted by Selenium users because of its features and feasibility. Running Selenium tests alone comes with numerous limitations. However, TestNG introduces an entire set of new features making it powerful and beneficial.&lt;/p&gt;

&lt;p&gt;A few critical features of TestNG are listed below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Easy report generation in desirable formats. Now it is easier to analyze how many test cases passed and how many failed during each test run.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supports parallel testing. Multiple test cases can be run simultaneously, reducing the overall test execution time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Possible to group test cases in TestNG effortlessly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;TestNG annotations like &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;, @BeforeTest,@AfterTest, etc., are supported.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easy to integrate with tools like Maven, Jenkins, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Parallel test execution in TestNG using TestNG XML file
&lt;/h2&gt;

&lt;p&gt;As pointed out, one of the key advantages of adopting the TestNG automation framework is that it supports parallel test execution. TestNG test automation framework allows you to run tests in parallel or multithreaded mode by utilizing the Multi-Threading concept of Java.&lt;/p&gt;

&lt;p&gt;Multi-Threading is the process of executing multiple threads simultaneously, without any dependence on each other. This means that the threads are executed in isolation, and therefore, exceptions occurring on one thread won’t affect the others. Multi-Threading helps in the efficient utilization of the CPU by greatly reducing idle time. In TestNG, we enable parallel testing by making required changes in the configuration file — &lt;strong&gt;TestNG XML&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;TestNG XML is the test suite configuration file in TestNG, which helps customize the execution of tests. It also allows you to run a single test file over numerous combinations of parameters and values specified. A simple TestNG XML file 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%2F2000%2F0%2A4Fgew9ZKEjmGZ7hj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2A4Fgew9ZKEjmGZ7hj.png" width="775" height="265"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To trigger parallel execution, we need to set the &lt;strong&gt;parallel&lt;/strong&gt; attribute. This attribute accepts four values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;classes&lt;/strong&gt; — to run all test cases present inside classes in the XML in parallel mode.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;methods&lt;/strong&gt; — to run all methods with &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; annotation in parallel mode.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;tests&lt;/strong&gt; — to run all test cases present inside tag in the XML in parallel mode.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;instances&lt;/strong&gt; — to run all test cases in the same instance in parallel mode.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can also set the number of threads we wish to allocate during the execution using the &lt;strong&gt;thread-count&lt;/strong&gt; attribute. The default value of thread count in TestNG is 5.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;suite name=”Regression Test Suite” parallel=”methods” thread-count=”2"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Moving forward, let us install and set up TestNG in our Eclipse. Also, let us learn how to rewrite our demo test case as a TestNG test and run it parallelly in three different browsers by leveraging the online Selenium Grid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation and Setup
&lt;/h2&gt;

&lt;p&gt;Installing TestNG in Eclipse is a simple process. Go to Eclipse Marketplace and install TestNG.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Help &amp;gt; Eclipse Marketplace.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once installed, you can locate it under the Installed 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%2F0%2AbxRSL58qaJN_TfUW.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AbxRSL58qaJN_TfUW.jpg" width="800" height="949"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the installation completed, let us move on to a basic project setup next.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a TestNG Project
&lt;/h2&gt;

&lt;p&gt;To create a new Java TestNG project in Eclipse, follow the below-mentioned steps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Launch Eclipse and create a new Java project.&lt;/p&gt;

&lt;p&gt;Go to &lt;strong&gt;&lt;em&gt;File &amp;gt; New &amp;gt; Java Project.&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%2F2000%2F0%2AkmndADmmicMq8uFk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AkmndADmmicMq8uFk.png" width="628" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Name your project and click &lt;strong&gt;Next.&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%2AFNeUd6fAYLeKoKDK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AFNeUd6fAYLeKoKDK.png" width="800" height="732"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Add libraries.&lt;/p&gt;

&lt;p&gt;Right-click on the project folder and select Properties.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Choose Java Build Path.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to Libraries, select Classpath, and click Add Library.&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%2F2000%2F0%2Al5AIO04XW0SC92cH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2Al5AIO04XW0SC92cH.png" width="800" height="732"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Select TestNG and click Next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2Aqy_CkYHWWLYwJ7Z6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2Aqy_CkYHWWLYwJ7Z6.png" width="800" height="689"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will see that TestNG has been added to the project library.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Add the Selenium API JAR files to the TestNG project and click Apply and Close.&lt;/p&gt;

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

&lt;p&gt;We have successfully created a TestNG project and are ready to write and run our first TestNG test case.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to run test cases in parallel with Selenium in TestNG on Cloud Grid?
&lt;/h2&gt;

&lt;p&gt;In this section of this article on Selenium with Java, we will learn how to achieve parallel test execution in TestNG. For the demo, we will be re-writing our previous test case into a TestNG test and executing it parallelly on five different browsers (Chrome, Safari, Firefox, Opera, and Edge) using the Online Selenium Grid provided by LambdaTest.&lt;/p&gt;

&lt;p&gt;The steps are detailed below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Go to the TestNG project we created in the previous step.&lt;br&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; Create a TestNG class file by following the below-mentioned steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Right-click on project src and find TestNG towards the bottom of the list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select Create TestNG Class.&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%2F2000%2F1%2AXsMGMUrqZQJambIQ1I2OjQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AXsMGMUrqZQJambIQ1I2OjQ.png" width="673" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3- Name the class, choose the required annotations, provide the XML file name and click Finish. Here, I have named the class as ParallelTest.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2ApelQ0RS9S__aYL_4a7fj4g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2ApelQ0RS9S__aYL_4a7fj4g.png" width="800" height="610"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The TestNG class file is created containing the skeleton code structure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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-25LAygIdy3vUUpu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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-25LAygIdy3vUUpu.png" width="484" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us quickly go through the TestNG annotations used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;/strong&gt; — Implies that the method is a test case. Therefore, all tests should be marked with this annotation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;@BeforeMethod&lt;/strong&gt; and &lt;strong&gt;@AfterMethod&lt;/strong&gt; — These annotations run once before and after each &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; method in a class.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Write our test case using TestNG and Selenium.&lt;br&gt;
I have re-written our previous test scenario as a TestNG test case. Some notable changes are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Inside the method marked by &lt;strong&gt;@BeforeMethod&lt;/strong&gt; annotation, I have placed all the setup steps since they need to be executed first before the test. Also, the method name has been renamed &lt;strong&gt;setUp()&lt;/strong&gt;. Additionally, a try-catch block has been implemented for exception handling.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also, I have added &lt;strong&gt;@Parameters&lt;/strong&gt; annotation on top of the method for receiving the browser, version, and platform values from our TestNG XML file.&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; [@BeforeMethod](http://twitter.com/BeforeMethod)
    [@Parameters](http://twitter.com/Parameters)(value={"browser","version","platform"})
    public void setUp(String browser, String version, String platform) throws Exception {
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("browserName", browser);
            capabilities.setCapability("browserVersion",version);
            HashMap&amp;lt;String, Object&amp;gt; ltOptions = new HashMap&amp;lt;String, Object&amp;gt;();
            ltOptions.put("user",username);
            ltOptions.put("accessKey",accesskey);
            ltOptions.put("build", "TestNG Parallel Test");
            ltOptions.put("name", "TestOne");
            ltOptions.put("platformName", platform);

            ltOptions.put("selenium_version","4.1.2");
            ltOptions.put("driver_version","101.0");
            capabilities.setCapability("LT:Options", ltOptions);

         try {
             driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
         } catch (MalformedURLException e) {
             System.out.println("Invalid grid URL");
         } catch (Exception e) {
             System.out.println(e.getMessage());
         }
     }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Inside the method marked by &lt;strong&gt;@AfterMethod&lt;/strong&gt; annotation, I have placed the tear-down steps since they need to be executed last after every test. Also the method name has been renamed as &lt;strong&gt;tearDown()&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&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 tearDown() throws Exception {
           if (driver != null) {
                driver.quit();
            }
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;We will be writing our actual code for test steps inside the method marked by &lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;/strong&gt; annotation. Since we are automating an additional scenario, I have renamed the method to addValues().
&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)
    public void addValues() {
            //Maximize window      
            driver.manage().window().maximize();    

            //Launch the Site.        driver.get("[https://www.lambdatest.com/selenium-playground/simple-form-demo](https://www.lambdatest.com/selenium-playground/simple-form-demo)");

            //Enter values into form and submit        
            driver.findElement(By.id("sum1")).sendKeys("2");                    
            driver.findElement(By.id("sum2")).sendKeys("5");    
            driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='gettotal']/button")).click();  

            //Verify the result
            String result = driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)=\'addmessage\']")).getText();

            if( Integer.parseInt(result) == 7) {
                 System.out.println("Test Success");
            } else {
                 System.out.println("Test Failed - " + result);
            }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The final code looks 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;g;

    import java.net.MalformedURLException;
    import java.net.URL;

    import org.openqa.selenium.By;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;

    public class ParallelTest {

    public String username = "YOUR_USERNAME";
    public String accesskey = "YOUR_ACCESSKEY";
    public RemoteWebDriver driver = null;
    public String gridURL = "[@hub](http://twitter.com/hub).lambdatest.com/wd/hub";
    boolean status = false;

    [@BeforeMethod](http://twitter.com/BeforeMethod)
    [@Parameters](http://twitter.com/Parameters)(value={"browser","version","platform"})
    public void setUp(String browser, String version, String platform) throws Exception {
            DesiredCapabilities capabilities = new DesiredCapabilities();
                    capabilities.setCapability("browserName", browser);
            capabilities.setCapability("browserVersion",version);
            HashMap&amp;lt;String, Object&amp;gt; ltOptions = new HashMap&amp;lt;String, Object&amp;gt;();
            ltOptions.put("user",username);
            ltOptions.put("accessKey",accesskey);
            ltOptions.put("build", "TestNG Parallel Test");
            ltOptions.put("name", "TestOne");
            ltOptions.put("platformName", platform);
            ltOptions.put("selenium_version","4.1.2");
            ltOptions.put("driver_version","101.0");
            capabilities.setCapability("LT:Options", ltOptions);

         try {
             driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
         } catch (MalformedURLException e) {
             System.out.println("Invalid grid URL");
         } catch (Exception e) {
             System.out.println(e.getMessage());
         }
     }
    [@Test](http://twitter.com/Test)
    public void addValues() {
            //Maximize window      
            driver.manage().window().maximize();    

            //Launch the Site.        driver.get("[https://www.lambdatest.com/selenium-playground/simple-form-demo](https://www.lambdatest.com/selenium-playground/simple-form-demo)");

            //Enter values into form and submit        
            driver.findElement(By.id("sum1")).sendKeys("2");                    
            driver.findElement(By.id("sum2")).sendKeys("5");    
            driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='gettotal']/button")).click();  

            //Verify the result
            String result = driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)=\'addmessage\']")).getText();

            if( Integer.parseInt(result) == 7) {
                 System.out.println("Test Success");
            } else {
                 System.out.println("Test Failed - " + result);
            }   

    [@AfterMethod](http://twitter.com/AfterMethod)
        public void tearDown() throws Exception {
           if (driver != null) {
                driver.quit();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/harita-R/testng-parallel-tests/blob/master/src-tests/testng/ParallelTest.java" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;**Step 4: **Configure the TestNG XML file to run in parallel mode. The steps are as below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open the testng.xml file. If not already created, you may create it by adding a new file to the project and naming it as testng.xml.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Edit the boilerplate. You can name the test suite and test name as per your liking. But the class name should be exact as that of our TestNG class.&lt;br&gt;
&lt;/p&gt;&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;?xml version="1.0" encoding="UTF-8"?&amp;gt;
    &amp;lt;!DOCTYPE suite SYSTEM "[https://testng.org/testng-1.0.dtd](https://testng.org/testng-1.0.dtd)"&amp;gt;
    &amp;lt;suite name="Parallel Test Suite"&amp;gt;
        &amp;lt;test name="Test"&amp;gt;
            &amp;lt;classes&amp;gt;
                &amp;lt;class name="testng.ParallelTest" /&amp;gt;
            &amp;lt;/classes&amp;gt;
        &amp;lt;/test&amp;gt; &amp;lt;!-- Test --&amp;gt;
    &amp;lt;/suite&amp;gt; &amp;lt;!-- Suite --&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3- To trigger parallel test execution in TestNG, provide the parallel attribute to . For parallel execution of test methods, set the attribute value as “tests.”&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;suite name="Parallel Test Suite" parallel="tests" thread-count="5"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4- Add parameters for configuring the tests to run in a specific browser, browser version, and OS. For example, below is the code for setting up the test case to run in Chrome Browser version 101.0 in Windows 11. Similarly, you can provide as many combinations as you like.&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;test name="Chrome Test"&amp;gt;
            &amp;lt;parameter name="browser" value="Chrome" /&amp;gt;
            &amp;lt;parameter name="version" value="101.0" /&amp;gt;

            &amp;lt;parameter name="platform" value="WIN11" /&amp;gt;

            &amp;lt;classes&amp;gt;
                &amp;lt;class name="testng.ParallelTest" /&amp;gt;
            &amp;lt;/classes&amp;gt;
        &amp;lt;/test&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final code for testng.xml looks 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;?xml version="1.0" encoding="UTF-8"?&amp;gt;
    &amp;lt;suite name="Suite" parallel="tests" thread-count="5"&amp;gt;

        &amp;lt;test name="Chrome Test"&amp;gt;
            &amp;lt;parameter name="browser" value="Chrome" /&amp;gt;
            &amp;lt;parameter name="version" value="101.0" /&amp;gt;
            &amp;lt;parameter name="platform" value="WIN11" /&amp;gt;
            &amp;lt;classes&amp;gt;
                &amp;lt;class name="testng.ParallelTest" /&amp;gt;
            &amp;lt;/classes&amp;gt;
        &amp;lt;/test&amp;gt;

        &amp;lt;test name="Firefox Test"&amp;gt;
            &amp;lt;parameter name="browser" value="Firefox" /&amp;gt;
            &amp;lt;parameter name="version" value="100.0" /&amp;gt;
            &amp;lt;parameter name="platform" value="WIN8" /&amp;gt;
            &amp;lt;classes&amp;gt;
                &amp;lt;class name="testng.ParallelTest" /&amp;gt;
            &amp;lt;/classes&amp;gt;
        &amp;lt;/test&amp;gt;

        &amp;lt;test name="Safari Test"&amp;gt;
            &amp;lt;parameter name="browser" value="safari" /&amp;gt;
            &amp;lt;parameter name="version" value="11.0" /&amp;gt;
            &amp;lt;parameter name="platform" value="macos 10.13" /&amp;gt;
            &amp;lt;classes&amp;gt;
                &amp;lt;class name="testng.ParallelTest" /&amp;gt;
            &amp;lt;/classes&amp;gt;
        &amp;lt;/test&amp;gt;

        &amp;lt;test name="MS Edge Test"&amp;gt;
            &amp;lt;parameter name="browser" value="Edge" /&amp;gt;
            &amp;lt;parameter name="version" value="101.0" /&amp;gt;
            &amp;lt;parameter name="platform" value="WIN10" /&amp;gt;
            &amp;lt;classes&amp;gt;
                &amp;lt;class name="testng.ParallelTest" /&amp;gt;
            &amp;lt;/classes&amp;gt;
        &amp;lt;/test&amp;gt;

        &amp;lt;test name="Opera Test"&amp;gt;
            &amp;lt;parameter name="browser" value="Opera" /&amp;gt;
            &amp;lt;parameter name="version" value="85.0" /&amp;gt;
            &amp;lt;parameter name="platform" value="WIN10" /&amp;gt;
            &amp;lt;classes&amp;gt;
                &amp;lt;class name="testng.ParallelTest" /&amp;gt;
            &amp;lt;/classes&amp;gt;
        &amp;lt;/test&amp;gt; &amp;lt;!-- Test --&amp;gt;
    &amp;lt;/suite&amp;gt; &amp;lt;!-- Suite --&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have a total of 5 different tags, and each of these will run on different threads.&lt;/p&gt;

&lt;p&gt;5- Finally, it is time to run our test case. Right-click on our testng.xml file and navigate to &lt;strong&gt;&lt;em&gt;Run As &amp;gt; TestNG Test Suite&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%2F1%2AMCksfwnERNAKsMgYg4xoVQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AMCksfwnERNAKsMgYg4xoVQ.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our test starts getting executed. Go to your &lt;a href="https://accounts.lambdatest.com/login?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=it_sign_up" rel="noopener noreferrer"&gt;LambdaTest Automation Dashboard&lt;/a&gt; to see the live running status.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://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%2AopHtFscoSmTxcyy9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AopHtFscoSmTxcyy9.png" width="800" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The test is executed parallelly in the 5 browsers, and reports and logs are generated 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%2F3200%2F0%2AubReSkrbraVZZodd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://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%2AubReSkrbraVZZodd.png" width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices for Selenium automation testing
&lt;/h2&gt;

&lt;p&gt;Through automation testing, we aim to reduce the labor of manual testing, speed up the execution, detect maximum bugs early, achieve better coverage and accelerate time to market. However, writing a stable and reliant test case can be challenging due to various factors like test flakiness, incorrect testing approach, etc. This is where &lt;a href="https://www.lambdatest.com/blog/why-did-we-build-hypertest/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jun02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;HyperExecute&lt;/a&gt; comes into the picture, which eliminates these factors and cuts down on test execution times. This allows businesses to test code and fix issues much quicker and hence achieve an accelerated time to market.&lt;/p&gt;

&lt;p&gt;In this section of this article on Selenium with Java, let us discuss some of the well-encouraged guidelines and design patterns recommended by the Selenium community to help you create well-architectured test suites. Remember, there is no “One Size Fits all” situation when it comes to test automation. Thoughtfully decide what will work the best for your particular environment.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Planning and Designing Test Cases beforehand&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before getting started with writing any automation tests, it is recommended that the QA teams invest time in creating a proper test plan. Surveys show that 80% of the projects dive straight into automation without having a tangible strategy.&lt;/p&gt;

&lt;p&gt;However, to assure fool-proof testing of the application, QAs should come up with all possible logical scenarios and create maximum test cases based on the end-user perspective.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Implementing Page Object Model&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When a UI change occurs, the Selenium locators may also change, and we are required to update the modified locators in all the test scripts consuming it. But, as the application complexity increases and UI changes become more frequent, creating and maintaining test cases becomes tiresome. The Page Object Model (POM) is a popular design pattern in Selenium automation testing, which helps in enhancing test maintenance and reducing code duplication.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Choosing the Best-suited Locators&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While writing automation test scripts, a large chunk of the time is spent locating the web elements. Selenium offers us multiple strategies to locate a web element such as ID, Name, Link Text, XPath, CSS Selector, DOM Locator, etc.&lt;/p&gt;

&lt;p&gt;However, some web locators are more brittle than others. As the UI implementation is prone to frequent changes, we must be cautious in choosing the right web locators, which will minimize the impact of UI code changes in the automation tests. If available, opt for unique locators like ID and Name, which are easy to find and more reliable as they are less prone to change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Implement Logging and Reporting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As the test suite becomes extensive, locating the failed test case and debugging becomes challenging. In these cases, reporting and logging practices can act as huge saviors.&lt;/p&gt;

&lt;p&gt;The test report is a document that contains all the information about a particular test run, including the number of test cases passed, the number of test cases failed, total time taken, error with a snapshot, etc. Even though default Selenium does not come with reporting, you can implement it by leveraging test automation frameworks like TestNG.&lt;/p&gt;

&lt;p&gt;Logging helps us to understand our code better and hence makes debugging easier. Examining logs is the quickest way to analyze why your code faces a problem. Some of the popular log levels are debug, info, warning, error, and critical. Good logging saves time. However, if overdone, it can slow down test execution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Incorporating Wait Commands&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The web browsers take some time to load the web page contents. The time taken for page load depends on many external factors like network speed, server issues, machine capabilities, etc. To tackle this, testers need to add some delay into the code so that the web element will be loaded and available before performing any action on it.&lt;/p&gt;

&lt;p&gt;In Java, testers often achieve this delay using Thread.sleep(). However, this is not the recommended practice. As the name implies, when the Thread.sleep() method is called, the thread goes to sleep for the specified amount of time and does absolutely nothing to pause the automation script. After the time is over, it moves on to executing the next line of code.&lt;/p&gt;

&lt;p&gt;In real-time, we won’t be able to predict the exact time it would take for the page load as it is dependent on many external factors. Sometimes the page may load quickly, lowering the test execution speed due to the remaining unnecessary delay caused by sleep(). And sometimes, it may take longer than the specified time, causing the test to fail as the element was not fully loaded. As a better and more efficient alternative, use the &lt;a href="https://www.selenium.dev/documentation/webdriver/waits/" rel="noopener noreferrer"&gt;Implicit wait and Explicit wait&lt;/a&gt; commands provided by Selenium instead of the sleep method.&lt;/p&gt;

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

&lt;p&gt;Selenium with Java is the most widely adopted test automation combination among QAs. In this tutorial, we explored how to get started with Selenium Automation Testing with Java and wrote our very first test case. On top of that, we also learned how to optimize test execution by leveraging parallel testing and other best practices to establish a reliable testing cycle. I hope this tutorial turns out to be beneficial. There’s much more to explore!&lt;/p&gt;

&lt;p&gt;Happy Testing!&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>java</category>
      <category>testing</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How To Use JavaScriptExecutor in Selenium WebDriver?</title>
      <dc:creator>HaritaLT</dc:creator>
      <pubDate>Fri, 22 Apr 2022 11:35:42 +0000</pubDate>
      <link>https://forem.com/haritalt/how-to-use-javascriptexecutor-in-selenium-webdriver-3888</link>
      <guid>https://forem.com/haritalt/how-to-use-javascriptexecutor-in-selenium-webdriver-3888</guid>
      <description>&lt;p&gt;Webdriver is a tool that requires no introduction in the &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harita-29062021&amp;amp;utm_term=Harita"&gt;Selenium automation testing&lt;/a&gt; domain. Webdriver is an open-source collection of APIs which is used for testing web applications across different browsers and platforms to verify whether it is working as expected or not. The latest version of Selenium — Selenium 4 is W3C compliant — however it is still in the Beta phase (i.e. 4.0.0-beta-3).&lt;/p&gt;

&lt;p&gt;In Selenium 3, the JSON Wire Protocol is used for communicating with the web browser from the local end. In Selenium test automation with the Selenium 3 framework, API encoding &amp;amp; decoding is involved in the entire process since web browsers use the W3C protocol and the Selenium Client Libraries use the JSON Wire Protocol.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://www.lambdatest.com/feature?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harish-29062021&amp;amp;utm_term=Harish"&gt;Selenium WebDriver&lt;/a&gt; allows you to communicate with various browsers to perform commands and assertions on DOM elements using which you can perform any kind of operations like sending the text, click on a button, select a radio button, select the drop-downs, etc.&lt;/p&gt;

&lt;p&gt;However, there are scenarios where the actual Webdriver commands will not work efficiently as expected. This is where JavaScriptExecutor comes into the picture.&lt;/p&gt;

&lt;p&gt;In this article, we will be discussing JavaScriptExecutor in Selenium WebDriver. Also I will cover everything you need to know about how to use JavaScriptExecutor in Selenium. Getting started with an overview, we will further dive into the implementation and explore different methods in JavascriptExecutor along with working examples.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  What is JavaScriptExecutor in Selenium?
&lt;/h2&gt;

&lt;p&gt;JavaScriptExecutor is an interface provided by Selenium Webdriver, which presents a way to execute &lt;a href="https://www.lambdatest.com/blog/responsive-web-design-all-you-need-to-know/?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harish-29062021&amp;amp;utm_term=Harish"&gt;JavaScript&lt;/a&gt; from Webdriver. This interface provides methods to run JavaScript on the selected window or the current page. JavaScriptExecutor is an interface which is available for all the languages that support the &lt;a href="https://www.lambdatest.com/blog/top-9-best-practices-to-avoid-browser-compatibility-issues/?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harish-29062021&amp;amp;utm_term=Harish"&gt;Selenium Framework&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As JavaScriptExecutor is a Selenium interface, there is no need for an extra plugin or add-on. You can use it directly by importing the package &lt;strong&gt;org.openqa.selenium.JavascriptExecutor&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use JavaScriptExecutor in Selenium?
&lt;/h2&gt;

&lt;p&gt;Selenium Webdriver alone lets you perform operations on web elements like sending data, clicking buttons, etc. However, as said before, there are scenarios where the Webdriver commands will not work as expected due to various reasons. In these cases, we take the help of JavaScriptExecutor.&lt;/p&gt;

&lt;p&gt;For example, traditionally to locate a web element we use different web locator strategies such as ID, Name, CSS selector, and XPath, etc. If these locators do not work as expected, or you are handling a tricky XPath, you can use JavaScriptExecutor to perform the desired operation on the WebElement..&lt;/p&gt;

&lt;p&gt;On similar lines, to click on a WebElement, we generally use the click() method provided by Selenium Webdriver.&lt;/p&gt;

&lt;p&gt;The WebDriver error — invalid cookie domain error, occurs if a cookie is illegally attempted to be set under a different domain from the current document.&lt;/p&gt;

&lt;p&gt;The WebDriver error — invalid selector-error, happens when an element fetch is tried with an unknown web element selection method.&lt;/p&gt;

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

&lt;/div&gt;
&lt;p&gt;At times, the click() method may not work on all the web browsers or the web controls might behave differently on different browsers. To overcome such situations, the JavaScriptExecutor interface should be used.&lt;/p&gt;

&lt;p&gt;As we know, browsers have Javascript implementation inside them and can understand the JavaScript commands. Hence, understanding &lt;a href="https://www.lambdatest.com/lt-browser?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harish-29062021&amp;amp;utm_term=Harish"&gt;JavaScriptExecutor in Selenium&lt;/a&gt; will enable you to perform a range of operations more efficiently and elegantly.&lt;/p&gt;

&lt;p&gt;Note: A scalable and reliable &lt;a href="https://www.lambdatest.com/testing-cloud/angularjs-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr22_rd&amp;amp;utm_term=rd&amp;amp;utm_content=testing_cloud"&gt;online testing&lt;/a&gt; cloud for both manual and automation testing of AngularJS websites&lt;/p&gt;
&lt;h2&gt;
  
  
  Introduction to JavaScriptExecutor Methods
&lt;/h2&gt;

&lt;p&gt;JavaScriptExecutor in Selenium provides two methods through which we can run JavaScript on the selected window or the current page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;a) executeScript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method executes JavaScript in the context of the currently selected window or frame in Selenium. The script will be executed as the body of an anonymous function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b) executeAsyncScript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method executes an asynchronous snippet of JavaScript in the context of the currently selected window or frame in Selenium. Here also, the script will be executed as the body of an anonymous function. The major difference between executeScript and executeAsyncScript is that the script invoked using the executeAsyncScript has to signal about the completion of execution using the callback function.&lt;/p&gt;

&lt;p&gt;Invoking methods using executeAsyncScript are majorly used when sleep has to be performed in the browser under test or when tests have to be synchronized within an AJAX application.&lt;/p&gt;

&lt;p&gt;Both the methods allow us to return a value using the ‘ return ' keyword and store them in a variable. The data types that can be returned are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Long&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;String&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Boolean&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;List&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;WebElement&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Basic Syntax of JavaScriptExecutor in Selenium
&lt;/h2&gt;

&lt;p&gt;The purpose of this section is to provide a high-level idea about the implementation steps of JavaScriptExecutor in Selenium. For demonstration, we have used Java as the prefered programming language.&lt;/p&gt;

&lt;p&gt;Let’s take a look at the key steps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Import the package associated with JavascriptExecutor.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import org.openqa.selenium.JavascriptExecutor;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Now to utilize JavascriptExecutor, create a reference for the interface and assign it to the WebDriver instance by type casting it.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavascriptExecutor js = (JavascriptExecutor) driver;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; We have created a JavascriptExecutor reference. Now we call the executeAsyncScript/executeScript methods. The syntax for &lt;strong&gt;executeScript&lt;/strong&gt; is given below:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript​(java.lang.String script, java.lang.Object... args)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Parameters:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;script&lt;/strong&gt; — The JavaScript to execute&lt;br&gt;
&lt;strong&gt;args&lt;/strong&gt; — The arguments to the script. May be empty.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Returns Value&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Boolean/Long/Double/String/List/Map/WebElement/Null.&lt;/p&gt;

&lt;p&gt;Now that we have covered the essentials of JavaScriptExecutor in Selenium, it’s time to get our hands dirty with some implementation! In the upcoming sections, we will check out some scenarios where we use JavaScriptExecutor in Selenium to perform test scenarios (that might not have been performed with common &lt;a href="https://www.lambdatest.com/blog/how-to-make-responsive-images-with-css-html-wordpress-more/?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harish-29062021&amp;amp;utm_term=Harish"&gt;Selenium WebDriver commands&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Note: Test your Appcelerator Titanium CSS framework-based websites across 3000+ different desktop and mobile browsers. A scalable and reliable online testing cloud for both manual and automation testing of &lt;a href="https://www.lambdatest.com/testing-cloud/appcelerator-titanium-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr22_rd&amp;amp;utm_term=rd&amp;amp;utm_content=testing_cloud"&gt;Appcelerator Titanium websitesJavaScript Design Pattern&lt;/a&gt;s.&lt;/p&gt;
&lt;h2&gt;
  
  
  Scenarios To Use JavaScriptExecutor in Selenium
&lt;/h2&gt;

&lt;p&gt;Let’s examine some scenarios we could handle using JavaScriptExecutor Interface for Selenium test automation.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;To Click on a Button&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;js.executeScript("document.getElementById('enter element id').click();");&lt;/p&gt;

&lt;p&gt;//or&lt;br&gt;
js.executeScript("arguments[0].click();", okButton);&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;2. To Type Text in a Text Box without using sendKeys() method&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript("document.getElementById(id').value='someValue';");
js.executeScript("document.getElementById('Email').value='SeleniumTesting.com';");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;3. To Handle Checkbox by passing the value as true or false&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript("document.getElementById('enter element id').checked=false;");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;4. To generate Alert Pop window in Selenium Webdriver&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript("alert('Welcome To Selenium Testing');");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;5. To refresh browser window using Javascript&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript("history.go(0)");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;6. To get the innertext of the entire webpage in Selenium&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String innerText = js.executeScript(" return document.documentElement.innerText;").toString();
System.out.println(innerText);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;7. To get the Title of our webpage&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String titleText =  js.executeScript("return document.title;").toString();
System.out.println(titleText);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;8. To get the domain name&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String domainName=  js.executeScript("return document.domain;").toString();
System.out.println(domainName);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;9. To get the URL of a webpage&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String url=  js.executeScript("return document.URL;").toString();
System.out.println(url);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;10. To get the Height and Width of a web page&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript(“return window.innerHeight;”).toString();
js.executeScript(“return window.innerWidth;”).toString();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;11. To find a hidden element in selenium using JavaScriptExecutor&lt;/strong&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].click();", element);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;12. To navigate to a different page using Javascript&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript("window.location = '[https://www.lambdatest.com](https://www.lambdatest.com)");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;13. To perform Scroll on an application using Selenium&lt;/strong&gt; &lt;strong&gt;a) To scroll the page vertically for 500px&lt;/strong&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,500)”);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;b) To scroll the page vertically till the end&lt;/strong&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,document.body.scrollHeight)”);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;14. Adding an element in DOM&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can also add an element in DOM if required. However, as are only concerned with mimicking user interactions in the browser, this option may not be used.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;js.executeScript("var btn=document.createElement('newButton');"

                     + "document.body.appendChild(btn);");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;On similar lines, we can practically execute any JavaScript command using Selenium.&lt;/p&gt;

&lt;p&gt;Note: Test your ASP.NET CSS framework-based websites across 3000+ different desktop and mobile browsers. A scalable and reliable online testing cloud for both manual and automation testing of &lt;a href="https://www.lambdatest.com/testing-cloud/asp.net-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr22_rd&amp;amp;utm_term=rd&amp;amp;utm_content=testing_cloud"&gt;ASP.NET websites&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  JavaScriptExecutor in Selenium WebDriver Example
&lt;/h2&gt;

&lt;p&gt;In the previous section, we looked at a widely-used JavaScriptExecutor in Selenium commands. Now it’s time to run an end-to-end test scenario! We will be continuing to use Java as the programming language for the demonstration JavaScript in Selenium WebDriver. Let’s dive in!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pre-requisites&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First and foremost, make sure that your system is equipped with the following:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Java SDK&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Download and install the Java Software Development Kit (JDK) from &lt;a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Eclipse or any IDE of choice.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Download the latest version of “Eclipse IDE for Java Developers” from &lt;a href="http://www.eclipse.org/downloads/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Selenium-Java client Driver&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Download Selenium Webdriver for Java Client Driver from &lt;a href="https://selenium.dev/downloads/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Browser Drivers (only for Local Selenium Grid)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Download various browser drivers from &lt;a href="https://www.selenium.dev/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once this criterion is checked, proceed to create a ‘New Project’ in Eclipse. Don’t forget to configure the IDE with Webdriver by adding all the Jars required to the project.&lt;/p&gt;

&lt;p&gt;Instead of the local Selenium Grid, the tests are run on a &lt;a href="https://www.lambdatest.com/blog/mobile-user-experience-drives-success/?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harish-29062021&amp;amp;utm_term=Harish"&gt;cloud based Selenium Grid&lt;/a&gt; by LambdaTest. It provides improved scalability, reliability, and better browser coverage since parallel tests can be run on a range of browser &amp;amp; OS combinations. LambdaTest Selenium Automation Grid is a cloud-based, scalable Selenium test automation platform that enables you to run your automation scripts on 2000+ different browsers and operating systems online.&lt;/p&gt;

&lt;p&gt;You can effortlessly migrate the Selenium tests running on a local Selenium Grid to the LambdaTest Selenium Grid through a few additional configurations. After creating a &lt;a href="https://accounts.lambdatest.com/register"&gt;LambdaTest account&lt;/a&gt;, make a note of the username &amp;amp; access-key available in the &lt;a href="https://accounts.lambdatest.com/detail/profile"&gt;LambdaTest profile&lt;/a&gt; section. The browser capabilities are generated using &lt;a href="https://www.lambdatest.com/capabilities-generator/"&gt;LambdaTest Capabilities Generator&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Time to get our hands dirty with some implementation!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test Scenario 1 (Demonstration of executeScript)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Login and generate an Alert window using JavaScriptExecutor&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our objective is to write a simple code to illustrate an example using executeScript, where we will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Launch the web browser (Chrome — latest version on Windows 10 platform)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to URL “&lt;a href="https://opensource-demo.orangehrmlive.com/"&gt;https://opensource-demo.orangehrmlive.com/&lt;/a&gt;"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter dummy credentials&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click the login button&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Print page title and domain name&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Display alert window on successful login.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j6ps3mwr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2048/0%2A_6AbyS5oFbdrumHi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j6ps3mwr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2048/0%2A_6AbyS5oFbdrumHi.png" alt="" width="880" height="547"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Run the script. The test gets executed successfully!&lt;/p&gt;

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

&lt;p&gt;Observe the console output and output from &lt;a href="https://automation.lambdatest.com/"&gt;Automation Dashboard&lt;/a&gt; on LambdaTest.&lt;/p&gt;

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

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

&lt;p&gt;&lt;strong&gt;Test Scenario 2 (Demonstration of executeAsyncScript)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Login and generate an Alert window using JavaScriptExecutor&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our objective is to write a simple code to illustrate an example using executeAsyncScript, where we will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Launch the web browser (Chrome — latest version on Windows 10 platform)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to URL “ &lt;a href="https://www.lambdatest.com/"&gt;https://www.lambdatest.com/&lt;/a&gt; “&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Wait for 10 seconds to perform a further action&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

Run the script. The test gets executed successfully! Here is the snapshot of the execution window (of the IDE) and automation dashboard on LambdaTest.&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;p&gt;With this, we have successfully run our test cases on remote Selenium Grid provided by LambdaTest through a hassle-free experience!&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping It Up!
&lt;/h2&gt;

&lt;p&gt;JavaScriptExecutor in Selenium is an interface that is used when Webdriver commands are not working as expected. JavaScriptExecutor allows us to execute JavaScript code on the web page from Webdriver, hence enabling us to handle various operations efficiently and elegantly which is otherwise not possible with languages like Java alone.&lt;/p&gt;

&lt;p&gt;In this blog, we learnt how to use JavascriptExecutor in Selenium WebDriver and also understood the different methods provided by JavascriptExecutor. Further we covered various scenarios to attain an effective solution using different methods along with practical examples.&lt;/p&gt;

&lt;p&gt;Additionally, we have also performed Selenium test automation by adopting a remote &lt;a href="https://www.lambdatest.com?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harish-29062021&amp;amp;utm_term=Harish"&gt;Selenium Grid&lt;/a&gt;. If you wish to test and optimize the web application by unlocking coverage in a wider variety of platforms or browsers or versions, feel free to explore the LambdaTest platform.&lt;/p&gt;

&lt;p&gt;Happy Testing!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>selenium</category>
    </item>
    <item>
      <title>How To Perform Parallel Test Execution In TestNG With Selenium</title>
      <dc:creator>HaritaLT</dc:creator>
      <pubDate>Thu, 21 Apr 2022 14:32:18 +0000</pubDate>
      <link>https://forem.com/haritalt/how-to-perform-parallel-test-execution-in-testng-with-selenium-3e86</link>
      <guid>https://forem.com/haritalt/how-to-perform-parallel-test-execution-in-testng-with-selenium-3e86</guid>
      <description>&lt;p&gt;The evolution in the process of software delivery in organizations in response to business agility has resulted in a paradigm shift from traditional release cycles to continuous release models. To achieve the product delivery objectives in this new paradigm, &lt;a href="https://www.lambdatest.com/blog/continuous-testing-using-shift-left-testing-approach/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr21_rd&amp;amp;utm_term=rd&amp;amp;utm_content=blog"&gt;continuous testing&lt;/a&gt; plays a vital role in ensuring the quality of end-to-end processes, along with ensuring effective collaboration between Quality Assurance (QA) and development teams.&lt;/p&gt;

&lt;p&gt;The new era of continuous testing demands QA teams to explore solutions to increase test execution speed. Enterprises are looking to leverage the benefits offered by automation testing while ensuring that manual testing is used in scenarios where automation is not possible.&lt;/p&gt;

&lt;p&gt;The traditional approach of manual testing is already taken over by automation testing. One popular automation testing strategy widely adopted by QA teams to further reduce the testing time while ensuring maximum test coverage is the shift from sequential testing to &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=apr21_rd&amp;amp;utm_term=rd&amp;amp;utm_content=blog"&gt;parallel testing&lt;/a&gt;. You can perform parallel test execution in TestNG or any other framework of your choice with Selenium.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RfPHzN0A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Ar9Py1hJ9r9tkdgeo9osJlw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RfPHzN0A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Ar9Py1hJ9r9tkdgeo9osJlw.gif" alt="" width="500" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this &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=apr21_rd&amp;amp;utm_term=rd&amp;amp;utm_content=blog"&gt;Selenium TestNG&lt;/a&gt; tutorial, we deep dive into the integral aspects of parallel testing in TestNG.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Introduction to Parallel Test Execution in TestNG
&lt;/h2&gt;

&lt;p&gt;Parallel testing is an approach in which independent tests are run parallel to reduce overall test execution time. A tester runs two or more versions of the software concurrently with the same input or test methods. Each test can be run on multiple devices, browsers, and OS combinations.&lt;/p&gt;

&lt;p&gt;The parallel testing process can be applied to a single application, multiple applications, or even multiple components of an application. Parallel execution in TestNG can be achieved at different levels — threads, methods, etc.&lt;/p&gt;

&lt;p&gt;Parallel testing was introduced to replace the traditional approach of sequential testing. As the name suggests, in sequential test execution tests are executed one after another, which is more time-consuming.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VxxnhEsT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AIdvudChXslX7lJV-yrVUug.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VxxnhEsT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AIdvudChXslX7lJV-yrVUug.jpeg" alt="" width="801" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because of the growing demand for cross-browser testing in today’s market, parallel testing is implemented heavily with Selenium automation testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of Parallel Test Execution in TestNG
&lt;/h2&gt;

&lt;p&gt;Parallel testing is a powerful approach that allows you to reduce the time taken to perform &lt;a href="https://www.lambdatest.com/learning-hub/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr21_rd&amp;amp;utm_term=rd&amp;amp;utm_content=learning_hub"&gt;automation testing&lt;/a&gt;. There are various reasons that make it important for testers to adopt parallel testing in Selenium. Some of the most popular reasons are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Accelerated Test Execution Speed&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If a sequential test takes 1 minute to complete a single test execution, it will take 5 minutes to run five tests one after another on the same device. However, overall test execution will take 1 minute (or less) when the same test is run in parallel on 5 devices (or input combinations).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Increased Test Coverage and Wider Compatibility&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Parallel testing enables you to test across more devices (desktop or mobile), browsers, and OS combinations at once. This extended test coverage will help in risk mitigation, reducing the chance of unnoticed bugs and offering wider compatibility, which will be instrumental in shipping a top-quality product.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Overcome the Testing Bottleneck&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Testing is often the bottleneck of DevOps. Tedious testing cycles can hold up the entire release process. But this bottleneck can be removed by accelerated test execution through parallel testing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fix Bugs Earlier&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Earlier the testing is completed, earlier the bugs can be unearthed and fixed. In addition, parallel testing is results-driven, since we can test more scenarios in less time, and there is more actionable data towards enhancements.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Flexibility&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can revert to sequential testing when necessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges of Parallel Test Execution in TestNG
&lt;/h2&gt;

&lt;p&gt;To achieve full-fledged parallel testing, as with any type of testing, you may face some challenges. The common challenges that you will run into are often related to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Scalability&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Infrastructure limitations. This includes:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Availability of secure, enterprise-grade lab that meet all the requirements&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Recurring investments involved in the maintenance and upgradation of test infrastructure.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The popular strategy recommended to overcome these challenges and move towards parallel testing is to use cloud-based testing providers. Through &lt;a href="https://www.lambdatest.com/blog/cloud-testing-tutorial/"&gt;Cloud testing&lt;/a&gt;, teams can test their apps on devices and browsers hosted on the cloud infrastructure. By utilizing a cloud-based testing infrastructure, you no longer need to maintain in-house infrastructure.&lt;/p&gt;

&lt;p&gt;Also, the scalability issues associated with the local testing infrastructure for covering all major browsers, their different versions, and OS will be taken care of. Needless to say, it is more cost-efficient as well.&lt;/p&gt;

&lt;p&gt;Cloud-based cross browser testing platforms like LambdaTest offer a cloud Selenium Grid with access to 3000+ real browsers and devices for your testing needs. You can choose a specific OS-device-browser combination as per your requirement. The platform also aids you in performing parallel testing in Selenium, which can drastically scale your automation test and reduce test execution time through easy setup and minimal configurations.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to perform Parallel Test Execution in TestNG?
&lt;/h2&gt;

&lt;p&gt;TestNG (where NG stands for ‘Next Generation’) is an open-source Java-based test automation framework that has gained huge popularity among developers and testers. The framework itself is inspired by JUnit, another popular unit testing framework in the Java ecosystem. However, it offers a variety of advanced and distinctive features, making it much more powerful than its peers. One such remarkable feature is the seamless support for Parallel Testing.&lt;/p&gt;

&lt;p&gt;The TestNG &lt;a href="https://www.lambdatest.com/automation-testing"&gt;test automation&lt;/a&gt; framework allows you to run tests in parallel or multithreaded mode by utilizing the Multi-Threading concept of Java. Multi-Threading is the process of executing multiple threads simultaneously, without any dependence on each other. Therefore, exceptions occurring on one thread won’t affect the others. In TestNG, we enable parallel testing by making required changes in the configuration file — TestNG XML file.&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;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/dzXX2hJhuCY"&gt;
&lt;/iframe&gt;
&lt;br&gt;
However, you can follow the &lt;a href="https://www.youtube.com/c/Lambdatest?sub_confirmation=1"&gt;LambdaTest YouTube Channel&lt;/a&gt; and stay updated with the latest certifications and tutorials around Selenium testing, &lt;a href="https://www.lambdatest.com/cypress-testing"&gt;Cypress testing&lt;/a&gt;, and more.&lt;/p&gt;
&lt;h2&gt;
  
  
  Parallel Test Execution in Selenium using Configuring TestNG XML in TestNG
&lt;/h2&gt;

&lt;p&gt;TestNG XML is the test suite configuration file in TestNG, which helps you organize your tests by defining test suites, grouping test cases in TestNG, and running them under one XML file. IN a nutshell, it helps in customizing the execution of tests.&lt;/p&gt;

&lt;p&gt;It also allows you to run a single test file over numerous combinations of parameters and values specified in it. First, let us go through the key features of the TestNG XML file, which will be useful for this tutorial.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Provides seamless parallel test execution&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supports &lt;a href="https://www.lambdatest.com/blog/parameterization-in-testng-dataprovider-and-testng-xml-examples/?utm_source=medium&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr21_rd&amp;amp;utm_term=rd&amp;amp;utm_content=blog"&gt;parameterization in TestNG&lt;/a&gt; using @Parameter annotation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Allows grouping of test methods and group execution&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Allows executing test cases inside multiple Java class files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;TestNG listeners automatically create logs&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A simple TestNG XML file without any configuration for the parallel tests will look like this.&lt;/p&gt;

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

&lt;p&gt;To trigger parallel test execution in TestNG, i.e., run tests on separate threads, we need to set the parallel attribute. This attribute accepts four values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;methods *&lt;/em&gt;— runs all methods with &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; annotation in parallel mode&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;tests&lt;/strong&gt; — runs all test cases present inside  tag in the XML in parallel mode&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;classes *&lt;/em&gt;— runs all test cases present inside classes in the XML in parallel mode&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;instances *&lt;/em&gt;— runs all test cases in the same instance in parallel mode&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can also set the number of threads we wish to allocate during the execution using the thread-count attribute. TestNG has a default value of thread = 5.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;suite name="Regression Test Suite" parallel="methods" thread-count="2"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Here, we have defined parallel test execution at suite level, and the &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; methods will start running simultaneously. The thread count is set to be 2, which means that only two threads will be created for this execution, on which 2 test methods will start running parallelly. If there are more than 2 methods, all the remaining ones need to wait until any of the threads get free.&lt;/p&gt;

&lt;p&gt;Now that we know how to configure TestNG for running tests simultaneously, it’s time to do the hands-on! In the upcoming section of this Selenium TestNG tutorial, we will learn how to create a TestNG framework setup and write tests and execute them parallelly through the online Selenium Grid.&lt;/p&gt;
&lt;h2&gt;
  
  
  Parallel Test Execution in multiple browsers using TestNG for Selenium automation testing
&lt;/h2&gt;

&lt;p&gt;In this section, we will be writing test cases in the TestNG framework and learning how to execute them parallelly at tests, classes, and methods levels by configuring the TestNG XML file. Our test cases will be executed on 5 different browsers — Chrome, Firefox, Safari, IE, and Opera by leveraging the online Selenium Grid provided by LambdaTest. Also, a custom configuration will be done for the browser as well as the OS version. Using LambdaTest, you can now test on Safari browser online on both desktop and mobile browsers for cross browser compatibility.&lt;/p&gt;

&lt;p&gt;If you are already working with TestNG, feel free to skip the installation and project set-up part, which we will be covering next.&lt;/p&gt;
&lt;h2&gt;
  
  
  Installation and Setup
&lt;/h2&gt;

&lt;p&gt;Before getting started with the project setup, make sure that your system is equipped with the following prerequisites:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;**Java&lt;br&gt;
**TestNG requires JDK 8 or higher. If you do not have Java installed in your system, you can &lt;a href="https://www.oracle.com/java/technologies/downloads/"&gt;download Java&lt;/a&gt; from the Oracle official website. Don’t forget to set the environment variables as well.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Eclipse IDE&lt;br&gt;
**In this tutorial, for the demo, we will be using Eclipse as IDE. If not installed already, you can &lt;a href="https://www.eclipse.org/downloads/"&gt;download Eclipse&lt;/a&gt; from the official Eclipse website.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Selenium Webdriver and Client for Java&lt;br&gt;
**You can download by visiting the Selenium WebDriver &lt;a href="https://www.selenium.dev/downloads/"&gt;official website&lt;/a&gt;. Selenium WebDriver is an open-source, free Selenium testing tool for browser automation.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once this list is checked, the next step is to &lt;a href="https://www.lambdatest.com/blog/how-to-install-testng-in-eclipse-step-by-step-guide/"&gt;install TestNG in Eclipse&lt;/a&gt;. First, go to Eclipse Marketplace and install TestNG. Once installed, you can locate it under the Installed section.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--01XXAG04--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AQXzwkFhqu5aFsif9uVohvA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--01XXAG04--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AQXzwkFhqu5aFsif9uVohvA.jpeg" alt="" width="853" height="1012"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You have now completed the installation. Let us do the basic project setup now.&lt;/p&gt;

&lt;p&gt;Note: &lt;a href="https://www.lambdatest.com/web-technologies/classlist?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr21_rd&amp;amp;utm_term=rd&amp;amp;utm_content=web_technologies"&gt;ClassList&lt;/a&gt; provides a way to manipulate classes using the DOMTokenList object.&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating a TestNG Project
&lt;/h2&gt;

&lt;p&gt;To use TestNG in a Java project, you must know how to create a TestNG project. You can follow the below-mentioned steps to create a new Java project in Eclipse.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Launch Eclipse and create a new Java project by navigating to &lt;strong&gt;File &amp;gt; New &amp;gt; Java Project&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HZ7d5-vd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AA0c2aAs3MXlaKywXlmC-gQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HZ7d5-vd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AA0c2aAs3MXlaKywXlmC-gQ.png" alt="" width="628" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Name your project and click Next.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Add libraries by following the below-mentioned steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Right-click on the project folder and select Properties.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose Java Build Path.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to Libraries, select Classpath, and click Add Library.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9eZmm2yu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AO-7VsSmGSQMMqokgagD7FQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9eZmm2yu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AO-7VsSmGSQMMqokgagD7FQ.png" alt="" width="846" height="775"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Select TestNG and click Next.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: Now, you will see that TestNG is added to the project library.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fkrtrI-k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2120/1%2APsDIjRnlE2iCaB38e_UrkA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fkrtrI-k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2120/1%2APsDIjRnlE2iCaB38e_UrkA.jpeg" alt="" width="880" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt;: Click Finish, and we have successfully created a TestNG project.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Step 7&lt;/strong&gt;: Add Selenium API JAR files to the TestNG project by following the below-mentioned steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Right-click on the project and select Properties.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select Java Build Path.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Under the libraries section, click on Add External JARs, which gets enabled upon opting for Classpath.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select the option and upload the JARs for Selenium Java language bindings by navigating to the path where you have downloaded and saved it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--knEzOXlm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2712/1%2A3XemaQjv9vlr3_t9kvLyJg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--knEzOXlm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2712/1%2A3XemaQjv9vlr3_t9kvLyJg.png" alt="" width="880" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Once done, select Apply and Close.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--94B6IVyH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2748/1%2AN4aUXQO0BXrQhE2rAo0OBg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--94B6IVyH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2748/1%2AN4aUXQO0BXrQhE2rAo0OBg.jpeg" alt="" width="880" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this, our project set-up is done, and we are good to start coding. Notice that the libraries are also visible in the left panel.&lt;/p&gt;

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

&lt;p&gt;Note: The &lt;a href="https://www.lambdatest.com/web-technologies/canvas?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr21_rd&amp;amp;utm_term=rd&amp;amp;utm_content=web_technologies"&gt;Canvas&lt;/a&gt; API provides a way to generate fast, dynamic graphics using JavaScript.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to run test cases in parallel with Selenium in TestNG on Cloud Grid
&lt;/h2&gt;

&lt;p&gt;In this section, we will learn how to achieve parallel test execution in TestNG by running the test cases on separate threads. Follow the below mentioned steps:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Launch Eclipse and create a new Java project for TestNG. I have named it ParallelTestNG. You can skip this step if you have followed along with me in the Project set-up part.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Create a TestNG class file by following the below-mentioned steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Right-click on project src and find TestNG towards the bottom of the list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select Create TestNG Class.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;ol&gt;
&lt;li&gt;Name the class, choose the required annotations and click Finish. Here, I have named the class as TestCasesParallelExecution.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Let us quickly go through the TestNG annotations that have been used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;/strong&gt; — Implies that the method is a test case. Therefore, all tests should be marked with this annotation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;@BeforeMethod&lt;/strong&gt; and &lt;strong&gt;@AfterMethod&lt;/strong&gt; — These annotations run once before and after each &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; method in a class.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Write our first test case using TestNG and Selenium. Finally, it is time to get our hands dirty! In this section, we will be writing our first test case using TestNG. For this demo, I will be writing an automation test for a simple login scenario. Following are the test steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Launch the website URL (&lt;a href="https://opensource-demo.orangehrmlive.com/"&gt;https://opensource-demo.orangehrmlive.com/&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter username and password.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the LOGIN button.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Like mentioned before, we will be running this test case parallelly on five different browsers through an online Selenium Grid provided by LambdaTest.&lt;/p&gt;

&lt;p&gt;Let us write our test case inside the TestNG class we created in the previous step. Follow along with me.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a free account in LambdaTest and secure your unique username and access key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Inside the ParallelTest class(the TestNG class), define all the required variables for connecting with the Remote Grid. In addition, you will need to provide your unique username, and access key.&lt;/p&gt;

&lt;p&gt;public class TestCasesParallelExecution {&lt;/p&gt;

&lt;p&gt;public String username = "YOUR_USERNAME";&lt;br&gt;
public String accesskey = "YOUR_ACCESSKEY";&lt;br&gt;
public RemoteWebDriver driver = null;&lt;br&gt;
public String gridURL = "&lt;a href="http://twitter.com/hub"&gt;@hub&lt;/a&gt;.lambdatest.com/wd/hub";&lt;br&gt;
boolean status = false;&lt;br&gt;
..&lt;br&gt;
}&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Define automation setup steps inside the method marked by @BeforeClass annotation. This method gets executed first. Notice that I have also changed the method name from beforeClass() to setUp().&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Inside the setUp(), we have defined the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DesiredCapabilities&lt;/strong&gt; for custom configuring our browser-device-OS version, enabling reporting and logging features, and naming the build. I have generated this by utilizing the &lt;a href="https://www.lambdatest.com/capabilities-generator/"&gt;Desired Capabilities Generator&lt;/a&gt; offered by LambdaTest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A New instance of &lt;strong&gt;RemoteWebDriver()&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Also, I have added &lt;strong&gt;@Parameters&lt;/strong&gt; annotation on top of the method for receiving the browser, version, and platform values from our TestNG XML file.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://twitter.com/BeforeMethod"&gt;@BeforeMethod&lt;/a&gt;&lt;br&gt;
&lt;a href="http://twitter.com/Parameters"&gt;@Parameters&lt;/a&gt;(value={"browser","version","platform"})&lt;br&gt;
public void setUp(String browser, String version, String platform) throws Exception {&lt;br&gt;
    DesiredCapabilities capabilities = new DesiredCapabilities();&lt;br&gt;
     capabilities.setCapability("browserName", browser);&lt;br&gt;
     capabilities.setCapability("version", version);&lt;br&gt;
     capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one&lt;br&gt;
     capabilities.setCapability("build", "ParallelTestNG");&lt;br&gt;
     capabilities.setCapability("name", "ParallelTestNG");&lt;br&gt;
     capabilities.setCapability("network", true); // To enable network logs&lt;br&gt;
     capabilities.setCapability("visual", true); // To enable step by step screenshot&lt;br&gt;
     capabilities.setCapability("video", true); // To enable video recording&lt;br&gt;
     capabilities.setCapability("console", true); // To capture console logs&lt;br&gt;
     try {&lt;br&gt;
         driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);&lt;br&gt;
     } catch (MalformedURLException e) {&lt;br&gt;
         System.out.println("Invalid grid URL");&lt;br&gt;
     } catch (Exception e) {&lt;br&gt;
         System.out.println(e.getMessage());&lt;br&gt;
     }&lt;br&gt;
 }&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Define tear-down steps inside the method marked by @AfterClass annotation. This method gets executed last. Notice that I have also changed the method name from afterClass() to tearDown().&lt;/p&gt;

&lt;p&gt;&lt;a href="http://twitter.com/AfterMethod"&gt;@AfterMethod&lt;/a&gt;&lt;br&gt;
    public void tearDown() throws Exception {&lt;br&gt;
       if (driver != null) {&lt;br&gt;
            driver.quit();&lt;br&gt;
        }&lt;br&gt;
    }&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Write our actual test steps. Our test methods will be marked by the &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; annotation. Since we are performing a login scenario, I have renamed the method to login().&lt;/p&gt;

&lt;p&gt;&lt;a href="http://twitter.com/Test"&gt;@Test&lt;/a&gt;&lt;br&gt;
public void login() {&lt;/p&gt;

&lt;p&gt;// To navigate to URL&lt;br&gt;
  driver.get("&lt;a href="https://opensource-demo.orangehrmlive.com/"&gt;https://opensource-demo.orangehrmlive.com/&lt;/a&gt;");&lt;br&gt;
  // To locate input field for username and enter value&lt;br&gt;
  driver.findElement(By.name("txtUsername")).sendKeys("Admin");&lt;br&gt;
  // To locate input field for password and enter value&lt;br&gt;
  driver.findElement(By.name("txtPassword")).sendKeys("admin123");&lt;br&gt;
  // To locate and click on login button&lt;br&gt;
   driver.findElement(By.className("button")).click();&lt;br&gt;&lt;br&gt;
}&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I have made use of Chrome Developer tools for locating the elements.&lt;/p&gt;

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

&lt;p&gt;Our final code looks like this:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package testng;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestCasesParallelExecution {

public String username = "YOUR_USERNAME";
public String accesskey = "YOUR_ACCESSKEY";
public RemoteWebDriver driver = null;
public String gridURL = "@hub.lambdatest.com/wd/hub";
boolean status = false;

@BeforeMethod
@Parameters(value={"browser","version","platform"})
public void setUp(String browser, String version, String platform) throws Exception {
    DesiredCapabilities capabilities = new DesiredCapabilities();
     capabilities.setCapability("browserName", browser);
     capabilities.setCapability("version", version);
     capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one
     capabilities.setCapability("build", "ParallelTestNG");
     capabilities.setCapability("name", "ParallelTestNG");
     capabilities.setCapability("network", true); // To enable network logs
     capabilities.setCapability("visual", true); // To enable step by step screenshot
     capabilities.setCapability("video", true); // To enable video recording
     capabilities.setCapability("console", true); // To capture console logs
     try {
         driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
     } catch (MalformedURLException e) {
         System.out.println("Invalid grid URL");
     } catch (Exception e) {
         System.out.println(e.getMessage());
     }
 }
@Test
public void login() {

  // To navigate to URL
  driver.get("https://opensource-demo.orangehrmlive.com/");
  // To locate input field for username and enter value
  driver.findElement(By.name("txtUsername")).sendKeys("Admin");
  // To locate input field for password and enter value
  driver.findElement(By.name("txtPassword")).sendKeys("admin123");
  // To locate and click on login button
   driver.findElement(By.className("button")).click();          
}

@AfterMethod
    public void tearDown() throws Exception {
       if (driver != null) {
            driver.quit();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Configure the TestNG XML file. In this step, we will create the testng.xml file for our project and configure it to enable our tests to run in parallel mode. The steps are as below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Right-click on our Project. Select New &amp;gt; File and name it as testng.xml.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the following boilerplate code to get started. You can name the test suite and test name as per your liking. But the class name should be exact as that of our TestNG class.&lt;/p&gt;

&lt;p&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br&gt;
&amp;lt;!DOCTYPE suite SYSTEM "&lt;a href="https://testng.org/testng-1.0.dtd"&gt;https://testng.org/testng-1.0.dtd&lt;/a&gt;"&amp;gt;&lt;br&gt;
&lt;br&gt;
    &lt;br&gt;
        &lt;br&gt;
            &lt;br&gt;
        &lt;br&gt;
     &amp;lt;!-- Test --&amp;gt;&lt;br&gt;
 &amp;lt;!-- Suite --&amp;gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To trigger parallel test execution in TestNG, provide the parallel attribute to . As mentioned before, we can assign any of the four values given below, as per our requirement. For parallel execution of test methods, set the attribute value as “tests.”&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add parameters for configuring the tests to run in a specific browser, browser version, and OS. For example, below is the code for setting up the test case to run in Chrome Browser version 79.0 in Windows 10. Likewise, you can provide as many combinations as you like.&lt;/p&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The final code for testng.xml looks like this.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"&amp;gt;
&amp;lt;suite name="Parallel Test Suite" parallel="tests" thread-count="5"&amp;gt;

    &amp;lt;test name="Chrome Test"&amp;gt;
        &amp;lt;parameter name="browser" value="chrome" /&amp;gt;
        &amp;lt;parameter name="version" value="79.0" /&amp;gt;
        &amp;lt;parameter name="platform" value="WIN10" /&amp;gt;
        &amp;lt;classes&amp;gt;
            &amp;lt;class name="testng.ParallelTest" /&amp;gt;
        &amp;lt;/classes&amp;gt;
    &amp;lt;/test&amp;gt;

    &amp;lt;test name="Firefox Test"&amp;gt;
        &amp;lt;parameter name="browser" value="firefox" /&amp;gt;
        &amp;lt;parameter name="version" value="62.0" /&amp;gt;
        &amp;lt;parameter name="platform" value="WIN8" /&amp;gt;
        &amp;lt;classes&amp;gt;
            &amp;lt;class name="testng.ParallelTest" /&amp;gt;
        &amp;lt;/classes&amp;gt;
    &amp;lt;/test&amp;gt;

    &amp;lt;test name="Safari Test"&amp;gt;
        &amp;lt;parameter name="browser" value="safari" /&amp;gt;
        &amp;lt;parameter name="version" value="11.0" /&amp;gt;
        &amp;lt;parameter name="platform" value="macos 10.13" /&amp;gt;
        &amp;lt;classes&amp;gt;
            &amp;lt;class name="testng.ParallelTest" /&amp;gt;
        &amp;lt;/classes&amp;gt;
    &amp;lt;/test&amp;gt;

        &amp;lt;test name="IE Test"&amp;gt;
        &amp;lt;parameter name="browser" value="ie" /&amp;gt;
        &amp;lt;parameter name="version" value="11.0" /&amp;gt;
        &amp;lt;parameter name="platform" value="WIN10" /&amp;gt;
        &amp;lt;classes&amp;gt;
            &amp;lt;class name="testng.ParallelTest" /&amp;gt;
        &amp;lt;/classes&amp;gt;
    &amp;lt;/test&amp;gt;

            &amp;lt;test name="Opera Test"&amp;gt;
        &amp;lt;parameter name="browser" value="opera" /&amp;gt;
        &amp;lt;parameter name="version" value="73.0" /&amp;gt;
        &amp;lt;parameter name="platform" value="WIN10" /&amp;gt;
        &amp;lt;classes&amp;gt;
            &amp;lt;class name="testng.ParallelTest" /&amp;gt;
        &amp;lt;/classes&amp;gt;
    &amp;lt;/test&amp;gt;


    &amp;lt;!-- Test --&amp;gt;
&amp;lt;/suite&amp;gt; &amp;lt;!-- Suite --&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;We have a total of 5 different tags, and each of these will run on different threads.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Finally, it is time to run our test case. Right-click on our testng.xml file and navigate to Run As &amp;gt; TestNG Test Suite.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--do3N641W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2026/1%2Af11AnA__r2aXKiGiBs16pQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--do3N641W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2026/1%2Af11AnA__r2aXKiGiBs16pQ.png" alt="" width="880" height="599"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our test starts getting executed. Go to your &lt;a href="https://automation.lambdatest.com/timeline"&gt;LambdaTest Automation Dashboard&lt;/a&gt; to see the live running status.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NRSXipND--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3200/1%2AaASAJdV6K9QbQ5Tmqq-63g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NRSXipND--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3200/1%2AaASAJdV6K9QbQ5Tmqq-63g.png" alt="" width="880" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The test is executed parallelly in the 5 browsers, and reports and logs are generated successfully.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vksuim88--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3200/1%2A4tKdU3oVq4MCRAxQbQrdWg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vksuim88--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3200/1%2A4tKdU3oVq4MCRAxQbQrdWg.png" alt="" width="880" height="421"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How to run test methods in parallel with TestNG
&lt;/h2&gt;

&lt;p&gt;Now let us see how to run test methods parallelly in the TestNG. In this example, we will create one more method and execute them on different threads. The steps are as below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Create a new TestNG class file. You can also opt for an auto-defined TestNG XML file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qelUttqN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AW9YWNuy-f4cz74eiaRXI2w.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qelUttqN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AW9YWNuy-f4cz74eiaRXI2w.jpeg" alt="" width="853" height="643"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Inside this class, I have added a new test method forgotPassword() which will:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Launch the website URL (&lt;a href="https://opensource-demo.orangehrmlive.com/"&gt;https://opensource-demo.orangehrmlive.com/&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on Forgot your password? link.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter Username.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click the Reset Password button.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tMm0J7_w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3200/1%2APhIRMl2OquRZNV85LCkYpw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tMm0J7_w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3200/1%2APhIRMl2OquRZNV85LCkYpw.png" alt="" width="880" height="277"&gt;&lt;/a&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)
  public void forgotPassword() {

    // To navigate to URL
    driver.get("[https://opensource-demo.orangehrmlive.com/](https://opensource-demo.orangehrmlive.com/)");
    // To locate and click on Forgot password link
    driver.findElement(By.linkText("Forgot your password?")).click();
    // To locate input field and enter username
    driver.findElement(By.id("securityAuthentication_userName")).sendKeys("testuser");
    // To locate and click on reset button
    driver.findElement(By.id("btnSearchValues")).click();          
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;So the final code will look like this:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package testng;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestMethodParallelExecution {

public String username = "YOUR_USERNAME";
public String accesskey = "YOUR_ACCESSKEY";
public RemoteWebDriver driver = null;
public String gridURL = "@hub.lambdatest.com/wd/hub";
boolean status = false;

@BeforeMethod
@Parameters(value={"browser","version","platform"})
public void setUp(String browser, String version, String platform) throws Exception {
    DesiredCapabilities capabilities = new DesiredCapabilities();
     capabilities.setCapability("browserName", browser);
     capabilities.setCapability("version", version);
     capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one
     capabilities.setCapability("build", "TestMethodParallelExecution");
     capabilities.setCapability("name", "TestMethodParallelExecution");
     capabilities.setCapability("network", true); // To enable network logs
     capabilities.setCapability("visual", true); // To enable step by step screenshot
     capabilities.setCapability("video", true); // To enable video recording
     capabilities.setCapability("console", true); // To capture console logs
     try {
         driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
     } catch (MalformedURLException e) {
         System.out.println("Invalid grid URL");
     } catch (Exception e) {
         System.out.println(e.getMessage());
     }
 }


@Test
public void login() {

  // To navigate to URL
  driver.get("https://opensource-demo.orangehrmlive.com/");
  // To locate input field for username and enter value
  driver.findElement(By.name("txtUsername")).sendKeys("Admin");
  // To locate input field for password and enter value
  driver.findElement(By.name("txtPassword")).sendKeys("admin123");
  // To locate and click on login button
   driver.findElement(By.className("button")).click();          
}

  @Test
  public void forgotPassword() {

    // To navigate to URL
    driver.get("https://opensource-demo.orangehrmlive.com/");
    // To locate and click on Forgot password link
    driver.findElement(By.linkText("Forgot your password?")).click();
    // To locate input field and enter username
    driver.findElement(By.id("securityAuthentication_userName")).sendKeys("testuser");
    // To locate and click on reset button
    driver.findElement(By.id("btnSearchValues")).click();          
  }

@AfterMethod
    public void tearDown() throws Exception {
       if (driver != null) {
            driver.quit();
        }
    }
}

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

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Configure the TestNG XML file. Set the value of the parallel attribute as ”methods”. My testng2.xml file now looks like this.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;!DOCTYPE suite SYSTEM "[https://testng.org/testng-1.0.dtd](https://testng.org/testng-1.0.dtd)"&amp;gt;
&amp;lt;suite name="Parallel Test Suite" parallel="methods" thread-count="2"&amp;gt;

    &amp;lt;test name="Chrome Test"&amp;gt;
        &amp;lt;parameter name="browser" value="chrome" /&amp;gt;
        &amp;lt;parameter name="version" value="79.0" /&amp;gt;
        &amp;lt;parameter name="platform" value="WIN10" /&amp;gt;
        &amp;lt;classes&amp;gt;
            &amp;lt;class name="testng2.TestMethodParallelExecution" /&amp;gt;
        &amp;lt;/classes&amp;gt;
    &amp;lt;/test&amp;gt;
    &amp;lt;!-- Test --&amp;gt;
&amp;lt;/suite&amp;gt; &amp;lt;!-- Suite --&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Run the tests. Our two test methods start running in parallel. You can observe the live running status in your LambdaTest dashboard.&lt;/p&gt;

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

&lt;p&gt;Note: &lt;a href="https://www.lambdatest.com/web-technologies/autofocus?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr21_rd&amp;amp;utm_term=rd&amp;amp;utm_content=web_technologies"&gt;Autofocus attribute&lt;/a&gt; — a lightweight JavaScript library that allows a form field to be immediately focused on page load.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to run test classes in parallel with TestNG
&lt;/h2&gt;

&lt;p&gt;Lastly, let us see how to run test classes parallelly in the TestNG. First, let us create 2 classes that will be running parallel on separate threads.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create 2 classes — TestClassOne and TestClassTwo. Each contains a test method.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;TestClassOne.java&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package testng3;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;

public class TestClassOne {

    public String username = "YOUR_USERNAME";
    public String accesskey = "YOUR_ACCESSKEY";
    public RemoteWebDriver driver = null;
    public String gridURL = "@hub.lambdatest.com/wd/hub";
    boolean status = false;


    @BeforeMethod
    @Parameters(value={"browser","version","platform"})
    public void setUp(String browser, String version, String platform) throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("browserName", browser);
        capabilities.setCapability("version", version);
        capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one
        capabilities.setCapability("build", "TestMethodParallelExecution");
        capabilities.setCapability("name", "TestMethodParallelExecution");
        capabilities.setCapability("network", true); // To enable network logs
        capabilities.setCapability("visual", true); // To enable step by step screenshot
        capabilities.setCapability("video", true); // To enable video recording
        capabilities.setCapability("console", true); // To capture console logs
        try {
            driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
        } catch (MalformedURLException e) {
            System.out.println("Invalid grid URL");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Test
    public void login() {

        // To navigate to URL
        driver.get("https://opensource-demo.orangehrmlive.com/");
        // To locate input field for username and enter value
        driver.findElement(By.name("txtUsername")).sendKeys("Admin");  
        // To locate input field for password and enter value
        driver.findElement(By.name("txtPassword")).sendKeys("admin123");
        // To locate and click on login button
        driver.findElement(By.className("button")).click();            
    }

    @AfterMethod
    public void tearDown() throws Exception {
        if (driver != null) {
            System.out.println("Tearing down..");
            driver.quit();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;TestClassTwo.java&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package testng3;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;

public class TestClassTwo {

    public String username = "haritagr16";
    public String accesskey = "KhRUhWBtSMg06DfGkKDY1khYNJm95695WV42HfaYPot7nqQfw6";
    public RemoteWebDriver driver = null;
    public String gridURL = "@hub.lambdatest.com/wd/hub";
    boolean status = false;

    @BeforeMethod
    @Parameters(value={"browser","version","platform"})
    public void setUp(String browser, String version, String platform) throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("browserName", browser);
        capabilities.setCapability("version", version);
        capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one
        capabilities.setCapability("build", "ParallelTestNG");
        capabilities.setCapability("name", "ParallelTestNG");
        capabilities.setCapability("network", true); // To enable network logs
        capabilities.setCapability("visual", true); // To enable step by step screenshot
        capabilities.setCapability("video", true); // To enable video recording
        capabilities.setCapability("console", true); // To capture console logs
        try {
            driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
        } catch (MalformedURLException e) {
            System.out.println("Invalid grid URL");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Test
    public void forgotPassword() {

        // To navigate to URL
        driver.get("https://opensource-demo.orangehrmlive.com/");
        // To locate and click on Forgot password link
        driver.findElement(By.linkText("Forgot your password?")).click();  
        // To locate input field and enter username
        driver.findElement(By.id("securityAuthentication_userName")).sendKeys("testuser");
        // To locate and click on reset button
        driver.findElement(By.id("btnSearchValues")).click();              
    }

    @AfterMethod
    public void tearDown() throws Exception {
        if (driver != null) {
            System.out.println("Tearing down..");
            driver.quit();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a new TestNG XML file and set the parallel attribute as “classes.”&lt;/p&gt;

&lt;p&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br&gt;
&amp;lt;!DOCTYPE suite SYSTEM "&lt;a href="https://testng.org/testng-1.0.dtd"&gt;https://testng.org/testng-1.0.dtd&lt;/a&gt;"&amp;gt;&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;test name="Chrome Test"&amp;gt;
    &amp;lt;parameter name="browser" value="chrome" /&amp;gt;
    &amp;lt;parameter name="version" value="79.0" /&amp;gt;
    &amp;lt;parameter name="platform" value="WIN10" /&amp;gt;
    &amp;lt;classes&amp;gt;
        &amp;lt;class name="testng3.TestClassOne" /&amp;gt;
        &amp;lt;class name="testng3.TestClassTwo" /&amp;gt;
    &amp;lt;/classes&amp;gt;
&amp;lt;/test&amp;gt; 
&amp;lt;!-- Test --&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt; &amp;lt;!-- Suite --&amp;gt;&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run your test suite. The test gets executed successfully.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

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

&lt;p&gt;Parallel testing is widely adopted by Agile teams to radically reduce the time and cost associated with the traditional testing methods. It enables you to speed up the overall test cycle and deliver projects at a faster rate. To maximize the test coverage while ensuring scalability, the responsibility of maintaining a testing grid is often outsourced to cloud-based platforms.&lt;/p&gt;

&lt;p&gt;In this Selenium TestNG tutorial, we learned how to achieve parallel test execution in the TestNG framework with Selenium. We have also leveraged the cloud-based Selenium Grid provided by LambdaTest to run our test cases parallelly in 5 different browsers 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=apr21_rd&amp;amp;utm_term=rd&amp;amp;utm_content=webpage"&gt;Selenium automation testing with TestNG&lt;/a&gt;. I hope this tutorial turned out to be beneficial to you. So, keep exploring different angles of parallel testing.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>testng</category>
      <category>selenium</category>
      <category>automation</category>
      <category>java</category>
    </item>
    <item>
      <title>Automation Testing with Selenium JavaScript [Tutorial]</title>
      <dc:creator>HaritaLT</dc:creator>
      <pubDate>Wed, 20 Apr 2022 09:02:30 +0000</pubDate>
      <link>https://forem.com/haritalt/automation-testing-with-selenium-javascript-tutorial-pkj</link>
      <guid>https://forem.com/haritalt/automation-testing-with-selenium-javascript-tutorial-pkj</guid>
      <description>&lt;p&gt;Automation testing is an integral part of software development. When it comes to web applications, it is essential to test the user interface to ensure that a flawless experience is delivered to the end-users. This is where &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harita-20072021&amp;amp;utm_term=Harita"&gt;Selenium automation testing&lt;/a&gt; is instrumental to verify the product across different combinations of browsers &amp;amp; operating systems. Selenium supports a wide range of programming languages which also includes JavaScript.&lt;/p&gt;

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

&lt;p&gt;In this Selenium Javascript tutorial, we take a deep dive into how Selenium Webdriver with Javascript can be used for automated testing of web products. By the end of this Selenium Javascript Tutorial, you would be in a comfortable position to perform Selenium automation testing using JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Selenium Automation Testing Using JavaScript?
&lt;/h2&gt;

&lt;p&gt;Before we get to the crux of the Selenium Javascript tutorial, it is essential to understand why the combination of Selenium Webdriver and Javascript is suited for automation testing? Let’s answer this important question:&lt;/p&gt;

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

&lt;p&gt;A number of tools and frameworks are available in the market to perform automation testing. Out of these, Selenium continues to be, hands down, the first choice of QA engineers over the past decade or so.&lt;/p&gt;

&lt;p&gt;Few of the key features of Selenium web automation are listed below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open-Source and portable&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supports multiple browsers like Google Chrome, Firefox, Safari, Opera, Internet Explorer, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supports multiple languages like Java, JavaScript, Ruby, Python, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Works on different OS like Windows, Linux, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easy to integrate with CI/CD tools like Jenkins, Circle CI, GitLab CI, and more&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easy to integrate with popular testing frameworks&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Over &amp;amp; above, parallel testing in Selenium expedites the entire automation testing process by providing a platform through which the same tests can be run across different environments. You can check out our Selenium learning hub to gather more insights into Selenium web automation.&lt;/p&gt;

&lt;p&gt;Now let’s look at why &lt;a href="https://www.lambdatest.com/blog/selenium-webdriver-tutorial-with-examples/?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harita-20072021&amp;amp;utm_term=Harita"&gt;Selenium Webdriver&lt;/a&gt; with Javascript! Well, JavaScript is one of the widely used programming languages as per the Stack Overflow 2020 annual survey.&lt;/p&gt;

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

&lt;p&gt;Since JavaScript is extensively used for web development, utilizing it with Selenium web automation makes perfect sense! Since most of the developers are familiar with JavaScript, it becomes easy for them to write automation tests in JavaScript and get quick feedback.&lt;/p&gt;

&lt;p&gt;With that said, let’s proceed to the next section in this Selenium Javascript tutorial where we focus on the setup and installation process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/ripemd320-hash-calculator?utm_source=medium&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr20_rd&amp;amp;utm_term=rd&amp;amp;utm_content=free_tools"&gt;Ripe MD 320 Hash Calculator&lt;/a&gt; — Free online tool for encrypting your personal data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting started with Selenium automation testing using JavaScript
&lt;/h2&gt;

&lt;p&gt;As mentioned earlier, the Selenium framework supports multiple languages (and frameworks) for realizing cross-browser compatibility tests. Here is how you can get started with JavaScript with Selenium WebDriver:&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites for Selenium Webdriver with Javascript
&lt;/h2&gt;

&lt;p&gt;Before you start using JavaScript for Selenium automation testing, you need to ensure that your system is installed with the following pre-requisites:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;NodeJS and NPM&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Verify if NodeJS and NPM are already installed in your system by running the commands node -v and npm -v on the terminal:&lt;/p&gt;

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

&lt;p&gt;If Node JS (or Node.js) is not present on your machine, you can install the latest LTS version from &lt;a href="https://nodejs.org/en/"&gt;https://nodejs.org/en/&lt;/a&gt;. NPM will be installed along with Node.js. So no need for separate installation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IDE of your choice&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For implementation, we will be using &lt;a href="https://code.visualstudio.com/download"&gt;Visual Studio Code&lt;/a&gt; as IDE. However, you can choose an IDE of your choice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project Setup for Selenium Webdriver with Javascript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With the required dependencies installed, let’s move on to the project setup in this section of the Selenium Javascript Tutorial.&lt;/p&gt;

&lt;p&gt;Here are the step-by-step instructions for creating a project to use JavaScript with Selenium WebDriver:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a project folder in the desired location in your system. Open the folder in your IDE.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Initialize the project in this folder. Open the IDE terminal and navigate to the project location. Run the following command&lt;/p&gt;

&lt;p&gt;npm init -y&lt;/p&gt;

&lt;p&gt;Here, -y stands for “yes”. This option is used to skip the questions asked related to project details.&lt;/p&gt;

&lt;p&gt;You will notice that a package.json file is automatically created. The file contains details about the project configuration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Now, install the project dependencies. We need to install Selenium WebDriver and browser drivers.&lt;/p&gt;

&lt;p&gt;Run the following command on the terminal to install browser drivers for Chrome and Firefox.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save selenium-webdriver chromedriver geckodriver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Once you run the command, you’ll notice that a “node_modules” folder is created in the project root. The folder contains all the dependencies. Also, package.json will be updated with these dependencies.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "Selenium_JavaScript",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "chromedriver": "^90.0.0",
    "geckodriver": "^1.22.3",
    "selenium-webdriver": "^4.0.0-beta.3"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;With this, the setup for Selenium Webdriver JavaScript is complete. It’s time to dirty our hands with code in the next section of the Selenium JavaScript Tutorial.&lt;/p&gt;

&lt;p&gt;Paste any number of words, sentences, and paragraphs and find out the final &lt;a href="https://www.lambdatest.com/free-online-tools/sentence-count?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr20_rd&amp;amp;utm_term=rd&amp;amp;utm_content=free_tools"&gt;Sentence count&lt;/a&gt; with a click.&lt;/p&gt;
&lt;h2&gt;
  
  
  Demonstration: Selenium Automation with JavaScript
&lt;/h2&gt;

&lt;p&gt;In this section of the Selenium JavaScript tutorial, we run a Selenium automation test with JavaScript. Here is the test scenario:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Launch the browser&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to &lt;a href="http://www.google.com"&gt;www.google.com&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Searches a string value&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verifies the result page title&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Close the browser&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a folder named “tests” in the project root. Inside this folder, we create our first test file named “test.js”.&lt;/p&gt;

&lt;p&gt;Shown below is the folder structure:&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Inside test.js, pull all the required functions from node_modules.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const {By,Key,Builder} = require("selenium-webdriver");
require("chromedriver");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Create a function example(), which will be containing your test script. Feel free to provide any function name of your choice.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function example(){

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

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Inside the function, let us write our test script using Selenium and JavaScript.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function example(){

       var searchString = "Automation testing with Selenium and JavaScript";

       //To wait for browser to build and launch properly
       let driver = await new Builder().forBrowser("chrome").build();

        //To fetch [http://google.com](http://google.com) from the browser with our code.
        await driver.get("[http://google.com](http://google.com)");

        //To send a search query by passing the value in searchString.
        await driver.findElement(By.name("q")).sendKeys(searchString,Key.RETURN);

        //Verify the page title and print it
        var title = await driver.getTitle();
        console.log('Title is:',title);

        //It is always a safe practice to quit the browser after execution
        await driver.quit();

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

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Now simply add the function call. Here is how the overall implementation looks like:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const {By,Key,Builder} = require("selenium-webdriver");
require("chromedriver");

async function example(){

       var searchString = "Automation testing with Selenium";

       //To wait for browser to build and launch properly
       let driver = await new Builder().forBrowser("chrome").build();

        //To fetch [http://google.com](http://google.com) from the browser with our code.
        await driver.get("[http://google.com](http://google.com)");

        //To send a search query by passing the value in searchString.
        await driver.findElement(By.name("q")).sendKeys(searchString,Key.RETURN);

        //Verify the page title and print it
        var title = await driver.getTitle();
        console.log('Title is:',title);

        //It is always a safe practice to quit the browser after execution
        await driver.quit();

}

example()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; Time for some action…let’s run the code by giving the following command on the terminal:&lt;/p&gt;

&lt;p&gt;node test.js&lt;/p&gt;

&lt;p&gt;An instance of Chrome browser is instantiated and the necessary &lt;a href="https://www.lambdatest.com/blog/what-is-actions-class-in-selenium/?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harita-20072021&amp;amp;utm_term=Harita"&gt;Selenium WebDriver actions&lt;/a&gt; are performed on the WebElements.&lt;/p&gt;

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

&lt;p&gt;The logs can be verified in the terminal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9JZZyPK3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2048/0%2A1BNTP8URtdXKG7W5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9JZZyPK3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2048/0%2A1BNTP8URtdXKG7W5.jpg" alt="" width="880" height="77"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! You have successfully written and executed your first test case using Selenium Webdriver and JavaScript.&lt;/p&gt;
&lt;h2&gt;
  
  
  Best Practices for Selenium with JavaScript
&lt;/h2&gt;

&lt;p&gt;In the previous section of the Selenium WebDriver tutorial, we demonstrated the creation of automation script with Selenium and JavaScript. However, the situation will differ when you have to write Selenium automation scripts for large projects. It is essential to follow the &lt;a href="https://www.lambdatest.com/blog/27-best-practices-selenium-test-automation/?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harita-20072021&amp;amp;utm_term=Harita"&gt;best practices in Selenium automation&lt;/a&gt; when designing test automation scripts.&lt;/p&gt;

&lt;p&gt;As QA engineers, we should come up with strategies that enable us to effectively and efficiently write test scripts, manage them, and speed up the overall execution. Here are some of the the best practices used for Selenium WebDriver with JavaScript:&lt;/p&gt;
&lt;h3&gt;
  
  
  Using Page Object Model (POM)
&lt;/h3&gt;

&lt;p&gt;Page Object Model (POM) is a design pattern popularly used in Selenium web automation. Here we will be creating an object repository for storing all web elements. A Page Class is created for each web page. Every Page class contains details of the particular page Web elements and Page Methods that perform tasks on those web elements.&lt;/p&gt;

&lt;p&gt;Here are the major advantage of &lt;a href="https://www.lambdatest.com/blog/page-object-model-in-selenium-python/?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harita-20072021&amp;amp;utm_term=Harita"&gt;Page Object Model for Selenium web automation&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Reduced maintenance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Minimal Code Duplication&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Increased Readability and Reliability of scripts&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Leveraging JavaScript Automation Frameworks
&lt;/h3&gt;

&lt;p&gt;Another strategy that is gaining tremendous popularity is leveraging an automation testing framework. A framework offers multiple benefits when it comes to script maintenance, third-party tool integration, reporting, etc. A variety of JavaScript-based testing frameworks are available for automation testers. Some of the popular JavaScript-based test frameworks are Nightwatch.js, Protractor, WedriverIO, Cypress.io, etc.&lt;/p&gt;
&lt;h3&gt;
  
  
  Parallel Testing
&lt;/h3&gt;

&lt;p&gt;Parallel testing in Selenium lets you run multiple tests simultaneously in different environments. This directly translates to increased execution speed, reduced overall execution time, and early detection of bugs. Selenium Grid supports parallel testing.&lt;/p&gt;
&lt;h3&gt;
  
  
  Implementing CI/CD Pipeline
&lt;/h3&gt;

&lt;p&gt;CI/CD as a practice is gaining huge attention on the backdrop of software development backed by Agile and DevOps. Building a CI/CD pipeline for Selenium web automation has the following benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Quicker execution leading to early bug detection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Consistent results&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Room for parallel testing capabilities&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Offers greater scalability&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Program automatic triggers etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A wide range of CI/CD tools is available from which you can choose the one matching your requirement. A few of the popular options are Jenkins, Bamboo, GitLab, etc.&lt;/p&gt;
&lt;h3&gt;
  
  
  Visual Testing
&lt;/h3&gt;

&lt;p&gt;Since we are dealing with testing the UI of the web app, it is vital to test the visual aspects of the app as well. In Visual testing, we will compare a web page with a base or reference screenshots were taken before to expose the significant visual differences. LambdaTest provides a &lt;a href="https://www.lambdatest.com/smart-visual-ui-testing?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harita-20072021&amp;amp;utm_term=Harita"&gt;visual regression testing tool&lt;/a&gt; that lets you perform visual UI testing with ease.&lt;/p&gt;
&lt;h3&gt;
  
  
  Test Coverage using Cross browser testing
&lt;/h3&gt;

&lt;p&gt;To stay ahead in the competitive market and gain user preference it is vital to make sure that your application is working as expected across different versions of browsers, platforms, and devices. This is exactly where test coverage and cross-browser testing comes into the picture.&lt;/p&gt;

&lt;p&gt;Here are the major benefits of adopting cloud based cross browser testing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Low investment cost in comparison to setting &amp;amp; scaling up an in-house infrastructure&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provides access to hundreds of browser and platform combinations&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Extends supports to old and outdated browser versions and OS devices&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No set-up or installation, or maintenance needs to be done locally&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Better security, reliability, and scalability&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;LambdaTest is a trusted cross browser testing platform that provides you access to 2000+ browsers and operating systems online. The Selenium automation tests are executed on a secure cloud-based Selenium Grid.allows you to execute automated tests on a remote Selenium Grid. Do check why you should test websites on different browsers remotely.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/sha384-hash-calculator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr20_rd&amp;amp;utm_term=rd&amp;amp;utm_content=free_tools"&gt;SHA384 Hash calculator&lt;/a&gt; — the program helps you determine the integrity of your data and challenge hash authentication.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to run automation scripts with JavaScript on cloud-based Selenium Grid
&lt;/h2&gt;

&lt;p&gt;In the earlier section of the Selenium JavaScript tutorial, we performed Selenium automation using JavaScript on a local grid. Now that you are aware about the shortcomings of the local Selenium Grid, it’s time to look at how to run JavaScript tests with Selenium Grid on a cloud-based Selenium Grid.&lt;/p&gt;

&lt;p&gt;LambdaTest is a popular cloud-based cross browser testing platform trusted by startups as well as large enterprises. LambdaTest provides you access to thousands of browsers for mobile and web testing to help you gain the maximum test coverage during the process of Selenium web automation.&lt;/p&gt;

&lt;p&gt;Porting existing implementations of JavaScript with Selenium WebDriver such that it runs on a cloud-based Selenium Grid requires minimal code changes. Let’s look at how to run the Selenium automation test with JavaScript on LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Let’s get started by creating a free account on the LambdaTest platform. Once you log in, fetch your unique username and access key from the &lt;a href="https://accounts.lambdatest.com/detail/profile"&gt;profile section&lt;/a&gt; of the website.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Make the following configuration changes into your existing code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Provide username and access key&lt;/p&gt;

&lt;p&gt;const USERNAME = 'YOUR_USERNAME'; //replace with your username&lt;br&gt;
const KEY = 'YOUR_KEY'; //replace with your accesskey&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Provide the host corresponding to the LambdaTest platform&lt;/p&gt;

&lt;p&gt;const GRID_HOST = 'hub.lambdatest.com/wd/hub';&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Generate the desired browser capabilities using &lt;a href="https://www.lambdatest.com/capabilities-generator/"&gt;LambdaTest Capabilities Generator&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OW3DA3UR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2048/0%2Axx6s5jzCcPd-yIDv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OW3DA3UR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2048/0%2Axx6s5jzCcPd-yIDv.png" alt="" width="880" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select the desired browser version and OS configuration. You may also provide the build name. Provide this object inside the test function.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function example(){

var capabilities = {
        "build" : "JavaScript and Selenium Testing",
        "name" : "Google search",
        "platform" : "Windows 10",
        "browserName" : "Chrome",
        "version" : "90.0",
        "selenium_version" : "3.13.0",
        "chrome.driver" : "90.0"
    }
     //…
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Provide the gridUrl. You can also find this value from &lt;a href="https://accounts.lambdatest.com/dashboard"&gt;Automation Dashboard of LambdaTest&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;const gridUrl = '&lt;a href="https://'"&gt;https://'&lt;/a&gt; + USERNAME + ':' + KEY + '@' + GRID_HOST;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Finally, edit the code to build and instantiate the browser on the designated platform&lt;/p&gt;

&lt;p&gt;let driver = await new Builder()&lt;br&gt;
    .usingServer(gridUrl)&lt;br&gt;
    .withCapabilities(capabilities)&lt;br&gt;
    .build();&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Step 3:&lt;/strong&gt; Finally, it’s time to run the test on the LambdaTest Grid. Trigger the following command on the terminal to run the JavaScript Automation test with Selenium Webdriver:&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;node test.js&lt;/p&gt;

&lt;p&gt;Shown below is the execution snapshot from the LambdaTest automation dashboard which indicates that the test execution was successful.&lt;/p&gt;

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

&lt;p&gt;Navigate to Automation Logs to view the test report containing video and logs.&lt;/p&gt;

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

&lt;p&gt;In this final section of the Selenium JavaScript tutorial, we ran the JavaScript automation test with Selenium WebDriver on a scalable and reliable cloud-based Selenium Grid by LambdaTest.&lt;/p&gt;

&lt;h2&gt;
  
  
  It’s a Wrap!
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Eu6zr3tr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/0%2AwVoPM0fVbHZUSnF5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Eu6zr3tr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/0%2AwVoPM0fVbHZUSnF5.gif" alt="" width="220" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JavaScript is one of the most preferred languages when it comes to Selenium automation testing. In this Selenium JavaScript tutorial, we deep dived into the basic aspects of Selenium WebDriver automation with JavaScript. We also explored the Selenium automation best practices that will help you create scalable automation tests with JavaScript and Selenium WebDriver.&lt;/p&gt;

&lt;p&gt;For better scalability, reliability, and performance; it is recommended to run Selenium WebDriver JavaScript tests on a cloud-based Selenium Grid from LambdaTest. With that setup, you can make the most out of the automation tests created using Selenium and JavaScript. In the next series of this Selenium JavaScript tutorial, we would deep dive into the other vital aspects of Selenium automation testing with JavaScript.&lt;/p&gt;

&lt;p&gt;Happy Testing!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>selenium</category>
      <category>automatio</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Using Page Object Model (POM) Pattern In Selenium JavaScript</title>
      <dc:creator>HaritaLT</dc:creator>
      <pubDate>Tue, 19 Apr 2022 11:25:47 +0000</pubDate>
      <link>https://forem.com/haritalt/using-page-object-model-pom-pattern-in-selenium-javascript-2ccf</link>
      <guid>https://forem.com/haritalt/using-page-object-model-pom-pattern-in-selenium-javascript-2ccf</guid>
      <description>&lt;p&gt;Design patterns in &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=feb22_rd&amp;amp;utm_term=rd&amp;amp;utm_content=webpage"&gt;Selenium automation testing&lt;/a&gt; provide a way to handle commonly occurring problems and structure your test code in a relatively standardized way. Using design patterns, you can solve some of the common issues such as testing complex user transactions, functional testing of several web pages, and unearth performance issues related to throttling internet connectivity.&lt;/p&gt;

&lt;p&gt;One of the popular &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=feb25_rd&amp;amp;utm_term=rd&amp;amp;utm_content=webpage"&gt;test automation&lt;/a&gt; design patterns is Page Object Pattern. It is one of the most important design patterns, as it makes tests maintainable, reusable, and pushes us to think about how to structure our automation code better.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ArzZldyf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ACDgMeDH1MyH1q9NCiK3okQ.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ArzZldyf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ACDgMeDH1MyH1q9NCiK3okQ.gif" alt="" width="480" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this Selenium JavaScript tutorial, we will be discussing in detail about the Page Object Pattern in JavaScript using Selenium WebDriver. We will try to understand the benefits of adopting this pattern and demonstrate practical implementation examples using the trending test automation framework — WebdriverIO.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  What is Page Object Pattern
&lt;/h2&gt;

&lt;p&gt;The Page Object Pattern is a design practice that is commonly adopted by testers when performing Selenium automation testing. The primary goal of this pattern is to avoid code duplication and enhance code reusability. This enhances the overall ease of test case maintenance and robustness of the test code, thereby making it more programmer-friendly.&lt;/p&gt;

&lt;p&gt;To understand the objective of this pattern, first of all, let us try to understand precisely what we can achieve with it when performing web automation testing. Automation tests allow us to navigate to different web pages and perform relevant actions with the WebElements that are identified using the appropriate Selenium locators. The Page Object Pattern wraps all the elements, actions, and validations happening on a page in one single Page Object.&lt;/p&gt;

&lt;p&gt;The creator of WebDriver, Simon Stewart, states-&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“If you have WebDriver APIs in your test methods, You’re Doing It Wrong.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The Page Object Pattern directs us to create an object that represents the UI of the page we want to test. As mentioned, this object will wrap all the HTML elements (locators) and encapsulates interactions with the UI (methods). For example, all the WebDriver calls will go inside these encapsulated methods. So, this is the only place that you need to modify when the UI changes, i.e. the element locators are centralized.&lt;/p&gt;

&lt;p&gt;Frequent UI changes will only impact the files that are housing the locators, with minimal (or zero) changes in the test implementation.&lt;/p&gt;

&lt;p&gt;This series of page objects are responsible for communicating with the web pages you are testing by cleanly separating the actual test code from the page-specific code.&lt;/p&gt;

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

&lt;p&gt;Keep the following best practices in mind when creating page objects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The page object can be created for a whole page/screen or even major fragments of it&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoid unnecessary complexity, which leads to hard-to-read spaghetti code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Page objects should only exist if some behavior makes sense from the user’s perspective&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Advantages of Page Object Pattern
&lt;/h2&gt;

&lt;p&gt;The benefits of utilizing this pattern becomes apparent while testing complex, large-scale applications where you will need to manage a larger suite of &lt;a href="https://www.lambdatest.com/blog/regression-testing-what-is-and-how-to-do-it/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=feb25_rd&amp;amp;utm_term=rd&amp;amp;utm_content=blog"&gt;regression tests&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As these applications are only expected to grow larger and more complex with every software release, adopting the Page Object Pattern makes sense as it can prove to be a huge time and cost saver for the project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EIhTB-tr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A9cAdGTQcLONiTiXFRYe-uA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EIhTB-tr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A9cAdGTQcLONiTiXFRYe-uA.png" alt="" width="512" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Major advantages of following the Page Object Pattern are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Ease of code maintenance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code Reusability across tests&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enhanced script readability and reliability&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Decouples your test code and page-specific code, such as locators and interactions&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Page Object Pattern in JavaScript using WebdriverIO
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/learning-hub/webdriverio?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=feb25_rd&amp;amp;utm_term=rd&amp;amp;utm_content=webpage"&gt;WebdriverIO&lt;/a&gt; is one of the top JavaScript test automation frameworks in the market. It is very popular among testers as well as developers backed up by an active community to support.&lt;/p&gt;

&lt;p&gt;According to the official WebdriverIO documentation, the latest versions of the framework were designed with Page Object Pattern in Selenium support in mind. To create Page Objects, no additional packages are required. All the necessary features are provided by the clean and modern classes such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Inheritance between Page Objects&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lazy loading of elements&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Encapsulation of methods and actions&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The elements are treated as “first-class citizens.” Hence, using this pattern, the framework enables you to build up larger test suites. In the upcoming section of this Selenium JavaScript tutorial, we will demonstrate how to implement the Page Object Pattern in JavaScript using the WebdriverIO framework and write automation tests. But before that, let’s check the prerequisites.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/random-decimal-generator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr19_rd&amp;amp;utm_term=rd&amp;amp;utm_content=free_tools"&gt;Random Decimal Generator&lt;/a&gt; allows users to generate random decimal numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites for writing automation tests in JavaScript
&lt;/h2&gt;

&lt;p&gt;Before proceeding to write Selenium automation tests, make sure that your system has met the following requirements:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Node.js and NPM&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To check if Node.js is installed in your machine, just execute the following command in your terminal:&lt;/p&gt;

&lt;p&gt;node -v&lt;/p&gt;

&lt;p&gt;If installed, the installed Node.js version will be displayed. Else you can download the latest LTS version from the Node.js official website.&lt;/p&gt;

&lt;p&gt;Similarly, you can check the installed NPM version using the following command:&lt;/p&gt;

&lt;p&gt;npm -v&lt;/p&gt;

&lt;p&gt;Note: NPM will be installed along with Node.js. There is no need for separate installation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Any IDE of choice&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For this Selenium JavaScript tutorial, we will be using Visual Studio Code as IDE. But feel free to pick any IDE of your preference.&lt;/p&gt;

&lt;p&gt;You can refer to our blog titled automation testing with Selenium JavaScript in case you want to deep dive into the basics of Selenium JavaScript.&lt;/p&gt;

&lt;p&gt;With these checked, let us move on to setting up the project workspace.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a folder for the project in the desired location in your system. We will call this folder “PageObjectPattern.” Then, open the folder in IDE.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Initialize the project with the package.json file. Then, type the following command in the terminal:&lt;/p&gt;

&lt;p&gt;npm init -y&lt;/p&gt;

&lt;p&gt;With this, we have successfully created the package.json file. The optional parameter -y allows you to skip all the configuration questions asking for “Yes” prompts and set up a default package.json setting for your project, as shown in the below screenshot.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; The next step would be the installation of the dependencies. Install the CLI tool for WebdriverIO by using the following command:&lt;/p&gt;

&lt;p&gt;npm install --save-dev @wdio/cli&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Finally, set up the WebdriverIO configuration file using the following command:&lt;/p&gt;

&lt;p&gt;npx wdio config&lt;/p&gt;

&lt;p&gt;You will be prompted to choose the configurations for getting started. Select them as per your requirement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; We will use Mocha as the framework and Chrome as the virtual browser for this Page Object Pattern in Selenium JavaScript tutorial.&lt;/p&gt;

&lt;p&gt;As mentioned earlier, the WebdriverIO framework natively supports Page Object Pattern. Therefore, by selecting the configuration highlighted below, you will be able to auto-generate the Page Object folder structure along with executable sample tests, as shown in the below screenshot.&lt;/p&gt;

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

&lt;p&gt;You can also skip the configuration questions and opt for the default set-up by adding the -y flag to the command below.&lt;/p&gt;

&lt;p&gt;npx wdio config -y&lt;/p&gt;

&lt;p&gt;Using this command, the following packages are configured automatically, which of course, you can later change if you want to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;@wdio/local-runner&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;@wdio/mocha-framework&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;@wdio/spec-reporter&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;@wdio-chromedriver-service&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;@wdio/sync — chromedriver&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;You will see that the &lt;strong&gt;wdio.conf.js&lt;/strong&gt; file gets created under the project root. Therefore, the overall folder structure will look like this.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In this tutorial on Page Object Pattern in JavaScript, we will set up our page object framework from scratch. So I have not opted for auto-generated tests.&lt;/p&gt;

&lt;p&gt;That’s pretty much it with the installation and configuration part while working with Page Object Pattern in JavaScript. Now it’s time to get your hands dirty!&lt;/p&gt;

&lt;p&gt;In the next section of this Selenium JavaScript tutorial, we will learn to write our first automated test case using the JavaScript Page Object Pattern.&lt;/p&gt;

&lt;p&gt;There are certain steps that have to be considered before writing the first test script in JavaScript. We have discussed these steps in detail, including setting up an environment for writing Selenium tests with JavaScript bindings in our earlier Selenium JavaScript tutorial series.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/random-hex-generator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr19_rd&amp;amp;utm_term=rd&amp;amp;utm_content=free_tools"&gt;Random Hex Generator&lt;/a&gt; allows users to generate random HEX numbers.&lt;/p&gt;

&lt;p&gt;However, no matter where you are in the Selenium JavaScript journey — planning how to start, looking for resources to improve, etc. — you can find helpful videos at &lt;a href="https://www.youtube.com/c/LambdaTest?utm_source=medium&amp;amp;utm_medium=organic&amp;amp;utm_campaign=feb25_rd&amp;amp;utm_term=rd&amp;amp;utm_content=blog"&gt;LambdaTest YouTube channel&lt;/a&gt;.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/w4cidssAdJg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing your first test case using Page Object Pattern in JavaScript
&lt;/h2&gt;

&lt;p&gt;We will be writing a test case for simple login functionality, which contains the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Launch the app (&lt;a href="https://opensource-demo.orangehrmlive.com/"&gt;https://opensource-demo.orangehrmlive.com/&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter the correct username and password.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click the Login button.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verify the navigation to the dashboard page on successful login.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_ICLQBHM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3840/1%2AZ1qvNl-QN2nWKG9lT-LLKg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_ICLQBHM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3840/1%2AZ1qvNl-QN2nWKG9lT-LLKg.png" alt="" width="880" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Let’s start with creating the folder structure. One of the primary goals of the Page Object Pattern in JavaScript is to provide abstraction between page information and actual tests. So it makes sense from the structural side to separate Page Objects and actual tests into different directories.&lt;/p&gt;

&lt;p&gt;In the project root, create a folder named “test.” Inside the “test” folder, create two sub-folders and name them as below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;“&lt;strong&gt;pages&lt;/strong&gt;” — for storing Page Objects&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;“&lt;strong&gt;specs&lt;/strong&gt;” — for storing Selenium tests.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Edit the baseUrl inside the wdio.conf.js file to that of our application under test.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;baseUrl: '[https://opensource-demo.orangehrmlive.com/index.php/'](https://opensource-demo.orangehrmlive.com/index.php/')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Create the main page object.&lt;/p&gt;

&lt;p&gt;Inside the “pages” folder, let us create the main Page Object page.js, containing all the general selectors and methods that all other page objects will inherit.&lt;/p&gt;

&lt;p&gt;Inside the page.js file, let us create a “Page” class, which will contain some common functions that can be used across. Also, export it.&lt;/p&gt;

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

module.exports = class Page {

    //to go to a URL 
    open (path) {
        return browser.url(path);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Create specific page objects.&lt;/p&gt;

&lt;p&gt;Since we have only two pages to deal with — the login page and dashboard (home) page, let us create two specific page objects. By convention, the page name should end with “.page.js,” as shown below.&lt;/p&gt;

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

&lt;p&gt;These pages should contain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A class extending the main page — page.js.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Page-specific element selectors using getter methods inside the class.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Page-specific actions/interactions as methods inside the class.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, export an instance of this class.&lt;/p&gt;

&lt;p&gt;Let us see the code for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Login Page&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;//login.page.js&lt;/p&gt;

&lt;p&gt;const Page = require ('./page');&lt;/p&gt;

&lt;p&gt;class LoginPage extends Page{&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; //page locators:
    get inputUsername() { return $('#txtUsername') }
    get inputPassword() { return $('#txtPassword') }
    get loginBtn() { return $('#btnLogin') }

  //page actions:  
    //to open a URL
    async open () {
        await super.open('/')
    }   
    //to enter username and password into login form and click login button
    async login (username,password) {
        await this.inputUsername.setValue(username);
        await this.inputPassword.setValue(password);
        await this.loginBtn.click();
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;module.exports = new LoginPage();&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Home Page&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;//home.page.js&lt;/p&gt;

&lt;p&gt;const Page = require ('./page');&lt;/p&gt;

&lt;p&gt;class HomePage extends Page{&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//page locators:

    get dashboardHeader() { return $('div.head &amp;amp;gt; h1') }

//page actions:  
   //to verify user is in dashboard page
    async isDashboardHeaderExist () {
      return await this.dashboardHeader.isDisplayed();  
    }

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;module.exports = new HomePage();&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Write tests. After defining the necessary elements and methods for the page, you can start to write the test for it.&lt;/p&gt;

&lt;p&gt;Inside the specs folder, create our first test file — “login.spec.js.” In this file, let us write the code for testing the login scenario. Then, to use the page objects, we need to import (or require).&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//login.spec.js

const assert = require('assert');
const LoginPage = require ('../pages/login.page');
const HomePage = require('../pages/home.page');

describe('My Login application', () =&amp;amp;gt; {

    it('I should open the main URL and verify the title', async () =&amp;amp;gt; {

      await browser.url('');
      const title = await browser.getTitle();
      await assert.strictEqual(title , 'OrangeHRM');

    });

    it('I enter valid username and password and login successfully' , async () =&amp;amp;gt; {
      await LoginPage.open();
      await LoginPage.login('Admin','admin123');
      await assert.equal(true ,await HomePage.isDashboardHeaderExist());

    });

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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Run the tests.&lt;/p&gt;

&lt;p&gt;Let us execute the tests by giving the following command:&lt;/p&gt;

&lt;p&gt;npx wdio wdio.conf.js&lt;/p&gt;

&lt;p&gt;The test starts running. The browser gets launched, and the code gets executed successfully, as shown in the below screenshots.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WuB_HbV_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Ax26cZgu-a8-Er74Mr0s7ow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WuB_HbV_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Ax26cZgu-a8-Er74Mr0s7ow.png" alt="" width="512" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&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=apr19_rd&amp;amp;utm_term=rd&amp;amp;utm_content=free_tools"&gt;Random Decimal Fraction Generator&lt;/a&gt; allows users to generate random decimal fractions in the [0,1] interval.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running your automation tests on a cloud Selenium Grid
&lt;/h2&gt;

&lt;p&gt;In the last section, we ran our test script in the local machine. In this step, let us additionally explore how to execute our tests on a cloud-based Selenium Grid.&lt;/p&gt;

&lt;p&gt;It is important to run our Selenium tests on a number of online browsers and devices. Adopting a cloud-based grid as an automation backend is a popular strategy adopted by companies worldwide as a measure to stay ahead in the market competition. LambdaTest offers a cloud Selenium Grid with access to 2000+ real browsers and devices for your testing needs. LambdaTest enables you to choose a specific OS-device-browser combination to ensure that your app is working as expected in all possible scenarios. The platform also aids you in performing parallel testing in Selenium, which can drastically scale your automation test for testing larger regression suites.&lt;/p&gt;

&lt;p&gt;The good news is that the WebdriverIO framework natively supports cloud-based cross browser testing platforms like LambdaTest. With effortless integration, you can now run your tests in the cloud Selenium Grid. Let us see how.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; In your project terminal, give the following command to save the LambdaTest service as a devDependency:&lt;/p&gt;

&lt;p&gt;npm i --save-dev wdio-lambdatest-service&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Create a free account or log in to your &lt;a href="https://accounts.lambdatest.com/dashboard"&gt;LambdaTest account&lt;/a&gt;. Fetch your unique username and access key. You can find the details in the LambdaTest Profile section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Now edit the wdio.conf.js configuration file as shown below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user= process.env.LT_USERNAME || "your_username",
key= process.env.LT_ACCESS_KEY || "your_accesskey",

exports.config = {
    user,
    key,
    specs: [
        './test/specs/**/*.js'
    ],
    exclude: [],

    capabilities: [{   

        name: "Page Object Pattern",
        build: "wdio-PageObjectPattern",
        maxInstances: 5,
        browserName: 'chrome',
        version:"64.0",
        acceptInsecureCerts: true,
        network: true,
        video: true,
        visual: true,
        console: true,

    }],

    logFile : './logDir/api.log',
    services: [
        ['lambdatest', {
            tunnel: true
        }]
    ],
    hostname: 'hub.lambdatest.com',
    path: '/wd/hub',
    port: 80,

    baseUrl: '[https://opensource-demo.orangehrmlive.com/index.php/'](https://opensource-demo.orangehrmlive.com/index.php/'),
    maxInstances: 10,
    logLevel: 'info',
    bail: 0,

    waitforTimeout: 10000,
    connectionRetryTimeout: 120000,
    connectionRetryCount: 3,

    framework: 'mocha',
    reporters: ['spec'],
    mochaOpts: {
        ui: 'bdd',
        timeout: 60000
    } 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Following are the changes to be made:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Pass your access token and user details by creating two variables, user and key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set the tunnel as true to enable routing connections from LambdaTest cloud through your computer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set the hostname corresponding to LambdaTest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optionally, you can also enable report formats like video recording, console, network logs, etc., to be true.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Run the test.&lt;/p&gt;

&lt;p&gt;npx wdio wdio.conf.js&lt;/p&gt;

&lt;p&gt;You can observe the live test running status in your LambdaTest Automation Dashboard, as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VwfFmqwc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3840/1%2AF2rIpG-Jk523WggnE9PztA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VwfFmqwc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3840/1%2AF2rIpG-Jk523WggnE9PztA.png" alt="" width="880" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above screenshot shows that the test has been executed successfully. To analyze the detailed report containing the video and logs, visit the Automation Logs section, as in the below screenshot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--enXJ2Si7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3840/1%2AstmnqJ7Q0N8KdAM7GftdBg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--enXJ2Si7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3840/1%2AstmnqJ7Q0N8KdAM7GftdBg.png" alt="" width="880" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;The Page Object Pattern is a framework design pattern widely adopted by Agile practitioners for ease of test script maintenance, enhanced code reusability, and readability. Whether you are a beginner getting started with Selenium automation testing or you need to manage a larger suite of regression tests, adopting the Page Object Pattern into your code will benefit you in multiple ways. However, the primary goal of this pattern is to provide abstraction between the UI-related code and test-related code.&lt;/p&gt;

&lt;p&gt;In this Selenium JavaScript tutorial on Page Object Pattern in JavaScript, we learned how to implement the Page Object Pattern in JavaScript with the help of the WebdriverIO framework. Additionally, we learned how to scale your automation testing by utilizing the cloud Selenium Grid provided by LambdaTest.&lt;/p&gt;

&lt;p&gt;Happy Testing!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>selenium</category>
      <category>tutorial</category>
      <category>automation</category>
    </item>
    <item>
      <title>How To Perform Automation Testing With Cucumber And Nightwatch JS?</title>
      <dc:creator>HaritaLT</dc:creator>
      <pubDate>Mon, 18 Apr 2022 12:04:51 +0000</pubDate>
      <link>https://forem.com/haritalt/how-to-perform-automation-testing-with-cucumber-and-nightwatch-js-44mj</link>
      <guid>https://forem.com/haritalt/how-to-perform-automation-testing-with-cucumber-and-nightwatch-js-44mj</guid>
      <description>&lt;p&gt;One of the key features of the agile way of software development is the combination of DevOps and &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harita-21092021&amp;amp;utm_term=Harita"&gt;automation testing&lt;/a&gt;. Test automation speeds up the testing cycle, aids in detecting bugs at an early stage, and helps in handling repetitive, time-consuming tasks at a faster pace. To ensure that quality is not compromised while leveraging the benefits of automated testing, adopting the right test automation framework and strategy becomes critical for the success of the project.&lt;/p&gt;

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

&lt;p&gt;In this Cucumber test automation tutorial, we explore Nightwatch.js, one of the widely used JavaScript-based automation frameworks. We will also deep dive into Cucumber, a widely adopted automation tool that supports BDD (Behaviour Driven Development). By the end of this tutorial, you would be in a comfortable position to integrate Cucumber into the &lt;a href="https://www.lambdatest.com/blog/nightwatch-js-tutorial-for-test-automation-beginners/?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harita-21092021&amp;amp;utm_term=Harita"&gt;Nightwatch.js framework&lt;/a&gt; and build a robust BDD framework setup for performing Selenium automation testing.&lt;/p&gt;

&lt;p&gt;So, let’s get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use Nightwatch.js for automation testing?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://nightwatchjs.org/"&gt;Nightwatch.js&lt;/a&gt; is a popular open-source, Selenium JavaScript-based test automation framework for automating browser-based web applications and websites. It is written in Node.js runtime and uses the W3C WebDriver API (formerly Selenium WebDriver) for interacting with various browsers to perform commands and assertions on DOM elements.&lt;/p&gt;

&lt;p&gt;It is an End-to-End (E2E) testing framework that aims to simplify the process of writing automation test scripts.&lt;/p&gt;

&lt;p&gt;Here are some of the noteworthy of the Nightwatch.js framework for Selenium testing:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easy to Use&lt;/strong&gt; — Write efficient code with a clean and simple syntax.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Built-In Test Runner — Comes with an in-built command-line test runner suite with Grunt style support for performing Selenium automation testing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Built-In Test Runner&lt;/strong&gt; — Comes with an in-built command-line test runner suite with Grunt style support for performing Selenium automation testing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Page Object Model&lt;/strong&gt; — Supports CSS and &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harita-21092021&amp;amp;utm_term=Harita"&gt;XPath locators&lt;/a&gt; to make browser automation more manageable. You can read our blog to know more about Selenium automation testing using Page Object Model (POM).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;Flexibility *&lt;/em&gt;— Enables unit, integration as well as &lt;a href="https://www.lambdatest.com/blog/all-you-need-to-know-about-end-to-end-testing/?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harita-21092021&amp;amp;utm_term=Harita"&gt;end-to-end testing&lt;/a&gt;. A robust automated acceptance test can be written with a Gherkin-based Cucumber setup.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integrations&lt;/strong&gt; — Provides a single integrated solution for application testing, thereby eliminating the dependency on third-party software(s). This makes the framework extremely lightweight.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Support for cloud services&lt;/strong&gt; — Compatible with cloud-based testing platforms like LambdaTest that lets you perform Selenium automation testing at a mammoth scale.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Continuous Integration (CI)&lt;/strong&gt; — Supports tools like Jenkins, TeamCity, etc., to assist developers in building and testing software continuously.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to Install and Configure Nightwatch.js
&lt;/h2&gt;

&lt;p&gt;In this part of the Cucumber test automation tutorial, we first proceed with the installation and environment set-up for Nightwatch.js. Before installing nightwatch.js, make sure your system is pre-equipped with the following:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites for Nightwatch.js:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Java&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Check whether Java is already installed in your system using the following command:&lt;/p&gt;

&lt;p&gt;java -version&lt;/p&gt;

&lt;p&gt;If not, you can download the latest JDK appropriate to your machine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. NodeJS and NPM&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Check whether Node.js is already installed in your system using the following command:&lt;/p&gt;

&lt;p&gt;node -v&lt;/p&gt;

&lt;p&gt;If not, download the latest LTS version from the &lt;a href="https://nodejs.org/en/"&gt;Node.js official website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: NPM will be installed along with Node.js. So no need for separate installation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. IDE of your choice.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here we will be using Visual Studio Code. You may download VSC from the Visual Studio official website.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to install and setup projects with Nightwatch.js:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With the pre-requisites for Nightwatch.js installed on the machine, it’s time to install this powerful automation framework. Follow the below-mentioned steps for install and setup projects with Nightwatch.js:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a folder for your project in the desired location in your system. Open the folder in the preferred IDE.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Initialize the project with the &lt;strong&gt;package.json&lt;/strong&gt; file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open the terminal in VS Code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verify whether it is pointing to the project folder.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Trigger the following command on the terminal to initialize the project:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;npm init&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fill in the project details or leave them blank.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You will notice the package.json file is created in your root folder. It contains details about the project configuration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;However, you can save some time by using the following command to generate a default empty npm project file by skipping the above interactive process.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;npm init -y&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here -y stands for “yes.”&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;With this, we have successfully created the package.json file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Run the following command on the terminal to install Nightwatch.js in the current project.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev nightwatch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This command will add the “node_modules” folder in your directory and download nightwatch.js as a devDependency in package.json.&lt;/p&gt;

&lt;p&gt;You will also notice the “package-lock.json” file in the project folder. This file is used to lock all the dependencies with the version number, making it easy to set up the code in another system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Finally, let us install our browser web drivers (i.e., ChromeDriver and GeckoDriver).&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev chromedriver geckodriver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The Selenium Server was required with older Nightwatch versions (‘v0.9’ and prior), starting with version ‘1.0’ Selenium is no longer necessary.&lt;/p&gt;

&lt;p&gt;With this, we are all done with the installation of Nightwatch.js. It’s time to get our hands dirty with some implementation 🙂&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/hash-mac-generator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr18_rd&amp;amp;utm_term=rd&amp;amp;utm_content=free_tools"&gt;Hash Mac Generator&lt;/a&gt; is a message authentication code that uses a cryptographic hash function such as SHA-256.&lt;/p&gt;
&lt;h2&gt;
  
  
  Running your first test with NightWatch.js
&lt;/h2&gt;

&lt;p&gt;In this part of the Cucumber test automation tutorial, we look into the additional configurations required for automation testing. By the end of this section, you will be able to write and run your first test case successfully!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Configuration for automation testing with Nightwatch.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our goal is to run our test files using the “npm test” command from the project’s base directory. Also, we are setting Chrome as the default browser. You can use the browser of your choice, please download the corresponding browser driver before writing the test.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Create two folders in the root directory:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;“tests” for keeping test files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;“reports” to store reports after each test run.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can provide any folder name of your choice; however, it should be properly mapped in the nightwatch.json file, which we will discuss in step 3.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: In your package.json file, replace “scripts -&amp;gt; test” with “nightwatch.”&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;“scripts”: {
  “test”: “nightwatch”
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This is how the package.json file looks like:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Step 3&lt;/strong&gt;: In the root, create the “nightwatch.json” file and paste the following snippet.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "src_folders" : "tests",
    "output_folder" : "reports",

    "webdriver" : {
        "start_process": true,
        "server_path": "node_modules/chromedriver/lib/chromedriver/chromedriver.exe",
        "host": "localhost",
        "port": 4444
      },
      "test_settings" : {
        "default" : {
            "desiredCapabilities" : {
                "browserName" : "chrome"            
              }           
          }
      }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The “nightwatch.json” is the configuration file that is required by the Nightwatch test runner.&lt;/p&gt;

&lt;p&gt;Let’s have a close look into the above code structure of “nightwatch.json.”&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;src_folders&lt;/strong&gt;: Contains the test suites. You can also give an array of folders.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;output_folder&lt;/strong&gt;: Test reports get saved in this location ( i.e., JUnit report files, XML reports, test logs, selenium log, screenshots, video logs, network logs, etc.).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;webdriver&lt;/strong&gt;: An object containing configurations related to &lt;a href="https://www.lambdatest.com/blog/selenium-webdriver-tutorial-with-examples/?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harita-21092021&amp;amp;utm_term=Harita"&gt;Selenium WebDriver&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;test-settings&lt;/strong&gt;: Defines the browser we want to use.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You may also create “nightwatch.conf.js” for configuration. If both these files are present in the project, the nightwatch.conf.js file will be given more precedence over the nightwatch.json (i.e., settings mentioned in nightwatch.conf.js will override the ones mentioned in nightwatch.json).&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Step 4:&lt;/strong&gt; With this, we are all done with the configurations required for automation testing with Nightwatch.js. Let’s write our first test.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Writing Nightwatch.js tests for automation testing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the previous section of this Cucumber test automation tutorial, we have created a “tests” folder. Now, we will be creating test files inside this folder. Each file will be loaded as a test suite by the Nightwatch test runner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Create a test file and name it as — testfile1.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Let’s create the test structure.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {

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

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Inside each file, the test runner will look for keys that are exported for usage by other modules. The key is the test case name. Each key is mapped to a function in which the browser instance will be passed as an argument.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
    "Step one: Navigate to google and verify URL and title" : function(browser){

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

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Inside this function, we write our test code. Let’ 's examine the snippet below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
This is a simple and straightforward test scenario. The code is written in two steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The code will launch the Chrome browser and navigate to google.com, and verify the page URL and title with the expected result.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The search field inputs “nightwatch” and verifies search results to match the text “nightwatch.”&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here are the major aspects of the implementation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;url()&lt;/strong&gt; — Navigates to a specific URL.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;urlContains() *&lt;/em&gt;— Checks if the current URL contains the given value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;title()&lt;/strong&gt; — Checks if the page title equals the given value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;containsText()&lt;/strong&gt; — Checks if the given element contains the specified text.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;setValue()&lt;/strong&gt; — Sets the value of the DOM element as the given value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;pause()&lt;/strong&gt; — Suspends the test for the given time in milliseconds.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;assert *&lt;/em&gt;— When an assertion fails, the test ends, skipping all other assertions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: To run the code, go to the terminal and navigate to the base directory of the project. Run the following command once you are in that directory:&lt;/p&gt;

&lt;p&gt;npm test&lt;/p&gt;

&lt;p&gt;The test starts running, and the browser will be launched.&lt;/p&gt;

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

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

&lt;p&gt;All assertions are passed!! We have successfully written our first test code with the Nightwatch.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt;: Check out the reports generated in your “reports” folder.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use Cucumber.js for Selenium automation testing?
&lt;/h2&gt;

&lt;p&gt;Behavior Driven Development (BDD) is an approach where the behavior of the feature is defined using simple Domain Specific Language (DSL). The major advantage is that the requirements are easily understandable to anyone (including the non-technical fraternity). DSL is widely used for writing test scripts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://cucumber.io/"&gt;Cucumber&lt;/a&gt; is an automation tool based on the BDD framework, using which you can write automated acceptance tests for web applications in the Gherkin language.&lt;/p&gt;

&lt;p&gt;Here are the major benefits of the Cucumber BDD framework:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It acts as a bridge between the technical team (Developers, QA, etc.) and the non-technical team (Business Analysts, stakeholders, etc.) as the features are written in simple language (i.e., Gherkin).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It focuses more on the user experience.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It has an easy installation and set-up process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is immense focus on the re-usability and improved maintenance of the test code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/find-and-replace-string?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr18_rd&amp;amp;utm_term=rd&amp;amp;utm_content=free_tools"&gt;Find and Replace String&lt;/a&gt; is a free online tool that lets you find-and-replace strings in a browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Cucumber test automation work?
&lt;/h2&gt;

&lt;p&gt;As mentioned in the previous section of this Cucumber test automation tutorial, we can use Cucumber to write scenarios in simple text using Gherkin’s syntax. Some commonly used Gherkin keywords are Feature, Scenario, Given, When, Then, And, etc.&lt;/p&gt;

&lt;p&gt;Feature represents the high-level description of the functionality that is used to group related scenarios. A Scenario is a collection of steps for Cucumber to work through. The steps are constructed using keywords Given, When, And, Then, etc., each serving a specific purpose. A Gherkin document is stored in a file called a feature file having a .feature extension.&lt;/p&gt;

&lt;p&gt;A typical feature file for login functionality will look like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;strong&gt;Step definitions&lt;/strong&gt; are used to connect Gherkin’s steps to programming code. There should be a step definition associated with each step where the code to be executed will be defined.

&lt;p&gt;With this, let’s get started with BDD-Cucumber.&lt;/p&gt;
&lt;h2&gt;
  
  
  Automation testing with Nightwatch.js and Cucumber.js
&lt;/h2&gt;

&lt;p&gt;In the previous sections, we have explored Nightwatch.js and Cucumber frameworks and their key features that are instrumental in Selenium automation testing. We successfully installed and wrote our first test case using the Nightwatch.js automation framework.&lt;/p&gt;

&lt;p&gt;In the upcoming sections of this Cucumber test automation tutorial, we look at how to integrate Cucumber into the Nightwatch.js framework for building a robust BDD setup for testing web applications.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to install and configure Cucumber
&lt;/h2&gt;

&lt;p&gt;Let’s start with the installation and setup of the Cucumber framework.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Installing dependencies:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You may skip previously installed dependencies like nightwatch and chromedriver.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev nightwatch-api nightwatch [@cucumber/cucumber](http://twitter.com/cucumber/cucumber) chromedriver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Nightwatch API adds huge flexibility and control to Nightwatch.js, which is extremely helpful in running acceptance tests based on feature requirements written in the Gherkin language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Configuring Nightwatch.js:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the Nightwatch.js automation framework set up, we configured the framework through the nightwatch.json file or the nightwatch.conf.js. Moving forward, we will be sticking with the nightwatch.conf.js file since it provides more flexibility in terms of configuration.&lt;/p&gt;

&lt;p&gt;The only difference here from the previous setup is that we have removed “src_folder” as tests are running using Cucumber.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const chromedriver = require('chromedriver');

module.exports = {
  test_settings: {
    default: {
      webdriver: {
        start_process: true,
        server_path: chromedriver.path,
        port: 4444,
      },
      desiredCapabilities: {
        browserName: 'chrome'
      }
    }
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The Cucumber test automation requires three types of files:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Cucumber configuration file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Feature file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step definition file&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Configuring Cucumber:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now it’s time to configure Cucumber. Create a file named cucumber.conf.js in the root folder of the project root and paste the following snippet.&lt;/p&gt;

&lt;p&gt;This file is responsible for setting up the default timeout, starting the WebDriver, and creating the browser session.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;strong&gt;Step 4: Writing a feature file:&lt;/strong&gt;

&lt;p&gt;Create a folder named “features” in the project root folder. All the feature files are maintained inside this folder.&lt;/p&gt;

&lt;p&gt;Create a feature file named “google.feature.” We will continue using the same example we used for Nightwatch.js setup.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;strong&gt;Step 5: Writing step definitions:&lt;/strong&gt;

&lt;p&gt;As mentioned in the previous section of this Cucumber test automation tutorial, each feature step should be associated with its corresponding step definition where the code to be executed is written.&lt;/p&gt;

&lt;p&gt;Create a step definition file named google.js under the step-definitions folder&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;strong&gt;Unsure how to write step definitions?&lt;/strong&gt;

&lt;p&gt;Simply execute the code using the run command (which we will be customizing in step 6). Due to the Cucumber’s design patterns, you will be displayed with a suggestion as shown below in the terminal for unimplemented steps. Next, copy-paste the code into the step definition file and fill it with your assertions.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt;: Creating npm script:&lt;/p&gt;

&lt;p&gt;In package.json, define any short command to execute the Cucumber tests. Here we have given the “e2e-test.” You have the flexibility to choose any name for it.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "e2e-test": "cucumber-js --require cucumber.conf.js --require step-definitions"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 7: Run the tests:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Trigger the following command on the terminal for running the tests:&lt;/p&gt;

&lt;p&gt;npm run e2e-test&lt;/p&gt;

&lt;p&gt;The test runs successfully, and all assertions are passed!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8YaHO3z9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/0%2ArcPpE4HF_tRmCqaY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8YaHO3z9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/0%2ArcPpE4HF_tRmCqaY.png" alt="" width="853" height="400"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Customizing test runs in Cucumber
&lt;/h2&gt;

&lt;p&gt;Use the following commands for customizing test runs in Cucumber:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;For running a single feature file:&lt;/p&gt;

&lt;p&gt;npm run e2e-test -- features/google.feature&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For running multiple feature files:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;npm run e2e-test -- features/google.feature -- features/firefox.feature&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For using glob patterns:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;npm run e2e-test -- features/*&lt;em&gt;/&lt;/em&gt;.feature&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For running Feature directory:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;npm run e2e-test -- features/dir&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For running a scenario by its line number:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;npm run e2e-test -- features/my_feature.feature:3&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For running a scenario by its name that matches a regular expression:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;npm run e2e-test -- --name "topic 1"&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For using tags (@) for a selective run:&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Add tags to the Feature or Scenario and mention the tag in the run command to run or skip the scenario or feature in a selective manner.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  How to create custom reporters in Nightwatch.js and Cucumber
&lt;/h2&gt;

&lt;p&gt;Cucumber provides a number of options for generating reports. A report is a very valuable tool in debugging. Also, it provides immediate visual feedback for analyzing potential problems.&lt;/p&gt;

&lt;p&gt;You can enable HTML reporting in the framework by installing the required dependencies and minimal configurations. Reports in the HTML format are easy to read and understand.&lt;/p&gt;

&lt;p&gt;Follow the below steps for generating reports in Cucumber and Nightwatch.js:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Installing dependencies:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev cucumber-html-reporter mkdirp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 2: Configuring Nightwatch.js:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Enable screenshots in default settings of nightwatch.config.json file.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default: {
      screenshots: {
        enabled: true,
        path: 'screenshots'
      },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 3: Configuring Cucumber config file:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Implement the “After” hook by adding code for handling screenshots and attaching them to the report.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Step 4: Updating the npm scripts:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the package.json file, update scripts to accommodate the report feature. Here, mkdirp is used to make sure the report folder exists before running the test.&lt;/p&gt;

&lt;p&gt;JSON formatter generates a JSON report, which is then utilized by cucumber-html-reporter to generate the HTML report.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "e2e-test": "mkdirp report &amp;amp;&amp;amp; cucumber-js --require cucumber.conf.js --require step-definitions --format json:report/cucumber_report.json"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 5: Running the tests:.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;npm run e2e-test&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After successful execution, you should see the HTML report getting generated and getting displayed in a new browser tab.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/decimal-to-roman?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr18_rd&amp;amp;utm_term=rd&amp;amp;utm_content=free_tools"&gt;Decimal To Roman&lt;/a&gt; converts decimal numerals to roman numerals.&lt;/p&gt;
&lt;h2&gt;
  
  
  Automation testing with Cucumber and Nightwatch.js on a cloud-based Selenium Grid
&lt;/h2&gt;

&lt;p&gt;So far in this Cucumber test automation tutorial, we deep-dived into the integration of the Nightwatch.js framework with Cucumber. The tests were executed on a local Selenium Grid, a solution that is not feasible for running automation tests at scale.&lt;/p&gt;

&lt;p&gt;To stay ahead in a highly competitive market, it is our duty as testers to ensure the quality of the application in all scenarios, i.e., the app (or website) should behave consistently across all platforms, browsers, and devices. This is where the importance of &lt;a href="https://www.lambdatest.com/blog/code-coverage-vs-test-coverage/?utm_source=devto&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Harita-21092021&amp;amp;utm_term=Harita"&gt;test coverage&lt;/a&gt; arises in cross browser testing.&lt;/p&gt;

&lt;p&gt;This can be achieved by building an in-house infrastructure or outsourcing to a third-party cloud platform like LambdaTest. Automation testing (or cross browser testing) on a cloud-based Selenium Grid lets you run tests at an expedited pace across different browsers, platforms, and devices. Your automation tests can leverage the advantages of parallel testing in Selenium to run automation tests at a super-fast pace.&lt;/p&gt;

&lt;p&gt;Using a combination of remote Selenium Grid with the right cloud-based solution provider like LambdaTest for cross browser compatibility testing is the optimum strategy to run automation tests at scale. LambdaTest provides you access to 2000+ browsers for mobile and desktop to help you gain the maximum browser coverage during the automated browser testing process.&lt;/p&gt;

&lt;p&gt;Follow the below steps to get started with cross browser testing on LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Log in to LambdaTest or &lt;a href="https://accounts.lambdatest.com/dashboard"&gt;Create a free LambdaTest account&lt;/a&gt;. Once an account is created, make a note of the username and access token from the &lt;a href="https://accounts.lambdatest.com/detail/profile"&gt;LambdaTest profile section&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Use the &lt;a href="https://www.lambdatest.com/capabilities-generator/"&gt;LambdaTest Desired Capabilities&lt;/a&gt; generator to generate browser and platform capabilities as per your test requirements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--67ruo9v6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2048/0%2A_v4Yt7cgDhTfmwsv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--67ruo9v6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2048/0%2A_v4Yt7cgDhTfmwsv.png" alt="" width="880" height="433"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;desiredCapabilities: {
            build:"Nightwatch-Cucumber-Test",
            platform : "Windows 10",
            browserName : "chrome",
            version : "87.0",
            selenium_version: "3.13.0"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Now it’s time to make the required configuration changes in your nightwatch.conf.js file. Let’s examine the code below.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Following are the additions made to the config file:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Pass your Access Token and User details (obtained from Step 1).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provide Desired Capabilities for the browser (obtained from Step 2).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set the Hostname corresponding to LambdaTest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enable video recording, console, network logs, etc., in the desired capabilities.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We have used the following additional parameters to configure “nightwatch.config.js” for cloud-based cross-browser testing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;selenium *&lt;/em&gt;— It is an object containing options related to the Selenium Server. If “selenium” is not used, “webdriver” options should be set instead (as we did in the local setup). Starting with Nightwatch 1.0, Selenium is only required when testing is done against a Grid setup or a cloud-based Selenium Grid like LambdaTest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;cli_args *&lt;/em&gt;— It contains a list of CLI args to be passed to the Selenium process (i.e., browser drivers).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;test_settings&lt;/strong&gt;:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;silent *&lt;/em&gt;— It is used to show the extended HTTP traffic command logs from the WebDriver or Selenium server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;desiredCapabilities *&lt;/em&gt;— It is an object to specify various capabilities like browser name, browser version, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;username and access_key&lt;/strong&gt; — It is needed for accessing the cloud-based Grid.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Add a new “scripts” argument value in package.json.&lt;/p&gt;

&lt;p&gt;Here we have added “remote-test,” which is specific for cloud-based Selenium testing. We have removed the custom HTML report params associated with local execution.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "e2e-test": "mkdirp report &amp;amp;&amp;amp; cucumber-js --require cucumber.conf.js --require step-definitions --format json:report/cucumber_report.json",
    "remote-test": "cucumber-js  --require cucumber.conf.js --require step-definitions"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: Run the test using the following command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run remote-test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code starts running, and logs can be observed in the terminal.&lt;/p&gt;

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

&lt;p&gt;Navigate to the &lt;a href="https://automation.lambdatest.com/timeline"&gt;Automation dashboard&lt;/a&gt; in LambdaTest to keep a watch on the status of test execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uVJEg1Qs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2048/0%2AX70JtBkBWZX-DL_T.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uVJEg1Qs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2048/0%2AX70JtBkBWZX-DL_T.png" alt="" width="880" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As shown below, the test was executed successfully:&lt;/p&gt;

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

&lt;p&gt;We can analyze individual test run reports from Automation Logs.&lt;/p&gt;

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

&lt;p&gt;We have successfully run our test cases using an automated cross-browser testing solution provided by LambdaTest through a hassle-free experience!&lt;/p&gt;

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

&lt;p&gt;Nightwatch.js is one of the most popular JavaScript-based Selenium automation frameworks, and Cucumber is one of the top tools for BDD implementation. Cucumber can be integrated with Nightwatch.js to build a robust automation testing framework. Together they enable you to create highly configurable test scripts along with easily readable documentation and additional features like custom HTML report generation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y8idf92Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/0%2A-1Ijj8krn6dem8n7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y8idf92Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/0%2A-1Ijj8krn6dem8n7.gif" alt="" width="220" height="124"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hopefully, this Cucumber test automation tutorial enables you to implement the Nightwatch.js automation with the Cucumber and run your first test successfully with alluring HTML reports. If you wish to further optimize your framework and guarantee your web application quality by achieving test coverage, please explore the LambdaTest platform.&lt;/p&gt;

&lt;p&gt;Happy Testing!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>automation</category>
      <category>cucumber</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How To Implement Drag And Drop In JavaScript Using Selenium?</title>
      <dc:creator>HaritaLT</dc:creator>
      <pubDate>Wed, 13 Apr 2022 14:37:55 +0000</pubDate>
      <link>https://forem.com/haritalt/how-to-implement-drag-and-drop-in-javascript-using-selenium-l0h</link>
      <guid>https://forem.com/haritalt/how-to-implement-drag-and-drop-in-javascript-using-selenium-l0h</guid>
      <description>&lt;p&gt;Drag and Drop is an adored web feature implemented in many modern web applications. The list is endless, from cloud storage services like Google Drive and Dropbox to project management tools like Jira and Trello. As automation testers, it is our duty to leave no feature of our application untested. But often, it is tricky to automate a feature with complex user interaction like Drag and Drop.&lt;/p&gt;

&lt;p&gt;Fortunately, the popular automation testing tool &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=apr13_rd&amp;amp;utm_term=rd&amp;amp;utm_content=Blog"&gt;Selenium WebDriver&lt;/a&gt; offers functionality to easily automate dragging a web element from one part of the site and dropping it at another. In this article on how to implement drag and drop in JavaScript, we will learn how to write automation test code for the Drag and Drop feature using &lt;a href="https://www.lambdatest.com/learning-hub/selenium-javascript?utm_source=devto&amp;amp;utm_medium=Organic&amp;amp;utm_campaign=apr13_rd&amp;amp;utm_term=rd&amp;amp;utm_content=learning_hub"&gt;Selenium JavaScript&lt;/a&gt;.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  What is Drag and Drop Action?
&lt;/h2&gt;

&lt;p&gt;Let us begin by understanding what exactly is the Drag and Drop operation in HTML.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b-OJfhY8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/3200/1%2Aa4tyNAZ4Q7VX9CkgzKb-mA.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b-OJfhY8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/3200/1%2Aa4tyNAZ4Q7VX9CkgzKb-mA.gif" alt="" width="880" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The drag and drop action is a GUI-based software feature done with the help of a mouse. This feature enables us to “hold” an element, “drag” it to a different location or onto another virtual object and “drop” it to the target location. In HTML, any element can be dragged and dropped.&lt;/p&gt;

&lt;p&gt;The workflow of a typical drag and drop feature is as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The element of interest, which is to be dragged and dropped, must be visible on the screen.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The user initiates interaction by acquiring the item. The selection technique can be a mouse click.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keeping the item selected, the user moves the pointing device (mouse) to the desired location or target through continuous press or “drag.”&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the desired location or target is reached, the user lets go of the item, and it is now “dropped.”&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Drag and Drop is a part of many user interfaces we use every day. Some common examples are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Uploading a file to a web application (Eg: Google Drive)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Moving a file from one folder to another (Eg: Gmail )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reordering, Resizing, and Grouping of objects (Eg: JIRA, Trello)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rearranging browser tabs (Eg: Chrome, Firefox)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to implement Drag and Drop in JavaScript?
&lt;/h2&gt;

&lt;p&gt;We can automate the Drag and Drop operation by leveraging the &lt;a href="https://www.lambdatest.com/blog/what-is-actions-class-in-selenium/?utm_source=devto&amp;amp;utm_medium=Organic&amp;amp;utm_campaign=apr13_rd&amp;amp;utm_term=rd&amp;amp;utm_content=Blog"&gt;Actions class in Selenium&lt;/a&gt; provided by the Selenium WebDriver API. The actions class supports two methods, using which we can drag and drop an element. They are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;dragAndDrop method&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;dragAndDropBy method&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition to these two methods, we can also make use of the following Actions class method to achieve drag and drop automation. They are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;moveByOffset method&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;moveToElement method&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before we get deeper into how to implement drag and drop in JavaScript, let us understand more about the Actions class itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the Actions class in Selenium?
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;WebElement commands&lt;/strong&gt; need no introduction, and they help us identify a WebElement and perform various actions on it. Most basic user interactions like clicking on a button or entering text to an input field can be achieved using the WebElement commands.&lt;/p&gt;

&lt;p&gt;Some popular commands are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;WebElement.click()&lt;/strong&gt; — to click on a web element&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;WebElement.sendKeys()&lt;/strong&gt; — to enter text to a field&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;WebElement.submit()&lt;/strong&gt; — to submit a form etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, if you need to perform complex interactions like Drag and Drop Double-click, or Click and Hold an element, they cannot be achieved using simple WebElement commands alone. And this is where the &lt;strong&gt;Actions class&lt;/strong&gt; comes into the picture.&lt;/p&gt;

&lt;p&gt;The official documentation describes the Actions class as:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“The user-facing API for emulating complex user gestures. Use this class rather than using the Keyboard or Mouse directly.”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In short, to automate advanced keyboard-based or mouse-based actions in Selenium, you need to utilize the Actions class in Selenium. The methods within the Actions class can be divided into two main categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keyboard Events&lt;/strong&gt; — keyDown(), keyUp(),sendKeys(), etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mouse Events&lt;/strong&gt; — dragAndDrop(), dragAndDropBy(), moveByOffset(), moveToElement() etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The various actions supported by the actions class are:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GCTF-5aZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AfiDD-PvhGbJiDokffBPkow.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GCTF-5aZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AfiDD-PvhGbJiDokffBPkow.gif" alt="" width="880" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax for Drag and Drop in Selenium
&lt;/h2&gt;

&lt;p&gt;At the beginning of this section on JavaScript drag and drop, we learned that the Actions class supports two methods that enable the automation of the drag and drop action. Now, let us study them in detail, along with the syntax required to perform the action.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;dragAndDrop method&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This method is used to perform a Drag and Drop operation where you need to left-click on an element, hold it and drag it to the desired target location and drop it.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Actions.dragAndDrop(WebElement source, WebElement target)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This method accepts two parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;source *&lt;/em&gt;— the web element which we need to click and start the drag&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;target&lt;/strong&gt; — the web element on which we need to drop the source element&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To find the source and target element, we can use any locator strategy.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(Any By strategy &amp;amp; locator)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;2. dragAndDropBy method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method is used to perform Drag and Drop operations in Selenium using x and y offsets. The source element will be dragged and dropped at the location specified via the x-axis and y-axis offsets in pixels.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Actions.dragAndDropBy(WebElement source, int xOffset, int yOffset)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This method accepts three parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;source *&lt;/em&gt;— the element which we need to click and start the drag&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;xOffset&lt;/strong&gt; — x-axis pixel value which defines the horizontal movement of the source element&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;yOffset *&lt;/em&gt;— y-axis pixel value which defines the vertical movement of the source element&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Again, to find the source element, we can use any locator strategy.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(Any By strategy &amp;amp; locator)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The dragAndDropBy() works similar to the dragAndDropBy(), but the only difference is that dragAndDropBy() moves the source element to the offsets and not to any target element.&lt;/p&gt;

&lt;p&gt;Finally, once the dragAndDrop() or dragAndDropBy() method is defined with parameters, the &lt;strong&gt;perform()&lt;/strong&gt; method needs to be invoked for performing the action.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Actions.dragAndDrop(WebElement source, WebElement target).perform();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;OR&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Actions.dragAndDropBy(WebElement source, int xOffset, int yOffset).perform();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Alternatively, we can also achieve drag and drop automation by &lt;strong&gt;&lt;em&gt;clicking and holding&lt;/em&gt;&lt;/strong&gt; an element, &lt;em&gt;**moving *&lt;/em&gt;&lt;em&gt;it to the target position, and then *&lt;/em&gt;&lt;em&gt;releasing *&lt;/em&gt;*it. To execute this operation sequence, we will be making use of the following methods from the Actions class.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;clicking and holding a web element via &lt;strong&gt;clickAndHold()&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;moving to the target location via &lt;strong&gt;moveToElement()&lt;/strong&gt; or &lt;strong&gt;moveByOffset()&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;release mouse button at target position via &lt;strong&gt;release()&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let us check out them in detail, along with the syntax below.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;3. moveToElement method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method is used to move the mouse to a specified web element. We can also move a draggable web element using this method.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Actions.clickAndHold(WebElement source).moveToElement(WebElement target).release().perform();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The web element which we desire to click and hold is passed to the clickAndHold()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, we move the mouse pointer to the target web element specified within in moveToElement()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, we release the element from the mouse button hold using release().&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, we invoke the perform() for performing this chain of actions currently built.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. moveByOffset method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method is used to move the mouse to the specified offset of the last known mouse coordinates. On the initial page load, the mouse coordinates will be (0,0). We can also move a draggable web element using this method.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Actions.clickAndHold(WebElement source).moveByOffset(int xOffset,int yOffset).release().perform();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The web element which we need to click and hold is passed to the &lt;strong&gt;clickAndHold()&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, we move the mouse pointer to the target location by passing the X and Y offsets within in &lt;strong&gt;moveByOffset()&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, we release the element from the mouse button hold using &lt;strong&gt;release()&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, we invoke the &lt;strong&gt;perform()&lt;/strong&gt; for performing this chain of actions currently built.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: With the help of &lt;a href="https://www.lambdatest.com/web-technologies/import-maps?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr13_rd&amp;amp;utm_term=rd&amp;amp;utm_content=web_technologies"&gt;**Import maps&lt;/a&gt;**, you can control what URLs get fetched by JavaScript’s &lt;code&gt;import&lt;/code&gt; statements and &lt;code&gt;import()&lt;/code&gt; expressions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax for Drag and Drop in JavaScript using Selenium
&lt;/h2&gt;

&lt;p&gt;To work with Selenium using JavaScript drag and drop, we will be making the &lt;strong&gt;selenium-webdriver&lt;/strong&gt; library. Here, the syntax for calling &lt;strong&gt;Actions class&lt;/strong&gt; and the available methods are slightly different. To use the JavaScript drag and drop feature in Selenium automation testing, we will be utilizing the &lt;strong&gt;dragAndDrop&lt;/strong&gt; method.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;dragAndDrop method&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This method helps us to configure drag and drop action by passing either the target web element or X and Y offsets.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.dragAndDrop(WebElement from, WebElement|{x: number, y: number} to ).perform();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The function accepts two parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;from&lt;/strong&gt; — the web element which we need to click and start the drag&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;to *&lt;/em&gt;— either a web element or offset (in pixel) to mark the target location where the element needs to be dropped.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next section of this article on how to implement drag and drop in JavaScript, we will demonstrate writing a test case to automate Drag and Drop in JavaScript using Selenium.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to write a test case to automate Drag and Drop in JavaScript using Selenium
&lt;/h2&gt;

&lt;p&gt;So far, we have discussed the Actions class and various methods to automate the Drag and Drop feature. Finally, it’s time to get your hands dirty!&lt;/p&gt;

&lt;p&gt;In this section of how to implement drag and drop in JavaScript, you will be learning how to drag and drop using JavaScript while performing &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=Organic&amp;amp;utm_campaign=apr13_rd&amp;amp;utm_term=rd&amp;amp;utm_content=webpage"&gt;Selenium test automation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;I am assuming that the following prerequisites are installed in your system.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Node.js and NPM&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Visual Studio Code or any IDE of choice&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: &lt;a href="https://www.lambdatest.com/web-technologies/intl-pluralrules?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr13_rd&amp;amp;utm_term=rd&amp;amp;utm_content=web_technologies"&gt;**Intl.PluralRules API&lt;/a&gt; -**API for plural language rules and plural sensitive formatting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test Scenario
&lt;/h2&gt;

&lt;p&gt;For this demo, I will be writing the test case for the following simple and straightforward scenario.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Launch the browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to the &lt;a href="https://www.lambdatest.com/selenium-playground/drag-and-drop-demo?utm_source=devto&amp;amp;utm_medium=Organic&amp;amp;utm_campaign=apr13_rd&amp;amp;utm_term=rd&amp;amp;utm_content=webpage"&gt;Selenium Playground&lt;/a&gt; website.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Perform Drag and Drop by selecting the first element and dragging and dropping it on the second element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Close the browser.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TASlRx2m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AbedtkpBPHB2Dmxzai9IkAA.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TASlRx2m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AbedtkpBPHB2Dmxzai9IkAA.gif" alt="" width="880" height="329"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I will be executing the test case on three different browsers — Firefox, Chrome, and Safari — using the online Selenium Grid provided by the LambdaTest cross-browser testing platform. Automation testing tools like LambdaTest gives you access to an online device farm of 3000+ browser, device, and OS configurations helping you to achieve test coverage on the maximum number of browser and OS combinations. On top of that, LambdaTest also supports web testing in the latest Selenium 4.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps for Writing the Drag and Drop test case
&lt;/h2&gt;

&lt;p&gt;Now, it’s time to write tests in Selenium WebDriver for automation testing. From project set-up to test execution, I will be walking you through the whole process step by step.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Using dragAndDrop() by passing source and target web elements&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a new folder for your project. Open the folder location via your IDE (Visual Studio Code) and initialize the project by giving the following command in the terminal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The project is now initialized, and the &lt;strong&gt;package.json&lt;/strong&gt; file is created in the root folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Install dependencies&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install selenium-webdriver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3: **Create a folder named “&lt;/strong&gt;tests*&lt;em&gt;” within your project root folder. We will be storing all our test cases here. Inside this folder, create your first test case file, “&lt;/em&gt;&lt;em&gt;test.js&lt;/em&gt;*.”&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; Inside the test.js file, add required imports and configurations to run the test on a cloud grid hosted by LambdaTest.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a free account in LambaTest and fetch your unique username and access key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Define the username and access key in your test.js file. Also, define the grid host.&lt;/p&gt;

&lt;p&gt;const USERNAME = "YOUR_USERNAME"; //replace with your username&lt;br&gt;
const KEY = "YOUR_ACCESSKEY"; //replace with your access key&lt;/p&gt;

&lt;p&gt;const GRID_HOST = "hub.lambdatest.com/wd/hub";&lt;br&gt;
const gridUrl = "https://" + USERNAME + ":" + KEY + "@" + GRID_HOST;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make the required imports.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;const { Builder, Key, By } = require("selenium-webdriver");&lt;/p&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Define the test method. Create a function &lt;strong&gt;example()&lt;/strong&gt;, which will be containing your test script. Feel free to provide any function name of your choice.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function example() {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;At the beginning of this test method, let us define the required capabilities. To generate your custom desired capability to meet your browser and OS version requirement, make use of the LambdaTest Capabilities Generator.&lt;/p&gt;

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

&lt;p&gt;Here I have set the capability to run my test case in Chrome browser version 91.0 in the windows 10 platform using Selenium 4.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var capabilities = {
      build: "Drag and Drop",
      name: "Drag and Drop",
      platform: "Windows 10",
      browserName: "Chrome",
      version: "91.0",
      selenium_version: "4.0.0",
      chrome.driver: "91.0",
    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; Now, let us write our test script inside our test method using Selenium and JavaScript. I am making use of the &lt;strong&gt;dragAndDrop()&lt;/strong&gt; and passing source and target web elements to achieve the drag and drop action here.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//To wait for browser to build and launch properly
    var driver = await new Builder()
      .usingServer(gridUrl)
      .withCapabilities(capabilities)
      .build();

    //To launch the website
    await driver.get(
      "[https://www.lambdatest.com/selenium-playground/drag-and-drop-demo](https://www.lambdatest.com/selenium-playground/drag-and-drop-demo)"
    );

    //To locate the source and target elements
    let draggable = driver.findElement(By.id("draggable"));
    let droppable = driver.findElement(By.id("droppable"));

    //To perform drag and drop operation
    await driver.actions().dragAndDrop(draggable, droppable).perform();

    //To verify drop success/fail by validating the text inside target element
    let targetText = await droppable.getText();
    if (targetText === "Dropped!") {
      console.log("PASS: Source is dropped at location");
    } else {
      console.log("FAIL: Source is not dropped at location");
    }


    //It is always a safe practice to quit the browser after execution
    await driver.quit();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Our final code looks like this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const USERNAME = "YOUR_USERNAME"; //replace with your username
const KEY = "YOUR_ACCESSKEY"; //replace with your access key

const GRID_HOST = "hub.lambdatest.com/wd/hub";
const gridUrl = "https://" + USERNAME + ":" + KEY + "@" + GRID_HOST;

const { Builder, Key, By } = require("selenium-webdriver");

async function example() {
  try {
    var capabilities = {
      build: "Drag and Drop",
      name: "Drag and Drop",
      platform: "Windows 10",
      browserName: "Chrome",
      version: "91.0",
      selenium_version: "4.0.0",
      chrome.driver: "91.0",
    };

    //To wait for browser to build and launch properly
    var driver = await new Builder()
      .usingServer(gridUrl)
      .withCapabilities(capabilities)
      .build();

    //To launch the website
    await driver.get(
      "[https://www.lambdatest.com/selenium-playground/drag-and-drop-demo](https://www.lambdatest.com/selenium-playground/drag-and-drop-demo)"
    );

    //To locate the source and target elements
    let draggable = driver.findElement(By.id("draggable"));
    let droppable = driver.findElement(By.id("droppable"));

    //To perform drag and drop operation
    await driver.actions().dragAndDrop(draggable, droppable).perform();

    //To verify drop success/fail by validating the text inside target element
    let targetText = await droppable.getText();
    if (targetText === "Dropped!") {
      console.log("PASS: Source is dropped at location");
    } else {
      console.log("FAIL: Source is not dropped at location");
    }

    //It is always a safe practice to quit the browser after execution
    await driver.quit();

  } catch (err) {
    handleFailure(err, driver);
  }
}

example();

function handleFailure(err, driver) {
  console.error("Something went wrong!\n", err.stack, "\n");
  driver.quit();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Additionally, I have added a try-catch block for error handling. Also have provided the function call to execute the method on run command.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; Execute the test. Make sure you are in the right folder and give the following command in the terminal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node test.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;strong&gt;test.js&lt;/strong&gt; file starts executing, and you can observe your live test running status on your LambdaTest Dashboard along with a detailed report containing video and logs.&lt;/p&gt;

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

&lt;p&gt;LambdaTest helps you track the state of your automated tests by showing a clear view of the tests that have passed, failed, and are still in progress. You can go through &lt;a href="https://analytics.lambdatest.com/test-overview?utm_source=devto&amp;amp;utm_medium=Organic&amp;amp;utm_campaign=apr13_rd&amp;amp;utm_term=rd&amp;amp;utm_content=webpage"&gt;LambdaTest Analytics&lt;/a&gt; to help you gain insight into your automated testing efforts.&lt;/p&gt;

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

&lt;p&gt;Similarly, you can run your test case in multiple browsers by defining the appropriate capabilities in your test file.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Firefox browser&lt;/p&gt;

&lt;p&gt;var capabilities = {&lt;br&gt;
      build: "Drag and Drop",&lt;br&gt;
      name: "Drag and Drop",&lt;br&gt;
      platform : "Windows 10",&lt;br&gt;
      browserName : "Firefox",&lt;br&gt;
      version : "86.0",&lt;br&gt;
      selenium_version : "4.1.0",&lt;br&gt;
      driver_version : "v0.36.0"&lt;br&gt;
    };&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Safari browser&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;var capabilities = {&lt;br&gt;
      build: "Drag and Drop",&lt;br&gt;
      name: "Drag and Drop",&lt;br&gt;
      platform: "MacOS Monterey",&lt;br&gt;
      browserName: "Safari",&lt;br&gt;
      version: "15.0",&lt;br&gt;
      selenium_version: "4.0.0",&lt;br&gt;
      driver_version: "v0.36.0",&lt;br&gt;
    };&lt;/p&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C-geXst1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2750/1%2AusrliixCb-jZq5OJNbWOXA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C-geXst1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2750/1%2AusrliixCb-jZq5OJNbWOXA.png" alt="" width="880" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using dragAndDrop() by passing X and Y offsets&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the above section of this article on how to implement drag and drop in JavaScript, we passed the source and target elements to drag and drop. Now, let us see the code to automate drag and drop by passing x and y offset values.&lt;/p&gt;

&lt;p&gt;The updated code for the test method is as below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var driver = await new Builder()
      .usingServer(gridUrl)
      .withCapabilities(capabilities)
      .build();

    //To launch the website
    await driver.get(
      "[https://www.lambdatest.com/selenium-playground/drag-and-drop-demo](https://www.lambdatest.com/selenium-playground/drag-and-drop-demo)"
    );

    //To locate the source elements 
    let draggable = driver.findElement(By.id("draggable"));

    //To perform drag and drop by offset
    await driver.actions().dragAndDrop(draggable, { x: 50, y: 50 }).perform();

    await driver.quit();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can also go through the Selenium JavaScript Tutorial to dive deep into how Selenium WebDriver with JavaScript can be used for automated testing of web applications.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/BQ-9e13kJ58"&gt;
&lt;/iframe&gt;
&lt;br&gt;
Note: Using JSON.stringify() and &lt;a href="https://www.lambdatest.com/web-technologies/json?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr13_rd&amp;amp;utm_term=rd&amp;amp;utm_content=web_technologies"&gt;**JSON.parse&lt;/a&gt;**(), you can convert JavaScript objects to JSON strings and JSON strings back to JavaScript objects.&lt;/p&gt;

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

&lt;p&gt;Selenium WebDriver provides Actions class, which can be leveraged to automate complex user interactions in the browser like Drag and Drop. In this JavaScript drag and drop tutorial, we explored what is Actions class in Selenium is and how to implement drag and drop in JavaScript while performing Selenium automation testing. Hoping this blog turned out to be beneficial.&lt;/p&gt;

&lt;p&gt;Happy Testing!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>selenium</category>
      <category>automation</category>
    </item>
    <item>
      <title>Jest vs Mocha vs Jasmine: Comparing The Top 3 JavaScript Testing Frameworks</title>
      <dc:creator>HaritaLT</dc:creator>
      <pubDate>Tue, 12 Apr 2022 13:32:16 +0000</pubDate>
      <link>https://forem.com/haritalt/jest-vs-mocha-vs-jasmine-comparing-the-top-3-javascript-testing-frameworks-422f</link>
      <guid>https://forem.com/haritalt/jest-vs-mocha-vs-jasmine-comparing-the-top-3-javascript-testing-frameworks-422f</guid>
      <description>&lt;p&gt;With the widespread adoption of &lt;a href="https://www.lambdatest.com/blog/enabling-agile-software-testing-with-automation/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_rd&amp;amp;utm_term=rd&amp;amp;utm_content=blog"&gt;agile methodologies&lt;/a&gt;, teams are now required to release more frequently and within shorter time frames. This requires not only shorter development cycles but also higher quality applications. With this approach, there is no immediate need for manual testing once the path is set for &lt;a href="https://www.lambdatest.com/learning-hub/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_rd&amp;amp;utm_term=rd&amp;amp;utm_content=webpage"&gt;automation testing&lt;/a&gt;. But with an increased pace of change, companies are requiring greater automation testing coverage along with sticking to traditional &lt;a href="https://www.lambdatest.com/blog/regression-testing-what-is-and-how-to-do-it/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_rd&amp;amp;utm_term=rd&amp;amp;utm_content=blog"&gt;regression testing&lt;/a&gt; techniques.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aNtJ4Pnq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Ai47VWDZ7NiVkl_O4Yj077Q.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aNtJ4Pnq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2Ai47VWDZ7NiVkl_O4Yj077Q.gif" alt="" width="480" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By utilizing a comprehensive approach to automation testing, we can ensure that the software product is robust and meets the desired threshold of quality. Given the wide range of programming languages and test automation frameworks available in the market, it is important to identify the right set of languages and corresponding frameworks for the success of your test automation strategy.&lt;/p&gt;

&lt;p&gt;JavaScript is a programming language that needs no introduction! JavaScript has been around for almost two decades, and it continues to be used by millions of developers around the world.&lt;/p&gt;

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

&lt;p&gt;As per &lt;a href="https://insights.stackoverflow.com/survey/2021#overview"&gt;Stack Overflow Developer Survey 2021&lt;/a&gt;, JavaScript is the most popular language among all the programming, scripting, and markup languages.&lt;/p&gt;

&lt;p&gt;When it comes to JavaScript testing, you could choose from a wide range of test automation frameworks to test your features at scale. In case you are new to JavaScript testing or confused in choosing the best-suited test automation framework, we can help you make the right choice!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XDng3nLs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2792/1%2Au4DOShX7kUkxNJH8O0GXwQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XDng3nLs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2792/1%2Au4DOShX7kUkxNJH8O0GXwQ.jpeg" alt="" width="880" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this blog on top JavaScript testing frameworks comparison, we do a detailed analysis of three of the top JavaScript testing frameworks as per the &lt;a href="https://2020.stateofjs.com/en-US/technologies/testing/"&gt;State of JS 2020 survey&lt;/a&gt; — Jest vs Mocha vs Jasmine. But before that, we shall also discuss why unit testing is important. So without further ado, let’s get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we need Unit Testing?
&lt;/h2&gt;

&lt;p&gt;A unit is the smallest piece of code that can be logically isolated in the system. Unit testing means testing these independent or individual units. Unit testing is often associated as a part of the programming phase, i.e., developer-based testing. But why should developers write tests? Let’s understand the benefits.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Early detection of bugs in the development phase itself, i.e., even before the QA phase. This, in turn, makes the testing phase easier&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When a developer further develops the code base, unit tests help ensure that the new code doesn’t break the existing old features. This will boost the developer’s confidence in maintaining the code quality&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easy to make code changes aided by the instant feedback loop&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Saves time and money in the long run&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unit testing might seem like a time-consuming process that can slow down the development process but yields awesome results in the long run. With the growing popularity of JavaScript as the &lt;a href="https://www.lambdatest.com/blog/top-11-front-end-development-trends-in-2021/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_rd&amp;amp;utm_term=rd&amp;amp;utm_content=blog"&gt;top front-end development trend&lt;/a&gt;, frameworks like React, Angular, and Vue have become even more vital for testing your client code.&lt;/p&gt;

&lt;p&gt;Now that you know why we need unit testing, let’s explore the most popular JavaScript unit testing frameworks in the market, i.e, Jest vs Mocha vs Jasmine.&lt;/p&gt;

&lt;p&gt;Note: With the next generation JavaScript language specification, &lt;a href="https://www.lambdatest.com/web-technologies/es6?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_rd&amp;amp;utm_term=rd&amp;amp;utm_content=web_technologies"&gt;ECMAScript 2015 (ES6)&lt;/a&gt; brings support for modules, classes, template string literals, arrow functions, let and const variables, and default parameters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Jest Framework for Selenium JavaScript Testing
&lt;/h2&gt;

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

&lt;p&gt;Developed by Facebook, Jest is one of the top JavaScript testing frameworks for test automation, focusing on simplicity. Jest was created to test JavaScript implementation of web applications primarily developed using React. Apart from this, Jest also seamlessly supports unit testing in Angular, Vue, NodeJs, etc., and can work with projects using TypeScript, Babel, and more.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f7L1TDmY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2270/1%2AX6ZBid_qqBW9Eu3VDo9_tg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f7L1TDmY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2270/1%2AX6ZBid_qqBW9Eu3VDo9_tg.jpeg" alt="" width="880" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Jest was rated as the most popular and top JavaScript framework for unit testing in 2020. What makes Jest special is the out-of-the-box experience it provides through zero configuration. In addition, Jest is available as an npm package that you can install to your JavaScript project using the following command:&lt;/p&gt;

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

&lt;p&gt;Now let us examine the pros and cons of Jest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use Jest for Selenium JavaScript Testing?
&lt;/h2&gt;

&lt;p&gt;In the world of &lt;a href="https://www.lambdatest.com/blog/automation-testing-with-selenium-javascript/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_rd&amp;amp;utm_term=rd&amp;amp;utm_content=blog"&gt;Selenium JavaScript testing&lt;/a&gt;, Jest is becoming the most preferred automation framework. It provides a reliable and simple way to write tests, and developers love it because of its flexibility.&lt;/p&gt;

&lt;p&gt;Below are some killer points that give Jest an edge over other top JavaScript Testing frameworks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The key benefit of using Jest is that it is very easy to install with zero setup hiccups. In addition, Jest provides you with an integrated framework that is ready to use, aiming for an out-of-the-box experience.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is maintained and well documented, making it easy to learn, especially for developers new to the TDD (Test Driven Development) approach.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Jest is a fast-performing test automation framework where isolated tests are executed by performing &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=apr12_rd&amp;amp;utm_term=rd&amp;amp;utm_content=blog"&gt;parallel testing&lt;/a&gt; in their processes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Jest provides testing support for asynchronous code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Jest framework supports execution of Smart Visual Regression Tests by capturing screenshots (i.e. Snapshot testing). During development using ReactJS, this particular feature comes in handy for detecting UI bugs. This is done by recording the screenshot of a rendered component and then comparing it with the component rendered in the future test run. Also, when a new feature is added, the screenshots can be easily updated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Auto mocking is another out-of-the-box feature of Jest. Jest can easily mock any dependency imported in the module that needs to be tested in the future.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Jest library provides pre-commit hooks that allow running only relevant tests or running tests that have changed after the last commit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The code mods module offers many ways to integrate your current projects to sync with the latest framework seamlessly.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other than the ones listed above, here are some more liked aspects of Jest, making it a likeable product for developers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8x5s-ZnY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2940/1%2As5aK48jDDpzfgAsQJZl6Uw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8x5s-ZnY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2940/1%2As5aK48jDDpzfgAsQJZl6Uw.jpeg" alt="" width="880" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Who uses Jest?
&lt;/h2&gt;

&lt;p&gt;Prominent companies that reportedly adopt Jest in their tech stack are:&lt;br&gt;
Facebook, Airbnb, Spotify, The New York Times, Travel Perk, Twitter, Instagram, etc.&lt;/p&gt;
&lt;h2&gt;
  
  
  Cons of Jest
&lt;/h2&gt;

&lt;p&gt;Jest has proven to be a powerful tool to do almost anything. However, while Jest has several advantages, it also has a few disadvantages. For example, it is an expensive framework, and at times its debugging capabilities need improvement. Below are a few cons of Jest:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Compared to Jasmine and other frameworks, not many libraries and toolings are supported by Jest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;People who are not comfortable with the Jest framework have asserted that the learning curve is pretty hard.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Leveraging auto mocking can make tests slow. This is because the more dependencies a module has, the more work needs to be done by Jest to mock it, which comes at the cost of performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Snapshot testing with Jest is not so feasible for larger snapshot files containing thousands of lines.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other than the ones listed above, here are some of the disliked aspects of Jest among developers who picked “used it and would not use it again.”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qFwnkXTM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2948/1%2A6yzcl3NRLPGKt8BEG0vtYQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qFwnkXTM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2948/1%2A6yzcl3NRLPGKt8BEG0vtYQ.jpeg" alt="" width="880" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: &lt;a href="https://www.lambdatest.com/web-technologies/es6-generators?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_rd&amp;amp;utm_term=rd&amp;amp;utm_content=web_technologies"&gt;ES6 Generators&lt;/a&gt; have a number of applications but mostly they can be used to control the iteration behavior of a loop.&lt;/p&gt;
&lt;h2&gt;
  
  
  Mocha Framework for Selenium JavaScript Testing
&lt;/h2&gt;

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

&lt;p&gt;Mocha is another top JavaScript testing framework that is designed for testing apps running in Node.js. It supports various types of testing such as unit, integration, and end-to-end testing. According to the State of JS 2020 survey, Mocha’s popularity has been consistent among the top JavaScript testing frameworks over the past few years.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z3CWMFCN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2336/1%2AK30Atj10whVbglEF5pgo0A.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z3CWMFCN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2336/1%2AK30Atj10whVbglEF5pgo0A.jpeg" alt="" width="880" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is quite easy to install the Mocha framework. All you require is a workstation and you can install Mocha using the NPM commands.&lt;/p&gt;

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

&lt;p&gt;Let us examine the pros and cons of Mocha.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why use Mocha for Selenium JavaScript Testing?
&lt;/h2&gt;

&lt;p&gt;Mocha is a feature-packed JavaScript testing framework that helps you write effective, maintainable tests at a quicker pace. It supports Node.js &amp;amp; browser agnostic and also supports asynchronous testing. By running your tests serially, Mocha allows you to be more flexible and precise with your reporting. Moreover, it maps uncaught exceptions to the correct test cases using stack traces.&lt;/p&gt;

&lt;p&gt;Mocha offers a programmatic way to write code for performing Selenium automation testing. It categorizes the tests into suites and test cases for execution, producing error reports after the run by mapping errors to corresponding test cases.&lt;/p&gt;

&lt;p&gt;So, if you’re curious about the world of Mocha, this video will help you get started. You’ll learn how to write and run your first test case in Mocha and much more.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/hUDQOcabs0Y"&gt;
&lt;/iframe&gt;
&lt;br&gt;
However, you can follow the LambdaTest YouTube Channel by scanning the below QR code and stay up to date with more such videos.&lt;/p&gt;

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

&lt;p&gt;Below are some points that favor Mocha as one of the top JavaScript testing frameworks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Clear and simple API. The Mocha API can be considered similar to Jasmine but coated with a bit of syntactic sugar to make it more conducive to a wider range of scenarios like BDD.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mocha is highly extensible and flexible. Unlike Jest, Mocha doesn’t come with the mocking, assertion, or stubbing library. This means that the developer can select any library or framework. One popular choice is Chai and Sinon for mocking. Cypress, the popular test automation framework, also leverages the Chai library. You can find more details about it in the Cypress Architecture blog that deep dives into the architectural aspects of the framework.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In Mocha, tests are run serially. This allows flexible and accurate reporting while mapping uncaught exceptions to the correct test cases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mocha is a library of functions for testing your code in different &lt;a href="https://www.lambdatest.com/virtual-browsers?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_rd&amp;amp;utm_term=rd&amp;amp;utm_content=webpage"&gt;online browsers&lt;/a&gt;. It is easy to use and makes running code on multiple browsers a breeze while giving you all the flexibility you need to test on each browser separately or in groups.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The reporting tool includes various report types, namely — list, progress bar, output file, JSON. The default reporter displays results hierarchically based on the structure of test cases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mocha supports both Behavior Driven Development (BDD) and Test Driven Development (TDD), allowing writing high-quality tests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since Mocha is mature with widespread adoption, lots of tutorials and documents are available online. In addition to being open-source, Mocha is backed by active support from the Mocha Dev community.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It can easily support generators.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Support for both synchronous and asynchronous testing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supports Headless running out of the box.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Many CI servers support Mocha.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other than the ones listed above, here are some more liked aspects of Mocha among developers who picked “used it and would use again.”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j6VqwCbr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2970/1%2ABtxFZULlmfE0E5i1Egms3g.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j6VqwCbr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2970/1%2ABtxFZULlmfE0E5i1Egms3g.jpeg" alt="" width="880" height="451"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Who uses Mocha?
&lt;/h2&gt;

&lt;p&gt;Prominent companies that reportedly adopt Mocha in their tech stack are:&lt;br&gt;
Accenture, Coursera, Netifly, Oyo rooms, Codefirst, Asana, Wix, Yahoo, etc.&lt;/p&gt;
&lt;h2&gt;
  
  
  Cons of Mocha
&lt;/h2&gt;

&lt;p&gt;Although Mocha has proven to be a powerful tool in most cases, some areas still need improvement. The following list mentions some of the major cons of using Mocha:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The introduction of Jest has significantly diminished the popularity of Mocha.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unlike Jest, which works as a single and independent framework, Mocha requires more configuration, making it weaker.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Even though it can boast of having a vast ecosystem, it is complicated as well.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In Mocha, auto-mocking and snapshot testing are not easy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Developer tooling and more onboard features can help Mocha perform better.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other than the ones listed above, here are some of the disliked aspects of Mocha among developers who picked “used it and would not use it again.”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GRdTsNHr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2928/1%2AF0oHDWpHU3EuxXJTt9CDVg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GRdTsNHr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2928/1%2AF0oHDWpHU3EuxXJTt9CDVg.jpeg" alt="" width="880" height="459"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Jasmine Framework for Selenium JavaScript Testing
&lt;/h2&gt;

&lt;p&gt;Jasmine is a feature-rich and top JavaScript testing framework widely used by developers, especially those who need to test Angular applications. In most scenarios, the application runs on Node.js and requires a runner such as Karma or Chutzpah, but some distros (like the jasmine-node npm) have built-in the package. According to the &lt;a href="https://2020.stateofjs.com/en-US/technologies/testing/"&gt;State of JS 2020 survey&lt;/a&gt;, Jasmine has been consistently performed well when it comes to ranking of top JavaScript testing frameworks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6tAbLcqt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2272/1%2AncB5sYobizUQOgyBPMU6KA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6tAbLcqt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2272/1%2AncB5sYobizUQOgyBPMU6KA.jpeg" alt="" width="880" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Installation of Jasmine is very simple. All you require is a workstation with internet connectivity and you can install Jasmine using the NPM commands.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1F1o8sl3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ANJoSZJCWoJ-69ch8A-txHg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1F1o8sl3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ANJoSZJCWoJ-69ch8A-txHg.png" alt="" width="340" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, compared to Jest, the configuration of Jasmine is a bit complex. Before running test cases, the test.js file needs to be set up and configured.&lt;/p&gt;

&lt;p&gt;Let us examine the pros and cons of Jasmine.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why use Jasmine for Selenium JavaScript Testing?
&lt;/h2&gt;

&lt;p&gt;Jasmine and Selenium are two popular tools for JavaScript and web UI automation testing, respectively. By combining their force, you can take advantage of both of them to improve the quality of your software.&lt;/p&gt;

&lt;p&gt;In this section of the JavaScript testing framework comparison between Jest vs Mocha vs Jasmine, we look into the advantages of the Selenium automation testing with Jasmine from an automation testing standpoint.&lt;/p&gt;

&lt;p&gt;Below are some points that edge Jasmine as one of the top JavaScript testing frameworks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Jasmine is compatible with almost every framework or library of your choice, making it one of the most flexible JavaScript testing frameworks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Jasmine provides a rich set of built-in matchers that can match expectations and add asserts to the test cases. For example, we can use toEqual, toBe, toBeTruthy, toBeFalsy, and more.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unlike other JavaScript testing frameworks, Jasmine has built-in assertion methods that help in improving the readability of the tests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Jasmine BDD library makes it easy to define tests, run them, and integrate them with JSHint, all without tying tests to a specific test runner.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Jasmine does not rely on any JavaScript framework, DOM, or browsers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It offers clean and polished syntax.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supports headless running out of the box.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Various CI servers support the Jasmine framework.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since it has been in the market for a long time, Jasmine is supported by an active community, and lots of tutorial materials are available online. This can make your learning curve smooth.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As of writing this blog, there are close to 13,000 questions that are tagged Jasmine on Stack Overflow.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t2Ehj-89--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3844/1%2Aj87bQ6OiM0D7Dz2srf791w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t2Ehj-89--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3844/1%2Aj87bQ6OiM0D7Dz2srf791w.png" alt="" width="880" height="549"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Other than the ones listed above, here are some more liked aspects of Jasmine among developers who picked “used it and would use again.”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hrkPCmCa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2944/1%2Alp26bT03O7TXQNAynm4DuQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hrkPCmCa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2944/1%2Alp26bT03O7TXQNAynm4DuQ.jpeg" alt="" width="880" height="462"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Who uses Jasmine?
&lt;/h2&gt;

&lt;p&gt;Prominent companies that reportedly adopt Jasmine in their tech stack are:&lt;br&gt;
Accenture, Walmart, Typeform, GitLab, Uniqlo, etc.&lt;/p&gt;
&lt;h2&gt;
  
  
  Cons of Jasmine
&lt;/h2&gt;

&lt;p&gt;Although Jasmine is powerful, they are not without their weaknesses. The following list mentions some of the major cons of Jasmine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Though the installation is easy, the setup is a bit complex. Users must select an assertion or mocking library before using it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supports snapshot testing by using the jasmine-snapshot library. However, the integration isn’t that easy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Asynchronous testing is difficult to achieve with Jasmine.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Expect a specific suffix to all test files (*.spec.js by default).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other than the ones listed above, here are some of the disliked aspects of Jasmine among developers who picked “used it and would not use it again.”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---qlFNt00--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2956/1%2AIKEs0Ch8R2Oj9QBpra7iFg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---qlFNt00--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2956/1%2AIKEs0Ch8R2Oj9QBpra7iFg.jpeg" alt="" width="880" height="455"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Jest vs Mocha vs Jasmine — Choosing the right framework
&lt;/h2&gt;

&lt;p&gt;We have evaluated the pros and cons of the three frameworks. But still, confused about which is the ideal framework for you?&lt;/p&gt;

&lt;p&gt;When it comes to selecting between frameworks, honestly, it is not that easy. We cannot claim that one framework is better than the other as each of these frameworks offers a unique set of features with a different approach in coding style. So finally, it all comes down to your project requirements.&lt;/p&gt;

&lt;p&gt;In this section of the Jest vs Mocha vs Jasmine comparison, we will shortlist some of the general criteria widely embraced by project teams to choose the right test automation framework.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/96n2b78CWrM"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For React or Next.js apps, it is advisable to try out Jest as it is the default option. At present, Facebook has been investing heavily in further improving this framework. Also, React developers are recording a positive experience after using Jest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the project requires debugging the test cases in an IDE that Jest does not support, then the next best choice would be Jasmine.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If your project is quite big and requires the integration of various external libraries, Jasmine would be a better choice. On the other hand, Jest would be more suitable for smaller projects as it provides a single integrated eco-space with pre-configured features like the test runner, assertion library, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When it comes to large Node.js projects, Mocha could be the way to go. Mocha is significantly more flexible and comes with a test runner, but you have to piece it yourself.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the Angular world, Jasmine is the recommended testing framework. This is because Angular CLI, by default, comes with Jasmine and Karma as the test runner. However, if there is a future requirement, migrating from Jasmine to Jest is easy.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The graph below compares the trend in NPM downloads of the three top JavaScript testing frameworks, i.e., Jest vs Mocha vs Jasmine, in the past one year.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mtpZc2Am--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3318/1%2Ap2jTr51znvQOZOSfluKohw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mtpZc2Am--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3318/1%2Ap2jTr51znvQOZOSfluKohw.jpeg" alt="" width="880" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is clear that currently, the popularity of Jest as a faster-performing framework with the least configuration requirement has surely made a significant impact in the automation testing domain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running your JavaScript tests on an online Selenium Grid
&lt;/h2&gt;

&lt;p&gt;As the development of web applications is becoming more dynamic, developers are expected to roll out an upgraded version of the application in shorter release cycles. As the testing needs to go hand in hand with the development velocity, it is natural for developers to go after tools that can make their job quicker, reliable, efficient, and at the same time, cost-effective.&lt;/p&gt;

&lt;p&gt;LambdaTest is one such tool. It is a scalable cloud-based cross-browser testing platform that enables you to run your selenium automation scripts on 2000+ different &lt;a href="https://www.lambdatest.com/virtual-browsers?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_rd&amp;amp;utm_term=rd&amp;amp;utm_content=webpage"&gt;virtual browsers&lt;/a&gt; and operating systems on an online Selenium Grid. So you can now run your test cases of JavaScript automation testing on a scalable Selenium infrastructure with ease.&lt;/p&gt;

&lt;p&gt;LambaTest now integrates with popular test automation frameworks to boost your go-to-market delivery. The good news is that whatever your test framework — Jest, Mocha, Jasmine, Protractor, Karma, etc. — LambdaTest got it all covered! Here’s a list of LambdaTest integrations. The platform provides you the infrastructure to execute your automation tests and ensure your development code renders seamlessly through an on-cloud Selenium Grid. Additionally, through parallel testing, you can drastically trim down your testing cycles.&lt;/p&gt;

&lt;p&gt;Sounds too good to be true, right? And all these benefits can be unlocked through a few simple configuration changes in your test automation framework. So do check out the LambdaTest platform to fast-track your automation testing today!&lt;/p&gt;

&lt;p&gt;Note: &lt;a href="https://www.lambdatest.com/web-technologies/js-regexp-lookbehind?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=apr12_rd&amp;amp;utm_term=rd&amp;amp;utm_content=web_technologies"&gt;Lookbehind in JS regular expressions&lt;/a&gt;, the positive lookbehind (&lt;code&gt;(?&amp;lt;= )&lt;/code&gt;) and negative lookbehind (&lt;code&gt;(?&amp;lt;! )&lt;/code&gt;) zero-width assertions can be used to confirm that one pattern is followed by another.&lt;/p&gt;

&lt;h2&gt;
  
  
  Jest vs Mocha vs Jasmine: The Final Showdown
&lt;/h2&gt;

&lt;p&gt;Unit testing undeniably contributes to the quality of the application under development by adding an extra layer of quality check before the code gets passed to the QA team while helping the developer to write efficient code by providing instant feedback. When it comes to top JavaScript Testing frameworks, Jest, Mocha, and Jasmine are the most popular ones.&lt;/p&gt;

&lt;p&gt;In this article, we did a quick rundown and evaluated each of these frameworks, i.e., Jest vs Mocha vs Jasmine, to help you analyze and choose the ideal one matching your project needs. We also discussed how cloud testing platforms like LambdaTest could help in optimizing the testing cycle by providing added benefits of faster execution, scalability, cost-effectiveness, etc. Hoping that this article turned out to be beneficial. Now execute free JavaScript automation testing online!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>jest</category>
      <category>mocha</category>
      <category>jasmine</category>
    </item>
  </channel>
</rss>
