为什么要使用Serenity报告?
Serenity报告跟其他报告有一些区别,它包含了更丰富的内容,不单只是显示每个feature的运行结果,它包括了feature的内容,运行失败的原因,还有数据统计,feature里面的输入数据,还能加入自己想添加的其他内容
如何在cucumber里面使用Serentiy报告
1.POM文件
我们需要在项目中使用宁静黄瓜。 因此,请确保为此添加依赖项:
<!-- https://mvnrepository.com/artifact/net.serenity-bdd/serenity-cucumber -->
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-cucumber</artifactId>
<version>1.9.45</version>
</dependency>
我们还需要添加一些插件以使用Maven构建宁静报告
<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*.java</include>
</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
}
或在每个步骤之后进行截图:
<code>
serenity {
take.screenshots = AFTER_EACH_STEP
}
</code>
3.页面对象
经验丰富的自动化测试是可以以抽象的方式实施测试以更好地理解和维护的人员。
为了在UI中实施工具测试的最佳做法,我们应该始终为要与之交互的网页定义页面对象类。
在这种情况下,网页具有许多功能和元素,我们应该根据其覆盖的功能将页面对象分成多个对象,以实现更好的维护。
例如,在qc CocCoc网站的“登录页面”中:
@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();
}
}
4.按照黄瓜进行测试: 首先,您需要声明功能文件。 功能文件应位于test / resources / features文件夹中:
@Login
Scenario Outline: Login successfully with email and password
Given Navigate to quang cao coc coc login site
When Login with '<email>' and '<password>'
Then Should navigate to home page site
Examples:
|email|password|
|xxxxxxxxxx|xxxxxxxxxx|
IntelliJ为我们提供了自动为每个步骤创建功能的方法 您可以单击该步骤,然后按“ Alt + Enter”,然后按照指南进行操作
我通常将黄瓜测试放在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(“ ^^ Login with’(。,。)和’(。)'$“)并定义函数的输入值(字符串电子邮件,字符串密码)
@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报告,只需运行以下命令行
mvn clean verify -Dtest = path_to_the_AcceptanceTest宁静:汇总
测试报告将为index.html,默认情况下位于target / site / serenity / index.html中
摘要报告将如下所示:
如果你想记录其他内容可以如下
Serenity.recordReportData()
.withTitle(“Reported Transactions”)
.asEvidence()
.fromFile(Paths.get(“src/test/resources/data/transactions.csv”));
transactions.csv是我想把这里面的数据放到报告里面