这是本节的多页打印视图。
点击此处打印.
返回本页常规视图.
鼓励的行为
Selenium项目的一些测试指南和建议.
关于"最佳实践"的注解:我们有意在本文档中避免使用"最佳实践"的说辞.
没有一种方法可以适用于所有情况.
我们更喜欢"指南和建议"的想法.
我们鼓励您通读这些内容, 并仔细地确定哪种方法适用于您的特定环境.
由于许多原因, 功能测试很难正确完成. 即便应用程序的状态, 复杂性, 依赖还不够让测试变得
足够复杂, 操作浏览器(特别是跨浏览器的兼容性测试)就已经使得写一个好的测试变成一种挑战.
Selenium提供了一些工具使得功能测试用户更简单的操作浏览器, 但是这些工具并不能帮助你来写一个好的
架构的测试套件. 这章我们会针对怎么来做web页面的功能测试的自动化给出一些忠告, 指南和建议.
这章记录了很多历年来成功的使用Selenium的用户的常用的软件设计模式.
1 - 测试自动化概述
首先,问问自己是否真的需要使用浏览器。
在某些情况下,如果您正在开发一个复杂的 web 应用程序,
您需要打开一个浏览器并进行实际测试,这种可能性是很大的。
然而,诸如 Selenium 之类的功能性最终用户测试运行起来很昂贵。
此外,它们通常需要大量的基础设施才能有效运行。
经常问问自己,您想要测试的东西是否可以使用更轻量级的测试方法(如单元测试)完成,
还是使用较低级的方法完成,这是一个很好的规则。
一旦确定您正在进行Web浏览器测试业务,
并且您的 Selenium 环境已经准备好开始编写测试,
您通常会执行以下三个步骤的组合:
您需要尽可能缩短这些步骤;
一到两个操作在大多数时间内应该足够了。
浏览器自动化具有“脆弱”的美誉,
但实际上那是因为用户经常对它要求过高。
在后面的章节中,我们将回到您可以使用的技术,
为了缓解测试中明显的间歇性问题,
特别是如何克服 浏览器 和 WebDriver 之间的竞争条件。
通过保持测试简短并仅在您完全没有替代方案时使用Web浏览器,您可以用最小的代码片段来完成很多测试。
Selenium测试的一个显著优势是,它能够从用户的角度测试应用程序的所有组件(从后端到前端)。
因此,换句话说,虽然功能测试运行起来可能很昂贵,但它们同时也包含了大量关键业务部分。
测试要求
如前所述,Selenium 测试运行起来可能很昂贵。
在多大程度上取决于您正在运行测试的浏览器,
但历史上浏览器的行为变化太大,以至于通常是针对多个浏览器进行交叉测试的既定目标。
Selenium 允许您在多个操作系统上的多个浏览器上运行相同的指令,
但是对所有可能的浏览器、它们的不同版本以及它们所运行的许多操作系统的枚举将很快成为一项繁重的工作。
让我们从一个例子开始
Larry 写了一个网站,允许用户订购他们自己定制的独角兽。
一般的工作流程(我们称之为“幸福之路”)是这样的:
- 创建一个账户
- 配置他们的独角兽
- 添加到购物车
- 检验并付款
- 给出关于他们独角兽的反馈
编写一个宏大的 Selenium 脚本来执行所有这些操作是很诱人的 — 很多人都会尝试这样做。
抵制诱惑!
这样做会导致测试:
a) 需要很长时间;
b) 会受到一些与页面呈现时间问题有关的常见问题的影响;
c) 如果失败,它不会给出一个简洁的、“可检查”的方法来诊断出了什么问题。
测试此场景的首选策略是将其分解为一系列独立的、快速的测试,每个测试都有一个存在的“理由”。
假设您想测试第二步:
配置您的独角兽。
它将执行以下操作:
请注意,我们跳过了这些步骤的其余部分,
在完成这一步之后,我们将在其他小的、离散的测试用例中测试工作流的其余部分。
首先,您需要创建一个帐户。在这里您可以做出一些选择:
- 您想使用现有帐户吗?
- 您想创建一个新帐户吗?
- 在配置开始之前,是否需要考虑有任何特殊属性的用户需要吗?
不管您如何回答这个问题,
解决方案是让它成为测试中“设置数据”部分的一部分 - 如果 Larry 公开了一个 API,
使您(或任何人)能够创建和更新用户帐户,
一定要用它来回答这个问题
请确保使用这个 API 来回答这个问题 — 如果可能的话,
您希望只有在您拥有一个用户之后才启动浏览器,您可以使用该用户的凭证进行登录。
如果每个工作流的每个测试都是从创建用户帐户开始的,那么每个测试的执行都会增加许多秒。
调用 API 并与数据库进行通信是快速、“无头”的操作,
不需要打开浏览器、导航到正确页面、点击并等待表单提交等昂贵的过程。
理想情况下,您可以在一行代码中处理这个设置阶段,这些代码将在任何浏览器启动之前执行:
// Create a user who has read-only permissions--they can configure a unicorn,
// but they do not have payment information set up, nor do they have
// administrative privileges. At the time the user is created, its email
// address and password are randomly generated--you don't even need to
// know them.
User user = UserFactory.createCommonUser(); //This method is defined elsewhere.
// Log in as this user.
// Logging in on this site takes you to your personal "My Account" page, so the
// AccountPage object is returned by the loginAs method, allowing you to then
// perform actions from the AccountPage.
AccountPage accountPage = loginAs(user.getEmail(), user.getPassword());
# Create a user who has read-only permissions--they can configure a unicorn,
# but they do not have payment information set up, nor do they have
# administrative privileges. At the time the user is created, its email
# address and password are randomly generated--you don't even need to
# know them.
user = user_factory.create_common_user() #This method is defined elsewhere.
# Log in as this user.
# Logging in on this site takes you to your personal "My Account" page, so the
# AccountPage object is returned by the loginAs method, allowing you to then
# perform actions from the AccountPage.
account_page = login_as(user.get_email(), user.get_password())
// Create a user who has read-only permissions--they can configure a unicorn,
// but they do not have payment information set up, nor do they have
// administrative privileges. At the time the user is created, its email
// address and password are randomly generated--you don't even need to
// know them.
User user = UserFactory.CreateCommonUser(); //This method is defined elsewhere.
// Log in as this user.
// Logging in on this site takes you to your personal "My Account" page, so the
// AccountPage object is returned by the loginAs method, allowing you to then
// perform actions from the AccountPage.
AccountPage accountPage = LoginAs(user.Email, user.Password);
# Create a user who has read-only permissions--they can configure a unicorn,
# but they do not have payment information set up, nor do they have
# administrative privileges. At the time the user is created, its email
# address and password are randomly generated--you don't even need to
# know them.
user = UserFactory.create_common_user #This method is defined elsewhere.
# Log in as this user.
# Logging in on this site takes you to your personal "My Account" page, so the
# AccountPage object is returned by the loginAs method, allowing you to then
# perform actions from the AccountPage.
account_page = login_as(user.email, user.password)
// Create a user who has read-only permissions--they can configure a unicorn,
// but they do not have payment information set up, nor do they have
// administrative privileges. At the time the user is created, its email
// address and password are randomly generated--you don't even need to
// know them.
var user = userFactory.createCommonUser(); //This method is defined elsewhere.
// Log in as this user.
// Logging in on this site takes you to your personal "My Account" page, so the
// AccountPage object is returned by the loginAs method, allowing you to then
// perform actions from the AccountPage.
var accountPage = loginAs(user.email, user.password);
// Create a user who has read-only permissions--they can configure a unicorn,
// but they do not have payment information set up, nor do they have
// administrative privileges. At the time the user is created, its email
// address and password are randomly generated--you don't even need to
// know them.
val user = UserFactory.createCommonUser() //This method is defined elsewhere.
// Log in as this user.
// Logging in on this site takes you to your personal "My Account" page, so the
// AccountPage object is returned by the loginAs method, allowing you to then
// perform actions from the AccountPage.
val accountPage = loginAs(user.getEmail(), user.getPassword())
您可以想象,UserFactory
可以扩展为提供诸如createAdminUser()
、createUserWithPayment()
的方法。
关键是,这两行代码不会分散您对此测试的最终目的的注意力:
配置独角兽。
页面对象模型
的复杂性将在后面的章节中讨论,但我们将在这里介绍这个概念:
您的测试应该由操作组成,从用户的角度出发,在站点的页面上下文中执行。
这些页面被存储为对象,
其中包含关于 web 页面如何组成以及如何执行操作的特定信息 — 作为测试人员,您应该很少关注这些信息。
您想要什么样的独角兽?
您可能想要粉红色,但不一定。
紫色最近很流行。
她需要太阳镜吗?
明星纹身?
这些选择虽然困难,但是作为测试人员,
您的主要关注点是 — 您需要确保您的订单履行中心将正确的独角兽发送给正确的人,而这就要从这些选择开始。
请注意,我们在该段落中没有讨论按钮,字段,下拉菜单,单选按钮或 Web 表单。
您的测试也不应该!
您希望像尝试解决问题的用户一样编写代码。
这是一种方法(从前面的例子继续):
// The Unicorn is a top-level Object--it has attributes, which are set here.
// This only stores the values; it does not fill out any web forms or interact
// with the browser in any way.
Unicorn sparkles = new Unicorn("Sparkles", UnicornColors.PURPLE, UnicornAccessories.SUNGLASSES, UnicornAdornments.STAR_TATTOOS);
// Since we're already "on" the account page, we have to use it to get to the
// actual place where you configure unicorns. Calling the "Add Unicorn" method
// takes us there.
AddUnicornPage addUnicornPage = accountPage.addUnicorn();
// Now that we're on the AddUnicornPage, we will pass the "sparkles" object to
// its createUnicorn() method. This method will take Sparkles' attributes,
// fill out the form, and click submit.
UnicornConfirmationPage unicornConfirmationPage = addUnicornPage.createUnicorn(sparkles);
# The Unicorn is a top-level Object--it has attributes, which are set here.
# This only stores the values; it does not fill out any web forms or interact
# with the browser in any way.
sparkles = Unicorn("Sparkles", UnicornColors.PURPLE, UnicornAccessories.SUNGLASSES, UnicornAdornments.STAR_TATTOOS)
# Since we're already "on" the account page, we have to use it to get to the
# actual place where you configure unicorns. Calling the "Add Unicorn" method
# takes us there.
add_unicorn_page = account_page.add_unicorn()
# Now that we're on the AddUnicornPage, we will pass the "sparkles" object to
# its createUnicorn() method. This method will take Sparkles' attributes,
# fill out the form, and click submit.
unicorn_confirmation_page = add_unicorn_page.create_unicorn(sparkles)
// The Unicorn is a top-level Object--it has attributes, which are set here.
// This only stores the values; it does not fill out any web forms or interact
// with the browser in any way.
Unicorn sparkles = new Unicorn("Sparkles", UnicornColors.Purple, UnicornAccessories.Sunglasses, UnicornAdornments.StarTattoos);
// Since we are already "on" the account page, we have to use it to get to the
// actual place where you configure unicorns. Calling the "Add Unicorn" method
// takes us there.
AddUnicornPage addUnicornPage = accountPage.AddUnicorn();
// Now that we're on the AddUnicornPage, we will pass the "sparkles" object to
// its createUnicorn() method. This method will take Sparkles' attributes,
// fill out the form, and click submit.
UnicornConfirmationPage unicornConfirmationPage = addUnicornPage.CreateUnicorn(sparkles);
# The Unicorn is a top-level Object--it has attributes, which are set here.
# This only stores the values; it does not fill out any web forms or interact
# with the browser in any way.
sparkles = Unicorn.new('Sparkles', UnicornColors.PURPLE, UnicornAccessories.SUNGLASSES, UnicornAdornments.STAR_TATTOOS)
# Since we're already "on" the account page, we have to use it to get to the
# actual place where you configure unicorns. Calling the "Add Unicorn" method
# takes us there.
add_unicorn_page = account_page.add_unicorn
# Now that we're on the AddUnicornPage, we will pass the "sparkles" object to
# its createUnicorn() method. This method will take Sparkles' attributes,
# fill out the form, and click submit.
unicorn_confirmation_page = add_unicorn_page.create_unicorn(sparkles)
// The Unicorn is a top-level Object--it has attributes, which are set here.
// This only stores the values; it does not fill out any web forms or interact
// with the browser in any way.
var sparkles = new Unicorn("Sparkles", UnicornColors.PURPLE, UnicornAccessories.SUNGLASSES, UnicornAdornments.STAR_TATTOOS);
// Since we are already "on" the account page, we have to use it to get to the
// actual place where you configure unicorns. Calling the "Add Unicorn" method
// takes us there.
var addUnicornPage = accountPage.addUnicorn();
// Now that we're on the AddUnicornPage, we will pass the "sparkles" object to
// its createUnicorn() method. This method will take Sparkles' attributes,
// fill out the form, and click submit.
var unicornConfirmationPage = addUnicornPage.createUnicorn(sparkles);
// The Unicorn is a top-level Object--it has attributes, which are set here.
// This only stores the values; it does not fill out any web forms or interact
// with the browser in any way.
val sparkles = Unicorn("Sparkles", UnicornColors.PURPLE, UnicornAccessories.SUNGLASSES, UnicornAdornments.STAR_TATTOOS)
// Since we are already "on" the account page, we have to use it to get to the
// actual place where you configure unicorns. Calling the "Add Unicorn" method
// takes us there.
val addUnicornPage = accountPage.addUnicorn()
// Now that we're on the AddUnicornPage, we will pass the "sparkles" object to
// its createUnicorn() method. This method will take Sparkles' attributes,
// fill out the form, and click submit.
unicornConfirmationPage = addUnicornPage.createUnicorn(sparkles)
既然您已经配置好了独角兽,
您需要进入第三步:确保它确实有效。
// The exists() method from UnicornConfirmationPage will take the Sparkles
// object--a specification of the attributes you want to see, and compare
// them with the fields on the page.
Assert.assertTrue("Sparkles should have been created, with all attributes intact", unicornConfirmationPage.exists(sparkles));
# The exists() method from UnicornConfirmationPage will take the Sparkles
# object--a specification of the attributes you want to see, and compare
# them with the fields on the page.
assert unicorn_confirmation_page.exists(sparkles), "Sparkles should have been created, with all attributes intact"
// The exists() method from UnicornConfirmationPage will take the Sparkles
// object--a specification of the attributes you want to see, and compare
// them with the fields on the page.
Assert.True(unicornConfirmationPage.Exists(sparkles), "Sparkles should have been created, with all attributes intact");
# The exists() method from UnicornConfirmationPage will take the Sparkles
# object--a specification of the attributes you want to see, and compare
# them with the fields on the page.
expect(unicorn_confirmation_page.exists?(sparkles)).to be, 'Sparkles should have been created, with all attributes intact'
// The exists() method from UnicornConfirmationPage will take the Sparkles
// object--a specification of the attributes you want to see, and compare
// them with the fields on the page.
assert(unicornConfirmationPage.exists(sparkles), "Sparkles should have been created, with all attributes intact");
// The exists() method from UnicornConfirmationPage will take the Sparkles
// object--a specification of the attributes you want to see, and compare
// them with the fields on the page.
assertTrue("Sparkles should have been created, with all attributes intact", unicornConfirmationPage.exists(sparkles))
请注意,测试人员在这段代码中除了谈论独角兽之外还没有做任何事情 — 没有按钮、定位器和浏览器控件。
这种对应用程序建模的方法允许您保持这些测试级别的命令不变,
即使 Larry 下周决定不再喜欢 Ruby-on-Rails,
并决定用最新的带有 Fortran 前端的 Haskell 绑定重新实现整个站点。
为了符合站点的重新设计,您的页面对象需要进行一些小的维护,但是这些测试将保持不变。
采用这一基本设计,您将希望继续使用尽可能少的面向浏览器的步骤来完成您的工作流。
您的下一个工作流程将包括在购物车中添加独角兽。
您可能需要多次迭代此测试,以确保购物车正确地保持其状态:
在开始之前,购物车中是否有多个独角兽?
购物车能装多少?
如果您创建多个具有相同名称或特性,它会崩溃吗?
它将只保留现有的一个还是添加另一个?
每次通过工作流时,您都希望尽量避免创建帐户、以用户身份登录和配置独角兽。
理想情况下,您将能够创建一个帐户,并通过 API 或数据库预先配置独角兽。
然后,您只需作为用户登录,找到 Sparkles,并将它添加到购物车中。
是否自动化?
自动化总是有优势吗? 什么时候应该决定去自动化测试用例?
自动化测试用例并不总是有利的. 有时候手动测试可能更合适. 例如,如果应用程序的用户界面,在不久的将来会发生很大变化,那么任何自动化都可能需要重写. 此外,有时根本没有足够的时间来构建自动化测试. 从短期来看,手动测试可能更有效. 如果应用程序的截止日期非常紧迫,当前没有可用的自动化测试,并且必须在特定时间范围内完成,那么手动测试是最好的解决方案.
2 - 设计模式和开发策略
Most of the documentation found in this section is still in English.
Please note we are not accepting pull requests to translate this content
as translating documentation of legacy components does not add value to
the community nor the project.
(previously located: https://github.com/SeleniumHQ/selenium/wiki/Bot-Style-Tests)
Overview
Over time, projects tend to accumulate large numbers of tests. As the total number of tests increases,
it becomes harder to make changes to the codebase — a single “simple” change may
cause numerous tests to fail, even though the application still works properly.
Sometimes these problems are unavoidable, but when they do occur you want to be
up and running again as quickly as possible. The following design patterns and
strategies have been used before with WebDriver to help to make tests easier to write and maintain. They may help you too.
DomainDrivenDesign: Express your tests in the language of the end-user of the app.
PageObjects: A simple abstraction of the UI of your web app.
LoadableComponent: Modeling PageObjects as components.
BotStyleTests: Using a command-based approach to automating tests, rather than the object-based approach that PageObjects encourage
Loadable Component
What Is It?
The LoadableComponent is a base class that aims to make writing PageObjects
less painful. It does this by providing a standard way of ensuring that pages
are loaded and providing hooks to make debugging the failure of a page to load
easier. You can use it to help reduce the amount of boilerplate code in your
tests, which in turn make maintaining your tests less tiresome.
There is currently an implementation in Java that ships as part of Selenium 2,
but the approach used is simple enough to be implemented in any language.
Simple Usage
As an example of a UI that we’d like to model, take a look at
the new issue page.
From the point of view of a test author, this offers the service of being
able to file a new issue. A basic Page Object would look like:
package com.example.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class EditIssue {
private final WebDriver driver;
public EditIssue(WebDriver driver) {
this.driver = driver;
}
public void setSummary(String summary) {
WebElement field = driver.findElement(By.name("summary"));
clearAndType(field, summary);
}
public void enterDescription(String description) {
WebElement field = driver.findElement(By.name("comment"));
clearAndType(field, description);
}
public IssueList submit() {
driver.findElement(By.id("submit")).click();
return new IssueList(driver);
}
private void clearAndType(WebElement field, String text) {
field.clear();
field.sendKeys(text);
}
}
In order to turn this into a LoadableComponent, all we need to do is to set that as the base type:
public class EditIssue extends LoadableComponent<EditIssue> {
// rest of class ignored for now
}
This signature looks a little unusual, but it all means is that
this class represents a LoadableComponent that loads the EditIssue page.
By extending this base class, we need to implement two new methods:
@Override
protected void load() {
driver.get("https://github.com/SeleniumHQ/selenium/issues/new");
}
@Override
protected void isLoaded() throws Error {
String url = driver.getCurrentUrl();
assertTrue("Not on the issue entry page: " + url, url.endsWith("/new"));
}
The load
method is used to navigate to the page, whilst the isLoaded
method
is used to determine whether we are on the right page. Although the
method looks like it should return a boolean, instead it performs a
series of assertions using JUnit’s Assert class. There can be as
few or as many assertions as you like. By using these assertions
it’s possible to give users of the class clear information that
can be used to debug tests.
With a little rework, our PageObject looks like:
package com.example.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import static junit.framework.Assert.assertTrue;
public class EditIssue extends LoadableComponent<EditIssue> {
private final WebDriver driver;
// By default the PageFactory will locate elements with the same name or id
// as the field. Since the summary element has a name attribute of "summary"
// we don't need any additional annotations.
private WebElement summary;
// Same with the submit element, which has the ID "submit"
private WebElement submit;
// But we'd prefer a different name in our code than "comment", so we use the
// FindBy annotation to tell the PageFactory how to locate the element.
@FindBy(name = "comment") private WebElement description;
public EditIssue(WebDriver driver) {
this.driver = driver;
// This call sets the WebElement fields.
PageFactory.initElements(driver, this);
}
@Override
protected void load() {
driver.get("https://github.com/SeleniumHQ/selenium/issues/new");
}
@Override
protected void isLoaded() throws Error {
String url = driver.getCurrentUrl();
assertTrue("Not on the issue entry page: " + url, url.endsWith("/new"));
}
public void setSummary(String issueSummary) {
clearAndType(summary, issueSummary);
}
public void enterDescription(String issueDescription) {
clearAndType(description, issueDescription);
}
public IssueList submit() {
submit.click();
return new IssueList(driver);
}
private void clearAndType(WebElement field, String text) {
field.clear();
field.sendKeys(text);
}
}
That doesn’t seem to have bought us much, right? One thing it has done
is encapsulate the information about how to navigate to the page into
the page itself, meaning that this information’s not scattered through
the code base. It also means that we can do this in our tests:
EditIssue page = new EditIssue(driver).get();
This call will cause the driver to navigate to the page if that’s necessary.
Nested Components
LoadableComponents start to become more useful when they are used in
conjunction with other LoadableComponents. Using our example, we could
view the “edit issue” page as a component within a project’s website
(after all, we access it via a tab on that site). You also need to be
logged in to file an issue. We could model this as a tree of nested components:
+ ProjectPage
+---+ SecuredPage
+---+ EditIssue
What would this look like in code? For a start, each logical component
would have its own class. The “load” method in each of them would “get”
the parent. The end result, in addition to the EditIssue class above is:
ProjectPage.java:
package com.example.webdriver;
import org.openqa.selenium.WebDriver;
import static org.junit.Assert.assertTrue;
public class ProjectPage extends LoadableComponent<ProjectPage> {
private final WebDriver driver;
private final String projectName;
public ProjectPage(WebDriver driver, String projectName) {
this.driver = driver;
this.projectName = projectName;
}
@Override
protected void load() {
driver.get("http://" + projectName + ".googlecode.com/");
}
@Override
protected void isLoaded() throws Error {
String url = driver.getCurrentUrl();
assertTrue(url.contains(projectName));
}
}
and SecuredPage.java:
package com.example.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import static org.junit.Assert.fail;
public class SecuredPage extends LoadableComponent<SecuredPage> {
private final WebDriver driver;
private final LoadableComponent<?> parent;
private final String username;
private final String password;
public SecuredPage(WebDriver driver, LoadableComponent<?> parent, String username, String password) {
this.driver = driver;
this.parent = parent;
this.username = username;
this.password = password;
}
@Override
protected void load() {
parent.get();
String originalUrl = driver.getCurrentUrl();
// Sign in
driver.get("https://www.google.com/accounts/ServiceLogin?service=code");
driver.findElement(By.name("Email")).sendKeys(username);
WebElement passwordField = driver.findElement(By.name("Passwd"));
passwordField.sendKeys(password);
passwordField.submit();
// Now return to the original URL
driver.get(originalUrl);
}
@Override
protected void isLoaded() throws Error {
// If you're signed in, you have the option of picking a different login.
// Let's check for the presence of that.
try {
WebElement div = driver.findElement(By.id("multilogin-dropdown"));
} catch (NoSuchElementException e) {
fail("Cannot locate user name link");
}
}
}
The “load” method in EditIssue now looks like:
@Override
protected void load() {
securedPage.get();
driver.get("https://github.com/SeleniumHQ/selenium/issues/new");
}
This shows that the components are all “nested” within each other.
A call to get()
in EditIssue will cause all its dependencies to load too. The example usage:
public class FooTest {
private EditIssue editIssue;
@Before
public void prepareComponents() {
WebDriver driver = new FirefoxDriver();
ProjectPage project = new ProjectPage(driver, "selenium");
SecuredPage securedPage = new SecuredPage(driver, project, "example", "top secret");
editIssue = new EditIssue(driver, securedPage);
}
@Test
public void demonstrateNestedLoadableComponents() {
editIssue.get();
editIssue.setSummary("Summary");
editIssue.enterDescription("This is an example");
}
}
If you’re using a library such as Guiceberry in your tests,
the preamble of setting up the PageObjects can be omitted leading to nice, clear, readable tests.
Bot Pattern
(previously located: https://github.com/SeleniumHQ/selenium/wiki/Bot-Style-Tests)
Although PageObjects are a useful way of reducing duplication in your tests,
it’s not always a pattern that teams feel comfortable following.
An alternative approach is to follow a more “command-like” style of testing.
A “bot” is an action-oriented abstraction over the raw Selenium APIs.
This means that if you find that commands aren’t doing the Right Thing
for your app, it’s easy to change them. As an example:
public class ActionBot {
private final WebDriver driver;
public ActionBot(WebDriver driver) {
this.driver = driver;
}
public void click(By locator) {
driver.findElement(locator).click();
}
public void submit(By locator) {
driver.findElement(locator).submit();
}
/**
* Type something into an input field. WebDriver doesn't normally clear these
* before typing, so this method does that first. It also sends a return key
* to move the focus out of the element.
*/
public void type(By locator, String text) {
WebElement element = driver.findElement(locator);
element.clear();
element.sendKeys(text + "\n");
}
}
Once these abstractions have been built and duplication in your tests
identified, it’s possible to layer PageObjects on top of bots.
3 - 测试类型
验收测试
进行这种类型的测试以确定功能或系统是否满足客户的期望和要求.
这种测试通常涉及客户的合作或反馈,
是一种验证活动,
可以用于回答以下问题:
我们是否在制造 正确的 产品?
对于Web应用程序,
可以通过模拟用户期望的行为
直接使用Selenium来完成此测试的自动化.
可以通过记录/回放, 或通过本文档中介绍的各种支持的语言来完成此类模拟.
注意:有些人可能还会提到, 验收测试是 功能测试 的子类型.
功能测试
进行这种类型的测试是为了确定功能或系统是否正常运行而没有问题.
它会在不同级别检查系统,
以确保涵盖所有方案并且系统能够执行预期的 工作 .
这是一个验证活动, 它回答了以下问题:
我们是否在 正确地 制造产品?
这通常包括:
测试没有错误 (404, 异常…) ,
以可用的方式 (正确的重定向) 正常运行,
以可访问的方式并匹配其规格 (请参见前述的 验收测试 ) .
对于Web应用程序,
可以通过模拟预期的结果, 直接使用Selenium来完成此测试的自动化.
可以通过记录/回放或通过本文档中说明的各种支持的语言来完成此模拟.
性能测试
顾名思义, 进行性能测试是为了衡量应用程序的性能.
性能测试主要有两种类型:
负载测试
进行了负载测试,
以验证应用程序在各种特定的负载
(通常是同时连接一定数量的用户) 下的运行状况
压力测试
进行压力测试,
以验证应用程序在压力 (或高于最大支持负载) 下的运行状况.
通常, 性能测试是通过执行一些Selenium书写的测试来完成的,
这些测试模拟了不同的用户
使用Web应用程序的特定功能
并检索了一些有意义的指标.
通常, 这是由其他检索指标的工具完成的.
JMeter 就是这样一种工具.
对于Web应用程序,
要测量的详细信息包括
吞吐量、 延迟、数据丢失、单个组件加载时间…
注意1:所有浏览器的开发人员工具
均具有“性能”标签 (可通过按F12进行访问)
注2:这属于 非功能测试 的类型,
因为它通常是按系统而不是按功能/特征进行测量.
回归测试
此测试通常在修改, 修复或添加功能之后进行.
为了确保所做的更改没有破坏任何现有功能,
将再次执行一些已经执行过的测试.
重新执行的测试集可以是全部或部分,
并且可以包括几种不同的类型,
具体取决于具体的应用程序和开发团队.
测试驱动开发 (TDD)
TDD本身不是一种测试类型,
而是一种迭代开发方法,
用于测试驱动功能的设计.
每个周期都从创建功能应通过的一组单元测试开始
(这将使首次执行失败) .
此后, 进行开发以使测试通过.
在另一个周期开始再次执行测试,
此过程一直持续到所有测试通过为止.
如此做的目的是基于以下情况,
既缺陷发现的时间越早成本越低,
从而加快了应用程序的开发.
行为驱动开发 (BDD)
BDD还是基于上述 (TDD) 的迭代开发方法,
其目的是让各方参与到应用程序的开发中.
每个周期都从创建一些规范开始 (应该失败) .
然后创建会失败的单元测试 (也应该失败) ,
之后着手开发.
重复此循环, 直到所有类型的测试通过.
为此, 使用了规范语言.
因为简单、标准和明确, 各方都应该能够理解.
大多数工具都使用 Gherkin 作为这种语言.
目标是为了解决潜在的验收问题,
从而能够检测出比TDD还要多的错误,
并使各方之间的沟通更加顺畅.
当前有一组工具可用于编写规范
并将其与编码功能 (例如 Cucumber 或 SpecFlow ) 匹配.
在Selenium之上构建了一系列工具,
可通过将BDD规范直接转换为可执行代码, 使得上述过程更为迅速.
例如 JBehave, Capybara 和 Robot Framework.
4 - 指南和建议
Selenium项目的一些测试指南和建议.
关于"最佳实践"的注解:我们有意在本文档中避免使用"最佳实践"的说辞.
没有一种方法可以适用于所有情况.
我们更喜欢"指南和建议"的想法.
我们鼓励您通读这些内容, 并仔细地确定哪种方法适用于您的特定环境.
由于许多原因, 功能测试很难正确完成. 即便应用程序的状态, 复杂性, 依赖还不够让测试变得
足够复杂, 操作浏览器(特别是跨浏览器的兼容性测试)就已经使得写一个好的测试变成一种挑战.
Selenium提供了一些工具使得功能测试用户更简单的操作浏览器, 但是这些工具并不能帮助你来写一个好的
架构的测试套件. 这章我们会针对怎么来做web页面的功能测试的自动化给出一些忠告, 指南和建议.
这章记录了很多历年来成功的使用Selenium的用户的常用的软件设计模式.
4.1 - PO设计模式
Note: this page has merged contents from multiple sources, including
the Selenium wiki
Overview
Within your web app’s UI, there are areas where your tests interact with.
A Page Object only models these as objects within the test code.
This reduces the amount of duplicated code and means that if the UI changes,
the fix needs only to be applied in one place.
Page Object is a Design Pattern that has become popular in test automation for
enhancing test maintenance and reducing code duplication. A page object is an
object-oriented class that serves as an interface to a page of your AUT. The
tests then use the methods of this page object class whenever they need to
interact with the UI of that page. The benefit is that if the UI changes for
the page, the tests themselves don’t need to change, only the code within the
page object needs to change. Subsequently, all changes to support that new UI
are located in one place.
Advantages
- There is a clean separation between the test code and page-specific code, such as
locators (or their use if you’re using a UI Map) and layout.
- There is a single repository for the services or operations the page offers
rather than having these services scattered throughout the tests.
In both cases, this allows any modifications required due to UI changes to all
be made in one place. Helpful information on this technique can be found on
numerous blogs as this ‘test design pattern’ is becoming widely used. We
encourage readers who wish to know more to search the internet for blogs
on this subject. Many have written on this design pattern and can provide
helpful tips beyond the scope of this user guide. To get you started,
we’ll illustrate page objects with a simple example.
Examples
First, consider an example, typical of test automation, that does not use a
page object:
/***
* Tests login feature
*/
public class Login {
public void testLogin() {
// fill login data on sign-in page
driver.findElement(By.name("user_name")).sendKeys("userName");
driver.findElement(By.name("password")).sendKeys("my supersecret password");
driver.findElement(By.name("sign-in")).click();
// verify h1 tag is "Hello userName" after login
driver.findElement(By.tagName("h1")).isDisplayed();
assertThat(driver.findElement(By.tagName("h1")).getText(), is("Hello userName"));
}
}
There are two problems with this approach.
- There is no separation between the test method and the AUT’s locators (IDs in
this example); both are intertwined in a single method. If the AUT’s UI changes
its identifiers, layout, or how a login is input and processed, the test itself
must change.
- The ID-locators would be spread in multiple tests, in all tests that had to
use this login page.
Applying the page object techniques, this example could be rewritten like this
in the following example of a page object for a Sign-in page.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
/**
* Page Object encapsulates the Sign-in page.
*/
public class SignInPage {
protected WebDriver driver;
// <input name="user_name" type="text" value="">
private By usernameBy = By.name("user_name");
// <input name="password" type="password" value="">
private By passwordBy = By.name("password");
// <input name="sign_in" type="submit" value="SignIn">
private By signinBy = By.name("sign_in");
public SignInPage(WebDriver driver){
this.driver = driver;
if (!driver.getTitle().equals("Sign In Page")) {
throw new IllegalStateException("This is not Sign In Page," +
" current page is: " + driver.getCurrentUrl());
}
}
/**
* Login as valid user
*
* @param userName
* @param password
* @return HomePage object
*/
public HomePage loginValidUser(String userName, String password) {
driver.findElement(usernameBy).sendKeys(userName);
driver.findElement(passwordBy).sendKeys(password);
driver.findElement(signinBy).click();
return new HomePage(driver);
}
}
and page object for a Home page could look like this.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
/**
* Page Object encapsulates the Home Page
*/
public class HomePage {
protected WebDriver driver;
// <h1>Hello userName</h1>
private By messageBy = By.tagName("h1");
public HomePage(WebDriver driver){
this.driver = driver;
if (!driver.getTitle().equals("Home Page of logged in user")) {
throw new IllegalStateException("This is not Home Page of logged in user," +
" current page is: " + driver.getCurrentUrl());
}
}
/**
* Get message (h1 tag)
*
* @return String message text
*/
public String getMessageText() {
return driver.findElement(messageBy).getText();
}
public HomePage manageProfile() {
// Page encapsulation to manage profile functionality
return new HomePage(driver);
}
/* More methods offering the services represented by Home Page
of Logged User. These methods in turn might return more Page Objects
for example click on Compose mail button could return ComposeMail class object */
}
So now, the login test would use these two page objects as follows.
/***
* Tests login feature
*/
public class TestLogin {
@Test
public void testLogin() {
SignInPage signInPage = new SignInPage(driver);
HomePage homePage = signInPage.loginValidUser("userName", "password");
assertThat(homePage.getMessageText(), is("Hello userName"));
}
}
There is a lot of flexibility in how the page objects may be designed, but
there are a few basic rules for getting the desired maintainability of your
test code.
Assertions in Page Objects
Page objects themselves should never make verifications or assertions. This is
part of your test and should always be within the test’s code, never in an page
object. The page object will contain the representation of the page, and the
services the page provides via methods but no code related to what is being
tested should be within the page object.
There is one, single, verification which can, and should, be within the page
object and that is to verify that the page, and possibly critical elements on
the page, were loaded correctly. This verification should be done while
instantiating the page object. In the examples above, both the SignInPage and
HomePage constructors check that the expected page is available and ready for
requests from the test.
Page Component Objects
A page object does not necessarily need to represent all the parts of a
page itself. The same principles used for page objects can be used to
create “Page Component Objects” that represent discrete chunks of the
page and can be included in page objects. These component objects can
provide references to the elements inside those discrete chunks, and
methods to leverage the functionality provided by them.
For example, a Product page has multiple products.
<!-- Products Page -->
<div class="header_container">
<span class="title">Products</span>
</div>
<div class="inventory_list">
<div class="inventory_item">
</div>
<div class="inventory_item">
</div>
<div class="inventory_item">
</div>
<div class="inventory_item">
</div>
<div class="inventory_item">
</div>
<div class="inventory_item">
</div>
</div>
Each product is a component of the Products page.
<!-- Inventory Item -->
<div class="inventory_item">
<div class="inventory_item_name">Backpack</div>
<div class="pricebar">
<div class="inventory_item_price">$29.99</div>
<button id="add-to-cart-backpack">Add to cart</button>
</div>
</div>
The Product page HAS-A list of products. This relationship is called Composition. In simpler terms, something is composed of another thing.
public abstract class BasePage {
protected WebDriver driver;
public BasePage(WebDriver driver) {
this.driver = driver;
}
}
// Page Object
public class ProductsPage extends BasePage {
public ProductsPage(WebDriver driver) {
super(driver);
// No assertions, throws an exception if the element is not loaded
new WebDriverWait(driver, Duration.ofSeconds(3))
.until(d -> d.findElement(By.className("header_container")));
}
// Returning a list of products is a service of the page
public List<Product> getProducts() {
return driver.findElements(By.className("inventory_item"))
.stream()
.map(e -> new Product(e)) // Map WebElement to a product component
.toList();
}
// Return a specific product using a boolean-valued function (predicate)
// This is the behavioral Strategy Pattern from GoF
public Product getProduct(Predicate<Product> condition) {
return getProducts()
.stream()
.filter(condition) // Filter by product name or price
.findFirst()
.orElseThrow();
}
}
The Product component object is used inside the Products page object.
public abstract class BaseComponent {
protected WebElement root;
public BaseComponent(WebElement root) {
this.root = root;
}
}
// Page Component Object
public class Product extends BaseComponent {
// The root element contains the entire component
public Product(WebElement root) {
super(root); // inventory_item
}
public String getName() {
// Locating an element begins at the root of the component
return root.findElement(By.className("inventory_item_name")).getText();
}
public BigDecimal getPrice() {
return new BigDecimal(
root.findElement(By.className("inventory_item_price"))
.getText()
.replace("$", "")
).setScale(2, RoundingMode.UNNECESSARY); // Sanitation and formatting
}
public void addToCart() {
root.findElement(By.id("add-to-cart-backpack")).click();
}
}
So now, the products test would use the page object and the page component object as follows.
public class ProductsTest {
@Test
public void testProductInventory() {
var productsPage = new ProductsPage(driver);
var products = productsPage.getProducts();
assertEquals(6, products.size()); // expected, actual
}
@Test
public void testProductPrices() {
var productsPage = new ProductsPage(driver);
// Pass a lambda expression (predicate) to filter the list of products
// The predicate or "strategy" is the behavior passed as parameter
var backpack = productsPage.getProduct(p -> p.getName().equals("Backpack"));
var bikeLight = productsPage.getProduct(p -> p.getName().equals("Bike Light"));
assertEquals(new BigDecimal("29.99"), backpack.getPrice());
assertEquals(new BigDecimal("9.99"), bikeLight.getPrice());
}
}
The page and component are represented by their own objects. Both objects only have methods for the services they offer, which matches the real-world application in object-oriented programming.
You can even
nest component objects inside other component objects for more complex
pages. If a page in the AUT has multiple components, or common
components used throughout the site (e.g. a navigation bar), then it
may improve maintainability and reduce code duplication.
Other Design Patterns Used in Testing
There are other design patterns that also may be used in testing. Some use a
Page Factory for instantiating their page objects. Discussing all of these is
beyond the scope of this user guide. Here, we merely want to introduce the
concepts to make the reader aware of some of the things that can be done. As
was mentioned earlier, many have blogged on this topic and we encourage the
reader to search for blogs on these topics.
Implementation Notes
PageObjects can be thought of as facing in two directions simultaneously. Facing toward the developer of a test, they represent the services offered by a particular page. Facing away from the developer, they should be the only thing that has a deep knowledge of the structure of the HTML of a page (or part of a page) It’s simplest to think of the methods on a Page Object as offering the “services” that a page offers rather than exposing the details and mechanics of the page. As an example, think of the inbox of any web-based email system. Amongst the services it offers are the ability to compose a new email, choose to read a single email, and list the subject lines of the emails in the inbox. How these are implemented shouldn’t matter to the test.
Because we’re encouraging the developer of a test to try and think about the services they’re interacting with rather than the implementation, PageObjects should seldom expose the underlying WebDriver instance. To facilitate this, methods on the PageObject should return other PageObjects. This means we can effectively model the user’s journey through our application. It also means that should the way that pages relate to one another change (like when the login page asks the user to change their password the first time they log into a service when it previously didn’t do that), simply changing the appropriate method’s signature will cause the tests to fail to compile. Put another way; we can tell which tests would fail without needing to run them when we change the relationship between pages and reflect this in the PageObjects.
One consequence of this approach is that it may be necessary to model (for example) both a successful and unsuccessful login; or a click could have a different result depending on the app’s state. When this happens, it is common to have multiple methods on the PageObject:
public class LoginPage {
public HomePage loginAs(String username, String password) {
// ... clever magic happens here
}
public LoginPage loginAsExpectingError(String username, String password) {
// ... failed login here, maybe because one or both of the username and password are wrong
}
public String getErrorMessage() {
// So we can verify that the correct error is shown
}
}
The code presented above shows an important point: the tests, not the PageObjects, should be responsible for making assertions about the state of a page. For example:
public void testMessagesAreReadOrUnread() {
Inbox inbox = new Inbox(driver);
inbox.assertMessageWithSubjectIsUnread("I like cheese");
inbox.assertMessageWithSubjectIsNotUnread("I'm not fond of tofu");
}
could be re-written as:
public void testMessagesAreReadOrUnread() {
Inbox inbox = new Inbox(driver);
assertTrue(inbox.isMessageWithSubjectIsUnread("I like cheese"));
assertFalse(inbox.isMessageWithSubjectIsUnread("I'm not fond of tofu"));
}
Of course, as with every guideline, there are exceptions, and one that is commonly seen with PageObjects is to check that the WebDriver is on the correct page when we instantiate the PageObject. This is done in the example below.
Finally, a PageObject need not represent an entire page. It may represent a section that appears frequently within a site or page, such as site navigation. The essential principle is that there is only one place in your test suite with knowledge of the structure of the HTML of a particular (part of a) page.
Summary
- The public methods represent the services that the page offers
- Try not to expose the internals of the page
- Generally don’t make assertions
- Methods return other PageObjects
- Need not represent an entire page
- Different results for the same action are modelled as different methods
Example
public class LoginPage {
private final WebDriver driver;
public LoginPage(WebDriver driver) {
this.driver = driver;
// Check that we're on the right page.
if (!"Login".equals(driver.getTitle())) {
// Alternatively, we could navigate to the login page, perhaps logging out first
throw new IllegalStateException("This is not the login page");
}
}
// The login page contains several HTML elements that will be represented as WebElements.
// The locators for these elements should only be defined once.
By usernameLocator = By.id("username");
By passwordLocator = By.id("passwd");
By loginButtonLocator = By.id("login");
// The login page allows the user to type their username into the username field
public LoginPage typeUsername(String username) {
// This is the only place that "knows" how to enter a username
driver.findElement(usernameLocator).sendKeys(username);
// Return the current page object as this action doesn't navigate to a page represented by another PageObject
return this;
}
// The login page allows the user to type their password into the password field
public LoginPage typePassword(String password) {
// This is the only place that "knows" how to enter a password
driver.findElement(passwordLocator).sendKeys(password);
// Return the current page object as this action doesn't navigate to a page represented by another PageObject
return this;
}
// The login page allows the user to submit the login form
public HomePage submitLogin() {
// This is the only place that submits the login form and expects the destination to be the home page.
// A seperate method should be created for the instance of clicking login whilst expecting a login failure.
driver.findElement(loginButtonLocator).submit();
// Return a new page object representing the destination. Should the login page ever
// go somewhere else (for example, a legal disclaimer) then changing the method signature
// for this method will mean that all tests that rely on this behaviour won't compile.
return new HomePage(driver);
}
// The login page allows the user to submit the login form knowing that an invalid username and / or password were entered
public LoginPage submitLoginExpectingFailure() {
// This is the only place that submits the login form and expects the destination to be the login page due to login failure.
driver.findElement(loginButtonLocator).submit();
// Return a new page object representing the destination. Should the user ever be navigated to the home page after submiting a login with credentials
// expected to fail login, the script will fail when it attempts to instantiate the LoginPage PageObject.
return new LoginPage(driver);
}
// Conceptually, the login page offers the user the service of being able to "log into"
// the application using a user name and password.
public HomePage loginAs(String username, String password) {
// The PageObject methods that enter username, password & submit login have already defined and should not be repeated here.
typeUsername(username);
typePassword(password);
return submitLogin();
}
}
Support in WebDriver
There is a PageFactory in the support package that provides support for this pattern and helps to remove some boiler-plate code from your Page Objects at the same time.
4.2 - 领域特定语言
领域特定语言 (DSL) 是一种为用户提供解决问题的表达方式的系统.
它使用户可以按照自己的术语与系统进行交互, 而不仅仅是通过程序员的语言.
您的用户通常并不关心您网站的外观.
他们不在乎装饰, 动画或图形.
他们希望借助于您的系统, 以最小的难度使新员工融入整个流程;
他们想预订去阿拉斯加的旅行;
他们想以折扣价配置和购买独角兽.
您作为测试人员的工作应尽可能接近"捕捉”这种思维定势.
考虑到这一点, 我们开始着手"建模”您正在工作的应用程序,
以使测试脚本 (发布前用户仅有的代理) “说话”并代表用户.
在Selenium中, DSL通常由方法表示,
其编写方式使API简单易读-它们使开发人员和干系人
(用户, 产品负责人, 商业智能专家等) 之间能够产生汇报.
好处
- 可读: 业务关系人可以理解.
- 可写: 易于编写, 避免不必要的重复.
- 可扩展: 可以 (合理地) 添加功能而无需打破约定以及现有功能.
- 可维护: 通过将实现细节排除在测试用例之外, 您可以很好地隔离 AUT* 的修改.
Java
以下是Java中合理的DSL方法的示例.
为简便起见, 假定 driver
对象是预定义的并且可用于该方法.
/**
* Takes a username and password, fills out the fields, and clicks "login".
* @return An instance of the AccountPage
*/
public AccountPage loginAsUser(String username, String password) {
WebElement loginField = driver.findElement(By.id("loginField"));
loginField.clear();
loginField.sendKeys(username);
// Fill out the password field. The locator we're using is "By.id", and we should
// have it defined elsewhere in the class.
WebElement passwordField = driver.findElement(By.id("password"));
passwordField.clear();
passwordField.sendKeys(password);
// Click the login button, which happens to have the id "submit".
driver.findElement(By.id("submit")).click();
// Create and return a new instance of the AccountPage (via the built-in Selenium
// PageFactory).
return PageFactory.newInstance(AccountPage.class);
}
此方法完全从测试代码中抽象出输入字段, 按钮, 单击甚至页面的概念.
使用这种方法, 测试人员要做的就是调用此方法.
这给您带来了维护方面的优势: 如果登录字段曾经更改过,
则只需更改此方法-而非您的测试.
public void loginTest() {
loginAsUser("cbrown", "cl0wn3");
// Now that we're logged in, do some other stuff--since we used a DSL to support
// our testers, it's as easy as choosing from available methods.
do.something();
do.somethingElse();
Assert.assertTrue("Something should have been done!", something.wasDone());
// Note that we still haven't referred to a button or web control anywhere in this
// script...
}
郑重强调: 您的主要目标之一应该是编写一个API,
该API允许您的测试解决 当前的问题, 而不是UI的问题.
用户界面是用户的次要问题–用户并不关心用户界面, 他们只是想完成工作.
您的测试脚本应该像用户希望做的事情以及他们想知道的事情的完整清单那样易于阅读.
测试不应该考虑UI如何要求您去做.
*AUT: 待测系统
4.3 - 生成应用程序状态
Selenium不应用于准备测试用例.
测试用例中所有重复性动作和准备工作, 都应通过其他方法来完成.
例如, 大多数Web UI都具有身份验证 (诸如一个登录表单) .
在每次测试之前通过Web浏览器进行登录的消除, 将提高测试的速度和稳定性.
应该创建一种方法来获取对 AUT* 的访问权限 (例如, 使用API登录并设置Cookie) .
此外, 不应使用Selenium创建预加载数据来进行测试的方法.
如前所述, 应利用现有的API为 AUT* 创建数据.
*AUT: 待测系统
4.4 - 模拟外部服务
消除对外部服务的依赖性将大大提高测试的速度和稳定性.
4.5 - 改善报告
Selenium并非旨在报告测试用例的运行状态.
利用单元测试框架的内置报告功能是一个好的开始.
大多数单元测试框架都有可以生成xUnit或HTML格式的报告.
xUnit报表很受欢迎, 可以将其结果导入到持续集成(CI)服务器,
例如Jenkins、Travis、Bamboo等.
以下是一些链接, 可获取关于几种语言报表输出的更多信息.
NUnit 3 Console Runner
NUnit 3 Console Command Line
xUnit getting test results in TeamCity
xUnit getting test results in CruiseControl.NET
xUnit getting test results in Azure DevOps
4.6 - 避免共享状态
尽管在多个地方都提到过, 但这点仍值得被再次提及. 确保测试相互隔离.
-
不要共享测试数据.
想象一下有几个测试, 每个测试都会在选择操作执行之前查询数据库中的有效订单.
如果两个测试采用相同的顺序, 则很可能会出现意外行为.
-
清理应用程序中过时的数据, 这些数据可能会被其他测试.
例如无效的订单记录.
-
每次测试都创建一个新的WebDriver实例.
这在确保测试隔离的同时可以保障并行化更为简单.
4.7 - 使用定位器的提示
何时使用哪些定位器以及如何在代码中最好地管理它们.
这里有一些 支持的定位策略 的例子 .
一般来说,如果 HTML 的 id 是可用的、唯一的且是可预测的,那么它就是在页面上定位元素的首选方法。它们的工作速度非常快,可以避免复杂的 DOM 遍历带来的大量处理。
如果没有唯一的 id,那么最好使用写得好的 CSS 选择器来查找元素。XPath 和 CSS 选择器一样好用,但是它语法很复杂,并且经常很难调试。尽管 XPath 选择器非常灵活,但是他们通常未经过浏览器厂商的性能测试,并且运行速度很慢。
基于链接文本和部分链接文本的选择策略有其缺点,即只能对链接元素起作用。此外,它们在 WebDriver 内部调用 querySelectorAll 选择器。
标签名可能是一种危险的定位元素的方法。页面上经常出现同一标签的多个元素。这在调用 findElements(By) 方法返回元素集合的时候非常有用。
建议您尽可能保持定位器的紧凑性和可读性。使用 WebDriver 遍历 DOM 结构是一项性能花销很大的操作,搜索范围越小越好。
4.8 - 测试的独立性
将每个测试编写为独立的单元.
以不依赖于其他测试完成的方式编写测试:
例如有一个内容管理系统,
您可以借助其创建一些自定义内容,
这些内容在发布后作为模块显示在您的网站上,
并且CMS和应用程序之间的同步可能需要一些时间.
测试模块的一种错误方法是在测试中创建并发布内容,
然后在另一测试中检查该模块.
这是不可取的, 因为发布后内容可能无法立即用于其他测试.
与之相反的事, 您可以创建在受影响的测试中打开和关闭的打桩内容, 并将其用于验证模块.
而且, 对于内容的创建, 您仍然可以进行单独的测试.
4.9 - 考虑使用Fluent API
Martin Fowler创造了术语 “Fluent API”.
Selenium已经在其 FluentWait
类中实现了类似的东西, 这是对标准 Wait
类的替代.
您可以在页面对象中启用Fluent API设计模式, 然后使用如下代码段查询Google搜索页面:
driver.get( "http://www.google.com/webhp?hl=en&tab=ww" );
GoogleSearchPage gsp = new GoogleSearchPage(driver);
gsp.setSearchString().clickSearchButton();
Google页面对象类具有这种流畅行为后可能看起来像这样:
public abstract class BasePage {
protected WebDriver driver;
public BasePage(WebDriver driver) {
this.driver = driver;
}
}
public class GoogleSearchPage extends BasePage {
public GoogleSearchPage(WebDriver driver) {
super(driver);
// Generally do not assert within pages or components.
// Effectively throws an exception if the lambda condition is not met.
new WebDriverWait(driver, Duration.ofSeconds(3)).until(d -> d.findElement(By.id("logo")));
}
public GoogleSearchPage setSearchString(String sstr) {
driver.findElement(By.id("gbqfq")).sendKeys(sstr);
return this;
}
public void clickSearchButton() {
driver.findElement(By.id("gbqfb")).click();
}
}
4.10 - 每次测试都刷新浏览器
每次测试都从一个干净的已知状态开始.
理想情况下, 为每次测试打开一个新的虚拟机.
如果打开新虚拟机不切实际, 则至少应为每次测试启动一个新的WebDriver.
对于Firefox, 请使用您已知的配置文件去启动WebDriver.
大多数浏览器驱动器,像GeckoDriver和ChromeDriver那样,默认都会以干净的已知状态和一个新的用户配置文件开始。
WebDriver driver = new FirefoxDriver();
5 - 不鼓励的行为
使用Selenium自动化浏览器时应避免的事项.
5.1 - 验证码
验证码 (CAPTCHA), 是 全自动区分计算机和人类的图灵测试 (Completely Automated Public Turing test to tell Computers and Humans Apart) 的简称,
是被明确地设计用于阻止自动化的, 所以不要尝试!
规避验证码的检查, 主要有两个策略:
- 在测试环境中禁用验证码
- 添加钩子以允许测试绕过验证码
5.2 - 文件下载
虽然可以通过在Selenium的控制下单击浏览器的链接来开始下载, 但是API并不会暴露下载进度, 因此这是一种不理想的测试下载文件的方式.
因为下载文件并非模拟用户与Web平台交互的重要方面. 取而代之的是, 应使用Selenium(以及任何必要的cookie)查找链接, 并将其传递给例如libcurl这样的HTTP请求库.
HtmlUnit driver
可以通过实现AttachmentHandler
接口将附件作为输入流进行访问来下载附件.
可以将AttachmentHandler添加到
HtmlUnit WebClient.
5.3 - HTTP响应码
对于Selenium RC中的某些浏览器配置,
Selenium充当了浏览器和自动化站点之间的代理.
这意味着可以捕获或操纵通过Selenium传递的所有浏览器流量.
captureNetworkTraffic()
方法旨在捕获浏览器和自动化站点之间的所有网络流量,包括HTTP响应码.
Selenium WebDriver是一种完全不同的浏览器自动化实现,
它更喜欢表现得像用户一样,这种方式来自于基于WebDriver编写测试的方式.
在自动化功能测试中,检查状态码并不是测试失败的特别重要的细节, 之前的步骤更重要.
浏览器将始终呈现HTTP状态代码,例如404或500错误页面.
遇到这些错误页面时,一种“快速失败”的简单方法是
在每次加载页面后检查页面标题或可信赖点的内容(例如 <h1>
标签).
如果使用的是页面对象模型,则可以将此检查置于类构造函数中或类似于期望的页面加载的位置.
有时,HTTP代码甚至可能出现在浏览器的错误页面中,
您可以使用WebDriver读取此信息并改善调试输出.
检查网页本身的一种理想实践是符合WebDriver的呈现以及用户的视角.
如果您坚持,捕获HTTP状态代码的高级解决方案是复刻Selenium RC的行为去使用代理.
WebDriver API提供了为浏览器设置代理的功能,
并且有许多代理可以通过编程方式来操纵发送到Web服务器和从Web服务器接收的请求的内容.
使用代理可以决定如何响应重定向响应代码.
此外,并非每个浏览器都将响应代码提供给WebDriver,
因此选择使用代理可以使您拥有适用于每个浏览器的解决方案.
5.4 - Gmail, email 和 Facebook 登录
由于多种原因, 不建议使用WebDriver登录Gmail和Facebook等网站.
除了违反这些网站的使用条款之外 (您可能会面临帐户被关闭的风险) ,
还有其运行速度缓慢且不可靠的因素.
理想的做法是使用电子邮件供应商提供的API,
或者对于Facebook, 使用开发者工具的服务,
该服务是被用于创建测试帐户、朋友等内容的API.
尽管使用API可能看起来有些额外的工作量,
但是您将获得基于速度、可靠性和稳定性的回报.
API不会频繁更改, 但是网页和HTML定位符经常变化,
并且需要您更新测试框架的代码.
在任何时候测试使用WebDriver登录第三方站点,
都会增加测试失败的风险,
因为这会使您的测试时间更长.
通常的经验是, 执行时间较长的测试会更加脆弱和不可靠.
符合W3C conformant
的WebDriver实现,
也会使用 WebDriver
的属性对 navigator
对象进行注释,
用于缓解拒绝服务的攻击.
5.5 - 测试依赖
关于自动化测试的一个常见想法和误解是关于特定的测试顺序. 您的测试应该能够以任何顺序运行,而不是依赖于完成其他测试才能成功.
5.6 - 性能测试
通常不建议使用Selenium和WebDriver进行性能测试.
并非因为不能做, 只是缺乏针对此类工作的优化,
因而难以得到乐观的结果.
对于用户而言, 在用户上下文中执行性能测试似乎是自然而然的选择,
但是WebDriver的测试会受到许多外部和内部的影响而变得脆弱,
这是您无法控制的. 例如,
浏览器的启动速度, HTTP服务器的速度,
托管JavaScript或CSS的第三方服务器的响应
以及WebDriver实现本身检测的损失.
这些因素的变化会影响结果.
很难区分网站自身与外部资源之间的性能差异,
并且也很难明确浏览器中使用WebDriver对性能的影响,
尤其是在注入脚本时.
另一个潜在的吸引点是"节省时间"-同时执行功能和性能测试.
但是, 功能和性能测试分别具有截然不同的目标.
要测试功能, 测试人员可能需要耐心等待加载,
但这会使性能测试结果蒙上阴影, 反之亦然.
为了提高网站的性能, 您需要不依赖于环境的差异来分析整体性能,
识别不良代码的实践,
对单个资源 (即CSS或JavaScript) 的性能进行细分
以了解需要改进的地方.
有很多性能测试工具已经可以完成这项工作,
并且提供了报告和分析结果, 甚至可以提出改进建议.
例如一种易于使用的 (开源) 软件包是: JMeter
5.7 - 爬取链接
建议您不要使用WebDriver来通过链接进行爬网,
并非因为无法完成,而是因为它绝对不是最理想的工具。
WebDriver需要一些时间来启动,并且可能要花几秒钟到一分钟的时间,
具体取决于测试的编写方式,仅仅是为了获取页面并遍历DOM.
除了使用WebDriver之外,
您还可以通过执行 curl 命令或
使用诸如BeautifulSoup之类的库来节省大量时间,
因为这些方法不依赖于创建浏览器和导航至页面.
通过不使用WebDriver可以节省大量时间.
5.8 - 双因素认证
双因素认证通常简写成 2FA 是一种一次性密码(OTP)通常用在移动应用上例如“谷歌认证器”,
“微软认证器”等等,或者通过短信或者邮件来认证。在Selenium自动化中这些都是影响有效自动化
的极大挑战。虽然也有一些方法可以自动化这些过程,但是同样对于Selenium自动化也引入了很多不安全因素。
所以你应该要避免对2FA自动化。
这里有一些对于如何绕过2FA校验的建议:
- 在测试环境中对特定用户禁止2FA校验,这样对于这些特定用户可以直接进行自动化测试。
- 禁止2FA校验在测试环境中。
- 对于特定IP区域禁止2FA校验,这样我们可以配置测试机器的IP在这些白名单区域中。