Friday, 21 March 2014

Page Object Pattern | Selenium - Part II


Note|  Text in Blue [#PYTHON] and Orange [#JAVA] can be edited or mentioned important for the entire blog. All the posts are practically done by me.


PageFactory is used to support PageObject pattern with a Factory class inside the WebDriver's support library. NullPointerExceptions will be thrown if you are not using PageFactory.

In PageFactory, by default you don't need to specify the Locator type for Name and ID;  Instead initialize the weblements using the below simple format.

private WebElement gbqfq; // here, 'gbqfq' is a name
private WebElement q; // here, 'q' is an id

gbqfq.sendKeys("Prashanth Sams");
or|
q.sendKeys("Prashanth Sams");


Lets see with an example,

Google TC | GoogleTest.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 GoogleTest {
private WebDriver driver; 

@BeforeTest
public void setUp() throws Exception {
 driver = new FirefoxDriver();  
}
    
 @Test
 public void Test01() throws Exception{
             GoogleSearch page = PageFactory.initElements(driver, GoogleSearch.class);
             page.searchFor("Prashanth Sams");
    }
 
@AfterTest
public void Teardown() throws Exception{
driver.quit();
}



Google Search | GoogleSearch.java


package packagename;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class GoogleSearch {

    private WebElement q; // Here's the Element
    public WebDriver driver; 
    
    public GoogleSearch(WebDriver driver) {
this.driver = driver;
driver.get("http://www.google.com/");
}

    public void searchFor(String text) {        
        q.sendKeys(text);
        q.submit();
    }


Working with Annotation | @FindBy

@FindBy and @FindBys lets you find elements using the locators. @FindBys plays a major role in finding elements.

Google Search | GoogleSearch.java

#USING @FindBy

package packagename;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class GoogleSearch {

 @FindBy(how = How.NAME, using = "q") // or use @FindBy(name = "q")
 @CacheLookup // keeps the element in cache for rapid execution
 private WebElement searchme;

 @FindBy(id = "gbqfq") // Less Verbose
 private WebElement submitme;
//Here, both searchme & submitme refers to the same element
    
public WebDriver driver; 
    
  public GoogleSearch(WebDriver driver) {
this.driver = driver;
driver.get("http://www.google.com/");
}

   public void searchFor(String text) {        
    searchme.sendKeys(text);
    submitme.submit();
   }




#USING @FindBys

package packagename;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.FindBys;

public class GoogleSearch {
   
    @FindBys({@FindBy(id = "gs_lc0"), @FindBy(name = "q")})
    @CacheLookup // keeps the element in cache for rapid execution
private WebElement searchme;

public WebDriver driver; 
    
    public GoogleSearch(WebDriver driver) {
this.driver = driver;
driver.get("http://www.google.com/");
}

    public void searchFor(String text) {        
    searchme.sendKeys(text);
    searchme.submit();    
    }

33 comments:

  1. It was very nice article and it is very useful to Selenium learners.We also provide Cub training software online training.

    ReplyDelete
  2. Everything is fine, am happy about your blog. Thanks admin for sharing the unique content, you have done a great job I appreciate your effort.
    Selenium training in chennai

    ReplyDelete
  3. I have read your blog its very attractive and impressive. I like it your blog.

    amazon-web-services training in chennai

    ReplyDelete
  4. Thanks for the great information in your blog.
    java training in chennai

    ReplyDelete
  5. I have read your blog its very attractive and impressive. I like it your blog.
    ios training in chennai

    ReplyDelete
  6. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
    Regards,

    Best Oracle DBA Online Training Institute By Real Time Faculties

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. 3. if you want girls mobile numbers then this website is best for you . you can visit on this website and get their information and you also can meet with thrm and go for a date . click here to use our website --- online dating website

    ReplyDelete
  9. 2. if you are searching for free unlimted tricks then visit now on Uvoffer.com and get unlimited offers and informations.
    film ka naam whatsapp puzzle answer 🐵🐵🐵 +🐘+3+🏹+🏚+💔 =

    ReplyDelete
  10. 1. many peoples want to join random whatsapp groups . as per your demand we are ready to serve you whatsapp group links . On this website you can join unlimited groups . click and get unlimited whatsapp group links 18+ america

    ReplyDelete
  11. hey guys goviral here want to know about goatfarming click here

    goviral


    goat farming

    ReplyDelete
  12. Really i found this article more informative, thanks for sharing this article! Also Check here
    CCC Previous Year Question Paper

    ReplyDelete
  13. Thanks for sharing information awesome blog post Online Education Quiz website For Exam Follow this website Gk in Hindi

    ReplyDelete
  14. The Blog contains the effective and attractive information’s and thanks for the blog.
    Java Training Institute in Chennai
    JAVA Training Institute in Bangalore

    ReplyDelete
  15. 이 웹 사이트는 내가이 주제에 대해 원했던 모든 정보를 가지고 있고 누구에게 물어봐야할지 몰랐습니다.메이저놀이터
    공유해 주셔서 감사합니다!

    ReplyDelete
  16. Shop for the latest Redmi mobiles from Helmet Don at the best prices in India. Xiaomi smartphones include Mi Series, Mi Note Series, Redmi Series, Pocophone, Mi Max Series, Mi Mix Series, and the Blackshark.
    HelmetDon
    dslr-camera
    oppo-phones

    ReplyDelete
  17. Login Your TriumphFX Account. Read In Depth TriumphFX Review.

    ReplyDelete
  18. Extraordinary Blog. Provides necessary information.
    Protractor Training
    Protractor Course

    ReplyDelete
  19. hi thanku so much this information this blog is very useful
    cs executive
    freecseetvideolectures/

    ReplyDelete
  20. AximTrade Is A Forex And Cfd Broker. It Offers Trading In Currency Pairs, Commodities, Indices, And Shares. It Also Provides A Range Of Tools, And 24/7 Customer Service. Sign Up For Aximtrade Login Account Today!

    ReplyDelete
  21. Visit Aximtrade Mt4 Download Login To View Your Personal Area Where You Can Manage Your Trading Accounts, Set Up Your Watchlist, And Track Your Recent Trades.

    ReplyDelete
  22. Thank you for sharing this valuable information with us.
    Girvi Software
    Girvi Software

    ReplyDelete
  23. thanks for sharing information awesome blog post
    Jewellery ERP Software Dubai
    Jewellery ERP Software Dubai

    ReplyDelete