如何使用Webdriverio + Mocha在Appium中实现自动滚动?

时间:2018-03-15 08:04:20

标签: javascript node.js mocha appium

我正在为使用react构建的移动应用程序自动化测试用例。我使用的是Appium框架,我使用的客户端是Webdriver.io,还有Mocha和Chai用于断言。

到目前为止,我的脚本运行良好,但我遇到滚动问题。我已经查看了用于滚动的Webdriver.io文档,并实现了touchAction(),touchPerform(),execute(),scrollTo(),但在这种情况下没有方法可用。任何人都可以帮我这个吗?

这是我正在使用的示例代码

var webdriverio = require("webdriverio");
var expect = require("chai").expect;
var config = require("./helpers/desiredCapabilities").options;
var client = webdriverio.remote(config);

describe("Sample Test", function() {
  beforeEach(function() {
  this.timeout(90000);
});

describe("Scroll Test", function() {
  before(function() {
    return client.init();
  });

  it("should scroll successfully", function() {
    return client
      .element("~mobile")
      .setValue("1000000000")
      .element("~continue")
      .click()
      .element("~passwordTextField")
      .setValue("12345")
      .element("~continue")
      .click()
      .element("~home")
      .touchPerform([{
          action: 'press', 
          options: {x: 60, y: 200}
       },
       {
          action:'moveTo', 
          options: {x: 700, y: 200}
       },
       {
          action: 'release'
       }
    ])
    .element("~article")
    .click()
  });
 });
});

此代码模拟一个简单的登录操作,它完美地运行到touchPerform(),它不执行任何滚动。

这是Appium所需的所需功能。

exports.options = {
  desiredCapabilities: {
    platformName: "android",
    platformVersion: "5.1",
    appPackage: "com.sample.app",
    appActivity: "com.sample.app.MainActivity",
    automationName: "uiautomator2",
    avdReadyTimeout: "2000",
    deviceName: "AVY9K17424903794",
    app: "C:/Users/MdArifuzzaman/Arif/Appium Test/app-universal- 
          release.apk",
    newCommandTimeout: 90
  },
  host: "127.0.0.1",
  port: 4723
}; 

这是我的package.json文件。

{
  "name": "sampletest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha scrollTest"
  },
  "author": "Md. Arifuzzaman",
  "license": "ISC",
  "dependencies": {
    "appium": "^1.7.2",
    "appium-uiautomator2-driver": "^1.1.1",
    "chai": "^4.1.2",
    "chai-as-promised": "^7.1.1",
    "mocha": "^5.0.0",
    "wd": "^1.5.0"
  },
  "devDependencies": {
    "randomstring": "^1.1.5",
    "wdio-appium-service": "^0.2.3",
    "wdio-mocha-framework": "^0.5.12",
    "webdriverio": "^4.10.2"
  }
}

1 个答案:

答案 0 :(得分:0)

我不确定这个awnser是否会帮助你以及移动设备。但我认为应该。

向下滚动:

client
.url('https://www.yourwebsite.com')
.leftClick('#yourtable') // <-- should also work with .click()
.scroll(0, 500) // the higher the more we scroll

要使用键,请确保在模拟鼠标位于要向下滚动的项目之前单击列表div框。然后你可以按下向下键:

 /*
  \uE014    "ArrowRight"
  \uE015    "ArrowDown"
 */

client
.leftClick('#yourtable') // <-- should also work with .click()
.keys('ArrowDown')
.keys('ArrowDown')
.keys('ArrowDown')
// or
.keys('\uE015')
.keys('\uE015')
.keys('\uE015')

来源:api/protocol/keys.html