Skip to main content

Page Object in selenium with example

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




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

Popular posts from this blog

Rational Functional Tester: Calling RFT scripts from using xml tags

Hello, This will help to call RFT scripts in xml, if anyone using 'ant' this will help them to embed code to invoke RFT scripts. <java classname="com.rational.test.ft.rational_ft" fork="true" maxmemory="1024m">         <classpath>         <fileset dir="C:\Program Files\IBM\SDP\FunctionalTester\bin\">         <include name="rational_ft.jar" />         </fileset>         </classpath>         <jvmarg line="-Drational_ft.install.dir=&quot;C:\Program Files\IBM\SDP\jdk_\jre\bin&quot;" />         <arg line="-rt.bring_up_logviewer false -datastore &quot;D:\RFT-Dev\Project&quot; -playback RFTTestSet5.testcase4.TestScript1" />         <arg line="-args -scriptArg ${scriptValue}" /> ...

Rational Functional Tester: Creating and Reading Datapool cell value in RFT

Hello, Hope this will help for my friends who is trying to use data pool for dynamic values.If you want to use any run time values in between scripts then you will be looking for the functions to read/write/create datapool cells dynamically. /************ Dp row count *************************/ IDatapoolIterator  ite; ite.dpCurrent().getEquivalenceClass().getRecordCount(); /***************** to set cell value **************/ IDatapoolIterator it; ((DatapoolCell) it.dpCurrent().getCell("RunTimeSheetForContainers")).setCellValue("Y"); /*********** to load CSV files (csv to Datapool)*******************************/ public IDatapoolIterator LoadCSV(String Sname)       {             File fname=getFileName(Sname);             IDatapool dp=DatapoolUtilities. loadCSV (fname, "," , true );  ...

How to install Gride 2.0 ?

In this section, you will use 2 machines. The first machine will be the system that will run the hub, while the other machine will run a node. For simplicity, let us call the machine where the hub runs as “Machine A” while the machine where the node runs will be “Machine B”. It is also important to note their IP addresses. Let us say that Machine A has an IP address of 192.168.1.3 while Machine B has an IP of 192.168.1.4. Step 1 Download the Selenium Server by   here .   Step 2 You can place the Selenium Server .jar file anywhere in your HardDrive.But for the purpose of this tutorial, place it on the C drive of both Machine A and Machine B. After doing this, you are now done installing Selenium Grid. The following steps will launch the hub and the node. Step 3 ·          * We are now going to launch a hub. Go to Machine A. Using the command prompt, navigate to the root of Machine A’s - C drive ,because that is the ...