我为我的测试目的写了一些CasperJS模块。它们由反复使用的各种功能组成。我注意到,相同的功能在不同的页面上不能用于相同的功能。当我研究它时,我发现实际上原生的CasperJS函数是造成麻烦的原因。一些CasperJS功能不一致
例如:
waitUntilVisible('.buy')
正在写新的代码块不返回true时不再为我工作的所有的时间和最近。我曾尝试与
waitFor(function(){
return this.exists('.buy');
}
,仍然没有运气来取代它......从那以后,我试图用评估这样
waitFor(function(){
return this.evaluate(function(){
return $('.buy').length > 0;
})
}
通常断言为true。在这段代码之前和之后看截图清楚地表明元素在那里。
奇怪的是,这个确切的代码片段,它检查购买按钮,并点击它,如果它存在,在其他相同的页面上工作。
下面是函数的不工作
this.checkButton = function(){
casper.waitUntilVisible('.buy',function(){
this.test.pass('Button visible');
this.click('.buy');
},function(){
this.test.fail('Button not visible');
}).waitUntilVisible('div.header h2', function(){
this.test.pass('Button works');
},function(){
this.test.fail('Button does not work');
})
}
听page.error和remote.message给任何东西的。
我当前的等待超时设置为30秒。
有没有人有任何想法可能出错?
感谢
编辑: 所以看起来这个问题是与模块有关。我从字面上将模块中各种功能的代码复制到1个程序文件中。它正常工作。该程序代码如下:
.then(function(){
var records = casper.evaluate(function(){
return document.querySelectorAll('.content table tr').length;
});
if(records > 0) this.test.pass(records +' records shown');
else this.test.fail(records +' records shown');
})
.waitForSelector('#main .btn.small',function(){
this.test.pass('Load more button found')
}, function(){
this.test.fail('Load more button not found')
})
.then(function(){
if(this.exists('#main .btn.small')) this.test.pass('Button exists');
else this.test.fail('Button does not exist !?');
})
.thenClick('#main .btn.small')
.then(function(){
this.sendKeys('.listfilter-wrapper input','2487');
})
.wait(5000)
.then(function(){
var length = casper.evaluate(function(){
return $('.content table tr').length;
});
if(length < 40) this.test.assert(true,'Search works, number of results for "2487": '+length);
else this.test.assert(false, 'Search does not work');
})
这是写在模块相同的代码:
this.isLoaded = function(){
casper.then(function(){
var records = casper.evaluate(function(){
return document.querySelectorAll('.content table tr').length;
});
if(records > 0) this.test.pass(records +' records shown');
else this.test.fail(records +' records shown');
});
},
this.loadMore = function(){
casper.then(function(){
if(this.exists('#main .btn.small')) {
this.test.pass('Load more button exists');
casper.click('#main .btn.small');
casper.wait(500);
}
else this.test.fail('Load more button does not exist')
})
},
this.search = function(){
casper.then(function(){
this.sendKeys('.listfilter-wrapper input','2487');
}).wait(5000);
casper.then(function(){
var length = casper.evaluate(function(){
return $('.content table tr').length;
});
if(length < 40) this.test.assert(true,'Search works, number of results for "2487": '+length);
else this.test.assert(false, 'Search does not work');
});
}
这是调用步骤
module.exports = function(){
this.check = function(){
platform.navigateTo('Actions');
this.isLoaded();
this.loadMore();
this.search();
}
}
的功能,这是呼叫模块
a = require('./modules/actions');
var actions = new a();
casper.test.begin('Testing',8,function suite(test){
casper.start();
actions.check();
casper.run(function(){
test.done();
});
})
嗨马里奥,你能提供有关casperjs使用版本的信息,并且你的配置对于不同的页面是否完全相同?就像在一个页面中使用phantomjs并为另一个页面使用slimerjs一样?我有没有理解,即使你已经用屏幕截图证明元素在那里,你总是得到测试的失败/超时?在这种情况下。checkButton正在运行? – solick
我还没有遇到这类问题。使用':nth-of-type()'和':nth-child()'选择器时有一个PhantomJS错误,但是看起来,你没有使用它们。请注册到'resource.error','page.error','remote.message'和'casper.page.onResourceTimeout'事件([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf)) 。也许有错误。如果没有,也许你可以提供一个完整的脚本来解决这个问题。 –
我只使用phantomJS v1.9.8和casperJS v1.1.0-beta3,所以没有slimerJS ... 我有一些错误,因为我猜TLS没有加载的资源。整个代码非常庞大,因为我正在测试用knockoutJS编写的整个平台。有没有知道的问题?我已经搜索了很长时间找不到任何...因为这是很大一部分代码,我会一次调查它1个函数,并会写回结果。 –