0

Could someone help me in the below situation?

I've to create a common method to get screenshots using Selenium Webdriver where the file name of the screenshot should get updated as the name of the method where I call it.

This is what I have now:

Created a method to get the Timestamp and use it to the file name:

public String getTimeStamp() {
    String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
    return timestamp;
}

//  Method to get the Screenshot at any given instance. The Screenshots taken are copied to Screenshots folder under `"build\jbehave\view"`.
public void getScreenShots() throws Exception {
    File srcfile = driver.getScreenshotAs(OutputType.FILE);
    File folder = new File("Screenshots");

    if(folder.exists()){
    FileUtils.copyFile(srcfile, new File("./build/jbehave/view/Screenshots/" + "Screenshot_" + getTimeStamp() + ".png"));
    }else {

        File dir1 = new File("Screenshots");
        dir1.mkdir();
        FileUtils.copyFile(srcfile, new File("./build/jbehave/view/Screenshots/" + "Screenshot_" + getTimeStamp() + ".png"));

    }
    }

I'm looking for a method which can get the name of the methods where I call the above getScreenShots() method and have it as my file name in run time instead of having Timestamps.

Igor Tyulkanov
  • 5,487
  • 2
  • 32
  • 49

3 Answers3

2

You can get method name from stack trace of current thread:

byte[] scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
String filename = Thread.currentThread().getStackTrace()[1].toString();
FileUtils.writeByteArrayToFile(new File(filename), scrFile);

In this example, the filename is a method, which executes this fragment (it is 1 in stack trace).

Alex Derkach
  • 741
  • 5
  • 8
  • I'm using the below method to get the screenshot: File srcfile = driver.getScreenshotAs(OutputType.FILE); I'm converting the file type to .png while copying to a specific path. Could you please tell me if it can be done in the method that you mentioned? – Seshadri Ravi Feb 05 '16 at 22:57
  • just use File.renameTo method https://docs.oracle.com/javase/7/docs/api/java/io/File.html#renameTo(java.io.File) – Alex Derkach Feb 06 '16 at 21:17
  • Hi Alex, Thanks for the updates. But renameTo method as mentioned in the link that you shared states that the rename operation might not be able to move a file from one filesystem to another. Could you please add that code which can be used to copy the file (filename in this case) to a desired location? – Seshadri Ravi Feb 08 '16 at 16:40
0

You can use this to get the method of the running test. And then use the selenium driver to get the screenshot.

Community
  • 1
  • 1
elTomato
  • 333
  • 3
  • 11
0

You should be able to get the method from Thread.currentThread().getStackTrace(), but there can be issues: Javadocs.

bradimus
  • 2,472
  • 1
  • 16
  • 23
  • I'm just learning Selenium and JBehave now with little knowledge on Java. Could you please explain me what issues can prop up if we use Thread.currentThread().getStackTrace()? – Seshadri Ravi Feb 05 '16 at 23:01