• 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();
        }
    
    }
  • 相关阅读:
    tomcat bug之部署应用的时候经常会发上startup failed due to previous errors
    maven编译项目理解
    MyReport报表引擎2.6.5.0新功能
    PHP入门-摘要表格处理问题
    EnumMap源代码阅读器
    MySQL几种方法的数据库备份
    工作日志2014-08-19
    Linux通过网卡驱动程序和版本号的信息
    JS于,子类调用父类的函数
    hdu 5007 水 弦
  • 原文地址:https://www.cnblogs.com/tobecrazy/p/5008432.html
Copyright © 2020-2023  润新知