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

Cookies AssertionError:查找文档正文casperJS/phantomJS中的文本,以及如何使用casperJS计算具有特定类名的Li总数,cookies,phantomjs,casperjs,Cookies,Phantomjs,Casperjs,我尝试了以下代码: var casper = require('casper').create(); var mouse = require("mouse").create(casper); phantom.casperTest = true; phantom.cookiesEnabled = true; phantom.addCookie({ 'name': 'authToken',

我尝试了以下代码:

        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的总数?

回答您的评论: 这取决于你是否掌握了CSS选择器:

this.click('li.history:nth-of-type(2) a');
但是需要注意的是,它从其父级获取第二个li元素,因此类名不会过滤搜索(它只用于返回正确的父级)。使用xpath(或带有eq()的jQuery)更直观(如果我记得很清楚的话,xpath就是这样):


使用tester模块:this.test.assertElementCount('li.yourClass',7);如果您想要数字:var tmp=this.getElementsInfo('li.yourClass')或this.getElementsBounds('li.yourClass')+tmp.length谢谢您的评论Yes thanku@Fanch这在li中对我有用count@Fanch如果我将其用于第一个元素this.click('li.history a');那么列表中的第二个和第三个元素应该使用什么呢。
var x = require('casper').selectXPath;
this.click(x("li[@class='layout_right'][2] a"));