Part 2: Page Object Model…

We concluded our last post Part 1: Page Object Model… by creating Page Objects for our test case. In this post we will focus on the TestNG script creation inside ‘NewTour’. This TestNG script will use the Page Objects to complete the execution.

  1. Inside ‘NewTour’ project, right click ‘src’ folder and click New -> Package. Name the package as ‘com.selenium.testcase’ and click Finish to close the window. This is where we will place our test case logic.
  2. Right click on ‘com.selenium.testcase’ package inside ‘src’ folder and select TestNG -> Create TestNG Class as shown below.

    Create TestNG Class
  3. Name the TestNG class as ‘BookFlight’. Select the following annotations – ‘@BeforeMethod’, ‘@AfterMethod’, ‘@DataProvider’, ‘@BeforeTest’.
  4. Copy the below code and paste into ‘BookFlight’ class.
    package com.selenium.testcase;
    
    import org.testng.annotations.Test;
    import com.selenium.pages.HomePage;
    import org.testng.annotations.BeforeMethod;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.BeforeTest;
    
    public class BookFlight {
    
    	WebDriver driver;
    	HomePage homePage;
    
    	//Test Case
    	@Test(dataProvider = "newTourData")
    	public void bookFlight(String uname, String pwd, String departFrom, String departDate, String fname, String lname,
    			String ccNum) {
    		// Create an object of HomePage
    		homePage = new HomePage(driver);
            /*
             * Test case logic.
             * Notice the return type of methods
             * are objects to next page.
             * */
    		homePage.loginUser(uname, pwd)
    		.findFlights(departFrom, departDate)
    		.reserveFlight()
    		.bookFlight(fname, lname,
    				ccNum);
    
    	}
    
    	//Driver Initialization
    	@BeforeMethod
    	public void beforeMethod() {
    		// Initialize driver
    		driver = new ChromeDriver();
    		// Maximize window
    		driver.manage().window().maximize();
    		// Nvaigate to URL
    		driver.get("http://newtours.demoaut.com");
    	}
    
    	//Driver closure
    	@AfterMethod
    	public void afterMethod() {
    		// Close and quit the driver to close the Browser
    		driver.close();
    		driver.quit();
    	}
    
    	//Create test data
    	@DataProvider
    	public Object[][] newTourData() {
    		return new Object[][] { { "demo", "demo", "London", "7", "john", "Doe", "56465465" } };
    	}
    
    	//Setting System property
    	@BeforeTest
    	public void beforeTest() {
    		// Set System Property
    		System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/Chrome/chromedriver");
    	}
    }
    
  5. Here we are using ‘@BeforeTest’ method to set the system property, ‘@BeforeMethod’ to initialize the driver, ‘@Test’ method for test case logic, ‘@AfterMethod’ to close the driver and ‘@DataProvider’ for passing our test data. Notice that in ‘@Test’ method we are using Page Objects to access different web elements.
  6. To run our TestNG script, right click on ‘BookFlight’ class and click Run As -> TestNG Test.

    Run As TestNG Test
  7. Below is how our project structure looks like.

    Project Structure
  8. We can also use ‘testng.xml’ to execute our TestNG script however in this post we will directly execute our ‘BookFlight’ class. For more details on ‘testng.xml’, refer Part 3: Scripting using TestNG….
  9. After successful execution, below result is displayed.
    TestNG Result

    With this we came to an end to our second part of POM. In the next post we will concentrate on Page Factory, which is another interesting topic and it’s use in POM. Till then, Happy Learning!