Saturday 2 July 2022

How to Take specific Element screenshot in Appium



Mobile Automation Testing Appium INterview Q&A SET - 01

How To Create Android Emulator Device

Mobile AUtomation Testing Appium Interview Q&A Set-02

Top API Interview Question 1-10



Lets us learn how to take screenshot for the specific element, following method i'm using to get screenshot, infact we can use this as a uitility class also and reuse it in the BaseTest Class.


public static String takeScreenshot(AppiumDriver<MobileElement> driver, MobileElement ele)
{
 
  File screenshotLocation = null;
  try{
  File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
 
  BufferedImage  fullImg = ImageIO.read(scrFile);

  //Get the location of the element on the page
  Point point = ele.getLocation();

  //Get width and height of the element
  int eleWidth = ele.getSize().getWidth();
  int eleHeight = ele.getSize().getHeight();

  //Crop the entire page screenshot to get only element screenshot
  BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth,
      eleHeight);
  ImageIO.write(eleScreenshot, "png", scrFile);

  String path = "screenshots/" + UUID.randomUUID() + "" + ".png";
 
  screenshotLocation = new File(System.getProperty("user.dir") + "/" + path);
  FileUtils.copyFile(scrFile, screenshotLocation);
 
      System.out.println(screenshotLocation.toString());
 
 
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
  return screenshotLocation.toString();


  }


******************

What are the Locators used for Mobile Automation Testing?


Ans: Click Here For Mobile Testing Locators


**************************

Complete code for taking screenshot of specific element:


package appium;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;

public class TakeElementScreenshot {

AppiumDriver<MobileElement> driver;

@BeforeClass
public void setUp() throws MalformedURLException
{
DesiredCapabilities cap=new DesiredCapabilities();
    cap.setCapability("deviceName", "emulator-5554");
    cap.setCapability("udid", "emulator-5554");
    cap.setCapability("appActivity", "com.android.calculator2.Calculator");
    cap.setCapability("appPackage", "com.android.calculator2");
    cap.setCapability("platformName", "Android");
    cap.setCapability("platformVersion", "9.0");
    driver=new AppiumDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), cap);
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}

@Test
public void testExample(){

MobileElement oneBtn=driver.findElement(By.id("com.android.calculator2:id/digit_1"));
oneBtn.click();
MobileElement plusBtn=driver.findElement(By.id("com.android.calculator2:id/op_add"));
plusBtn.click();
MobileElement fiveBtn=driver.findElement(By.id("com.android.calculator2:id/digit_5"));
fiveBtn.click();
MobileElement equalToBtn=driver.findElement(By.id("com.android.calculator2:id/eq"));
equalToBtn.click();

//passing equalToBtn element to take screenshot
elementScreenshot(driver,equalToBtn);
}



  @AfterClass
public void tearDown(){
driver.quit();
}


public static String elementScreenshot(AppiumDriver<MobileElement> driver,MobileElement ele)
{

File screenshotLocation = null;
try{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

BufferedImage  fullImg = ImageIO.read(scrFile);


//Get the location of element on the page
Point point = ele.getLocation();


//Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();


//Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth,
    eleHeight);
ImageIO.write(eleScreenshot, "png", scrFile);

  String path = "screenshots/" + UUID.randomUUID() + "" + ".png";

screenshotLocation = new File(System.getProperty("user.dir") + "/" + path);
FileUtils.copyFile(scrFile, screenshotLocation);

      System.out.println(screenshotLocation.toString());
 
 
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return screenshotLocation.toString();


  }

}



There is one more scenario where we need to take screenshot of a specific element in Appium, for that I have written a post describing the usage and steps: refer to the post below:

TAKE SCREENSHOT OF SPECIFIC ELEMENT IN APPIUM

**************


REVISION:==>

==>. How we can verify the Appium installation, what is Appium doctor?


Ans:   To verify that all of Appium's dependencies are met we can use appium-doctor.
Install it with commandnpm install -g appium-doctor, then run the appium-doctor command, supplying the --ios or --android flags to verify that all of the dependencies are set up correctly.


==>. What is the default port for Appium?

Ans: 4723 

This port information is important since we will have to direct your test client to make sure to connect to Appium on this port. If we want to change, the port, we can do so by using the -p flag when starting Appium.

==> What are the important Appium Desired Capabilities?

Ans

{

"platformName": "iOS", "platformVersion": "11.0", "deviceName": "iPhone 11", "automationName": "XCUITest", "app": "/path to app" }

==> Mention the Automation engine for android and IOS?

Ans:
Android: UiAutomator2
IOS : XCUITest

Learn (API-Microservice)Testing+(CoreJava+UI)-SDET with Self Paced Videos and one LIVE Doubt Session

TRANING VIDEOS AVAILABLE with Live Doubt Session @4500/-(course-1 below,API TRaining Videos With ClassNotes and Coding Set) and 6500/- (API+UI, both course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122

Entire course content can be found below:  COURSE CONTENT
Check below link for question and answers with code:

Top API Interview Question 1-10

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions
Mobile Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

Python Interview Question Set for QAE - SDET - SDE:
https://automationreinvented.blogspot.com/search/label/Python

No comments:

Post a Comment

All Time Popular Posts

Most Featured Post

API Status Codes with examples for QA-Testers

  🔺 LinkedIn: https://www.linkedin.com/in/sidharth-shukla-77b53145/ 🔺 Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 🏮In API testing, it...