我怎么能等待状态? [英] How can I wait for a condition?

查看:176
本文介绍了我怎么能等待状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在量角器新的,和我想要实现一个端到端的测试。
我不知道这是否是这样做的正确的方式,但...
我想测试的页面是不是基于一个全角页面,所以...我有一些麻烦。

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.

在我的第一个规范我有:

On my first spec I have:

describe('should open contact page', function() {
var ptor = protractor.getInstance();

beforeEach(function(){

   var Login = require('./util/Login');
   new Login(ptor);
});

我创造了这个登录类,但登录后我想打开联系人页面,但是量角器立即设法找到元素之前的页面完全加载。

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类有:

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 ='#/联系人前量角器尝试一下就可以了?

How can I wait for this @href='#/contacts before protractor tries to click on it?

推荐答案

我在使用量角器你有时间最长的同样的问题。在我的测试端到端我开始在非角的应用程序,然后进入一个棱角的部分,然后回来到非角部分。使事情变得棘手。关键是要了解的承诺,以及它们如何工作。下面是一个有效的端到端测试我的现实世界code的一些例子。希望这给你如何构建你的测试的想法。也许在这个code一些不好的做法,请随时在此改进,但我知道,它的作品,也许不是最好的办法。

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);

而在这个角度code ++工程

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);

退出后角

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆