我试图在运行一些 CasperJs 浏览器测试之前运行一些设置例程。
有一次我无法填写表单数据,因为有一些错误的 HTML(表单标签在表格中错误放置):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<table>
<form id="test1">
<input type="text" name="selector_1" />
</form>
</table>
</body>
</html>
这是简单的测试用例:
casper.test.begin('Test', 0, function suite(test) {
casper.start('http://localhost:8000', function() {
this.fill('form#test1', {
"selector_1": 'Yo!'
}, true);
});
casper.run(function() {
test.done();
});
});
测试结果是:error: Errors encountered while filling form: no field matching names selector "selector_1" in form
当我简单地删除本例中的表标签时,它就起作用了。
不幸的是,我无法在“现实世界”中更改此设置,因为损坏的 HTML 来自我无法访问源代码的应用程序。
这个问题可以直接用CasperJs解决吗?
我想我也可以尝试通过替换损坏的部分来“修复” HTML。这是使其发挥作用的唯一方法吗?
您应该尝试使用 fillXPath (但它在 1.1 版本中可用):
casper.test.begin('Test', 0, function suite(test) {
casper.start('http://127.0.0.1:8020/test_casper/testme.html', function() {
this.test.assertExists({
type: 'xpath',
path: '//*[@name="selector_1"]'
}, 'the element exists');
this.fillXPath('form#test1', {
'//input[@name="selector_1"]': 'Yo!'
}, true);
});
casper.run(function() {
test.done();
});
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)