如何使用Appium截取不包括状态栏的应用程序的屏幕截图

How to take screenshot of application excluding the statusbar using Appium

环境详情

  1. Appium 版本 1.7
  2. Android 牛轧糖版本

下面是我的代码

    public class CaptureScreenShot {
    AppiumDriver<WebElement> driver;
 Dimension size;
 String destDir;
 DateFormat dateFormat;
 BufferedImage expectedImage;
 BufferedImage actualImage;

 @BeforeTest
 public void setUp() throws Exception {
     DesiredCapabilities dc = new DesiredCapabilities();
        dc.setCapability(CapabilityType.BROWSER_NAME,"Android");
        dc.setCapability(CapabilityType.VERSION,"7.0");
        dc.setCapability("deviceName","ce041714c44ebd1802");
        dc.setCapability("platformName", "Android");
        dc.setCapability("appPackage", "io.ionic.starter");
        dc.setCapability("appActivity", "io.ionic.starter.MainActivity");
                try {
                    driver= new AppiumDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), dc);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

 }

 @Test
 public void ScrollToTab() {
  takeScreenShot();

 }

 public void takeScreenShot() {
   destDir = "<<local path>>\";

  File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

  dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");

  new File(destDir).mkdirs();

  String destFile = dateFormat.format(new Date()) + ".png";

  try {

   FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile));
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 @Test
 public void verify(){
     try {
        expectedImage = ImageIO.read(new File("<local path>"));
    } catch (IOException e) {
      e.printStackTrace();
    }
     try {
        actualImage = ImageIO.read(new File("<local path>"));
    } catch (IOException e) {
        e.printStackTrace();
    }
     ImageDiffer imgDiff = new ImageDiffer();
     ImageDiff diff = imgDiff.makeDiff(expectedImage, actualImage);
     Assert.assertFalse(diff.hasDiff(),"Images are Same");
 }

 @AfterTest
 public void End() {
  driver.quit();
 }
}

此代码工作正常,但与屏幕截图一起,android 状态栏也被捕获并且测试失败,因为状态屏幕元素是动态的。 谁能帮我截取不包括状态栏的屏幕截图。

此代码可能会通过从完整屏幕截图中获取子图像来解决您的问题:

import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;

AppiumDriver driver;
By locator = By.xpath("(//android.widget.FrameLayout)[1]");
// in my case this locator match everything except status bar

public BufferedImage getSubImage() throws IOException {
    final byte[] srcImage = ((TakesScreenshot) driver).
      getScreenshotAs(OutputType.BYTES);
    final BufferedImage screenshot = ImageIO.read(    
      new ByteArrayInputStream(srcImage));

    final WebElement element = driver.findElement(locator);
    final Point elementLocation = element.getLocation();
    final Dimension elementSize = element.getSize();
    return screenshot.getSubimage(elementLocation.x, elementLocation.y,
      elementSize.width, elementSize.height);
}