简体   繁体   中英

How can I access the 'until' object directly in Protractor?

I am writing a middleware framework for testing an AngularJS web app and I want to be able to wait for certain conditions to be met before I move on to another action in my tests. I am fairly new to Protractor and from what I have found in their doc and other forums, there are a few different ways to do this using browser.driver.wait or similar techniques that use a Condition or a promise including this answer .

What I am specifically trying to achieve is to follow the Page Object model design in my framework and to allow each page to provide its own function to determines whether that page is visible in the browser or not. In order to do that, I ended up writing page objects as below:

var pages = {
  home: {
    name: 'home',
    url: server + '/#/home',
    title: 'Home',
    isVisible: until.titleIs(this.title)
  },
  ...
}

There are two issues that I am trying to wrap my head around and fix:

  1. First and foremost, I get an error saying that until is undefined. I have not been able to find the right way to use this object yet and all my searches lead to alternative ways for waiting for an element to be visible or an expected condition to be true. How can I access this object directly? And, is that considered bad/obsolete design?
  2. Assuming that I find a way to use the until object directly, I can see in Protractor source code that the until.titleIs() function returns a Condition , so my next question is how I can use this Condition object to get its the boolean result after the waiting is over (or times out)? I will need to do this for some special cases such as below:

     var condition = until.titleIs('some title'); var result = browser.wait(condition, timeout).then(function(res) { return res; }); if (result) // do something else // do another thing 

I will really appreciate your help with this!

until is the protractor.ExpectedConditions object. I would define it globally, put the following into onPrepare() function in your config:

onPrepare: function () {
    global.until = protractor.ExpectedConditions;
},

As for your second question, I would make isVisible a function, that would return a promise:

isVisible: function () {
    return browser.wait(until.titleIs(this.title), 5000).then(function () {
        return true;
    },
    function () {
        return false;
    });
},

Then, you can expect it in your test:

expect(myPageObject.isVisible()).toBe(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM