2017-10-20 141 views
0

我已经写在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(); 
    } 

} 
+0

欢迎来到StackOverflow!请查看[提问问题指南](https://stackoverflow.com/help/asking),特别是[如何创建最小,完整和可验证示例](https://stackoverflow.com/help/MCVE) – AesSedai101

回答

1

使用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存储的脚本对象()来获取标签

0

如果你的测试是单线程的,你可以使用前的勾,以获得方案执行作为@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从步骤中检索正在执行的场景。