如何生成页面对象模式类 (SeleniumQA) 来测试 Web/移动页面





0/5 (0投票)
一个用于生成基于 Selenium 的页面对象模式类的实用程序,用于 Web/移动测试用例。
引言
什么是 NEO PageFactory?
SeleniumQA PageFactory 简介
Selenium QA PageFactory 是一个众所周知的实用程序,它使开发人员和测试人员能够基于 页面对象模式 实现自动化 Web/移动测试。 简而言之,所有正在测试的页面都单独(1:1)映射到物理(Java、C# .NET 等)页面测试类。页面测试类的实例被称为 页面对象。 此对象通常用于以编程方式与正在测试的页面进行交互,例如,通过自动填写表单。
页面对象包含 getter 和 setter,它们 代表正在测试页面的页面元素。 这些 getter/setter 通常用于将数据从这些元素设置/获取 到/从这些元素。 类中也有方法用于触发物理页面事件,例如 click、doubleClick 等。
使用 SeleniumQA PageFactory 时的问题
使用开箱即用的 Selenium QA PageFactory,所有页面类都由测试人员(或开发人员)手动创建。 这通常会导致并发症,尤其是在正在测试的页面具有丰富页面元素的情况下(即,页面上的元素数量非常大)。 从测试人员的角度来看,问题也可能发生在开发的初始阶段,在这种情况下,元素 id、名称、xpath 位置等经常被开发人员更改(尤其是在这些更改未被预料到的情况下)。
使用 NEO PageFactory 可以最大限度地减少 SeleniumQA 问题
NEO PageFactory 的开发旨在(至少)最大限度地减少这些问题。 它的做法是 更进一步并生成用于所有测试的所有页面类。 这 a. 减少了创建页面类所需的时间(因为这些类可以直接在测试中使用)并且还可以在每次构建的单元测试阶段使用,作为页面类生成的差异。 后者变得非常有用,因为它最大限度地减少了验收测试阶段的问题(通常是发现页面修改问题的阶段)。
它是如何工作的?
Neo PageFactory 通过扫描所有 HTML 文档(通过 HTTP 或本地 HTML 文本文件)来查找标准(或定制)元素,例如 input、a、select 等,并且基于发现的元素,生成包含 getter 和 setter 等的页面对象模式类文件。 这些 getter/setter(以及操作方法,例如 click、doubleClick 等)代表找到的元素。 每个方法的命名约定由其元素 id、名称或用于显示该元素的文本决定。
用法
1. 克隆并安装 neopagefactory
(注意:此步骤只需执行一次)
# Assuming your root workspace for all projects is ~/projects
cd ~/projects
# Clone NEO PageFactory ...
git clone https://github.com/desmccarter/neopagefactory
# Go into the newly created/cloned folder ...
cd neopagefactory
# Install ...
mvn clean install
2. 生成你的 POP(页面对象模式)类
# Assuming your project workspace is ~/projects/myproject
# and that you are working in a BASH instance ...
# Go to YOUR test project folder
cd ~/project/myproject
# Generate Google.com (for example) Page Object Pattern classes
# for your project ...
~/project/nopagefactory/generateclasses -url https://www.google.com
页面类输出
页面类(目前默认)在当前工作目录的 src/main/java/com/dmcc/sample/pages 下创建。 命名约定为 NameOfPagePage.java,其中 NameOfPage 由 URL 名称构成。 例如,从 www.google.com 生成的页面类将具有名称 GooglePage.java,位于 src/main/java/com/dmcc/sample/pages/google 下。
NEO PageFactory 生成的示例类
以下显示的页面类由 NEO PageFactory 生成。
Google.com
package com.dmcc.sample.pages.google;
import com.dmcc.pagegen.exceptions.PageException;
import com.dmcc.sample.pages.google.GoogleField;
import com.dmcc.pagegen.page.mccarterp.McCarterPage;
public class GooglePage extends McCarterPage{
private final String url="https://www.google.com";
private final String rRoot="../pgenexamples/src/test/resources";
public GooglePage navigate()throws PageException {
this.setResourcesRoot(rRoot);
this.navigate(url);
return this;
}
public void setQ(final String value) throws PageException{
this.setValue(GoogleField.Q, value);
}
public void clickQ()throws PageException{
this.click(GoogleField.Q);
}
public void clickGoogleSearch()throws PageException{
this.click(GoogleField.GoogleSearch);
}
public void clickIMFeelingLucky()throws PageException{
this.click(GoogleField.IMFeelingLucky);
}
}
Facebook.com
package com.dmcc.sample.pages.facebook;
import com.dmcc.pagegen.exceptions.PageException;
import com.dmcc.sample.pages.facebook.FacebookField;
import com.dmcc.pagegen.page.mccarterp.McCarterPage;
public class FacebookPage extends McCarterPage{
private final String url="https://#";
private final String rRoot="../pgenexamples/src/test/resources";
public FacebookPage navigate()throws PageException {
this.setResourcesRoot(rRoot);
this.navigate(url);
return this;
}
public void setEmail(final String value) throws PageException{
this.setValue(FacebookField.Email, value);
}
public void clickEmail()throws PageException{
this.click(FacebookField.Email);
}
public void setPass(final String value) throws PageException{
this.setValue(FacebookField.Pass, value);
}
public void clickPass()throws PageException{
this.click(FacebookField.Pass);
}
public void clickLogIn()throws PageException{
this.click(FacebookField.LogIn);
}
public void setFirstname(final String value) throws PageException{
this.setValue(FacebookField.Firstname, value);
}
public void clickFirstname()throws PageException{
this.click(FacebookField.Firstname);
}
public void setLastname(final String value) throws PageException{
this.setValue(FacebookField.Lastname, value);
}
public void clickLastname()throws PageException{
this.click(FacebookField.Lastname);
}
public void setRegEmail(final String value) throws PageException{
this.setValue(FacebookField.RegEmail, value);
}
public void clickRegEmail()throws PageException{
this.click(FacebookField.RegEmail);
}
...
Lastminute.com
package com.dmcc.sample.pages.lastminute;
import com.dmcc.pagegen.exceptions.PageException;
import com.dmcc.sample.pages.lastminute.LastminuteField;
import com.dmcc.pagegen.page.mccarterp.McCarterPage;
public class LastminutePage extends McCarterPage{
private final String url="https://www.lastminute.com";
private final String rRoot="../pgenexamples/src/test/resources";
public LastminutePage navigate()throws PageException {
this.setResourcesRoot(rRoot);
this.navigate(url);
return this;
}
public void setDpSearchFrom(final String value) throws PageException{
this.setValue(LastminuteField.DpSearchFrom, value);
}
public void clickDpSearchFrom()throws PageException{
this.click(LastminuteField.DpSearchFrom);
}
public void setDpSearchTo(final String value) throws PageException{
this.setValue(LastminuteField.DpSearchTo, value);
}
public void clickDpSearchTo()throws PageException{
this.click(LastminuteField.DpSearchTo);
}
public void setDpCheckIn(final String value) throws PageException{
this.setValue(LastminuteField.DpCheckIn, value);
}
public void clickDpCheckIn()throws PageException{
this.click(LastminuteField.DpCheckIn);
}
public void setDpCheckOut(final String value) throws PageException{
this.setValue(LastminuteField.DpCheckOut, value);
}
public void clickDpCheckOut()throws PageException{
this.click(LastminuteField.DpCheckOut);
}
public void setGoingTo(final GoingToEnum value) throws PageException{
this.setValue(LastminuteField.GoingTo, value.toString());
}
public void clickGoingTo()throws PageException{
this.click(LastminuteField.GoingTo);
}
public void setLeavingFrom(final LeavingFromEnum value) throws PageException{
this.setValue(LastminuteField.LeavingFrom, value.toString());
}
public void clickLeavingFrom()throws PageException{
this.click(LastminuteField.LeavingFrom);
}
}
参考文献
- NEO PageFactory README: https://github.com/desmccarter/neopagefactory
- Selenium HQ PageFactory: http://toolsqa.com/selenium-cucumber-framework/page-object-design-pattern-with-selenium-pagefactory-in-cucumber/