硒中的组件对象而不是页面对象

Kon*_*tov 2 selenium pageobjects selenium-webdriver

页面对象是基于硒的测试中最受欢迎的模式之一。不幸的是,如果我们照原样使用它,我们经常需要复制代码。请考虑以下情况:

  • 我们使用带有通用组件的UI框架,说一些花式表
  • 表相当复杂,具有过滤,排序,搜索
  • 该表在应用程序的多个页面上使用

是否存在任何现有的基础结构来创建更细粒度的组件对象,而不是硒中或第三方中的页面对象?我的意思是注释和相关的基础架构?

Jef*_*ffC 5

页面对象有点用词不当。它们不必是专门的完整页面来遵循页面对象模型。我将创建一个 Table 类(页面对象),其中包含 Table 对象的所有定位器和方法,然后将其包含在它出现的页面/页面对象中。

例如,如果主页包含表对象,则该类HomePage将引用该类Table


Gra*_*per 5

硒webdriver的移动实现的Appium具有小部件的概念,它是pageobject的扩展。有一个Widget类,它允许人们相对于Web浏览器中的某个元素进行搜索。您可以在appium来源测试部分中对此进行检查。看看包装io.appium.java_client.pagefactory_tests.widgets。这支持FindBy注释和WebElement构造以及PageFactory初始化。

所以不要使用

@FindBy(.......)
private WebElement myTable;
Run Code Online (Sandbox Code Playgroud)

您可以使用

@FindBy(container of the table....)
private Table myTable;
Run Code Online (Sandbox Code Playgroud)

表类现在可以具有所有相关的变量和方法。

POM.xml的一部分

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-api</artifactId>
    <version>2.53.1</version>
</dependency>
<dependency>
    <groupId>io.appium</groupId>
    <artifactId>java-client</artifactId>
    <version>4.1.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

测试类别

import io.appium.java_client.pagefactory.AppiumFieldDecorator;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;

public class WidgetBrowTest {

    @Test
    public void test() {
        System.setProperty("webdriver.chrome.driver", "E:/Software Testing/Selenium/Jars/chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        driver.get("http://stackoverflow.com/");


        SOHome soHome = new SOHome(driver);
        PageFactory.initElements(new AppiumFieldDecorator(driver), soHome);

        //Below two are from widget - First question in stackoverflow homepage
        System.out.println(soHome.getFirstQues().getQuesTitle());
        System.out.println(soHome.getFirstQues().getQuesTags());

        //Below two are from home page
        System.out.println(soHome.getLogoText());
        System.out.println(soHome.getMenuText());
    }
}
Run Code Online (Sandbox Code Playgroud)

StackOverflow主页-

import java.util.List;
import java.util.stream.Collectors;

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

public class SOHome {

    @FindBy(css="div[id='hlogo'] > a")
    private WebElement logo;

    @FindBy(xpath="//div[@id='hmenus']//li/a")
    private List<WebElement> menuOpt;

    @FindBy(css="div[class='summary']")
    private SOQuesWidget firstQues;

    private WebDriver driver;


    public SOHome(WebDriver driver) {
        this.driver = driver;
    }

    public String getLogoText() {
        return logo.getText();
    }

    public List<String> getMenuText() {
        return menuOpt.stream().map(t -> t.getText()).collect(Collectors.toList());
    }

    public SOQuesWidget getFirstQues() {
        return firstQues;
    }
}
Run Code Online (Sandbox Code Playgroud)

问题小工具-第一个问题

import java.util.List;
import java.util.stream.Collectors;

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

import io.appium.java_client.pagefactory.Widget;

public class SOQuesWidget extends Widget {

    @FindBy(css="a[class='question-hyperlink']")
    private WebElement quesTitle;

    @FindBy(xpath=".//div[starts-with(@class,'tags')]/a")
    private List<WebElement> quesTags;

    protected SOQuesWidget(WebElement element) {
        super(element);
    }

    public String getQuesTitle() {
        return quesTitle.getText();
    }

    public List<String> getQuesTags() {
        return quesTags.stream().map(t -> t.getText()).collect(Collectors.toList());
    }
}
Run Code Online (Sandbox Code Playgroud)