How to do UI testing with Selenium?

SeleniumAutomation TestingTesting Tools

We can do UI testing with Selenium webdriver. To achieve this, we have to follow the steps listed below which can be applied to any script developed to test the UI of the application −

Step1 − Object of Webdriver should be created. For example,

WebDriver driver = new ChromeDriver();

The above piece of code is used to create a webdriver instance and initiate the script execution in the Chrome browser.

Step2 − Launch the URL on which we want to perform the UI testing. For example,


The above piece of code shall launch the URL passed as a parameter to the get method.

Step3 − Identify webelement with the help of any of the locators like id, class, name, tagname, link text, partial link text, css or xpath. The method - findElement is used to identify the element with these locators. For example,

WebElement elm = driver.findElement(By.tagName("h4"));

The above code is used to identify an element with the help of the locator tagname.

Step4 − After the element has been located, perform an action on it like inputting a text, clicking, and so on. For example,


The above code is used to input a text on the element identified in Step3.

Step5 − Validate the impact on the webpage on performing the operation in Step4. For example,

Assert.assertEquals(s, "Selenium");

The above code is used to compare and verify if the actual value is equal to expected value - Selenium.

Step6 − Execute the test and record the result created with a test framework.

Step7 − Finish the test by quitting the webdriver session. For example,



Code Implementation

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;
public class NewTest {
   public void f() {
      System.setProperty("", "chromedriver");
      //webdriver instance
      WebDriver driver = new ChromeDriver();
      // implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //url launch
      //element identify
      WebElement elm = driver.findElement(By.tagName("input"));
      //perform action - input text
      String s = elm.getAttribute("value");
      //validate result with Assertion
      Assert.assertEquals(s, "Selenium");
      //quit browser


Updated on 29-Jun-2021 08:37:35