Selenium LoadableComponent加载组件
Selenium LoadableComponent加载组件继承LoadableComponent类可以在打开地址时,判断浏览器是否打开了预期的网址,需要重写load()与isLoad()方法;即使没有定义get()方法,也可以进行get()方法的调用,get()方法会默认调用页面对象类中的load()方法。
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.openqa.selenium.support.ui.LoadableComponent;
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 extends LoadableComponent<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");
driver = new FirefoxDriver();
PageFactory.initElements(driver, this);
//构造函数,初始化PageFactory对象
this.get();
driver.manage().window().maximize();
Thread.sleep(2000);
}
@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();
}
@Override
protected void load() {
String baiduHomePage = "https://www.baidu.com/";
driver.get(baiduHomePage);
}
@Override
protected void isLoaded() throws Error {
Assert.assertEquals(driver.getTitle(), "百度一下,你就知道");
}
}
页:
[1]