Selenium Test Automation: How to build tesNG tests?

Selenium

BriefTitle: 
testNG tests

Step 1: Review first test case (positive test)

Step Action Expected Page Before Action Page After Action
1 Visit ExampleSite.com ExampleSite.com home page should display. Home Page
2 Click Login Login page should display. Home Page Log In Page
3 Enter valid user name User name should be entered. Log In Page Log In Page
4 Enter valid password Valid password should be entered. Log In Page Log In Page
5 Click login. User logged in. Home Page Profile Page
6 Verify correct page title is displayed. Verified Profile Page Profile Page
7 Verify correct user name is displayed. Verified. Profile Page Profile Page
8 Click Log Out User logged out the system. Profile Page Home Page

Step 2: Create first positive testng test

2.1 Create a package

Right click on src/test/java folder, select New > Package
Enter package name as com.examplesite
Click Finish

2.2 Create a class LogInTest

Right click on package com.examplesite, select New > Class
Enter class name as LogInTest
Click Finish

2.3 Create the first test

Add the following code to LoginTest.java file:

public class LoginTest extends BaseTest{
@Parameters({ "browser" , "ch" })
@SuppressWarnings("deprecation")
@Test
public void positiveLoginTest(){
 
  String expectedPageTitle = "Seeker Dashboard - Profile";
  String Expectedprofilename = "md";
 
  LogInPage logInPage = new LogInPage(driver);
  logInPage.OpenLoginPage();
  logInPage.fillupemailpw("reidmv1978@gmail.com", "Welcome123@");
  ProfilePage profilePage = logInPage.pushsignin();
  String ActualTitle = profilePage.GetTitle();
  Assert.assertTrue(expectedPageTitle.contains(ActualTitle), "not equal");
  System.out.println("Correct page title is displayed. expectedPageTitle " + expectedPageTitle +
    " profilePage" + ActualTitle);
  Assert.assertTrue(profilePage.isCorrectProfileLoaded(Expectedprofilename), " correct name not loadded");
 
  System.out.println("Correct profile is displayed. Expectedprofilename " + Expectedprofilename +
    " profilePage" + profilePage);
}

2.4 Save the first test

Do one of the following to save the test:

  • From the menu bar, select File > Save.
  • From the toolbar, click the Save button.

Step 3: Review second test case (negative test)

Step Action Expected Page Before Action Page After Action
1 Visit ExampleSite.com ExampleSite.com home page should display. Home Page
2 Click Login Login page should display. Home Page Log In Page
3 Enter valid user name User name should be entered. Log In Page Log In Page
4 Enter invalid password Invalid password should be entered. Log In Page Log In Page
5 Click login. User receives an error message and unable to login. Log In Page Log In Page
6 Verify correct error message "Invalid password" is displayed. Verified. Log In Page Log In Page

Step 4: Create negative testng test

4.1 Create the 2nd test

In the LoginTest.java file, add the following code after the first test method.

@Test
public void negativeLoginTest() {
 
  String expectedPageTitle = "Seeker Dashboard - Profile";
  String Expectedprofilename = "mohammed safiullah";
  //String ExpectederrorMessage = "Email and/or password incorrect";
  String ExpectederrorMessage = "Email and/or password incorrect.";
  String testno = testData.get("no");
  String email = testData.get("email");
  String password =testData.get("password");
  String desc =testData.get("description");
 
   System.out.println(testno + " email " + email + " pw " + password + " desc " + desc);   
  LogInPage logInPage = new LogInPage(driver);
  logInPage.OpenLoginPage();
  logInPage.fillupemailpw(email, password);
  logInPage.pushsignin();
  String ActualErrorMessage = logInPage.getLoginErrorMessage();
  Assert.assertTrue(ActualErrorMessage.contains(ExpectederrorMessage), "Expected message not found" + ActualErrorMessage + "exp" + ExpectederrorMessage);
}

4.2 Save the 2nd test

Do one of the following to save the test:

  • From the menu bar, select File > Save.
  • From the toolbar, click the Save button.

Step 5: Summary

We created two testNG tests in the LoginTest.java file which will extend BaseTest.java file.

Below is the complete code inside the LoginTest.java file.

public class LoginTest extends BaseTest{
@Parameters({ "browser" , "ch" })
@SuppressWarnings("deprecation")
@Test
public void positiveLoginTest(){
 
  String expectedPageTitle = "Seeker Dashboard - Profile";
  String Expectedprofilename = "md";
 
  LogInPage logInPage = new LogInPage(driver);
  logInPage.OpenLoginPage();
  logInPage.fillupemailpw("reidmv1978@gmail.com", "Welcome123@");
  ProfilePage profilePage = logInPage.pushsignin();
  String ActualTitle = profilePage.GetTitle();
  Assert.assertTrue(expectedPageTitle.contains(ActualTitle), "not equal");
  System.out.println("Correct page title is displayed. expectedPageTitle " + expectedPageTitle +
    " profilePage" + ActualTitle);
  Assert.assertTrue(profilePage.isCorrectProfileLoaded(Expectedprofilename), " correct name not loadded");
 
  System.out.println("Correct profile is displayed. Expectedprofilename " + Expectedprofilename +
    " profilePage" + profilePage);
}

@Test
public void negativeLoginTest() {
 
  String expectedPageTitle = "Seeker Dashboard - Profile";
  String Expectedprofilename = "mohammed safiullah";
  //String ExpectederrorMessage = "Email and/or password incorrect";
  String ExpectederrorMessage = "Email and/or password incorrect.";
  String testno = testData.get("no");
  String email = testData.get("email");
  String password =testData.get("password");
  String desc =testData.get("description");
 
   System.out.println(testno + " email " + email + " pw " + password + " desc " + desc);   
  LogInPage logInPage = new LogInPage(driver);
  logInPage.OpenLoginPage();
  logInPage.fillupemailpw(email, password);
  logInPage.pushsignin();
  String ActualErrorMessage = logInPage.getLoginErrorMessage();
  Assert.assertTrue(ActualErrorMessage.contains(ExpectederrorMessage), "Expected message not found" + ActualErrorMessage + "exp" + ExpectederrorMessage);
}

}

Sorting: 
14

Add new comment

Selenium