请查看此内容,获取有关Serenity的先前帖子。
Serenity的核心在于BDD。
Serenity的理念是像实时文档一样进行测试。
在这篇博客文章中,我将与大家分享如何使用Cucumber和ScreenPlay Pattern在Serenity中实现UI测试。
而且不要忘了,如何创建漂亮而详细的报告,如下所示:
一,为什么是Cucumber
Cucumber是计算机程序员使用的一种软件工具,它支持行为驱动开发(BDD)。Cucumber BDD方法的核心是其纯语言解析器Gherkin。它允许以客户可以理解的逻辑语言来指定预期的软件行为。
通过使用Cucumber,我们将测试的意图与实现的方式分开了。
非技术人员(例如BA或PO)可以轻松地从功能文件(例如
Feature: Allow users to login to quang cao coc coc website
@Login
Scenario Outline: Login successfully with email and password
Given Navigate to quang cao coc coc login site
When Login with ' ' and ' '
Then Should navigate to home page site
Examples:
|email|password|
|xxxxxxxxxx|xxxxxxxxxx|
@Login
Scenario Outline: Login failed with invalid email
Given Navigate to quang cao coc coc login site
When Login with ' ' and ' '
Then Should prompt with ' '
Examples:
|email|password|errormessage|
|a|FernandoTorres12345#|abc@example.com|
二,实施
我们将通过使用带有Serenity的Cucumber进行工具测试所需的设置。
1.POM文件
我们需要在项目中使用 serenity-cucumber 。
因此,请确保为此添加依赖项:
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-cucumber</artifactId>
<version>1.9.45</version>
</dependency>
我们还需要添加一些插件来使用Maven构建serenity报告
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18</version>
<configuration>
<includes>
<include>**/features/**/When*.javanclude>
</includes>
<systemProperties>
<webdriver.driver>${webdriver.driver}</webdriver.driver>
</systemProperties>
</configuration>
</plugin>
<plugin>
<groupId>net.serenity-bdd.maven.plugins</groupId>
<artifactId>serenity-maven-plugin</artifactId>
<version>${serenity.maven.version}</version>
<executions>
<execution>
<id>serenity-reports</id>
<phase>post-integration-test</phase>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
2.安全配置文件
为了设置默认配置为serenity,我们可以使用serenity.conf文件或serenity.properties
在此示例中,我想向您展示serenity.conf:
webdriver {
base.url = "https://cp.qc.coccoc.com/sign-in?lang=vi-VN"
driver = chrome
}
headless.mode=false
serenity {
project.name = "Serenity Guidelines"
tag.failures = "true"
linked.tags = "issue"
restart.browser.for.each = scenario
take.screenshots = AFTER_EACH_STEP
console.headings = minimal
browser.maximized = true
}
jira {
url = "https://jira.tcbs.com.vn"
project = Auto
username = username
password = password
}
drivers {
windows {
webdriver.chrome.driver = src/main/resources/webdriver/windows/chromedriver.exe
}
mac {
webdriver.chrome.driver = src/main/resources/chromedriver
}
linux {
webdriver.chrome.driver = src/main/resources/webdriver/linux/chromedriver
}
}
我们定义了一些常见的东西,例如每种环境的驱动程序存储位置:
drivers {
windows {
webdriver.chrome.driver = src/main/resources/webdriver/windows/chromedriver.exe
}
mac {
webdriver.chrome.driver = src/main/resources/chromedriver
}
linux {
webdriver.chrome.driver = src/main/resources/webdriver/linux/chromedriver
}
}
或在每个步骤后截取屏幕截图:
serenity {
take.screenshots = AFTER_EACH_STEP
}
3.页面对象
经验丰富的自动化测试是可以以抽象的方式实施测试以更好地理解和维护的人员。
为了在UI中实施工具测试的最佳做法,我们应该始终为要与之交互的网页定义页面对象类。
在这种情况下,网页具有许多功能和元素,我们应该根据其覆盖的功能将页面对象分成多个对象,来实现更好的维护。
例如,使用qcCocCoc站点的LoginPage:
@DefaultUrl("https://cp.qc.coccoc.com/sign-in?lang=vi-VN")
public class LoginPage extends PageObject {
@FindBy(name = "email")
private WebElementFacade emailField;
@FindBy(name = "password")
private WebElementFacade passwordField;
@FindBy(css = "button[data-track_event-action='Login']")
private WebElementFacade btnLogin;
@FindBy(xpath = "//form[@method='post'][not(@name)]//div[@class='form-errors clearfix']")
private WebElementFacade errorMessageElement;
public void login(String email, String password) {
waitFor(emailField);
emailField.sendKeys(email);
passwordField.sendKeys(password);
btnLogin.click();
}
public String getMessageError(){
waitFor(errorMessageElement);
return errorMessageElement.getTextContent();
}
}
在这里,我们定义了如何查找Web元素,以及在该页面中需要使用什么方法。
通常,我们应该摆脱Thread.sleep,并像示例中那样找到更流畅的等待
public void login(String email, String password) {
waitFor(emailField);
emailField.sendKeys(email);
passwordField.sendKeys(password);
btnLogin.click();
}
在上面,我们将等待emailField出现,之后,我们将运行下一个脚本。
如果未出现该字段,则会发生超时错误。
4.按照Cucumber进行测试:
首先,您需要声明功能文件。
功能文件应位于test / resources / features文件夹中:
@Login
Scenario Outline: Login successfully with email and password
Given Navigate to quang cao coc coc login site
When Login with ' ' and ' '
Then Should navigate to home page site
Examples:
|email|password|
|xxxxxxxxxx|xxxxxxxxxx|
IntelliJ为我们提供了自动为每个步骤创建功能的方法,
您可以单击该步骤并按“ Alt + Enter”,然后按照指南进行操作
我通常将cucumber测试放在test / ui / cucumber / qc_coccoc中,并在步骤包中定义测试:
public class LoginPage extends BaseTest {
@Steps
private pages.qcCocCoc.LoginPage loginPage_pageobject;
@cucumber.api.java.en.Given("^Navigate to quang cao coc coc login site$")
public void navigateToQuangCaoCocCocLoginSite() {
loginPage_pageobject.open();
}
@When("^Login with '(.*)' and '(.*)'$")
public void loginWithEmailAndPassword(String email, String password) {
loginPage_pageobject.login(email,password);
}
@Then("^Should navigate to home page site$")
public void shouldNavigateToHomePageSite() {
WebDriverWait wait = new WebDriverWait(getDriver(),2);
wait.until(ExpectedConditions.urlContains("welcome"));
softAssertImpl.assertAll();
}
@Then("^Should prompt with '(.*)'$")
public void shouldPromptWithErrormessage(String errorMessage) {
softAssertImpl.assertThat("Verify message error",loginPage_pageobject.getMessageError().contains(errorMessage),true);
softAssertImpl.assertAll();
}
}
在这里,我们扩展了BaseTest以使用断言的好处。
可以使用正则表达式(如@When(“ ^使用'(。)'和'(。)'$”)来获取放置在功能文件中的电子邮件和密码的值。并定义函数的输入值(字符串电子邮件,字符串密码)
@RunWith(CucumberWithSerenity.class)
@CucumberOptions(features = "src/test/resources/features/qcCocCoc/", tags = { "@Login" }, glue = { "ui.cucumber.qc_coccoc.step" })
public class AcceptanceTest {
}
我们应该创建AcceptanceTest类,以更灵活的方式运行带有标签的测试。
我们需要指定功能文件“ src / test / resources / features / qcCocCoc /”的路径,以及步骤文件的路径:“ ui.cucumber.qc_coccoc.step”
5,如何进行测试
-
您可以通过右键单击场景从功能文件运行测试,然后选择在IDE中运行
-
或者,您可以从命令行运行:
mvn clean verify -Dtest = path_to_the_AcceptanceTest
6.Serenity 报告
要创建漂亮的Serenity报告,只需运行以下命令行
mvn clean verify -Dtest = path_to_the_AcceptanceTestserenity:aggregate
测试报告将为index.html,默认情况下位于target / site / serenity / index.html中
摘要报告将如下所示:
在“测试结果”选项卡中的每个步骤之后捕获屏幕截图:
与往常一样,您始终可以从github检出源代码:serenity-guideline
好极了。今天就这样。
如果您喜欢该博客文章,请发表意见。
原文链接:https://dev.to//cuongld2/serenity-automation-framework-part-2-4-automation-test-with-ui-using-cucumber-3n7b