Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在webdriverio中编写自定义命令_Javascript_Webdriver Io - Fatal编程技术网

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

Javascript 如何在webdriverio中编写自定义命令,javascript,webdriver-io,Javascript,Webdriver Io,我正在尝试编写这样的自定义命令 module.exports = (function() { browser.addCommand('selectABC', (element) => { let elem = element ... }); }) class NewPage { public createnew(data) { browser.selectABC($('abc')) } } 在conf.t

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

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

   });
 })
  class NewPage {
     public createnew(data) {
         browser.selectABC($('abc'))
     }
  }
在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]);
  })
},

这不起作用并引发此错误

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


我错过了什么?谢谢

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]);
    })
  }
}
module.exports = {
    getUrlAndTitle: function () {
        return {
            url: this.getUrl(),
            title: this.getTitle()
        };
    },
    otherCommand: function () {}
}
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");
  });
});

commands.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]);
    })
  }
}
module.exports = {
    getUrlAndTitle: function () {
        return {
            url: this.getUrl(),
            title: this.getTitle()
        };
    },
    otherCommand: function () {}
}
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");
  });
});

test.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]);
    })
  }
}
module.exports = {
    getUrlAndTitle: function () {
        return {
            url: this.getUrl(),
            title: this.getTitle()
        };
    },
    otherCommand: function () {}
}
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
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");
  });
});
在您的包中。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"
          }
        }
      ]
    ]
  }
}
    mochaOpts: {
      ui: "bdd",
      timeout: 10000,
      compilers: ["js:@babel/register"]
    }
您的测试文件现在可能如下所示:

npm i @babel/cli @babel/core @babel/preset-env @babel/register --save
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中

{
  "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"
          }
        }
      ]
    ]
  }
}
    mochaOpts: {
      ui: "bdd",
      timeout: 10000,
      compilers: ["js:@babel/register"]
    }