如何使用Appium

时间:2017-11-15 05:55:20

标签: appium-android

环境详情

  1. Appium Version 1.7
  2. Android版Nougat
  3. 以下是我的代码

        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状态栏也被捕获,测试失败,因为状态屏幕元素是动态的。 任何人都可以帮我拍摄截图而不包括状态栏。

1 个答案:

答案 0 :(得分:0)

此代码可以通过从完整屏幕截图中获取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);
}