Selenium with TestNG

TestNG is a testing framework like JUnit that provides some new functionalities. Now we will modify our previous project and integrate it with TestNG.

Run IntelIJ. Go to File > Open

Go to project location and select build.gradle

Now click on Open as Project

Then click on This Window or New Window

Now open build.gradle file and add this command to install testNG dependencies.

You will find gradle command line for your required package from this website.

https://mvnrepository.com/

Just simply type your package name and hit enter. It will show the command for maven/gradle etc

Now copy the command line and paste into build.gradle

Then click on import icon to install testNG

Now edit the BasePage.class file :
Replace Before annotation with BeforeClass. Previously was @Before as this is using JUnit. But @BeforeClass use TestNG.

Now right click on the Pages folder and create another class named SearchProduct. Inspect elements and add the following code.

package Pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;

public class SearchProduct {
    WebDriver driver;
    @FindBy(id = "search_query_top")
    WebElement txtSearch;
    @FindBy(name = "submit_search")
    WebElement btnSearch;
    @FindBy(xpath = "//span[@class='lighter']")
    WebElement lblSearchedItem;

    public SearchProduct(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }
    public void searchItem(String itemname) throws InterruptedException {
        txtSearch.sendKeys(itemname);
        Thread.sleep(2000);
        btnSearch.click();
        Assert.assertEquals(lblSearchedItem.getText().replace("\"", ""),itemname.toUpperCase());
    }
}

Now double click on TestCase.class and edit code like this:

package TestCase;

import BasePage.BasePage;
import Pages.Login;
import Pages.SearchProduct;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

public class TestCase extends BasePage{
    Login objLogin;
    SearchProduct objItem;
    @Test(priority = 1,enabled = true,description = "Check if login is successful")
    public void doLogin() throws Exception {
        driver.get("http://automationpractice.com");
        objLogin=new Login(driver);
        objLogin.doLogin("testuser0568@test.com","P@ssword123");

    }
    @Test(priority = 2,enabled = true,description = "Search by item name")
    public void searchItem() throws Exception {
        objItem=new SearchProduct(driver);
        objItem.searchItem("Dress");

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

}

Now right click on project name and click on New > File

Now give the filename as testng.xml

Add following code to the file

<suite thread-count="1" verbose="1" name="E-commerce portal automation">

    <test name="E-commerce portal automation">
        <classes>
            <class name="TestCase.TestCase"/>
        </classes>
    </test>
</suite>

Now add configuration as following image

Now edit configuration like this:

Click apply and then Ok

Now run the project. And you will find test result like below.

Now run this command:

gradle clean test

In my case I face a problem. It’s showing error!!

Troubleshooting

If you face any issues with testNG or could not run the program, then simply downgrade the testNG version. Now I will change testNG version.

replace 7.3.0 with 7.1.0

testCompile group: 'org.testng', name: 'testng', version: '7.1.0'

previous was:

testCompile group: 'org.testng', name: 'testng', version: '7.3.0'

Now again run the command:

gradle clean test

Now its running and showing build successful.

Next tutorial will show how to generate report.

about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *