Thursday 20 March 2014

Page Object Pattern | Selenium - Part I


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);
}
}

18 comments:

  1. 매우 유익한 게시물입니다. 성공적인 소셜 네트워킹 캠페인을 시작하는 데 도움이되는 많은 정보가 여기에 있습니다. 먹튀검증

    ReplyDelete
  2. Awesome and interesting article. Great things you've always shared with us. Thanks. Just continue composing this kind of post. 먹튀사이트

    ReplyDelete
  3. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more. 먹튀검증사이트

    ReplyDelete
  4. i was just browsing along and came upon your blog. just wanted to say good blog and this article really helped me 안전놀이터

    ReplyDelete
  5. I can suggest essentially not too bad and even dependable tips, accordingly see it:  buy traffic

    ReplyDelete
  6. Impressive web site, Distinguished feedback that I can tackle. Im moving forward and may apply to my current job as a  pet sitter, which is very enjoyable, but I need to additional  expand. Regards 토토

    ReplyDelete
  7. I just want to let you know that I just check out your site and I find it very interesting and informative. 먹튀폴리스

    ReplyDelete
  8. You have done a amazing job with you website 사설토토

    ReplyDelete
  9. This is very useful, although it will be important to help simply click that web page link: seo tijuana

    ReplyDelete
  10. Thank you for the update, very nice site. auto glass san diego

    ReplyDelete
  11. Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign ktm motorcycles

    ReplyDelete
  12. I’ve found every article I’ve read very helpful. Good one, and keep it going. 안전놀이터

    ReplyDelete
  13. Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates Buy Klonopin Online

    ReplyDelete
  14. I have a hard time describing my thoughts on content, but I really felt I should here. Your article is really great. I like the way you wrote this information. voyance

    ReplyDelete
  15. Thankyou for this wondrous post, I am glad I observed this website on yahoo. online marketing agencies in the world

    ReplyDelete
  16. I’ve loaded your blog in 3 different browsers and
    I must say this blog loads a lot quicker then most.
    Can you suggest a good internet hosting provider at a reasonable price?
    Many thanks, I appreciate it!

    Also visit my web page ::부달


    ReplyDelete
  17. Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get
    there! Thank you
    Also visit my site:부달

    ReplyDelete