Mak*_*zik 0 mocha.js node.js nightmare
我正在使用NightmareJS和Mocha.一切似乎工作正常,但我有分离测试的问题,因为我的会话不会持续通过不同的测试用例.
第一次测试正常,但第二次测试Should be able to edit失败,因为即使我使用分区选项,我也会在同一个登录页面上结束.我怎么能克服这个?
require('mocha-generators').install();
var Nightmare = require('nightmare');
var expect = require('chai').expect;
describe('Nightmare JS tests', function() {
this.timeout(30000);
var url = 'http://localhost/app/';
describe('base functionality', function() {
it('Should be able to login', function*() {
var nightmare = Nightmare({
show: true,
'webPreferences': {
partition: 'persist:somesession'
}
});
var result = yield nightmare
.goto(url)
.wait('.login')
.click('.login')
.wait('h4.heading')
.wait(1000)
.evaluate(function () {
return document.querySelector('h4.heading').innerHTML;
})
.end();
expect(result).to.equal("This is heading");
});
it('Should be able to edit', function*() {
var nightmare = Nightmare({
show: true,
'webPreferences': {
partition: 'persist:somesession'
}
})
var result = yield nightmare
.goto('http://localhost/app/company')
.wait("button.edit")
.click("button.edit")
.wait("input[type='text']")
.insert("input[type='text']", false)
.insert("input[type='text']", "This is some address")
.click("button[type='submit']")
.wait("div.success")
.evaluate(function () {
return document.querySelector("div.success").innerText;
})
.end()
expect(result).to.contain("Updated!");
});
});
});
Run Code Online (Sandbox Code Playgroud)
关于共享会话,有关Nightmare Github项目(有关更多信息,请参阅#430和#398)的讨论.TL; DR版本是您可以重置会话,但跨实例使用相同的会话未经过充分测试.(从你的例子来看,看起来你可能已经看过这些了.)
无论如何,我想我可以帮助解决你眼前的问题.两个建议:
before()块中,并使用相同的实例进行测试.但这引入了测试依赖性,并且通常被认为是不合需要的.beforeEach()块.将整个登录单元测试移动到一个beforeEach()块,以便为每个测试获得新的登录.您还需要添加一个afterEach()块来清理噩梦实例.这将使您的测试可独立运行,但也会为运行整个单元测试电池带来大量时间开销.基于您的示例的(未经测试的)#2示例可能会有所帮助:
require('mocha-generators').install();
var Nightmare = require('nightmare');
var expect = require('chai').expect;
describe('Nightmare JS tests', function() {
this.timeout(30000);
var url = 'http://localhost/app/';
describe('base functionality', function() {
var nightmare;
beforeEach(function*() {
nightmare = Nightmare({
show: true,
});
yield nightmare
.goto(url)
.wait('.login')
.click('.login')
.wait('h4.heading')
.wait(1000)
});
afterEach(function*() {
yield nightmare.end();
});
it('Should be able to edit', function*() {
var result =
yield nightmare
.goto('http://localhost/app/company')
.wait("button.edit")
.click("button.edit")
.wait("input[type='text']")
.insert("input[type='text']", false)
.insert("input[type='text']", "This is some address")
.click("button[type='submit']")
.wait("div.success")
.evaluate(function() {
return document.querySelector("div.success").innerText;
});
expect(result).to.contain("Updated!");
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2441 次 |
| 最近记录: |