使用casperjs测试损坏的html [英] Testing broken html with casperjs

查看:95
本文介绍了使用casperjs测试损坏的html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一些设置例程,然后再运行一些CasperJs浏览器测试。

I am trying to run some setup routines before running some CasperJs browser tests.

有一点我无法填写表单数据,因为有一些错误的HTML (表格标签放错在表格中):

At one point I am unable to fill in form data because there is some misplaced HTML (a form tag is misplaced in a table):

<!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>

这是一个简单的测试案例:

This is the simple test case:

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();
    });

});

测试结果为:错误:填写表格时遇到错误:无字段在表单中匹配名称选择器selector_1

当我在此示例中删除表标记时,它可以正常工作。

It works when I simply remove the table tag in this example.

不幸的是,我无法在真实世界中更改此内容,因为破坏的HTML来自我没有源代码访问权限的应用程序。

Unfortunatly I cannot change this in the "real world" because the broken HTML is from an application I have no source code access.

可以这可以直接用CasperJs解决吗?

Can this be solved with CasperJs directly?

我想我也可以尝试通过更换损坏的部分来修复HTML。这可能是让这个工作的唯一方法吗?

I guess I could also try to "fix" the HTML by replacing the broken parts. Could this be the only way to get this working?

推荐答案

您应该尝试使用fillXPath(但它在版本1.1中可用) ):

You should try to use fillXPath (but it is available in version 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();
    });

});

这篇关于使用casperjs测试损坏的html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆