Reputation: 60
I'm trying to use the WebdriverJS wait() method in my page objects to make sure that any time I access an element, Webdriver will wait until it is present. Currently, the code I have "looks" correct, but it won't let me click the element once it's returned from wait(). Based on the research I've done, it looks like there have been others who have been able to do this successfully but as of yet I have not been able to figure it out.
I am getting the error: TypeError: undefined is not a function - on element.click()
Method in my Page Object:
FacilityPage.prototype.selectFacility = function (facilityName) {
var self = this;
this.driver.wait(function () {
return self.driver.isElementPresent(by.linkText(facilityName)).then(function (elm) {
console.log('This is elm: ', elm);
return elm;
});
}, 10000).then(function (element) {
element.click();
});
};
How I call from my test file:
facilityPage.selectFacility('Facility 1');
Upvotes: 1
Views: 116
Reputation: 3731
Protractor had a breaking change in 2.0 that killed this way of testing for availability. The new hotness is ExpectedConditions. Here's how I might handle your case...
If you have a basePage (if not just the page), set up your expected condition (I rename them to be a bit more useful):
var EC = protractor.ExpectedConditions;
this.isClickable = function(locator) {
return EC.elementToBeClickable(locator);
};
I also use a general purpose waitAndClick
er, which is the heart of what we're talking about here.
this.waitAndClick = function(element) {
browser.wait(this.isClickable(element), 5000).then(function() {
element.click();
});
};
Then in the page object, handle your specific case.
FacilityPage.prototype.selectFacility = function (facilityName) {
this.waitAndClick(element(by.linkText(facilityName)));
};
Call it the same way...
facilityPage.selectFacility('Facility 1');
Upvotes: 2