AssertionError:在文档体casperJS / phantomJS中查找文本以及如何使用casperJS计算具有特定类名的Li的总数

时间:2014-04-22 12:32:42

标签: cookies phantomjs casperjs

我试过这段代码:

        var casper = require('casper').create();
        var mouse = require("mouse").create(casper);
        phantom.casperTest = true;
        phantom.cookiesEnabled = true;

        phantom.addCookie({
            'name': 'authToken',
            'value': '88f115b38585155c1',
            'domain': 'localhost',
            'path': '/'
        });
        phantom.addCookie({
            'name': 'activationId',
            'value': '1',
            'domain': 'localhost',
            'path': '/'
        });
        phantom.addCookie({
            'name': 'userId',
            'value': '1',
            'domain': 'localhost',
            'path': '/'
        });
        casper.start('http://localhost/history.php', function() {
            casper.on('remote.message', function(message) {
                console.log(message);
            });
            casper.thenOpen('http://localhost/history.php', function() {
                this.page.render('cookie.jpeg');
                this.evaluate(function() {
                    console.log('cookie it here');
                    console.log("title--" + document.title);
                    console.log(document.cookie);
                });

            });
        });

        casper.then(function() {
            this.test.assertTextExists('Yesterday');
        });

        casper.run();

它给了我这个错误

                    root@debian:/var/www# casperjs histroyuitest_today.js
        document ready
        devicePixelRatio---1
        load history products
        [object Object],[object Object]
        cookie it here
        title--Zen
        authToken=88f115b38585155c1; activationId=1; userId=1
        FAIL Find text within the document body
        #    type: assertTextExists
        #    subject: false
        #    text: "Yesterday"
        AssertionError: Find text within the document body
          /root/phantomjs-1.9.2-linux-i686/casperjs/modules/tester.js:323 in assert
          /root/phantomjs-1.9.2-linux-i686/casperjs/modules/tester.js:766 in assertTextE                                                                                             xists
          /var/www/histroyuitest_today.js:40
          /root/phantomjs-1.9.2-linux-i686/casperjs/modules/casper.js:1558 in runStep
          /root/phantomjs-1.9.2-linux-i686/casperjs/modules/casper.js:399 in checkStep
        ⚠  looks like you did not use begin(), which is mandatory since 1.1
        FAIL AssertionError: Find text within the document body
        #    type: error
        #    subject: false
        #    error: "AssertionError: Find text within the document body"
        #    stack: in assert() in /root/phantomjs-1.9.2-linux-i686/casperjs/modules/tes                                                                                             ter.js:323
        in assertTextExists() in /root/phantomjs-1.9.2-linux-i686/casperjs/modules/teste                                                                                             r.js:766
        in anonymous() in histroyuitest_today.js:40
        in runStep() in /root/phantomjs-1.9.2-linux-i686/casperjs/modules/casper.js:1558
        in checkStep() in /root/phantomjs-1.9.2-linux-i686/casperjs/modules/casper.js:39                                                                                             9

为什么我收到此错误?我的饼干是设置还是未设置?如何在casperJS中用表格计算具有特定类名的Li的总数?

1 个答案:

答案 0 :(得分:1)

回答你的意见: 嗯,这取决于,我不知道你是否掌握了CSS选择器:

this.click('li.history:nth-of-type(2) a');

但是小心,它从其父级获取第二个li元素,因此classname不会过滤搜索(它仅用于获取正确的父级)。使用xpath(或jQuery与eq())更直观(如果我记得很好,就像使用xpath那样):

var x = require('casper').selectXPath;
this.click(x("li[@class='layout_right'][2] a"));