How to run your test using selenium and TestNG-Intellij IDEA

Chalitha
2 min readJan 13, 2021

--

Why we use selenium with TestNG?

These two are very powerful automation tools which can make our lives easier while writing testcases. But default selenium is not providing user friendly reports for test. Using selenium with TestNG, we can generate more readable test results.

How we can start?

  1. As the first step, create a maven project in Intellij

File->New-> project->maven

maven project window

2. click on Next button and enter a project name,location,GroupId,Artifactid and Version.

3. Click on finish button and now you have a newly created maven project.

4. Add dependencies related to selenium and TestNG inside the newly created pom.xml

pom.xml with dependencies
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>

How we can write test?

Example :Let’s assume we want to write a test case for login to an web application by a particular user.

  • user enter a valid ID
  • user enter a valid password
  • user logged in to the web application
  • Verify whether user logged in valid url
  1. Create a new Java class and add your test methods. Here is the sample I prepared.
public class TestNg {
public String baseurl="http://www.demo.guru99.com/V4/";
public WebDriver driver;
String driverpath="/home/chali/Downloads/geckodriver";
@BeforeTest
public void LaunchBrowser()
{
System.out.println("Launching the Web application");
System.setProperty("webdriver.gecko.driver",driverpath);
driver=new FirefoxDriver();
driver.get(baseurl);
}
@BeforeMethod
public void loginHomepage()
{
driver.findElement(By.name("uid")).sendKeys("****");
driver.findElement(By.name("password")).sendKeys("****");
driver.findElement(By.name("btnLogin")).click();
}
@Test
public void verifyHomepage()
{
String ActualURL=driver.getCurrentUrl();
String ExpectedURL="http://www.demo.guru99.com/V4/manager/Managerhomepage.php";
Assert.assertEquals(ActualURL,ExpectedURL);
}

@AfterTest
public void closeBrowser()
{
driver.close();
}
}

Sample Output of test execution

--

--