总结最近了解的前端测试的相关内容,发现前端这里真的是太庞大了,而且各种测试工具层出不穷,需要总结东西太多了,如有遗漏请大家见谅。
TDD vs BDD:
TDD与BDD概念不再描述了,直奔主题。
前端BDD测试框架
jasmine
Installation
To seed your project with some examples
mocha(推荐,简洁明了)
Installation
Install with npm:
Getting Started
In your editor:
you need to install chai.
Back in the terminal:
$ npm i qunit
npm install --save-dev jest-cli
function sum(a, b) {
return a + b;
}
module.exports = sum;
jest.unmock('../sum'); // unmock to use the actual implementation of sum
describe('sum', () => {
it('adds 1 + 2 to equal 3', () => {
const sum = require('../sum');
expect(sum(1, 2)).toBe(3);
});
});
"scripts": {
"test": "jest"
}
[PASS] __tests__/sum-test.js (0.010s)
npm install blanket
this.demoTestGoogle = function (browser) {
browser
.url(“http://www.google.com”)
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', 'nightwatch')
.waitForElementVisible('button[name=btnG]', 1000)
.click('button[name=btnG]')
.pause(1000)
.assert.containsText('#main', 'The Night Watch')
.end();
};
Feature('CodeceptJS Demonstration');
Scenario('test some forms', (I) => {
I.amOnPage('http://simple-form-bootstrap.plataformatec.com.br/documentation');
I.fillField('Email', 'hello@world.com');
I.fillField('Password', '123456');
I.checkOption('Active');
I.checkOption('Male');
I.click('Create User');
I.see('User is valid');
I.dontSeeInCurrentUrl('/documentation');
});
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
var expect = chai.expect;
describe('angularjs 首页', function() {
it('应该欢迎一个具名的用户', function() {
//要求浏览器访问网址http://www.angularjs.org
browser.get('http://www.angularjs.org');
//找到ng-model名为'youname'的HTML元素,要求浏览器键入名字
element(by.model('yourName')).sendKeys('tanshuai');
var greeting = element(by.binding('yourName'));
//取得结果并作断言测试
expect(greeting.getText()).to.eventually.equal('Hello tanshuai!');
});
});
var Nightmare = require(‘nightmare’);
var expect = require(‘chai’).expect; // jshint ignore:line
describe('test yahoo search results', function() {
it('should find the nightmare github link first', function*() {
var nightmare = Nightmare()
var link = yield nightmare
.goto('http://yahoo.com')
.type('form[action*="/search"] [name=p]', 'github nightmare')
.click('form[action*="/search"] [type=submit]')
.wait('#main')
.evaluate(function () {
return document.querySelector('#main .searchCenterMiddle li a').href
})
expect(link).to.equal('https://github.com/segmentio/nightmare');
});
});
var casper = require(‘casper’).create();
var fs = require(‘fs’)
casper.start(‘https://github.com/login/‘);
casper.waitForSelector(‘input.btn.btn-primary.btn-block’); // wait for the form node to be added
casper.then(function() {
this.fillSelectors(‘.auth-form-body’, {
‘#login_field’: ‘qileilove’,
‘#password’: ‘*‘
});
this.click(‘input.btn.btn-primary.btn-block’); // Click the login button instead of submitting the form
this.echo(‘Browser Cookie: ‘ + this.evaluate(function() {
return document.cookie;
}));
casper.run(function() {
var cookies = JSON.stringify((this.page.cookies));
fs.write('cookie.txt', cookies, 'w');
this.exit();
});
casper.wait(3000); // Wait for ajax form submission
casper.then(function() {
this.capture('logged-in.png')
});
casper.run();
"scenarios": [
{
"label": "My Local Test",
"url": "../../index.html",
"hideSelectors": [],
"removeSelectors": [
],
"selectors": [
"nav",
".jumbotron",
"body .col-md-4:nth-of-type(1)",
"body .col-md-4:nth-of-type(2)",
"body .col-md-4:nth-of-type(3)",
"footer"
],
"readyEvent": null,
"delay": 0,
"onReadyScript": null,
"onBeforeScript": null
}
],
casper.
start( url ).
then(function(){
// do something
casper.click('button#open-dialog');
// Take a screenshot of the UI component
phantomcss.screenshot('#the-dialog', 'a screenshot of my dialog');
});
var app = require('../app');
var request = require('supertest');
describe('router testing', function () {
it('site root response', function (done) {
request(app)
.get('/')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200)
.end(function(err, res){
if (err) throw err;
done();
});
});
```
利用Mocha + Chai + SuperTest就可以搭建一套 前端rest-api测试框架
最新内容请见作者的GitHub页:http://qaseven.github.io/