-
Notifications
You must be signed in to change notification settings - Fork 33
PageFactory Annotations
Sushil Kumar Gupta edited this page Jul 5, 2021
·
2 revisions
- @FindElementBy annotation can be used with PageFactory model to find elements based on css_selector or xpath.
- To achieve this you will need to modify page initialization method as
PageFactory.initElements(new ElementFieldDecorator(new DefaultElementLocatorFactory(driver), this)
. - For more example on PageFactory see this page.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;
import io.github.sukgu.support.ElementFieldDecorator;
import io.github.sukgu.support.FindElementBy;
public class LocalTestPage {
WebDriver driver;
@FindElementBy(css = "#container")
WebElement container;
@FindBy(css = "#h3")
WebElement h3;
@FindBy(css = "#h3")
List<WebElement> allH3;
@FindElementBy(css = "#inside")
List<WebElement> insides;
@FindElementBy(xpath = "//body")
WebElement bodyByXPath;
@FindElementBy(xpath = "//body//div[1]")
WebElement divByIndex;
public LocalTestPage(WebDriver driver) {
this.driver = driver;
ElementFieldDecorator decorator = new ElementFieldDecorator(new DefaultElementLocatorFactory(driver));
// need to use decorator if you want to use @FindElementBy in your PageFactory model.
PageFactory.initElements(decorator, this);
}
//...
}
To check all test codes see here