我在同一页面上有两个casper测试,但上面的测试一直干扰下面的测试。 (即如果我将每个测试放在它自己的页面上,它就可以工作。如果它是同一个页面,第一个测试工作而第二个测试没有。)
我的印象是,在casper测试中,启动和运行之间没有内存,后面跟着任何东西,测试依次运行。这是真的,如果是这样,我在语法上做错了吗?
var routine = {
login: function(email, pass, test) {
// casper.open(url);
casper.then(function() {
this.withFrame(0, function() {
this.waitUntilVisible('form#login-form', function() {
this.fill('form#login-form', {
email: email,
password: pass
}, false);
});
});
});
casper.then(function(){
casper.withFrame(0, function() {
this.click("#modalButtonLogin");
});
});
}
};
casper.test.begin('Get login', 1, function(test) {
var fs = require('fs')
var data = fs.read('cookies.txt');
phantom.cookies = JSON.parse(data);
casper.start("https://web.test.com/#")
.wait(1000, function(){
routine.login('******@test.com', '********', test);
})
.wait(1000, function(){
this.withFrame(0, function() {
test.assertExists('.wkKitTitle');
});
})
.then(function(){
phantom.clearCookies();
})
.run(function() {
test.done();
});
});
casper.test.begin('Directs to correct url on form submition', 2, function(test) {
casper.start("https://web.test.com/#")
.then(function(){
test.assertUrlMatch(this.getCurrentUrl(), 'https://web.test.com');
})
.then(function(test){
routine.login('*****@test.com', '*****', test);
})
.then(function(){
test.assertUrlMatch(this.getCurrentUrl(), 'https://web.test.com/#kit/176637403872624641');
})
.then(function(){
phantom.clearCookies();
})
.run(function() {
test.done();
});
});
编辑:
对于正在发生的事情的不同例子,以下作品:(唯一的区别是顺序)
casper.test.begin('sign up components assertExists()', 7, function(test) {
casper.start("http://web.dev.test.com/#/@page=signup.html")
.then(function() {
casper.withFrame(0, function() {
test.assertExist('form#signup-form', 'Sign up form exists');
test.assertExist('#modalButton', 'Sign Up button exists');
test.assertExist('[name="first_name"]', 'First name field exists');
test.assertExist('[name="last_name"]', 'Last name field exists');
test.assertExist('[name="login"]', 'Email field exists');
test.assertExist('[name="password"]', 'Password field exists');
test.assertExist('[name="re_enter_password"]', 'Password field exists');
});
})
.run(function() {
test.done();
});
});
casper.test.begin('Get started model opens', 2, function(test) {
casper.start("http://web.dev.test.com/#/@page=signup.html")
.then(function(){
this.withFrame(0, function() {
this.waitUntilVisible('form#signup-form', function() {
this.fill('form#signup-form', {
first_name: 'casper',
last_name: 'test',
login: 'testing@test.com',
password: 'pass',
re_enter_password: 'pass'
}, false);
});
});
})
.then(function(){
casper.withFrame(0, function() {
this.click("#modalButton");
});
})
.wait(500, function() {
casper.withFrame(0, function() {
test.assertExist('#getStarted', 'modal opens');
});
})
.then(function(){
phantom.clearCookies();
})
.run(function() {
test.done();
});
});
但这不是:
casper.test.begin('Get started model opens', 2, function(test) {
casper.start("http://web.dev.test.com/#/@page=signup.html")
.then(function(){
this.withFrame(0, function() {
this.waitUntilVisible('form#signup-form', function() {
this.fill('form#signup-form', {
first_name: 'casper',
last_name: 'test',
login: 'testing@test.com',
password: 'pass',
re_enter_password: 'pass'
}, false);
});
});
})
.then(function(){
casper.withFrame(0, function() {
this.click("#modalButton");
});
})
.wait(500, function() {
casper.withFrame(0, function() {
test.assertExist('#getStarted', 'modal opens');
});
})
.then(function(){
phantom.clearCookies();
})
.run(function() {
test.done();
});
});
casper.test.begin('sign up components assertExists()', 7, function(test) {
casper.start("http://web.dev.test.com/#/@page=signup.html")
.then(function() {
casper.withFrame(0, function() {
test.assertExist('form#signup-form', 'Sign up form exists');
test.assertExist('#modalButton', 'Sign Up button exists');
test.assertExist('[name="first_name"]', 'First name field exists');
test.assertExist('[name="last_name"]', 'Last name field exists');
test.assertExist('[name="login"]', 'Email field exists');
test.assertExist('[name="password"]', 'Password field exists');
test.assertExist('[name="re_enter_password"]', 'Password field exists');
});
})
.run(function() {
test.done();
});
});