zoukankan      html  css  js  c++  java
  • selenium(六)Page Object模式(使用selenium的PageFactory)

    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("百度"),"断言失败");
        }
    }
  • 相关阅读:
    第4章 栈和队列
    第3章 线性表
    第2章 算法
    第1章 数据结构绪论
    First Blood
    第52条:通过接口引用对象
    第51条:当心字符串连接的性能
    第50条:如果其他类型更合适,则尽量避免使用字符串
    第49条:基本类型优先于装箱基本类型
    第48条:如果需要精确的答案,请避免使用float和double
  • 原文地址:https://www.cnblogs.com/yjh1995/p/11986574.html
Copyright © 2011-2022 走看看