I am working on Selenium WebDriver with Java for automation and TestNG is my framework. I am running Login Test in which i log each step in the Extent report. I have a function for each step and for each step, i am attaching a screenshot.
I am unsure on how to name each screenshot with a unique descriptive name. I tried getting the current method(step) name but it seems like I need to create an anonymous class eveytime and eveywhere to get the current running method name as per the below code.
String name = new Object(){}.getClass().getEnclosingMethod().getName();
Here is my code.
@Test(priority = 0, testName="Verify Login")
public void login() throws Exception {
lp = new LoginPage(driver, test);
tm = new TabMenu(driver, test);
driver.get(Constants.url);
lp.verifyLoginPageLogo();
lp.setUserName("admin");
lp.setPassword("admin");
lp.clickLoginBtn();
tm.isCurrentTab("Dashboard");
}
public void verifyLoginPageLogo() throws IOException {
String name = new Object(){}.getClass().getEnclosingMethod().getName();
Assert.assertTrue(loginLogo.isDisplayed());
test.log(LogStatus.PASS, "Logo is displayed", Screenshots.takeScreenshot(driver, name, test));
}
public static String takeScreenshot(WebDriver driver, String name, ExtentTest test) throws IOException {
String directory = "C:\\Users\\JACK\\Documents\\eclipse-workspace\\OrangeHRM\\Screenshots\\";
String fileName = name + ".png";
File destFile = new File(directory + fileName);
TakesScreenshot ss = (TakesScreenshot) driver;
File sourceFile = ss.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, destFile);
String imgPath = test.addScreenCapture(directory+fileName);
return imgPath;
}
Is there any other way to do this?