begin
PageObject模式:
/** * Takes a username and password, fills out the fields, and clicks "login". * @return An instance of the AccountPage */ public AccountPage loginAsUser(String username, String password) { WebElement loginField = driver.findElement(By.id("loginField")); loginField.clear(); loginField.sendKeys(username); // Fill out the password field. The locator we're using is "By.id", and we should // have it defined elsewhere in the class. WebElement passwordField = driver.findElement(By.id("password")); passwordField.clear(); passwordField.sendKeys(password); // Click the login button, which happens to have the id "submit". driver.findElement(By.id("submit")).click(); // Create and return a new instance of the AccountPage (via the built-in Selenium // PageFactory). return PageFactory.newInstance(AccountPage.class); }
样例:
public class GoogleSearchPage extends LoadableComponent<GoogleSearchPage> { private final WebDriver driver; private GSPFluentInterface gspfi; public class GSPFluentInterface { private GoogleSearchPage gsp; public GSPFluentInterface(GoogleSearchPage googleSearchPage) { gsp = googleSearchPage; } public GSPFluentInterface clickSearchButton() { gsp.searchButton.click(); return this; } public GSPFluentInterface setSearchString( String sstr ) { clearAndType( gsp.searchField, sstr ); return this; } } @FindBy(id = "gbqfq") private WebElement searchField; @FindBy(id = "gbqfb") private WebElement searchButton; public GoogleSearchPage(WebDriver driver) { gspfi = new GSPFluentInterface( this ); this.get(); // If load() fails, calls isLoaded() until page is finished loading PageFactory.initElements(driver, this); // Initialize WebElements on page } public GSPFluentInterface withFluent() { return gspfi; } public void clickSearchButton() { searchButton.click(); } public void setSearchString( String sstr ) { clearAndType( searchField, sstr ); } @Override protected void isLoaded() throws Error { Assert.assertTrue("Google search page is not yet loaded.", isSearchFieldVisible() ); } @Override protected void load() { if ( isSFieldPresent ) { Wait<WebDriver> wait = new WebDriverWait( driver, 3 ); wait.until( visibilityOfElementLocated( By.id("gbqfq") ) ).click(); } } }
Selenium官网:
https://www.w3.org/TR/webdriver/#dependencies
https://seleniumhq.github.io/docs/
Selenium_doc
https://seleniumhq.github.io/selenium/docs/api/java/index.html?overview-summary.html