【问题标题】:How to take screenshot of application excluding the statusbar using Appium如何使用 Appium 截取不包括状态栏的应用程序的屏幕截图
【发布时间】:2017-11-17 21:59:30
【问题描述】:

环境详情

  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 状态栏也被捕获,并且测试失败,因为状态屏幕元素是动态的。 谁能帮我截图不包括状态栏。

【问题讨论】:

    标签: appium-android


    【解决方案1】:

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

    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);
    }
    

    【讨论】:

    • 感谢您的回答。我会检查并通知您。
    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 2021-08-27
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 2011-01-20
    相关资源
    最近更新 更多