我在Mac上和Vagrant框内运行CasperJS脚本。 测试在Vagrant之外成功但不在其中。为什么?
作为参考,我已将测试结果放在本文的底部。
如果需要更多调试,请告诉我,我会补充: - )
这是我在控制台中用来运行测试的内容。
casperjs test price_filter.js
这也适用于您,因为www3.smatch.com
可公开访问。基本上,该剧本将发送给Lady' Lady Accessoires'部分,在DOM中查找产品并获取其价格。然后检查至少有一种产品价格高,并在应用价格过滤器后检查没有更高价的产品。
var x = require('casper').selectXPath;
casper.test.begin(
'max price filter',
function suite(test) {
casper.start('http://www3.smatch.com/');
casper.then(function() {
this.mouseEvent('mouseover', '.header a[title="Damenmode"]');
});
casper.thenClick(x('//*[text()="Accessoires"]'));
casper.then(function() {
var elements = this.evaluate(function() {
var elements = __utils__.findAll('.category-content .products[data-result] .price');
return elements.map(function(el) {
return el.textContent.trim();
});
});
var haveExpensive = false;
for (var i = 0; i < elements.length; i++)
{
if (elements[i].match(/^[0-9]{2,},[0-9][0-9]/))
{
haveExpensive = true;
}
else if (!elements[i].match(/^[0-9]+,[0-9][0-9]/))
{
throw new Error('invalid price');
}
}
if (!haveExpensive)
throw new Error('no expensive products');
test.assert(elements.length > 0, 'Before filter, we have expensive products');
});
casper.then(function() {
this.fillSelectors('#price-filter-form', {
'input[name="max"]': '7'
});
});
casper.thenClick('#price-filter-submit');
casper.then(function() {
var elements = this.evaluate(function() {
var elements = __utils__.findAll('.category-content .products[data-result] .price');
return elements.map(function(el) {
return el.textContent.trim();
});
});
for (var i = 0; i < elements.length; i++)
{
if ( ! elements[i].match(/^[0-7],[0-9][0-9]/))
throw new Error(elements[i]);
}
test.assert(elements.length > 0, 'After filter, we have cheap products');
});
casper.run(function () {
test.done();
});
}
);
外部流浪者
Test file: price_filter.js
# max price filter
PASS Before filter, we have expensive products
PASS After filter, we have cheap products
PASS 2 tests executed in 1.202s, 2 passed, 0 failed, 0 dubious, 0 skipped.
内部流浪汉
Test file: critical/price_filter.js
# max price filter
PASS Before filter, we have expensive products
FAIL Error: 89,90
* €
# type: uncaughtError
# file: critical/price_filter.js:62
# error: 89,90
* €
# Error: 89,90
#
# * €
# at critical/price_filter.js:62
# at runStep (/usr/lib/node_modules/casperjs/modules/casper.js:1553)
# at checkStep (/usr/lib/node_modules/casperjs/modules/casper.js:399)
# stack: not provided
FAIL 2 tests executed in 21.873s, 1 passed, 1 failed, 0 dubious, 0 skipped.
Details for the 1 failed test:
In critical/price_filter.js:62
max price filter
uncaughtError: Error: 89,90
* €
答案 0 :(得分:1)
这真的很奇怪,但似乎可以通过在点击后面添加一个空步骤来解决:
casper.thenClick('#price-filter-submit');
casper.then(function(){});
或
casper.thenClick('#price-filter-submit');
casper.wait(1); // 1 msec
我无法提供任何见解,为什么会这样。也许这是一个casperjs bug。