简体   繁体   English

如何使用标签名称在黄瓜中截屏

[英]How to take screenshot in cucumber using tag name

I have the following code written in Selenium/Java but I want to parameterize this code and add the tag name for which the screenshot is taken: 我有以下用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();
    }

}

Use the Before hook and add the Scenario object as argument. 使用Before hook and add the Scenario object为参数。 Cucumber will inject this with the currently executing scenario. 黄瓜将其与当前正在执行的方案一起注入。

private Scenario sce;

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


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

You can access the stored scenario object in your step definitions to call the getSourceTagNames() to get tags 您可以在步骤定义中访问存储的方案对象,以调用getSourceTagNames()来获取标签。

If your tests are single-threaded you can use the Before Hook to get the scenario being executed as @Grasshoper mentioned and store it in a global variable and then access scenario from the step being executed to retrieve the tag names: 如果您的测试是单线程的,则可以使用“之前的钩子”以@Grasshoper所述的方式获取正在执行的方案并将其存储在全局变量中,然后从正在执行的步骤中访问方案以检索标记名:

private Scenario scenario;

@Before
public void setUp(Scenario scenario) {
  this.scenario = scenario;
}

@Then("^Take Screenshot$")
public void tearDown() {
  this.scenario.getSourceTagNames();
  ...
}

For multi-threaded executions I would use use a ConcurrentHashMap to maintain a link between the Thread ID and the Scenario being executed. 对于多线程执行,我将使用ConcurrentHashMap来维护线程ID和正在执行的方案之间的链接。 You can then retrieve the Scenario being executed from the Step using the Thread ID. 然后,您可以使用线程ID从步骤中检索正在执行的方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM