
Page Object Factory
Page Object Factory הוא תבנית עיצוב ליצירת מאגר אובייקטים עבור רכיבי ממשק משתמש באינטרנט.
בדומה ל- Page Object Model ול- Object Repository
מומלץ ביותר להשתמש ב- Page Object Factory
הסבר
1
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class pObjectFactory
{
@FindBy(how = How.CSS, using = "a[href='registration/index.htmr]")
public WebElement lnk_MyAccount;
@FindBy(how = How.ID, using = "userName2")
public WebElement txt_UserName;
@FindBy(how = How.NAME, using = "password2")
public WebElement txt_Pass;
@FindBy(how = How.XPATH, using = "//*[@label='refres']/a")
public WebElement btn_refresh;
2 init Elements
// איתחול כל ה- WebElements
gsp = PageFactory.initElements( driver, GoogleSearchPage.class);
דוגמה
1
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class GoogleSearchPage {
private WebDriver driver;
// Objects declarations
@FindBy(how = How.NAME, using = "q")
public WebElement searchField;
@FindBy(how = How.NAME, using = "q")
public WebElement searchButton;
// Constractor
public GoogleSearchPage(WebDriver driver)
{
this.driver = driver;
}
// Fanction
public void searchAction(String searchValue)
{
searchField.sendKeys(searchValue);
searchButton.submit();
}
}
------------------------------------------------------------------------
2
// Main
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
public class objectFactory {
private static WebDriver driver;
public static GoogleSearchPage gsp;
@BeforeClass
public static void openBrowser()
{
System.setProperty("webdriver.gecko.driver","C:\\eclipse\\Firefox driver\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://google.com/");
gsp = PageFactory.initElements( driver, GoogleSearchPage.class); // initElements
}
@Test // Test cases
public void tests()
{
gsp.searchAction("Selenium atomation testing"); // Call to function
}
@AfterClass
public static void closeBrowser()
{
driver.quit();
}
}