Page Object Pattern is a good approach of implementing Automation tests.
It is a language neutral pattern for representing a complete page or
portion of a page in an Object Oriented manner. Pattern is a page
object, which encapsulates the behavior of the page in an Object
oriented manner. It need some programming skills too.
Why Page Object?
1| Maintenance
2| Readability of scripts
3| Separation of Concerns
Page Object Pattern | Page Factory
Lets start with an example using Google.com
SCENARIO
1| Open URL
2| Search google
3| Assert page title
package packagename;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TC {
private WebDriver driver;
public ASSERT Task;
@BeforeTest
public void setUp() throws Exception {
System.out.println("Instantiating Firefox Driver");
driver = new FirefoxDriver();
}
@Test
public void Test01() throws Exception {
URL url = PageFactory.initElements(driver,
URL.class);
url.geturl();
}
@Test
public void Test02() throws Exception {
Task = PageFactory.initElements(driver,
ASSERT.class);
Task.search();
}
@Test
public void Test03() throws Exception {
Task = PageFactory.initElements(driver,
ASSERT.class);
Task.assertsearch();
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
}
package packagename;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;
public class URL{
public WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
public URL(WebDriver driver) {
this.driver = driver;
driver.get("http://www.google.co.in");
}
public ASSERT geturl() {
System.out.println("URL opened successfully");
return PageFactory.initElements(driver, ASSERT.class);
}
}
package packagename;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
public class ASSERT {
public WebDriver driver;
public ASSERT(WebDriver driver) {
this.driver = driver;
//driver.get("https://seleniumworkz.wordpress.com/wp-admin/edit.php");
}
public void search() throws Exception{
// search google
driver.findElement(By.name("q")).sendKeys("Prashanth Sams");
driver.findElement(By.name("q")).submit();
Thread.sleep(4000);
}
public void assertsearch() throws Exception{
// assert google search
Boolean b = driver.getTitle().contains("Prashanth Sams");
System.out.println(b);
}
}
Why Page Object?
1| Maintenance
2| Readability of scripts
3| Separation of Concerns
Page Object Pattern | Page Factory
Lets start with an example using Google.com
SCENARIO
1| Open URL
2| Search google
3| Assert page title
TEST CLASS | TC.java
package packagename;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TC {
private WebDriver driver;
public ASSERT Task;
@BeforeTest
public void setUp() throws Exception {
System.out.println("Instantiating Firefox Driver");
driver = new FirefoxDriver();
}
@Test
public void Test01() throws Exception {
URL url = PageFactory.initElements(driver,
URL.class);
url.geturl();
}
@Test
public void Test02() throws Exception {
Task = PageFactory.initElements(driver,
ASSERT.class);
Task.search();
}
@Test
public void Test03() throws Exception {
Task = PageFactory.initElements(driver,
ASSERT.class);
Task.assertsearch();
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
}
Open Google | URL.java
package packagename;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;
public class URL{
public WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
public URL(WebDriver driver) {
this.driver = driver;
driver.get("http://www.google.co.in");
}
public ASSERT geturl() {
System.out.println("URL opened successfully");
return PageFactory.initElements(driver, ASSERT.class);
}
}
Search & Assert | last.java
package packagename;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
public class ASSERT {
public WebDriver driver;
public ASSERT(WebDriver driver) {
this.driver = driver;
//driver.get("https://seleniumworkz.wordpress.com/wp-admin/edit.php");
}
public void search() throws Exception{
// search google
driver.findElement(By.name("q")).sendKeys("Prashanth Sams");
driver.findElement(By.name("q")).submit();
Thread.sleep(4000);
}
public void assertsearch() throws Exception{
// assert google search
Boolean b = driver.getTitle().contains("Prashanth Sams");
System.out.println(b);
}
}
Comments
Post a Comment