2017-10-20 143 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從步驟中檢索正在執行的場景。