Selenium - Page Object Model
Selenium - Page Object Model
Selenium - Page Object Model
import org.openqa.selenium.WebDriver;
public class BlogPage {
private final By inputBox=By.name("txtbox1");
private final By submitBtn=By.name("btnsub");
private final String googleLink="Google";
//
private final WebElement inputBox;
//
private final WebElement submitBtn;
//
private final WebElement googleLink;
private final WebDriver driver;
public BlogPage(WebDriver driver) {
this.driver = driver;
//
this.inputBox = driver.findElement(By.name("txtbox1"));
//
this.googleLink = driver.findElement(By.linkText("Google"));
//
this.submitBtn = driver.findElement(By.name("btnsub"));
}
/**********************************
* This Method Return BlogPage Object
* Because After Submit It's Stay On The
* Same Page
**********************************/
public BlogPage submitForm(String inputTxt){
driver.findElement(inputBox).sendKeys(inputTxt);
driver.findElement(submitBtn).click();
//
inputBox.sendKeys(inputTxt);
//
submitBtn.click();
return new BlogPage(driver);
}
/**********************************
* This Method Return GoolePage Object
* Because After Click On This Link
* It's Redirect To The Google Page
**********************************/
public GooglePage clickonGoogleLink(){
driver.findElement(By.linkText(googleLink)).click();
googleLink.click();
return new GooglePage(driver);
//
}
}
this.driver = driver;
this.searchBox=driver.findElement(By.name("q"));
//
}
/**********************************
* This Method Return Goole Page Object
* Because After Click On Search Button
* Redirect To The Google Page
**********************************/
public GooglePage searchTxt(String searchTxt){
driver.findElement(searchBox).sendKeys(searchTxt);
//
searchBox.sendKeys(searchTxt);
return new GooglePage(driver);
}
}
Page Object Model Test
--------------------------------------------------------------------------package com.core.pageobject;
import
import
import
import
import
import
import
import
import
java.io.File;
org.openqa.selenium.WebDriver;
org.openqa.selenium.firefox.FirefoxBinary;
org.openqa.selenium.firefox.FirefoxDriver;
org.openqa.selenium.firefox.FirefoxProfile;
org.testng.annotations.AfterTest;
org.testng.annotations.BeforeMethod;
org.testng.annotations.BeforeTest;
org.testng.annotations.Test;
blog.submitForm("Test");
}
@Test
public void testGoogleLink()throws Exception{
BlogPage blog=new BlogPage(driver);
blog.clickonGoogleLink().searchTxt("Test");
}
/**********************************
* This Is Created To Close The Driver
**********************************/
@AfterTest
public void tearDown(){
driver.quit();
}
}