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");
});
});
Если вы также хотите использовать javascript в стиле ES6 в своих тестах, вы можете сделать следующее :
npm i @babel/cli @babel/core @babel/preset-env @babel/register --save
В вашем пакете. 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");
});
});
Если вы используете mocha, обязательно включите это в ваш wdio.conf.js :
mochaOpts: {
ui: "bdd",
timeout: 10000,
compilers: ["js:@babel/register"]
}