使用POM构建selenium框架,需要一些专家的建议



使用POM构建selenium框架,并尝试为重复使用的功能(如set text, get text, set单选按钮,listbox等)添加通用操作类,结构如下

测试-> POM页面对象

POM页面对象->公共操作类

@Test
public void verifyGooglebuttontext() {
    System.out.println("set search value");
            googlepage.setSearchValue("Google Test");
    Assert.assertEquals(addC.verifytext(),"Google");
}

POM

@FindBy(name="q")
WebElement txtGoogleSearch;
public static setSearchValue(String txt) {
         setText(txtGoogleSearch, txt)
} 

Common Action类

public void setText(WebElement element,String Value ){
        element.sendKeys(Value);
}

我的查询是这是否会影响性能,因为我必须调用2个函数,而不是直接在Page对象类中设置和获取控件值。

请建议。

考虑到大多数情况下您将等待页面加载并在加载页面的DOM中搜索元素,我不会太担心两个额外的函数调用。

实际上,与方法调用(微秒或更少)相比,页面加载将花费更长的时间(>毫秒)。

您从中获得的好处是:如果UI发生变化,您将不得不重新访问少量地方以适应新的元素位置/类型/执行操作的方式(而您的@Test逻辑实际上可以保持不变)。这意味着您将在几个小时内启动并运行,而不是花几天时间检查所有代码并定位/更改受UI更改影响的所有地方。

在这种情况下,您不是在减少冗余,而是在增加冗余。你用一个单行driver.findElement(...).sendKeys(value),把它改成另一个单行setText(driver.findElement(...), value),但是创建了一个额外的函数setText()。这里没有必要再增加一层复杂性。它既不节省时间,也不节省维修时间。

@FindBy(name="q")
WebElement txtGoogleSearch;
public static setSearchValue(String txt) {
    txtGoogleSearch.sendKeys(txt);
}

我不会使用PageFactory。如果您测试任何复杂的站点,您将发现自己在初始加载后重新获取页面上的元素。为了避免这种情况,将定位器定义为页面对象的一部分,并在需要时使用它们来获取元素。

示例页面对象

public class AutomationPracticeTablePage
{
    private WebDriver driver;
    private By pageTitleLocator = By.xpath("//h1[text()='Automation Practice Table' and @class='h1-size']");
    private By tableHeadingLocator = By.cssSelector("table > thead th");
    public AutomationPracticeTablePage(WebDriver webDriver) throws IllegalStateException
    {
        this.driver = webDriver;
        // wait for page to finish loading
        new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(pageTitleLocator));
        // see if we're on the right page
        if (!driver.getCurrentUrl().contains("/automation-practice-table/"))
        {
            throw new IllegalStateException("This is not the Automation Practice Table page. Current URL: " + driver.getCurrentUrl());
        }
    }
    public List<String> getHeadings()
    {
        List<String> headings = new ArrayList<String>();
        for (WebElement heading : driver.findElements(tableHeadingLocator))
        {
            headings.add(heading.getText());
        }
        return headings;
    }
}

使用页面对象

的示例代码
driver.get("http://toolsqa.com/automation-practice-table/");
AutomationPracticeTablePage automationPracticeTablePage = new AutomationPracticeTablePage(driver);
System.out.println(automationPracticeTablePage.getHeadings());

我建议您首先不要将页面对象模型缩写为POM。POM作为一个项目依赖管理文件与Maven相关联。也是为了让你的问题更清楚。你写得越清楚、越简洁,你得到的支持就越好、越快。您可以访问http://toolsqa.com/以更好地理解页面对象模型的结构,获得创造性,并使您的术语更好。

这是一个@Test脚本(方法),不是一个类。它也不是页面对象模型。任何带有此注释的方法都被认为是要执行的测试脚本。

@Test
public void verifyGooglebuttontext() {
    System.out.println("set search value");
            googlepage.setSearchValue("Google Test");
    Assert.assertEquals(addC.verifytext(),"Google");
}

这是一个web元素和一个页面对象中的函数方法。非页面对象模型

@FindBy(name="q")
WebElement txtGoogleSearch;
public static setSearchValue(String txt) {
         setText(txtGoogleSearch, txt)
} 

这不是一个类。它是类中的一个方法。

public void setText(WebElement element,String Value ){
        element.sendKeys(Value);
}

好了,现在,你的setText(WebElement, String)方法太薄了,不能提供任何值。但是,如果您觉得有必要,您可以在方法中添加额外的支持。我的大多数经验不需要额外的输入字段的支持,但我遇到了一些需要额外的支持,当涉及javascript;加载后的DOM操作

这里的要点和建议是,不要把不必要的事情复杂化。使用当前可用的内容,并在运行时调整支持。现在,如果您可以进一步看到项目的前景,您将需要这样的支持,那么就去做,并提前实现支持。

最新更新