我怎样才能等待一个条件? [英] How can I wait for a condition?
问题描述
我是量角器的新手,我正在尝试实施 e2e 测试.我不知道这是否是正确的方法,但是...我要测试的页面不是基于完整角度的页面,所以...我遇到了一些麻烦.
I'm new on protractor, and I'm trying to implement an e2e test. I don't know if this is the right way to do this, but... The page that I want to test is not a full angular page based, so... I'm having some trouble.
在我的第一个规范中:
describe('should open contact page', function() {
var ptor = protractor.getInstance();
beforeEach(function(){
var Login = require('./util/Login');
new Login(ptor);
});
我已经创建了这个 Login 类,但登录后我想打开联系页面,但量角器立即尝试在页面完全加载之前查找元素.
I have created this Login class, but after login I want to open the contact page, but protractor immediately try to find element before the page is fully loaded.
我尝试使用:
browser.driver.wait(function() {
expect(browser.findElement(by.xpath("//a[@href='#/contacts']")).isDisplayed());
ptor.findElement(by.xpath("//a[@href='#/contacts']")).click();
});
但它不起作用......它总是在页面加载之前尝试找到元素.我也试过这个:
But it doesn't work... it always try to find the element before the page loads. I tried this one too:
browser.driver.wait(function() {
expect(ptor.isElementPresent(by.xpath("//a[@href='#/contacts']")));
ptor.findElement(by.xpath("//a[@href='#/contacts']")).click();
});
我可以使用 browser.sleep();
做到这一点,但我认为这不是一个好的选择.任何的想法?在我的登录课程中,我有:
I'm able to do that using browser.sleep();
but I don't think that is a good option. Any idea? On my login class I have:
ptor.ignoreSynchronization = true;
在量角器尝试点击它之前,我如何等待这个 @href='#/contacts
?
How can I wait for this @href='#/contacts
before protractor tries to click on it?
推荐答案
我遇到了与您在使用量角器时遇到的相同问题.在我的 e2e 测试中,我从一个非角度应用程序开始,然后进入一个角度部分,然后回到一个非角度部分.让事情变得棘手.关键是要了解承诺及其运作方式.这是我在运行 e2e 测试中的真实世界代码的一些示例.希望这能让您了解如何构建测试.这段代码中可能有一些不好的做法,请随时对此进行改进,但我知道它有效,也许不是最好的方法.
I had the same problem you were having for the longest time while using protractor. In my e2e test I start in a non angular app, then get into an angular portion, then get back out to a non angular portion. Made things tricky. The key is to understand promises and how they work. Here's some examples of my real world code in a functioning e2e test. Hoping this gives you an idea of how to structure your tests. Probably some bad practice in this code, please feel free to improve upon this, but I know that it works, maybe not the best way.
为了达到角度,我使用
var ptor;
var events = require('events');
var eventEmitter = new events.EventEmitter();
var secondClick = require('./second-click');
beforeEach(function () {
browser.driver.get('http://localhost:8080/');
},10000);
it("should start the test", function () {
describe("starting", function () {
it("should find the link and start the test", function(){
var elementToFind = by.linkText('Start'); //what element we are looking for
browser.driver.isElementPresent(elementToFind).then(function(isPresent){
expect(isPresent).toBe(true); //the test, kind of redundant but it helps pass or fail
browser.driver.findElement(elementToFind).then(function(start){
start.click().then(function(){ //once we've found the element and its on the page click it!! :)
ptor = protractor.getInstance(); //pass down protractor and the events to other files so we can emit events
secondClick(eventEmitter, ptor); //this is your callback to keep going on to other actions or test in another file
});
});
});
});
});
},60000);
虽然在 angular 中此代码有效
While in angular this code works
describe("type in a message ", function(){
it("should find and type in a random message", function(){
var elementToFind = by.css('form textarea.limited');
browser.driver.isElementPresent(elementToFind).then(function(isPresent){
element(elementToFind).sendKeys(randomSentence).then(function(){
console.log("typed in random message");
continueOn();
});
});
});
},15000);
退出angular后
browser.driver.wait(function(){
console.log("polling for a firstName to appear");
return browser.driver.isElementPresent(by.name('firstName')).then(function(el){
return el === true;
});
}).
then(function(){
somefunctionToExecute()
});
希望能给您一些指导和帮助!
Hope that gives some guidance and helps you out!
这篇关于我怎样才能等待一个条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!