我已经为CasperJS编写了一些模块用于测试目的。它们由反复使用的各种功能组成。我注意到相同的功能不适用于不同页面的相同功能。当我调查它时,我发现实际上原生的CasperJS函数会导致麻烦。
例如:
waitUntilVisible('.buy')
一直在为我工作,最近在编写新代码时,它不再回归真实。我试图用
替换它waitFor(function(){
return this.exists('.buy');
}
仍然没有运气......之后我尝试使用这样的评估
waitFor(function(){
return this.evaluate(function(){
return $('.buy').length > 0;
})
}
通常断言为真。在此片段之前和之后看到屏幕截图,清楚地显示元素就在那里。
奇怪的是,这个完整的代码片段会检查购买按钮并点击它,如果它在那里,则可以在其他相同的页面上工作。
这是一个无效的功能
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();
});
})
答案 0 :(得分:0)
在调查了更多之后,我认为这个问题与phantomJS问题有关,css选择器与nth-of-type的一致性并调用了casper.reload()。通过删除reload()函数,我在测试中消除了大部分失败,我还需要更换更多的选择器来测试它是否与之相关。
希望有所帮助。