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();
} |
******************
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
No comments:
Post a Comment