截屏以获取ios设备的特定元素-野生动物园浏览器

我正尝试通过appium + webdriverio + browserStack为Safari浏览器中的任何特定元素拍摄屏幕截图。

appium版本:1.15.0

这是错误日志:

Calling AppiumDriver.getElementScreenshot() with args: ["5028","e154c6f0-73b9-4306-b661-d3206aa7ba8e"] [debug] [XCUITest] Executing command 'getElementScreenshot' [debug] [RemoteDebugger] Executing atom 'getElementScreenshot' [debug] [MJSONWP (e154c6f0)] Encountered internal error running command: Error: Unable to load Atom 'getElementScreenshot' from file '/usr/local/.browserstack/appium_1.14.0_bstack/node_modules/appium-remote-debugger/atoms/getElementScreenshot.js' [debug] [MJSONWP (e154c6f0)] at getatoms (/usr/local/.browserstack/appium_1.14.0_bstack/node_modules/appium-remote-debugger/lib/atoms.js:17:13) 2019-11-21 06:44:37:879 - [HTTP] <-- GET

请帮助,我在做错什么吗?请提出建议,因为我想在野生动物园浏览器中通过appium拍摄特定webElement的屏幕截图

yyaiwdk1 回答:截屏以获取ios设备的特定元素-野生动物园浏览器

Afaik当前无法仅通过webdriverio和appium截取特定Web元素的屏幕截图(通过Web选择器)。尽管webdriverio提供了element.saveScreenshot功能,但它似乎不适用于带有appium的移动设备。

我要解决的方法是使用browser.saveScreenshot(filepath)截取整个页面的屏幕快照(确保为此切换到本机上下文),然后将其裁剪为图像库中的矩形元素(清晰) (以我为例)。

我的util函数如下所示:

import fs from 'fs';
import path from 'path';
import sharp from 'sharp';

export function takeScreenshot(element) {
  const timestamp = moment().format('YYYYMMDD-HHmmss.SSS');
  if (fs.existsSync('reports/screenshots/')) {
    const filePath = path.join('reports/screenshots/',timestamp + '.png');
    WebView.switchToContext(CONTEXT_REF.NATIVE);
    browser.saveScreenshot(filePath);
    WebView.switchToContext(CONTEXT_REF.WEBVIEW);

    if (element) {
      const size = getRealSize(element);
      const location = getRealLocation(element);
      const outputPath = path.join('reports/screenshots/',element.selector + timestamp + '.png');
      sharp(filePath).extract({ width: size.width,height: size.height,left: location.x,top: location.y }).toFile(outputPath)
        .then(function () {
          fs.unlinkSync(filePath);
          fs.renameSync(outputPath,filePath);
        })
        .catch(function (err) {
          console.log(err);
        });
    }
  }
}

export function getPixelRatio() {
  return browser.capabilities.pixelRatio ? browser.capabilities.pixelRatio : 1;
}

export function getRealLocation(element) {
  const location = element.getLocation();
  return {
    x: Math.round(location.x * getPixelRatio()),y: Math.round(location.y * getPixelRatio())
  }
}

export function getRealSize(element) {
  const size = element.getSize();
  return {
    width: Math.round(size.width * getPixelRatio()),height: Math.round(size.height * getPixelRatio())
  }
}

本文链接:https://www.f2er.com/3059543.html

大家都在问