Selenium LoadableComponent加载组件

读立写生 2018-01-20

继承LoadableComponent类可以在打开地址时,

判断浏览器是否打开了预期的网址,

需要重写load()与isLoad()方法;

即使没有定义get()方法,

也可以进行get()方法的调用,

get()方法会默认调用页面对象类中的load()方法。

import org.openqa.selenium.WebDriver;<br />import org.openqa.selenium.WebElement;<br />import org.openqa.selenium.firefox.FirefoxDriver;<br />import org.openqa.selenium.support.FindBy;<br />import org.openqa.selenium.support.PageFactory;<br />import org.openqa.selenium.support.ui.LoadableComponent;<br />import org.testng.Assert;<br />import org.testng.Reporter;<br />import org.testng.annotations.AfterMethod;<br />import org.testng.annotations.BeforeMethod;<br />import org.testng.annotations.Test;<br /><br />public class TestNG extends LoadableComponent<TestNG> {<br />    private WebDriver driver;<br /><br />    @FindBy(xpath = ".//*[@id='kw']")<br />    private WebElement inputBox;<br />    //输入框<br /><br />    @FindBy(xpath = ".//*[@id='su']")<br />    private WebElement searchButton;<br />    //搜索按钮<br /><br />    @BeforeMethod<br />    public void beforeMethod() throws InterruptedException {<br />        System.setProperty("webdriver.firefox.marionette",<br />                "src/main/resourcec/geckodriver.exe");<br /><br />        driver = new FirefoxDriver();<br />        PageFactory.initElements(driver, this);<br />        //构造函数,初始化PageFactory对象<br />        this.get();<br /><br />        driver.manage().window().maximize();<br />        Thread.sleep(2000);<br />    }<br /><br />    @Test<br />    public void testNG() throws InterruptedException {<br />        inputBox.clear();<br />        inputBox.sendKeys("Selenium");<br /><br />        searchButton.click();<br />        Thread.sleep(3000);<br /><br />        Reporter.log("搜索Selenium的测试用例");<br />        Assert.assertEquals(driver.getTitle(), "Selenium_百度搜索");<br />    }<br /><br />    @AfterMethod<br />    public void afterMethod(){<br />        driver.close();<br />        driver.quit();<br />    }<br /><br />    @Override<br />    protected void load() {<br />        String baiduHomePage = "https://www.baidu.com/";<br />        this.driver.get(baiduHomePage);<br />    }<br /><br />    @Override<br />    protected void isLoaded() throws Error {<br />        Assert.assertEquals(driver.getTitle(), "百度一下,你就知道");<br />    }<br /><br />}

相关推荐