gpt4 book ai didi

java - 如何使用标签名称在 cucumber 中截图

转载 作者:行者123 更新时间:2023-12-02 12:06:53 24 4
gpt4 key购买 nike

我用 Selenium/Java 编写了以下代码,但我想参数化此代码并添加为其截取屏幕截图的标签名称:

@Then("^Take Screenshot$")
public void tearDown() {
// take the screenshot at the end of every test
String location = "D:/ubdd/screenshots/";
DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy h-m-s");
Date date = new Date();
File scrFile =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// now save the screenshto to a file some place
try {
FileUtils.copyFile(scrFile, new File(location +
dateFormat.format(date)+".png"));
System.out.println("Screenshot saved");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

最佳答案

使用Before Hook 并添加 Scenario 对象 作为参数。 Cucumber 会将其注入(inject)当前正在执行的场景中。

private Scenario sce;

@Before
public void beforeHook(Scenario scenario) {
this.sce = scenario


List<String> tags = sce.getSourceTagNames();
}

您可以访问步骤定义中存储的场景对象来调用 getSourceTagNames() 来获取标签

关于java - 如何使用标签名称在 cucumber 中截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46843648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com