Automating Chrome Browser with Selenium and TestNG: Step-by-Step Guide

Automating Chrome Browser with Selenium and TestNG: Step-by-Step Guide person Vishnu Hari | Published: Sun Sep 2023

Selenium and TestNG combine seamlessly to empower efficient automated testing. Selenium offers powerful browser automation capabilities, while TestNG enhances test organization and reporting, resulting in reliable and comprehensive test suites.

Before we dive into the tutorial, let me make it clear: I'm working on a Windows 10 machine with OpenJDK, Eclipse IDE, and the TestNG plugin already set up. Plus, I've created a Maven project with all the TestNG and Selenium dependencies in place. With everything set up, you're now all set to embark on creating your very first Maven Project with Selenium and TestNG.

To open the Chrome browser using Selenium, you'll need to download the ChromeDriver that matches your installed Chrome version. You can easily download it from the provided link.

https://chromedriver.chromium.org/downloads

Here's the code to set up the ChromeDriver and perform a Signup Form Submit on TinkerBridge Test Automation Practice Page.

 

package SeleniumTest;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class browserTest {

    WebDriver driver;

    @BeforeMethod
    public void setUp() {

        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
        driver = new ChromeDriver();

    }

    @Test
    public void testGoogleSearch() {

        driver.get("https://www.tinkerbridge.com/automation-test-page");

        driver.findElement(By.id("s_username")).sendKeys("vishnu");
        driver.findElement(By.id("s_email")).sendKeys("vishnu@example.com");
        driver.findElement(By.id("s_password")).sendKeys("example");
        driver.findElement(By.id("sfrm_01")).click();

    }

    @AfterMethod
    public void tearDown() {

        if (driver != null) {
            driver.quit();
        }

    }

}