每日分享最新,最流行的软件开发知识与最新行业趋势,希望大家能够一键三连,多多支持,跪求关注,点赞,留言。
如何在 Selenium WebDriver 中生成范围报告
在本文中,我们将学习如何在 Selenium WebDriver 中生成范围报告。欲了解更多信息,请立即阅读全文。
在本教程中,我们将学习如何在 Selenium WebDriver 中生成范围报告。
范围报告到底是什么?
Extent Reports 是一个开源报告框架,可用于测试自动化。它可以与 JUnit、NUnit、TestNG 等流行的测试结构无缝连接。这些报告是带有显示结果的饼图的 HTML 文档。它们还支持创建自定义日志、图片和其他数据。
当自动化测试脚本成功完成时,测试人员必须生成测试执行报告。虽然 TestNG 提供了失职报告,但它不提供信息。
在 Selenium 中使用范围报告
Selenium 范围报告包含两个常用的重要类。
ExtentReports 类
范围测试类
句法:
ExtentReports reports = new ExtentReports("Path of directory to store the resultant HTML file", true/false);
ExtentTest test = reports.startTest("TestName");
ExtentReports 类根据验证者提供的路由生成 HTML 报告。根据布尔标志,应重写现有报告,或准备新报告。“True”是默认值,这意味着将重写所有现有数据。
ExtentTest 类将测试步骤保存到以前生成的 HTML 报告中。
这两个类可以通过以下方式组合:
startTest:启动测试用例的先决条件。
endTest:满足以下测试用例条件。
日志:在生成的 HTML 报告中记录每个测试步骤的状态。
刷新: 从相关报告中删除所有先前的数据并生成新的数据。
测试状态可以用以下值表示:
经过
失败
跳过
信息
句法:
报告技术考虑了两个参数。 第一个是测试的状态,第二个是创建的报告中显示的消息。
报告技术考虑了两个参数。第一个是测试的状态,第二个是创建的报告中显示的消息。
如何在 Selenium 中生成范围报告?
导入 JAR 文件 degreereports-java-2.1.2.jar。下载 ZIP 文件后,将其内容解压缩到一个文件夹中。
通过选择 Build Path -> Set Build Path,将 JAR 文件添加到项目的构建路径中。
使用以下代码为 Scope Report 创建一个新的 JAVA 类。
package com.browserstack.demo;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
public class ExtentDemo {
static ExtentTest test;
static ExtentReports report;
@BeforeClass
public static void startTest()
{
report = new ExtentReports(System.getProperty("user.dir")+"ExtentReportResults.html");
test = report.startTest("ExtentDemo");
}
@Test
public void extentReportsDemo()
{
System.setProperty("webdriver.chrome.driver", "D:SubmittalExchange_TFSQAAutomation3rdpartychromechromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in");
if(driver.getTitle().equals("Google"))
{
test.log(LogStatus.PASS, "Navigated to the specified URL");
}
else
{
test.log(LogStatus.FAIL, "Test Failed");
}
}
@AfterClass
public static void endTest()
{
report.endTest(test);
report.flush();
}
}
用最聪明的方法开始测试。它还设置 Scope Reports 对象。任何有效的自定义路由都可以作为参数提供给 Scope Reports 对象。
@Test:此类自动执行以下操作:
使用URL打开 Chrome 浏览器。
当您打开页面时,请检查页面标题是否符合预期。
使用上述记录方法将测试用例状态记录为 PASS/FAIL。
@AfterClass:发布运行测试用例的条件:结束测试(使用 endTest 方法)并清除报告。
如何使用 NUnit 在 Selenium 中生成范围报告?
使用设置夹具:
[SetUpFixture]
public abstract class Base
{
protected ExtentReports _extent;
protected ExtentTest _test;
[OneTimeSetUp]
protected void Setup()
{
var dir = TestContext.CurrentContext.TestDirectory + "\\";
var fileName = this.GetType().ToString() + ".html";
var htmlReporter = new ExtentHtmlReporter(dir + fileName);
_extent = new ExtentReports();
_extent.AttachReporter(htmlReporter);
}
[OneTimeTearDown]
protected void TearDown()
{
_extent.Flush();
}
[TestFixture]
public class TestInitializeWithNullValues : Base
{
[Test]
public void TestNameNull()
{
Assert.Throws(() => testNameNull());
}
}
[SetUp]
public void BeforeTest()
{
_test = _extent.CreateTest(TestContext.CurrentContext.Test.Name);
}
[TearDown]
public void AfterTest()
{
var status = TestContext.CurrentContext.Result.Outcome.Status;
var stacktrace = string.IsNullOrEmpty(TestContext.CurrentContext.Result.StackTrace)
? ""
: string.Format("{0}", TestContext.CurrentContext.Result.StackTrace);
Status logstatus;
switch (status)
{
case TestStatus.Failed:
logstatus = Status.Fail;
break;
case TestStatus.Inconclusive:
logstatus = Status.Warning;
break;
case TestStatus.Skipped:
logstatus = Status.Skip;
break;
default:
logstatus = Status.Pass;
break;
}
_test.Log(logstatus, "Test ended with " + logstatus + stacktrace);
_extent.Flush();
}
}
如何在范围报告中捕获屏幕截图?
截屏可以让测试人员更好地确定软件在测试过程中出现故障时出现的问题。只有在测试失败时才截屏,因为它们会消耗大量内存。
尝试使用以下代码截取屏幕截图:
使用代码截取屏幕截图。
getScreenShotAs():此方法捕获当前 WebDriver 实例的屏幕截图,并将其保存为各种输出格式。
此方法捕获当前 WebDriver 实例的屏幕截图并将其保存为各种输出格式。
此方法生成一个文件对象,该对象保存在文件变量中。该方法需要将 Web 驱动程序实例提供给 Take Screenshot 函数。
该方法需要将 Web 驱动程序实例提供给 Take Screenshot 函数。
此语句在“src”文件夹中创建一个名为“ BStackImages ”的文件夹,并根据当前系统时间命名文件。
此语句在“src”文件夹中创建一个名为“BStackImages”的文件夹,并根据当前系统时间命名文件。
通过这些语句将所有错误图片复制到目标目录。
使用 log 方法,因为它捕获屏幕截图并使用 Extent Test 类的addScreenCapture 方法将其添加到范围报告中。
使用 log 方法,因为它捕获屏幕截图并使用 Extent Test 类的 addScreenCapture 方法将其添加到范围报告中。
使用范围报告的优点:
它们与 TestNG 和 JUnit 兼容。
如有必要,可以记录和显示测试的每个阶段的屏幕截图。
它们使测试人员能够跟踪单个测试套件中的各种测试用例。
它们代表完成测试所需的时间。
可以调整它们以图形方式描述测试的每个级别。