第十四章 页面类-自动化框架

十四章 页面类-自动化框架

1.自动化框架

什么是框架?

a:定义公司/团队的做事方法。

b:通用的和结构化的编码标准。

c:团队中的每一个成员都应该遵循同样的标准。

d:编码的标准和类型上差异更小。

e:提高自动化过程的速度和准确性。

将一些公共使用的封装成单独的类方法。

设计模式–页面对象模型(Page Object Model)

a:为每个Web UI元素创建对象仓库

b:应用程序中的每个web页封装成一个页面类

c:页面类-查找web页面的WebElements

d:页面类-包含在这些WebElements上执行操作的页面方法

优势

a:使我们的代码更清晰易懂

b:可以直观地查看测试场景的每一步、查看和编辑测试用例

c:缩短测试人员的学习时间、帮助QA团队按时完成任务

e:减少冗余的代码

f:提高代码复用性

页面对象工厂

a:Page Factory是Selenium WebDriver的内置页面对象模型概念,但它是优化的。

b:PageFactory类我们使用注解@FindBy来查找WebElement

c:我们使用initElements方法初始化web elements

2.PageObjectModel

新建page类(定位元素位置、操作元素)

package PageClass;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/13
 */


public class SearchPage {
    public static WebElement element = null;
    /**
     * 返回出发地文本框元素
     *
     * @param driver
     * @return
     */
    public static WebElement originTextBox(WebDriver driver) {
        element = driver.findElement(By.id("flight-origin"));
        return element;
    }
    public static void fillOriginTextBox(WebDriver driver, String origin) {
        element = originTextBox(driver);
        element.sendKeys(origin);
        ;
    }
    /**
     * 返回目的地文本框元素
     *
     * @param driver
     * @return
     */
    public static WebElement destinationTextBox(WebDriver driver) {
        element = driver.findElement(By.id("flight-destination"));
        element.clear();
        return element;
    }
    /**
     * 返回出发日期文本框元素
     *
     * @param driver
     * @return
     */
    public static WebElement departureDateTextBox(WebDriver driver) {
        element = driver.findElement(By.id("flight-departing"));
        return element;
    }
    /**
     * 返回返回日期文本框元素
     *
     * @param driver
     * @return
     */
    public static WebElement returnDateTextBox(WebDriver driver) {
        element = driver.findElement(By.id("flight-returning"));
        return element;
    }
    /**
     * 返回搜索按钮元素
     *
     * @param driver
     * @return
     */
    public static WebElement searchButton(WebDriver driver) {
        element = driver.findElement(By.id("search-button"));
        return element;
    }

    /**
     * 点击搜索按钮
     *
     * @param driver
     */
    public static void clickSearchButton(WebDriver driver) {
        element = searchButton(driver);
        element.click();
    }

    /**
     * 关闭欢迎提示
     *
     * @param driver
     */

    public static void closeWelcomeNote(WebDriver driver) {
        element = driver.findElement(By.xpath("//button/span[@class='icon icon-close']"));
        element.click();
    }
}

调用page类中的属性和方法编写case

package Pomtestcase;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import PageClass.SearchPage;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/13
 */


public class PageObjectModel {
    private WebDriver driver;
    private String baseUrl;

    @Before
    public void setUp() throws Exception {
        driver = new ChromeDriver();
        baseUrl = "https://www.expedia-cn.com/";

        // 窗口最大化
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test
    public void test() {
        driver.get(baseUrl);
        SearchPage.closeWelcomeNote(driver);
        SearchPage.fillOriginTextBox(driver, "北京, 中国 (PEK-首都国际机场)");
//		SearchPage.originTextBox(driver).sendKeys("北京, 中国 (PEK-首都国际机场)");
        SearchPage.destinationTextBox(driver).sendKeys("上海, 中国 (SHA-虹桥国际机场)");
        SearchPage.departureDateTextBox(driver).sendKeys("2018/04/15");
        SearchPage.returnDateTextBox(driver).clear();
        SearchPage.returnDateTextBox(driver).sendKeys("2018/04/20");
        SearchPage.clickSearchButton(driver);
    }

    @After
    public void tearDown() throws Exception {
    }

}
3.对象仓库和页面类(手写的定位一定要在网页上查看是否仅含有一个并且搜索的时候高亮显示的是自己要定位的元素)
package PageClass;

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

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/14
 */
public class SearchPageFactory {
    WebDriver driver;

    public SearchPageFactory(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver,this);
    }

    @FindBy(id = "tab-flight-tab-hp")
    WebElement airporttextBox;

    public void AirporttextBoxClick(){
        airporttextBox.click();
    }

    @FindBy(id = "flight-type-one-way-label-hp-flight")
    WebElement onlyOne;

    public void setOnlyOneClick(){
        onlyOne.click();
    }

    @FindBy(id = "flight-origin-hp-flight")
    WebElement startingPoint;

    public void setStartingPoint(String StartingPoint){
        startingPoint.sendKeys(StartingPoint);
    }

    @FindBy(id = "flight-destination-hp-flight")
    WebElement onlyOnedestination;

    public void setOnlyOnedestination(String OnlyOnedestination){
        onlyOnedestination.sendKeys(OnlyOnedestination);
    }

    @FindBy(id = "flight-departing-single-hp-flight")
    WebElement beginDate;
    public void setBeginDate(String BeginDate){
        beginDate.sendKeys(BeginDate);
    }
    @FindBy(xpath = "//div[@class='menu-bar gcw-travel-selector-wrapper']//button[@class='trigger-utility menu-trigger btn-utility btn-secondary dropdown-toggle theme-standard pin-left menu-arrow gcw-component gcw-traveler-amount-select gcw-component-initialized']")
    WebElement peoplenum;

    public void peoplenumClick(){
        peoplenum.click();
    }

    @FindBy(xpath = "//form[@id='gcw-flights-form-hp-flight']//button[@class='btn-primary btn-action gcw-submit']")
    WebElement find;

    public void findClick(){
        find.click();
    }

}

package Pomtestcase;

import PageClass.SearchPage;
import PageClass.SearchPageFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/13
 */


public class PageFactoryObjectModel {
    private WebDriver driver;
    private String baseUrl;
    JavascriptExecutor js;
    SearchPageFactory searchPageFactory;

    @Before
    public void setUp() throws Exception {
        driver = new ChromeDriver();
        baseUrl = "https://www.expedia-cn.com/";
        js = (JavascriptExecutor) driver;
        // 窗口最大化
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        searchPageFactory =new SearchPageFactory(driver);
    }

    @Test
    public void test() throws InterruptedException {
        driver.get(baseUrl);
        searchPageFactory.AirporttextBoxClick();
        searchPageFactory.setOnlyOneClick();
        searchPageFactory.setStartingPoint("北京");
        searchPageFactory.setOnlyOnedestination("天津");
        searchPageFactory.setBeginDate("2022/04/15");
        searchPageFactory.peoplenumClick();
        searchPageFactory.peoplenumClick();
        searchPageFactory.findClick();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }

}
4.找到当前页面所有可以点击的连接点击并且输出相应信息(回归测试常用)
package usefulMethod;

import PageClass.SearchPageFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/7/14
 */
public class FindLinks {
    private WebDriver driver;
    private String baseUrl;
    SearchPageFactory searchPage;

    @Before
    public void setUp() throws Exception {
        driver = new ChromeDriver();
        baseUrl = "https://www.expedia-cn.com/";
        searchPage = new SearchPageFactory(driver);

        // Maximize the browser's window
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test
    public void testFindLinks() throws InterruptedException {
        driver.get(baseUrl);
        Thread.sleep(3000);
        searchPage.AirporttextBoxClick();
        List<WebElement> linkList = clickableLinks(driver);
        //获取所有元素定位 包括a img类型
        for(WebElement link:linkList){
         遍历list 包含href属性 点击并获取响应信息
            String href = link.getAttribute("href");
            try {
                System.out.println("URL"+href+"returned"+linkStatus(new URL(href)));
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println(e.getMessage());
            }
        }

    }

    public static List<WebElement> clickableLinks(WebDriver driver) {
        List<WebElement> linksToClick = new ArrayList<WebElement>();
        List<WebElement> elements = driver.findElements(By.tagName("a"));
        //将所有a标签的元素加入到List
        elements.addAll(driver.findElements(By.tagName("img")));
        //将所有img标签的元素加入到List
        for(WebElement e:elements){
        遍历如果有herf属性放到准备点击的List里面
            if(e.getAttribute("href")!=null){
                linksToClick.add(e);
            }
        }
        return linksToClick;
    }

    public static String linkStatus(URL url) {
        try {
            //将URLConnection强制转换成httpURLConnection
            HttpURLConnection http = (HttpURLConnection)url.openConnection();
            //打开链接
            http.connect();
            //获取响应信息
            String responseMessage = http.getResponseMessage();
            //关闭链接
            http.disconnect();
            return responseMessage;
        }
        catch (Exception e) {
            return e.getMessage();
        }
    }
    @After
    public void tearDown() throws Exception {
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值