Monday 7 March 2016

How to Take Screenshot in Appium ?



In any android automation tool capturing screenshot is very important part.In any android test suit if you take screenshot during your test failures then you can identify and capture the defect very quickly and effectively. The same screenshot you can share with developers to report your bug. Taking screenshot while running your automation tool saves lot of time for tester. In this post we will learn how we can take screenshot for our appium test case failure.

Check out the simple example which I have prepared for taking screenshot in appium test case failure. First create a method to captureScreenShot() in this method you can decide how you are going to name your directories and .png files for each screenshot. You can use date or testcase name to uniquely name your .png files.

In the below test case we will store our screenshots in the folder/dir named as screenshot. I will show you two ways to store the screen shot first using  current date and secondly using any name for your choice.

Video Tutorial -

 

First Method-
1. Use interface given by Web Driver "TakeScreenShot".
2. Get current date and time using DateFormat class.
3. Then store the it in screenshot folder with name set to current date and time.


public void CaptureScreenShot
{
    AppiumDriver driver;
    String folder_name;
    DateFormat df;
   @Before
    public void testCaseSetup()throws  Exception
    {

        DesiredCapabilities cap=new DesiredCapabilities();
        cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android device");
        cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "4000");
        cap.setCapability(MobileCapabilityType.APP, "c://apks//seekbarsample.apk");
        cap.setCapability("noReset", true);
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);

    }
    @Test
    public void testSeekBar()throws  Exception
    {
           //Locating seekbar using resource id
            WebElement seek_bar=driver.findElement(By.id("seek_bar"));
            // get start co-ordinate of seekbar
            int start=seek_bar.getLocation().getX();
            //Get width of seekbar
            int end=seek_bar.getSize().getWidth();
            //get location of seekbar vertically
            int y=seek_bar.getLocation().getY();

        // Select till which position you want to move the seekbar
        TouchAction action=new TouchAction(driver);

        //Move it will the end
        action.press(start,y).moveTo(end,y).release().perform();

        //Move it 40%
        int moveTo=(int)(end*0.4);
        action.press(start,y).moveTo(moveTo,y).release().perform();
        
        captureScreenShots();
    }
    public void captureScreenShots() throws IOException {
        folder_name="screenshot";
        File f=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        //Date format fot screenshot file name
        df=new  SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
        //create dir with given folder name
        new File(folder_name).mkdir();
        //Setting file name
        String file_name=df.format(new Date())+".png";
        //coppy screenshot file into screenshot folder.
        FileUtils.copyFile(f, new File(folder_name + "/" + file_name));
    }
    @After
    public void testCaseTearDown()
    {
        driver.quit();
    }
}

Second method-
1. Use interface given by Web Driver "TakeScreenShot".
2. Get the name from test case calling captureScreenShot() method.
3.  Then store the it in screenshot folder with name set to the string passes by test case.


public void CaptureScreenShot
{
    AppiumDriver driver;
    String folder_name;
    DateFormat df; 
    @Before
    public void testCaseSetup()throws  Exception
    {

        DesiredCapabilities cap=new DesiredCapabilities();
        cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android device");
        cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "4000");
        cap.setCapability(MobileCapabilityType.APP, "c://apks//seekbarsample.apk");
        cap.setCapability("noReset", true);
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);

    }
    @Test
    public void testSeekBar()throws  Exception
    {
           //Locating seekbar using resource id
            WebElement seek_bar=driver.findElement(By.id("seek_bar"));
            // get start co-ordinate of seekbar
            int start=seek_bar.getLocation().getX();
            //Get width of seekbar
            int end=seek_bar.getSize().getWidth();
            //get location of seekbar vertically
            int y=seek_bar.getLocation().getY();

        // Select till which position you want to move the seekbar
        TouchAction action=new TouchAction(driver);

        //Move it will the end
        action.press(start,y).moveTo(end,y).release().perform();

        //Move it 40%
        int moveTo=(int)(end*0.4);
        action.press(start,y).moveTo(moveTo,y).release().perform();
        
                captureScreenShots("Seekbar");
    }
   public void captureScreenShots(String file_name) throws IOException {
        folder_name="screenshot";
        File f=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        //create dir with given folder name
        new File(folder_name).mkdir();
        //Setting file name
        file_name=df.format(new Date())+".png";
        //coppy screenshot file into screenshot folder.
        FileUtils.copyFile(f,new File(folder_name + "/" + file_name));
    }
    @After
    public void testCaseTearDown()
    {
        driver.quit();
    }
}
I hope this tutorial will help you take screen shots in your appium test case.

With this tutorial you can write test code for taking screenshot when you are appium test case fails, taking screenshot for verifying UI  on multiple devices.

I hope you find this tutorial useful. Do share your feedback and questions in comments section below.Please follow QA Automated on social media to get latest post updates. Happy Testing :-)

7 comments: