package demo; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; public class HomePage { public String base_url = "https://www.baidu.com/"; public WebDriver driver; WebElement inputBox; WebElement searchButton; /** * @author Young * @param message */ public void typeInputBox(String message) { inputBox = driver.findElement(By.id("kw")); inputBox.clear(); inputBox.sendKeys(message); } /** * @author Young */ public void clickSearchButton() { searchButton = driver.findElement((By.cssSelector("input#su"))); searchButton.click(); driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS); } /** * @author Young * @param d */ HomePage(WebDriver d) { this.driver = d; driver.get(base_url); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS); } /** * @author Young * @return */ public String getTitle() { return driver.getTitle(); } }
Page factory:
package demo; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.How; import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory; public class HomePage { public String base_url = "https://www.baidu.com/"; public WebDriver driver; @FindBy(how = How.ID, using = "kw") WebElement inputBox; @FindBy(how = How.CSS, using = "input#su") WebElement searchButton; /** * @author Young * @param message */ public void typeInputBox(String message) { inputBox.clear(); inputBox.sendKeys(message); } /** * @author Young */ public void clickSearchButton() { searchButton.click(); driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS); } /** * @author Young * @param d */ HomePage(WebDriver d) { this.driver = d; driver.get(base_url); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS); PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this); } /** * @author Young * @return */ public String getTitle() { return driver.getTitle(); } }