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

16 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. Hi, this is Yasmin from Chennai. I have read your blog. It’s very informative and useful blog. You have done really great job. Keep update your blog. Thanks..
    Regards.
    Selenium Training in Chennai | Selenium Training in Chennai

    ReplyDelete
  3. 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
  4. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium

    ReplyDelete
  5. Thanks for the great information in your blog Selenium Training in Chennai

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

    amazon-web-services training in chennai

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

    ReplyDelete
  8. It was very nice blog to learn about SAP HR.Thanks for sharing new things.SAP HR

    ReplyDelete
  9. 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
  10. Advanced Selenium Framework Training in Chennai by Vishwa

    Hi, Reach Mr.Vishwa for Best Selenium and Coded UI Training in Chennai with real time project assistance. This people will teach everything from the basics up to advanced level scenarios from frameworks.

    I learned from Mr.Vishwa and Raj they are very good automation people for Selenium Frameworks. After joined with them I learned many things and now I am writing automation scripts in own project. Vishwa 9003085882
    selenium training in chennai

    ReplyDelete


  11. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...
    Android training in chennai
    Ios training in chennai

    ReplyDelete
  12. Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..please sharing like this information......
    Web Design Development Company
    Mobile App Development Company

    ReplyDelete