Selenium PageFactory頁面對象
阿新 • • 發佈:2018-01-21
method aid 修改 get 用例 exce ons set http
使用頁面對象的好處是:
當頁面元素的位置發生變化時,
我們只需要去修改id或者xpath,
而不用去修改測試用例。
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestNG {
private WebDriver driver;
@FindBy(xpath = ".//*[@id=‘kw‘]")
private WebElement inputBox;
//輸入框
@FindBy(xpath = ".//*[@id=‘su‘]")
private WebElement searchButton;
//搜索按鈕
@BeforeMethod
public void beforeMethod() throws InterruptedException {
System.setProperty("webdriver.firefox.marionette",
"src/main/resourcec/geckodriver.exe");
String baiduHomePage;
baiduHomePage = "https://www.baidu.com/";
driver = new FirefoxDriver();
PageFactory.initElements(driver, this);
//構造函數,初始化PageFactory對象
driver.manage().window().maximize();
driver.get(baiduHomePage);
Thread.sleep(2000);
Assert.assertEquals(driver.getTitle(), "百度一下,你就知道");
}
@Test
public void testNG() throws InterruptedException {
inputBox.clear();
inputBox.sendKeys("Selenium");
searchButton.click();
Thread.sleep(3000);
Reporter.log("搜索Selenium的測試用例");
Assert.assertEquals(driver.getTitle(), "Selenium_百度搜索");
}
@AfterMethod
public void afterMethod(){
driver.close();
driver.quit();
}
}
Selenium PageFactory頁面對象