首页 > 其他 > 详细

selenium(六)Page Object模式(使用selenium的PageFactory)

时间:2019-12-05 10:33:17      阅读:88      评论:0      收藏:0      [点我收藏+]

PageObject 类

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class PageObject {
    private String url="http://www.baidu.com";
    //声明所有页面中用到的元素,作为类中的变量。
    //将@FindBy注解通过对应的定位方法找到的元素赋值给成员变量
    @FindBy(xpath = "//input[@id=‘kw‘]")
    public WebElement input;

    @FindBy(xpath = "//input[@id=‘su‘]")
    public WebElement  submit;

    public PageObject(WebDriver driver){
        initPage(driver);
    }

    private void initPage(WebDriver driver){
        //打开网页
        driver.get(url);
        //使用selenium的pageFactory,完成元素的初始化
        PageFactory.initElements(driver,this);
    }
    //使用搜索
    public void search(){
        input.sendKeys("12306");
        submit.click();
    }
}

测试类

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class Test1 {
    @Test
    public static void test() {
        System.setProperty("webdriver.chrome.driver","E:\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        PageObject pageObject=new PageObject(driver);
        pageObject.search();
        Assert.assertTrue(driver.getTitle().contains("百度"),"断言失败");
    }
}

 

selenium(六)Page Object模式(使用selenium的PageFactory)

原文:https://www.cnblogs.com/yjh1995/p/11986574.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!