如何在webdriverio中编写自定义命令

时间:2019-01-04 18:23:28

标签: javascript webdriver-io

我正在尝试编写这样的自定义命令

 module.exports = (function() {
    browser.addCommand('selectABC', (element) => {
    let elem = element
    ...

   });
 })

在conf.ts中,我已经添加了

import * as custom from '../services/customCommands.service';

exports.config = {

/**
 * Gets executed before test execution begins. At this point you can access to all global
 * variables like `browser`. It is the perfect place to define custom commands.
 * @param {Array.<Object>} capabilities list of capabilities details
 * @param {Array.<String>} specs List of spec file paths that are to be run
 */
before: function (capabilities, specs) {
  // Add commands to WebdriverIO
  Object.keys(commands).forEach(key => {
    browser.addCommand(key, commands[key]);
  })
},

但是当我尝试这样的代码

  class NewPage {
     public createnew(data) {
         browser.selectABC($('abc'))
     }
  }

 class NewPage {
     public createnew(data) {
         $('abc').selectABC()
     }
  }

这不起作用,并引发此错误

错误TS2339:类型“客户端”上不存在属性“ selectABC”。

我想念什么?谢谢!

1 个答案:

答案 0 :(得分:1)

wdio.conf.js

const commands = require('./commands.js')

exports.config = {
  before: function (capabilities, specs) {
    // Add commands to WebdriverIO
    Object.keys(commands).forEach(key => {
      browser.addCommand(key, commands[key]);
    })
  }
}

commands.js

module.exports = {
    getUrlAndTitle: function () {
        return {
            url: this.getUrl(),
            title: this.getTitle()
        };
    },
    otherCommand: function () {}
}

test.js

const chai = require('chai');
const assert = require("assert");
const expect = require('chai').expect;
const chaiWebdriver = require('chai-webdriverio').default;
chai.use(chaiWebdriver(browser));

describe("custom commands", () => {
  it("should have custom commands", () => {
    const getUrlAndTitle = browser.getUrlAndTitle();
    const title = getUrlAndTitle.title;
    assert.equal(title, "Custom Commands");

    // You could do the same equality check with:
    expect(title === "Custom Commands").to.be.true;

    // Or also check equality with: 
    expect(title).to.equal("Custom Commands");
  });
});

如果您还想在测试中使用ES6样式的javascript,则可以执行以下操作

npm i @babel/cli @babel/core @babel/preset-env @babel/register --save

在您的package.json中

{
  "name": "babelify-webdriverIO-mocha-chai",
  "version": "2.0.0",
  "description": "babelify-webdriverIO-mocha-chai",
  "scripts": {
    "test": "node node_modules/.bin/wdio ./config/wdio.dev.conf.js"
  },
  "author": "Zero Cool",
  "dependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.2.2",
    "@babel/preset-env": "^7.2.3",
    "@babel/register": "^7.0.0",
    "@wdio/sauce-service": "^5.3.2",
    "@wdio/selenium-standalone-service": "^5.2.2",
    "@wdio/spec-reporter": "^5.2.3",
    "@wdio/sync": "^5.3.2",
    "chai": "^4.2.0",
    "webdriverio": "^5.3.5"
  },
  "devDependencies": {
    "@wdio/cli": "^5.3.5",
    "@wdio/local-runner": "^5.3.5",
    "@wdio/mocha-framework": "^5.3.2",
    "chai-webdriverio": "^1.0.0",
    "selenium-standalone": "^6.15.4"
  },
  "babel": {
    "presets": [
      [
        "@babel/preset-env",
        {
          "targets": {
            "node": "current"
          }
        }
      ]
    ]
  }
}

您的测试文件现在看起来像这样

import chai from "chai";
import { assert, expect } from "chai";
import chaiWebdriver from "chai-webdriverio";
chai.use(chaiWebdriver(browser));

describe("custom commands", () => {
  it("should have custom commands", () => {
    const getUrlAndTitle = browser.getUrlAndTitle();
    const title = getUrlAndTitle.title;
    assert.equal(title, "Custom Commands");

    // You could do the same equality check with:
    expect(title === "Custom Commands").to.be.true;

    // Or also check equality with: 
    expect(title).to.equal("Custom Commands");
  });
});

如果您正在使用摩卡咖啡,请确保将其包含在wdio.conf.js中

    mochaOpts: {
      ui: "bdd",
      timeout: 10000,
      compilers: ["js:@babel/register"]
    }