章节十五、3-对象仓库、Page Factory实例应用

一、如何创建对象仓库

 1 package pageclasses;
 2 
 3 import org.openqa.selenium.WebDriver;
 4 import org.openqa.selenium.WebElement;
 5 import org.openqa.selenium.support.FindBy;
 6 import org.openqa.selenium.support.PageFactory;
 7 
 8 public class SearchPageFactory {
 9     
10     WebDriver driver;
11     
12     
13     //如何创建对象工厂
14     //创建对象仓库需要用到FindBy这个组件,FindBy可以告诉selenium用什么方式来查找元素
15     @FindBy(id="tab-flight-tab-hp")
16     WebElement fly;
17     
18     @FindBy(xpath="//input[@id='flight-origin-hp-flight']")
19     WebElement startText;
20     
21     @FindBy(xpath="//input[@id='flight-destination-hp-flight']")
22     WebElement endText;
23     
24     @FindBy(xpath="//input[@id='flight-departing-hp-flight']")
25     WebElement startData;
26     
27     @FindBy(xpath="//input[@id='flight-returning-hp-flight']")
28     WebElement returnData;
29     
30     @FindBy(xpath="//div[@class='cols-nested ab25184-submit']//button[@class='btn-primary btn-action gcw-submit']")
31     WebElement searchButton;
32     
33     public SearchPageFactory(WebDriver driver) {
34         this.driver = driver;
35 //        对查找的元素进行初始化,然好我们就可以使用了
36 //        this表示本类型的对象
37         PageFactory.initElements(driver, this);
38     }
39     
40     //操作仓库中的元素
41     public void clickFly() {
42         fly.click();
43     }
44     
45     public void writStartText(String text) {
46         startText.sendKeys(text);
47     }
48     
49     public void writEndText(String text) {
50         endText.sendKeys(text);
51     }
52     
53     public void writStartData(String text) {
54         startData.sendKeys(text);
55     }
56     
57     public void writReturnData(String text) {
58         returnData.sendKeys(text);
59     }
60     
61     public void clickSearchButton() {
62         searchButton.click();
63     }
64 }

1、如何创建对象仓库:

@FindBy(id="tab-flight-tab-hp")

WebElement fly;

Id:表示查找元素的方式(还有XPathname等)

tab-flight-tab-hp:表示需要查找的元素

Fly:表示是WebElement类型的变量名

2、对对象仓库查找的元素进行初始化,以便于我们可以使用:PageFactory.initElements(driver, this);

driver:表示浏览器对象

This:表示本类型的对象

二、实例引用对象仓库

package pageclasses;

import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.notification.RunListener.ThreadSafe;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

class FarmeworkTestCase {
    
    WebDriver driver;
    String url;
    SearchPageFactory search;

    @BeforeEach
    void setUp() throws Exception {
        driver = new ChromeDriver();
        search = new SearchPageFactory(driver);
        url = "https://www.expedia.com/";
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(url);
}

    @Test
    void test() throws InterruptedException {
//        点击机票按钮,进入机票预订页面
        search.clickFly();
//        舒服出发地
        search.writStartText("长沙, 中国 (CSX-黄花国际机场)");
//        输入目的地
        Thread.sleep(2000);
        search.writEndText("上海, 中国 (PVG-浦东国际机场)");
//        输入出发日期
        search.writStartData("08/01/2019");
//        输入返程日期
        search.writReturnData("08/03/2019");
//        点搜索按钮
        search.clickSearchButton();
    }
    
    @AfterEach
    void tearDown() throws Exception {
        Thread.sleep(2000);
        driver.quit();
    }
}

猜你喜欢

转载自www.cnblogs.com/luohuasheng/p/11139844.html