我有以下用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();
}
}
发布于 2017-10-20 10:11:12
使用Before hook and add the Scenario object
作为参数。Cucumber将使用当前执行的场景注入此脚本。
private Scenario sce;
@Before
public void beforeHook(Scenario scenario) {
this.sce = scenario
List<String> tags = sce.getSourceTagNames();
}
您可以在步骤定义中访问存储的方案对象以调用getSourceTagNames()来获取标签
发布于 2017-10-20 21:48:11
如果您的测试是单线程的,您可以使用之前的钩子来获取正在执行的场景,就像@Grasshoper提到的那样,并将其存储在全局变量中,然后从正在执行的步骤中访问场景来检索标记名:
private Scenario scenario;
@Before
public void setUp(Scenario scenario) {
this.scenario = scenario;
}
@Then("^Take Screenshot$")
public void tearDown() {
this.scenario.getSourceTagNames();
...
}
对于多线程执行,我将使用ConcurrentHashMap
来维护线程ID和正在执行的场景之间的链接。然后,您可以使用线程ID从步骤中检索正在执行的方案。
https://stackoverflow.com/questions/46843648
复制相似问题