Appium - Capture Screenshot Android App Mobile Screen
2016-09-01 22:16
225 查看
show bug or some design Issue to development/design team. For android software application automation testing using appium, We can useTakesScreenshot Interface of WebDriver to capture screenshot of android app screen. I have
prepared simple example onhow to capture screenshot In android appium automation software test using TakesScreenshot Interface of WebDriver.
App To Use Capture Screenshot Test
We will use API Demos android software app for Capture Screenshot test. You can download it from HERE.
Aim to Achieve In Capture Screenshot Test
We wants to capture screenshot of android mobile screen on some stage of appium automation test. And then we will store It Inside screenshots folder under project. We will prepare file name using current date time programmatically
as shown In bellow Image.
Above Image shows screenshot which Is taken during android appium automation test.
Create And Run Capture Screenshot Test
Create new file CaptureScreenShot.java file under your Android package of your project and write bellow given test script In It.
package Android; import io.appium.java_client.android.AndroidDriver; import java.io.File; import java.io.IOException; import java.net.URL; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class CaptureScreenShot { AndroidDriver driver; Dimension size; String destDir; DateFormat dateFormat; @BeforeTest public void setUp() throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("deviceName", "ZX1B32FFXF"); capabilities.setCapability("browserName", "Android"); capabilities.setCapability("platformVersion", "4.4.2"); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("appPackage", "io.appium.android.apis"); capabilities.setCapability("appActivity", "io.appium.android.apis.ApiDemos"); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); } @Test public void ScrollToTab() { // Scroll till element which contains "Views" text If It Is not visible on screen. driver.scrollTo("Views"); // Click on Views. driver.findElement(By.name("Views")).click(); // Scroll till element which contains "Tabs" text If It Is not visible on screen. driver.scrollTo("Tabs"); //Call takeScreenShot() function to capture screenshot of android screen. takeScreenShot(); } public void takeScreenShot() { // Set folder name to store screenshots. destDir = "screenshots"; // Capture screenshot. File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); // Set date format to set It as screenshot file name. dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa"); // Create folder under project with name "screenshots" provided to destDir. new File(destDir).mkdirs(); // Set file name using current date time. String destFile = dateFormat.format(new Date()) + ".png"; try { // Copy paste file at destination folder location FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile)); } catch (IOException e) { e.printStackTrace(); } } @AfterTest public void End() { driver.quit(); } }
Test Description
Above android appium software test will navigate to Views screen and scroll down till Tabs text on API Demos android app. Then It will call takeScreenShot(); method to capture and store android app screenshot. takeScreenShot()
method will,
Capture screenshot using TakesScreenshot Interface of webdriver.
Get current date and time and store It in variable.
Create new folder with name "screenshots" under your project.
Set file name using current date and time.
Store file In "screenshots" folder.
When you run above test, It will create screenshots folder under your project and store screenshot file In It. Refresh your project folder after test execution to see new created folder and screenshot file.
This way you can capture and store screenshot of any android app.
相关文章推荐
- Appium - Capture Screenshot On Failure Or Pass In Android Test Automation
- Android: How to Capture Screen in Gingerbread(2.3中实现截屏)
- Android: How to Capture Screen in Gingerbread(2.3中实现截屏)(续)
- Android: How to Capture Screen in Gingerbread(2.3中实现截屏)
- appium获取android app的包名和主Activity
- How to Install Kali Linux on Android - Tutorial With Screenshot
- Screen Capture Shotcuts
- Capture a Screen Shot
- android screenshot
- Capture a Screen Shot
- cordova SplashScreen插件在android上退出app后不再显示启动画面的问题
- Capture Screen in Gingerbread(Android 2.3实现截屏)
- android adb screen shot
- Load image/css/js locally from Webview in Android Mobile app
- appium简明教程(9)——如何获取android app的Activity(即反编译.apk文件)
- Android: How to Capture Screen in Gingerbread(2.3中实现截屏)
- Grab Android screenshot to computer via ADB
- Capture a Screen Shot
- android 截图,视频录制命令 screencap screenrecord screenshot
- how to capture screenshot by using adb