2016-05-12 39 views
1

我一直在使用/学习量角器超过一个月了。量角器asnchronus执行问题

我知道,量角器文件说,它等待的角度调用来完成(http://www.protractortest.org/#/),这将确保所有 步骤同步执行等。

但我不找到它的方式。或者至少,我在脚本中找不到这种方式 许多时间量角器都在前面运行,例如,如果我点击一个链接,获取当前网址,然后验证网址。

大多数情况下,URL值将会陈旧,即点击链接后不会执行。下面是我的页面对象代码示例和相应的测试。

请建议如何确保所有测试步骤均以连续顺序执行

Page Object 
this.getSPageLink(){ 
    return element(by.xpath("//a[@title='S']")); 
}; 
this.getLPageLink(){ 
    return element(by.xpath("//a[@title='L']")); 
}; 
this.goToSPage = function() { 
    (this.getSPageLink()).click(); 
    *//ERROR here, sometimes second click (below) doesn't wait for first 
    click to complete, and complains that link for 2 click (below) is 
    not found* 
    (this.getSLPageLink()).click(); 
    return browser.currentURL(); 
    *//ERROR HERE url line (e) is sometimes executed before* 
} 

Test Class 
it('::test SL page', function() { 
     pageObject.goToSpage(); 
     var currentURL=browser.getCurrentURL(); 
     expect(currentURL).toContain("SLink"); 
     *//ERROR HERE value in this variable "currentURL" is most of the 
     times Stale* 
}); 

it('::test SL2 page', function() { 
     pageObject.goToLpage(); 
     var currentURL=browser.getCurrentURL(); 
     expect(currentURL).toContain("Link"); 
     console.log("this line is always executed first")); 
     //ERROR here , this print line is always executed first 
}); 

回答

1

这是相当混乱和量角器文档不是对这个很清楚,但量角器等待页面使用getrefresh命令时,只有同步。在你的情况下,你正在使用click命令加载一个新页面,所以在继续之前,Protractor不会等待新页面同步。 See the reasoning来自Protractor开发人员的此行为。

所以在你的情况下,你需要照顾你的测试或页面对象。在这两种情况下,你可以点击使用后是这样的:

browser.wait(function() { 
    return browser.driver.getCurrentUrl().then(function (url) { 
     return /SLink/.test(url); 
    }); 
}, 10000); 

这将经常轮询当前URL越好(在实践中每500ms左右一次)长达10秒。当它在当前URL中找到“Slink”字符串时,它将继续执行下一个命令。

+0

谢谢你的很好的解释。 – sathya

+0

另外,有没有一种首选的方法来确保所有测试行(IT块)都是连续执行的?例如,console.log始终首先执行,而不是按它所在的顺序执行。 – sathya

+0

实际上webdriverjs/protractor绑定中没有500 ms的轮询间隔.http://stackoverflow.com/a/34377095/771848。谢谢。 – alecxe