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

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

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

<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

--

--

Software Quality Assurance Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store