我有以下用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 and add the Scenario object作为参数。黄瓜将其与当前正在执行的方案一起注入。

private Scenario sce;

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


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


您可以在步骤定义中访问存储的方案对象,以调用getSourceTagNames()来获取标签。

10-07 16:42
查看更多