zoukankan      html  css  js  c++  java
  • selenium page object & Page Factory

    package demo;
    
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    
    public class HomePage
    {
        public String    base_url = "https://www.baidu.com/";
        public WebDriver driver;
    
        WebElement inputBox;
        WebElement searchButton;
    
        /**
         * @author Young
         * @param message
         */
        public void typeInputBox(String message)
        {
            inputBox = driver.findElement(By.id("kw"));
            inputBox.clear();
            inputBox.sendKeys(message);
        }
    
        /**
         * @author Young
         */
        public void clickSearchButton()
    
        {
            searchButton = driver.findElement((By.cssSelector("input#su")));
            searchButton.click();
            driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
        }
    
        /**
         * @author Young
         * @param d
         */
        HomePage(WebDriver d)
        {
            this.driver = d;
            driver.get(base_url);
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
        }
    
        /**
         * @author Young
         * @return
         */
        public String getTitle()
        {
            return driver.getTitle();
        }
    
    }

    Page factory:

    package demo;
    
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.How;
    import org.openqa.selenium.support.PageFactory;
    import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
    
    public class HomePage
    {
        public String    base_url = "https://www.baidu.com/";
        public WebDriver driver;
    
        @FindBy(how = How.ID, using = "kw")
        WebElement inputBox;
        @FindBy(how = How.CSS, using = "input#su")
        WebElement searchButton;
    
        /**
         * @author Young
         * @param message
         */
        public void typeInputBox(String message)
        {
            inputBox.clear();
            inputBox.sendKeys(message);
        }
    
        /**
         * @author Young
         */
        public void clickSearchButton()
        {
            searchButton.click();
            driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
        }
    
        /**
         * @author Young
         * @param d
         */
        HomePage(WebDriver d)
        {
            this.driver = d;
            driver.get(base_url);
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
            PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);
        }
    
        /**
         * @author Young
         * @return
         */
        public String getTitle()
        {
            return driver.getTitle();
        }
    
    }
  • 相关阅读:
    spring: web学习笔记1异常处理:No adapter for handler
    maven: maven编译时指定maven库,即指定profile
    spring: 一些基本配置也许只有自己能够读懂
    ruby: rdoc生成文档的好工具
    php: eclipse 编辑 php
    ssh免密码登录, 发送命令到多个Linux
    ruby: 使用netbeans debug ruby
    maven: maven创建工程,web工程等普通java app, web app
    IE, firefox竖向 横向滚动条处理
    获取指定进程在一段时间内cpu和内存的最大最小值【调试版】
  • 原文地址:https://www.cnblogs.com/tobecrazy/p/5008432.html
Copyright © 2011-2022 走看看