CasperJS / PhantomJS,一些测试随机失败

时间:2014-05-20 16:50:50

标签: javascript testing random phantomjs casperjs

我有一些场景,其中一些测试随机失败(可能是5/65),通常使用svg,自动完成等等......虽然CPU很好,但它来自我编写脚本的方式。

但我不知道如何改变它们以使它们健壮可靠。这是我使用wait()函数 - >有时候我有超时错误,有时会通过。

最奇怪的是,当我用两个脚本分割我的脚本时,失败的次数少于一次....


添加waitForResource尝试

向Artjom B.求助。

casper.test.begin('\n********* Navigation on directories : ***********', 4,{
setUp: function(test) {
    setLevel("normal");
},

tearDown: function(test) {
    getJSON();
},

test: function(test){
    "use strict";
    var url = 'http://www.linternaute.com/';
    casper.start()
   .thenOpen(url + "/ville/", function(){
        if(this.exists('.ui-dialog-titlebar-close')){this.click('.ui-dialog-titlebar-close');}
        if(this.exists('#top_oas')){this.click('#top_oas > .right');}
        this.test.assertExists(".jODMainSearch", "Search toolbar present");
        this.sendKeys(".jODMainSearch", "Ren");
        this.click(".submit.search");
    })
    .waitForSelector(".odListGlossary", function(){
        this.test.assertExists(".odListGlossary", "Search result ok");
        this.clickLabel("Rennes (35000)");
    })
    .waitFor(function check() {
            return this.fetchText('h1').indexOf("Rennes") !== -1;
            }
            , function then() {
                this.test.assertSelectorHasText("h1", "Rennes");
            }
    )
    //auto-completion
    .thenOpen(url + "/ville/", function(){

        //this.waitForResource(/odmainsearch/, function(){
            /*var fs = require('fs');
            fs.write("results1.html", this.getPageContent(), 'w');
            this.wait(8000, function(){
                var fs = require('fs');
            fs.write("results2.html", this.getPageContent(), 'w');
            });*/

            /*casper.waitFor(function check() {
                    return this.fetchText('script').indexOf("fn.odMainsearch=function") !== -1;
                    }
                    , function then() {
                        this.echo('cc');
                        this.sendKeys('.jODMainSearch', 'Ren', {keepFocus: true});
                    }
            );*/
        /*** the changes ***/   
        this.waitForResource(function testResource(resource) {
            return resource.url.indexOf("http://static.ccmbg.com/www.linternaute.com/asset/js?m=odmainsearch") !== -1;
        }, function onReceived() {
            this.echo('JS for auto-completion present');
            this.sendKeys('.jODMainSearch', 'Ren', {keepFocus: true});
        });
        this.waitForSelector('ul.ui-autocomplete[viewbox="true"]', function() {
            this.mouse.move('a[href="/ville/rennes/ville-35238"]');
            this.click('a[href="/ville/rennes/ville-35238"]');
        });
    })
    .waitFor(function check() {
            return this.fetchText('h1').indexOf("Rennes") !== -1;
            }
            , function then() {
                this.test.assertSelectorHasText("h1", "Rennes");
            }
    )
    .run(function() {
            this.test.comment('--- Done ---\n');
            test.done();
    });
}
});

首先尝试:

这是一个随机失败的例子:

casper.test.begin('\n********* Navigation on directories : ***********', 4,{
setUp: function(test) {
    //setLevel("normal");
},

tearDown: function(test) {
    //getJSON();
},

test: function(test){
    "use strict";
    var url = 'http://www.linternaute.com/';
    casper.start()
   .thenOpen(url + "/ville/", function(){
        //close some ads
        if(this.exists('.ui-dialog-titlebar-close')){this.click('.ui-dialog-titlebar-close');}
        if(this.exists('#top_oas')){this.click('#top_oas > .right');}
        this.test.assertExists(".jODMainSearch", "Search toolbar present");
        this.sendKeys(".jODMainSearch", "Ren");
        this.click(".submit.search");
    })
    .waitForSelector(".odListGlossary", function(){
        this.test.assertExists(".odListGlossary", "Search result ok");
        this.clickLabel("Rennes (35000)");
    })
    .waitFor(function check() {
            return this.fetchText('h1').indexOf("Rennes") !== -1;
            }
            , function then() {
                this.test.assertSelectorHasText("h1", "Rennes");
            }
    )
    //auto-completion
    .thenOpen(url + "/ville/", function(){
        this.sendKeys('.jODMainSearch', 'Ren', {keepFocus: true});
        this.waitForSelector('ul.ui-autocomplete[viewbox="true"]', function() {
            this.mouse.move('a[href="/ville/rennes/ville-35238"]');
            this.click('a[href="/ville/rennes/ville-35238"]');
        });
    })
    .waitFor(function check() {
            return this.fetchText('h1').indexOf("Rennes") !== -1;
            }
            , function then() {
                this.test.assertSelectorHasText("h1", "Rennes");
            }
    )
    .run(function() {
            this.test.comment('--- Done ---\n');
            test.done();
    });
}
});

如果我只接受脚本的结尾(在第一个示例中它随机失败,我已经评论了开头):

casper.test.begin('\n********* Navigation on directories : ***********', 1,{
setUp: function(test) {
    //setLevel("normal");
},

tearDown: function(test) {
    //getJSON();
},

test: function(test){
    "use strict";
    var url = 'http://www.linternaute.com/';
    casper.start()
   /*.thenOpen(url + "/ville/", function(){
        if(this.exists('.ui-dialog-titlebar-close')){this.click('.ui-dialog-titlebar-close');}
        if(this.exists('#top_oas')){this.click('#top_oas > .right');}
        this.test.assertExists(".jODMainSearch", "Search toolbar present");
        this.sendKeys(".jODMainSearch", "Ren");
        this.click(".submit.search");
    })
    .waitForSelector(".odListGlossary", function(){
        this.test.assertExists(".odListGlossary", "Search result ok");
        this.clickLabel("Rennes (35000)");
    })
    .waitFor(function check() {
            return this.fetchText('h1').indexOf("Rennes") !== -1;
            }
            , function then() {
                this.test.assertSelectorHasText("h1", "Rennes");
            }
    )*/
    //auto-completion
    .thenOpen(url + "/ville/", function(){
        this.sendKeys('.jODMainSearch', 'Ren', {keepFocus: true});
        this.waitForSelector('ul.ui-autocomplete[viewbox="true"]', function() {
            this.mouse.move('a[href="/ville/rennes/ville-35238"]');
            this.click('a[href="/ville/rennes/ville-35238"]');
        });
    })
    .waitFor(function check() {
            return this.fetchText('h1').indexOf("Rennes") !== -1;
            }
            , function then() {
                this.test.assertSelectorHasText("h1", "Rennes");
            }
    )
    .run(function() {
            this.test.comment('--- Done ---\n');
            test.done();
    });
}
});

那么问题是什么,你有什么想法吗? - >堆栈中的两个步骤:它经常失败,只有一个:它有时会失败......

2 个答案:

答案 0 :(得分:2)

加载页面时,看起来并非所有脚本都存在于DOM中。在评估内联脚本块之后,至少有一个js资源被加载,因此在window.onload之后加载,因此在thenOpen将控件提供给下一步之后加载。您的案例中的资源是:

http://static.ccmbg.com/www.linternaute.com/asset/js?m=odmainsearch&v=201405201‌​800

如果你想根据代码采取行动,你需要等待这么晚的资源。在您的情况下,自动完成下拉列表必须打开。所以我改变了

casper.thenOpen(url + "/ville/", function(){
    this.sendKeys('.jODMainSearch', 'Ren', {keepFocus: true});
    this.waitForSelector('ul.ui-autocomplete[viewbox="true"]', function() {
        this.mouse.move('a[href="/ville/rennes/ville-35238"]');
        this.click('a[href="/ville/rennes/ville-35238"]');
    });
});

casper.thenOpen(url + "/ville/")
.waitForResource(/linternaute\.com\/asset\/js/, function(){
    this.wait(500); // this seem necessary: maybe to wait for the script evaluation
})
.then(function(){
    this.sendKeys('.jODMainSearch', 'Ren', {keepFocus: true});
})
.waitForSelector('ul.ui-autocomplete[viewbox="true"]', function() {
    this.mouse.move('a[href="/ville/rennes/ville-35238"]');
    this.click('a[href="/ville/rennes/ville-35238"]');
});

我使用完整的脚本而没有取消注释第一部分。 casper仅用于定位。 20次运行没有错误。

答案 1 :(得分:0)

只需精确测试即可确保测试:

casper.test.begin('\n********* Navigation : ***********', 4,{
setUp: function(test) {
    //setLevel("normal");
},

tearDown: function(test) {
    //getJSON();
},

test: function(test){
    "use strict";
    var url = 'http://www.linternaute.com/';
    casper.start()
   .thenOpen(url + "/ville/", function(){
        if(this.exists('.ui-dialog-titlebar-close')){this.click('.ui-dialog-titlebar-close');}
        if(this.exists('#top_oas')){this.click('#top_oas > .right');}
        this.test.assertExists(".jODMainSearch", "Search toolbar present");
        this.sendKeys(".jODMainSearch", "Ren");
        this.click(".submit.search");
    })
    .waitForSelector(".odListGlossary", function(){
        this.test.assertExists(".odListGlossary", "Search result ok");
        this.clickLabel("Rennes (35000)");
    })
    .waitFor(function check() {
            return this.fetchText('h1').indexOf("Rennes") !== -1;
            }
            , function then() {
                this.test.assertSelectorHasText("h1", "Rennes");
            }
    )
    //auto-completion
    .thenOpen(url + "ville/")
    .waitForResource(/linternaute\.com\/asset\/js/, function(){
        this.wait(1000); // this seem necessary: maybe to wait for the script evaluation of 'http://static.ccmbg.com/www.linternaute.com/asset/js?m=odmainsearch&v=201405201‌​800'
    })
    .then(function(){
        this.sendKeys('.jODMainSearch', 'Ren', {keepFocus: true});
        this.waitFor(function checkAutoComplete(){
            return this.exists('ul.ui-autocomplete[viewbox="true"]');
            }
            , function then() {
                this.mouse.move('a[href="/ville/rennes/ville-35238"]');
                this.click('a[href="/ville/rennes/ville-35238"]');
            }
            , function timeout() { 
                // step to execute if checkAutoComplete has failed, it makes the test reliable if there is a freeze
                // or something wrong in the execution of the previous resource. 1 build on 50 fails without that. 
                // Like that you haven't to wait 5~9sec in waitForResource to be 'sure'.
                this.sendKeys('.jODMainSearch', 'n', {keepFocus: true});
                this.waitForSelector('ul.ui-autocomplete[viewbox="true"]', function() {
                    this.mouse.move('a[href="/ville/rennes/ville-35238"]');
                    this.click('a[href="/ville/rennes/ville-35238"]');
                });
            }
        );
    })
    .waitFor(function check() {
            return this.fetchText('h1').indexOf("Rennes") !== -1;
            }
            , function then() {
                this.test.assertSelectorHasText("h1", "Rennes");
            }
    )
    .run(function() {
            this.test.comment('--- Done ---\n');
            test.done();
    });
}
});