20221120 App自动化实战二

课堂ppt

https://pdf.ceshiren.com/jck25/appium2

PPT地址

截图元素框标红

private void ElementScreenBase(WebElement element) throws IOException {
        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        org.openqa.selenium.Point elementLocation = element.getLocation();
        org.openqa.selenium.Dimension elementSize = element.getSize();
        int eleX = elementLocation.x;
        int eleY = elementLocation.y;
        int eleH = elementSize.height;
        int eleW = elementSize.width;
        logger.info("location=${} size=${} x=${} y=${} width=${} height=${}",
                elementLocation,elementSize,eleX,eleY,eleW,
                eleH);
        //读取截图文件
        BufferedImage img = ImageIO.read(screenshot);
        //创建一个 Graphics2D,可用于绘制到此 BufferedImage 中
        Graphics2D graph = img.createGraphics();
        //BasicStroke 指定线宽的实心  描边 Shape 的 Stroke 对象
        graph.setStroke(new BasicStroke(5));
        graph.setColor(Color.RED);//绘制形状的颜色
        graph.drawRect(eleX, eleY, eleW, eleH);//绘制指定矩形的轮廓
        graph.dispose();//处理此图形上下文并释放它正在使用的任何系统资源
        ImageIO.write(img, "png", getPngPath().toFile());
    }

代码启动appium server

  • 保存log
  • 指定server端口
CommandLine commandLine = new CommandLine("appium");
commandLine.addArgument("-g");
commandLine.addArgument("0000.log");
commandLine.addArgument("--port");
commandLine.addArgument("4723");

DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
  executor.execute(commandLine, handler);
} catch (IOException e) {
  e.printStackTrace();
}