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.
- 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.
- Right click on ‘com.selenium.testcase’ package inside ‘src’ folder and select TestNG -> Create TestNG Class as shown below.
- Name the TestNG class as ‘BookFlight’. Select the following annotations – ‘@BeforeMethod’, ‘@AfterMethod’, ‘@DataProvider’, ‘@BeforeTest’.
- 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"); } }
- 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.
- To run our TestNG script, right click on ‘BookFlight’ class and click Run As -> TestNG Test.
- Below is how our project structure looks like.
- 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….
- After successful execution, below result is displayed.
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!