Showing posts with label selenium webdriver. Show all posts
Showing posts with label selenium webdriver. Show all posts

Tuesday, July 9, 2013

Page Object Pattern using JUnit Webdriver and Java Classes

Page Object pattern at  beginner's Level is nothing but dividing Test Cases and Page Object Classes . The Page Object Class is nothing but a Class with methods ( such as doing any function on web Element , returning any other page Object etc kind of stuff ) for that page and instantiate of those objects ( using page Factory ) .
Lets take example of One of our Sample Test Class that have 2 Test in it.

package com.tests;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.PageFactory;
import com.pages.LocatorPage;
import com.pages.ContactUs;
import com.pages.Home;
import com.pages.searchResults;
public class SearchWebPage {
private Home page;
searchResults searchAgainpage;
public String url;
public WebDriver driver1;

       @Before
public void opentheBrowser() throws InterruptedException
{
driver1 = new InternetExplorerDriver();
page = PageFactory.initElements(driver1, Home.class);
url = "any url you want to explore";
page.open(url);
Thread.sleep(3000);
}

// @Test
public void userSearch() throws InterruptedException
{
searchAgainpage = page.searchText("AAA");
searchAgainpage.searchAgain("AAA");
searchAgainpage.closeBrowser();
}

@Test
public void findFA() throws InterruptedException
{
ContactUs contactuspageobject = page.goToContactUsPage();
LocatorPage locatorpageobject = contactuspageobject.individualBranchLocator();
locatorpageobject.findFinancialAdvisor();
}
}

The website which i have used for writing sample codes has below fields on its Homepage


HomePage.Java should contains the following code:

package com.pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class Home {
protected WebDriver driver;
protected String url = null;

@FindBy(name = "query")  // This is method to find search text box location
private WebElement q_text;

@FindBy(xpath = "/html/body/center/div/div/div[2]/div/div/div/form/table/tbody/tr/td[3]/input")
private WebElement button_btng; // the xpath above is for search Button

@FindBy(css = "html body td > a[href='contact_us.html']")
private WebElement contactUs; // Contact Us link button

public Home(WebDriver d) {
this.driver = d;
}

public void open(String str){
driver.get(str);
}

public void close() {
driver.close();
}

public String getStringTitle() {
return driver.getTitle();
}

// search a keyword and Returns a searchResults Class Objects to Caller
// Class.

public SearchResults searchText(String search)
throws InterruptedException {
q_text.sendKeys(search);
Thread.sleep(3000);
button_btng.click();
return (PageFactory.initElements(driver, SearchResults.class));
}

public void clickOnSearch() {
button_btng.click();
}

public ContactUs goToContactUsPage()
{
contactUs.click();
return (PageFactory.initElements(driver, ContactUs.class));
}

}

Third page is SearchResults.Java--- This page is returned when user search any text and click search on HomePage.Java Class.



public class SearchResults {

protected WebDriver driver;
@FindBy(xpath = "/html/body/div[2]/div[2]/div/a/img")
private WebElement homepage;
@FindBy(css = "html body div td.BarHeader input[type='text']")
private WebElement searchAgainText;
@FindBy(css = "html body input[type='image'][src='/img/btn_search.gif']")
private WebElement searchAgainSubmitButton;

public SearchResults(WebDriver d)
{
this.driver = d;
}
public String getTitle()
{
return (driver.getTitle());
}
public void closeBrowser()
{
driver.quit();
}

public void takeMeHomeFromSearchAgainPage()
{
homepage.click();
}
public void searchAgain(String search) throws InterruptedException
{
searchAgainText.clear();
searchAgainText.sendKeys(search);
Thread.sleep(3000);
searchAgainSubmitButton.click();
}
}

Now when User click on contact Us link , a contactUs page is returned to caller method.
package com.pages; -- Contact us page has mainly few links to go to Locator page. So on clicking any of the link it returns the Locator page object.

ContactUs.Java

public class ContactUs {
           WebDriver driver ;
           @FindBy(css="html body a[href=''locator"]")
            private WebElement indi_investor;
            @FindBy(css="html body a[href='about/offices/locator/index.html']")
            private WebElement insti_investor;
            public ContactUs(WebDriver d)
            {
             this.driver=d;
             }
           public LocatorPage individualBranchLocator() throws InterruptedException
           {
           indi_investor.click();
           Thread.sleep(3000);
           return (PageFactory.initElements(driver, LocatorPage.class) );
          }
}

and last but not the least  LocatorPage - On entering Location here and entering search button , a page is returned with Title " Locator Page" , which we need to verify by using assert statements.




public class LocatorPage {
           WebDriver driver;
           @FindBy(css="html body input#zipCode")
           WebElement locationZip; // Enter ZIP CODE field on Locaror page
           @FindBy(css="html body img[src='img/btn_search.png']")
           WebElement searchbutton; // Search Button on Locator Page
           public LocatorPage(WebDriver d)
           {
          this.driver=d;
          }
          public void findFinancialAdvisor()
          {
           locationZip.sendKeys("10004"); //For NY 
           searchbutton.click();
           if (driver.getTitle().equalsIgnoreCase("Locator Page ") ) // this is comparing if on clicking search   //button the page which is coming has title "locator Page" then its expected result is equal to actual one.
           {
            System.out.println("We have Entered correct page");
            assert(true);
            driver.quit();
           }
           else
           {
            assert(false);
            driver.quit();
           }
          }
}
below Diagram will be helpful in understanding what is calling what. 











Sunday, June 16, 2013

Selection multiple options from Drop-down with Checkbox List

In Continuation of the Drop-down topic that i have written earlier ( Click to read) , i forget to mention one more kind of Drop-down  as given below :-


 This works as follow   , on clicking All residential Properties , a drop down will appear with multiple checkbox to select . user can select Multiple option at a time from this Checkbox list . The Concept for this is again same

Click on Main element , wait for subelement to appear on the screen and click on submenu one by one using ActionBuilder Class. Let's come to code

    Actions action = new Actions(driver);
    WebElement mainDropdownElement = driver.findElement(By.id("proptypecheck")); // main Element
    WebElement subElement1 = driver.findElement(By.id("ptypecheck_5")); // Sub Element 1
    WebElement SubElement2 = driver.findElement(By.id("ptypecheck_8")); // SubElement 2
    /* Action_one basically dealing with clicking main Element and action after that is clicking on Checkboxex one by one */   
    Action action_one = action.click(mainDropdownElement).build();
    action_one.perform();
    Thread.sleep(2000);   
     action.click(subElement1).click(SubElement2).build().perform()    ;

Hope that Helped ....