1

I am using protractor to test a series of webpages (actually mostly one webpage of Angular design, but others as well). I have created a series of page objects to handle this. To minimize code maintenance I have created an object with key value pairs like so:

var object = {
    pageLogo: element(by.id('logo')),
    searchBar: element.all(by.className('searchThing')),
    ...
    };

The assumption being that I will only need to add something to the object to make it usable everywhere in the page object file. Of course, the file has functions (assuming you are not familiar with the page object pattern) as such:

var pageNamePageObject = function () {
var object = {...};  //list of elements
this.get = function() {
    brower.get('#/someWebTag');
}
this.getElementText = function(someName){
     if (typeof someName == 'number')
            ... (convert or handle exception, whatever)
     return object[name].getText();
}
...

*Note these are just examples and these promises can be handled in a variety of ways in the page object or main tests

The issue comes from attempting to "cycle" through the object. Given that the particular test is attempting to verify, among other things, that all the elements are on the particular web page I am attempting to loop through these objects using the "isPresent()" function. I have made many attempts, and for brevities sake I will not list them here, however they include creating a wrapper promise (using "Q", which I must admit I have no idea how it works) and attempting to run the function in the 'expect' hoping that the jasmine core will wait for all the looping promises resolve and then read the output (it was more of a last ditch effort really).

jsNoob
  • 139
  • 1
  • 10
  • FYI I am leaving town for the next 5 days and will be intermittently checking the update so please bare with my infrequent responses. Thank you in advance. – jsNoob Oct 27 '15 at 15:26
  • How are you looping through an object of elements (can you provide any hints on how you are using the objects to loop, so that you dont get an answer that you have already tried) ? It would be easier if you could use an array instead of an object to loop through the elements. – giri-sh Oct 27 '15 at 16:50

1 Answers1

1

You should loop like you did before on all of the elements, if you want it in a particular order, create a recursive function that just calls itself with the next element in the JSON.

Now, to handle jasmine specs finishing before and that stuff. this function needs to be added to protractor's flow control for it to wait to continue, read more about it here. and also, dont use Q in protractor, use protractor's implementation of webdriverJS promises.

Also, consider using isDisplayed instead, assuming you want it to be dispalayed on the page.

So basically, your code skeleton will look like this:

it(.....{
    var flow = webdriver.promise.controlFlow();
    return webdriver.execute(function () {//your checks on the page here,
//if you need extract to external function as i described in my first paragraph

well i think that should provide you enough info on how to handle waiting for promises in protractor, hope i helped.

Community
  • 1
  • 1
vrachlin
  • 817
  • 5
  • 15