NodeJs developer ( 1 year)
SDET ( 8 month)
Java developer ( 5 years )
2014/04 ~ 2014/12
是你最好的好朋友
網站自動化測試
Webdriver.IO 介紹
Getting Started
工具介紹
建構測試環境
快速建立前端測試 & Live demo
關於導入的建議
Summary
nightwatch
protractor
webdriver.io
selenium-server restful api
var util = require('util'),
events = require('events');
var CustomReporter = function(options) {
};
// Inherit from EventEmitter
util.inherits(CustomReporter, events.EventEmitter);
// Expose Custom Reporter
exports = module.exports = CustomReporter;
// wdio.config
var CustomReporter = require('./reporter/my.custom.reporter');
exports.config = {
reporters: [CustomReporter],
};
browser.addCommand('doExternalJob', function async (params) {
return externalLib.command(params);
});
it('execute external library in a sync way', function() {
browser.url('...');
browser.doExternalJob('someParam');
console.log(browser.getTitle());
});
client
.url('http://google.com')
.setValue('#q', 'webdriver')
.click('#btnG')
// Chain Selectors
driver.get('http://www.google.com');
driver.findElement(webdriver.By.id('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.id('btnG')).click();
import { RuntimeError } from '../utils/ErrorHandler'
let click = function (selector) {
return this.element(selector).then((elem) => {
// lib/helpers/findElementStrategy.js
if (!elem.value) throw new RuntimeError(7);
return this.elementIdClick(elem.value.ELEMENT)
})
}
export default click
browser.click('h2.subheading a');
// click
browser.click('#myButton');
// setValue
var input = browser.element('.input');
input.setValue('test123');
// submitForm
browser.submitForm('#loginForm');
// title
browser.getTitle();
// get value
browser.getValue('.input');
// get url
browser.getUrl();
// single element
browser.element('.h2');
// Collection of elements
browser.elements('li');
// url
browser.url('http://webdriver.io');
browser.isEnabled(selector);
browser.isExisting(selector);
browser.isSelected(selector);
browser.element('.notification').waitForExist(5000);
browser.pause(5000);
browser.debug();
browser.saveScreenshot('front_page.png');
// ends session and close browser
client
.init()
.url('http://google.com')
.end();
browser.addCommand();
a test runner that helps you to build a reliable test suite that is easy to read and maintain.
var assert = require('assert');
describe('blog front page', function() {
it('should have the right title', function() {
browser.url('http://alincode.github.io/blog/');
var title = browser.getTitle();
assert.equal(title, 'alincode');
});
});
var webdriverio = require('webdriverio');
var options = { desiredCapabilities: { browserName: 'chrome' } };
var client = webdriverio.remote(options);
client
.init()
.url('https://duckduckgo.com/')
.setValue('#search_form_input_homepage', 'WebdriverIO')
.click('#search_button_homepage')
.getTitle().then(function(title) {
console.log('Title is: ' + title);
// outputs: "Title is: WebdriverIO (Software) at DuckDuckGo"
})
.end();
// wdio.conf.js
exports.config = {
// define all tests
specs: ['./test/specs/**/*.spec.js'],
// define specific suites
suites: {
login: [
'./test/specs/login.success.spec.js',
'./test/specs/login.failure.spec.js'
],
otherFeature: []
}
}
wdio wdio.conf.js --suite login
wdio wdio.conf.js --suite login,otherFeature
client
.init()
.url('https://webdriver.io/')
.getTitle().then(function(title) {
console.log(title);
// outputs: "WebdriverIO - Selenium 2.0 javascript bindings for nodejs"
})
.end();
var client = require('webdriverio').remote({
desiredCapabilities: {
browserName: 'chrome'
}
});
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.Should();
chai.use(chaiAsPromised);
chaiAsPromised.transferPromiseness = client.transferPromiseness;
describe('my app', function() {
before(function () {
return client.init();
});
it('should contain a certain text after clicking', function() {
return client
.click('button=Send')
.isVisible('#status_message').should.eventually.be.true
.getText('#status_message').should.eventually.be.equal('Message sent!');
});
});
browser.options.bio
// wdio.conf.js
exports.config = {
// ...
bio: 'hello world',
// ...
}
describe('DuckDuckGo search', function() {
it('searches for WebdriverIO', function() {
browser.url('https://duckduckgo.com/');
browser.setValue('#search_form_input_homepage', 'WebdriverIO');
browser.click('#search_button_homepage');
var title = browser.getTitle();
console.log('Title is: ' + title);
});
});
function Page() {}
Page.prototype.open = function(path) {
browser.url('/' + path)
}
module.exports = new Page();
// login.page.js
var Page = require('./page')
var LoginPage = Object.create(Page, {
// define elements
username: { get: function () { return browser.element('#username'); } },
password: { get: function () { return browser.element('#password'); } },
form: { get: function () { return browser.element('#login'); } },
flash: { get: function () { return browser.element('#flash'); } },
// define or overwrite page methods
open: { value: function() {
Page.open.call(this, 'login');
} },
submit: { value: function() {
this.form.submitForm();
} }
});
module.exports = LoginPage
// login.spec.js
var expect = require('chai').expect;
var LoginPage = require('../pageobjects/login.page');
describe('login form', function () {
it('登入失敗', function () {
LoginPage.open();
LoginPage.username.setValue('foo');
LoginPage.password.setValue('bar');
LoginPage.submit();
expect(LoginPage.flash.getText()).to.contain('Your username is invalid!');
});
it('登入成功', function () {
LoginPage.open();
LoginPage.username.setValue('tomsmith');
LoginPage.password.setValue('SuperSecretPassword!');
LoginPage.submit();
expect(LoginPage.flash.getText()).to.contain('login success');
});
});
java -jar selenium-server-standalone-2.42.2.jar
chimp --mocha --watch --path=test
const assert = require('assert');
describe('Google search', function() {
it('case 1: @watch', function() {
browser.url('http://www.google.com/ncr');
browser.setValue('[name=q]', 'alincode blog');
browser.click('[name=btnG]');
assert.equal(browser.getTitle(), 'Google');
});
});
只是缺點是...
mkdir jsdc-webdriverio-sandbox
cd jsdc-webdriverio-sandbox
npm init -y
// install package
npm i webdriverio -D
./node_modules/webdriverio/bin/wdio
capabilities: [{
browserName: 'chrome'
}],
mochaOpts: {
timeout: 60000
},
mkdir -p ./test/specs/
mkdir -p ./errorShots/
var assert = require('assert');
describe('page', function() {
it('blog', function() {
browser.url('http://alincode.github.io/blog');
var title = browser.getTitle();
assert.equal(title, 'alincode');
});
});
./node_modules/webdriverio/bin/wdio wdio.conf.js
npm test
"scripts": {
"test": "wdio wdio.conf.js"
}