Selenium PageObject实例

教你如何使用Selenium PageObject,已Baidu查询为例子.

1.编写BaiduSearchPage类.代码如下:

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

public class BaiduSearchPage {

        private WebDriver driver;
    
        @FindBy(id="kw")
        private WebElement searchValue;  
        
        @FindBy(id="su")
        private WebElement btnButton;  
    
        public void searchValue(String seachConditon){
            searchValue.sendKeys(seachConditon);
            btnButton.click();
        }
}


2.编写测试类

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;


public class TestPageObject {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WebDriver driver = new FirefoxDriver();  
          
        driver.get("http://www.baidu.com/");  
          
        BaiduSearchPage page = PageFactory.initElements(driver, BaiduSearchPage.class);
 
        page.searchValue("China");
    }

}



你可能感兴趣的:(Selenium PageObject实例)