Question about avoiding ptor.sleep() in e2e test
I have the following sample code and I want to avoid use of sleep()
describe("E2E Sample",function(){
var ptor = protractor.getInstance();
it("should display all names",function(){
ptor.get("/");
var button = ptor.findElement(protractor.By.linkText('view names'));
button.click();
ptor.actions().mouseMove(ptor.findElement(protractor.By.linkText('option menu'))).perform();
ptor.sleep(500);
ptor.actions().mouseMove(ptor.findElement(protractor.By.linkText('view all'))).click().perform();
var elements = element.all(by.repeater('name in names'));
expect(elements.count()).toEqual(2);
},30000)
});
I tried with ptor.waitForAngular();,setTimeout(...);,driver.manage().timeouts().implicitlyWait(...)
but the count of expected elements is always 0. Am I missing something ? How can I avoid ptor.sleep(500);?
Does your menu actually have a delay in it that requires the 500ms wait?
Instead of using an action sequence for the click event, can you try:
ptor.findElement(protractor.By.linkText('view all')).click();
Protractor doesn't try to synchronize for action sequences, so that might fix it.
Thanks for the reply @juliemr, in your way it works great and delay is not necessary. We can close this issue.
I am using the same pattern ptor.findElement(protractor.By.xpath('/html/body/main/section/header/div/div/article[1]/div/div[2]')).click();
but it gives me this error
NoSuchElementError: No element found using locator: By.xpath("/html/body/main/section/header/div/div/article[1]/div/div[2]")
but if I am doing
ptor.sleep(3000); ptor.findElement(protractor.By.xpath('/html/body/main/section/header/div/div/article[1]/div/div[2]')).click();
it works fine, please inform something..
Hi Rajit, Use browser.wait() rather than using ptor.sleep(3000). I think you just need to make browser wait til element is present before going to click it.
try this:
browser.wait(function(){ return browser.isElementPresent(by.xpath('....')).then(function(result){ if(result){return true;}}); },20000); element(by.xpath(...)).click();
Hope it will works...
Hi sallojusuresh,
You are right, This is working, but what if I have required to find multiple element one after the other, for each case I have required to put different browser.wait(funnction(){ }) element().click() ///or any other action like
browser.wait(function () { return browser.isElementPresent(by.xpath('/html/body/main/section/header/div/div/article[1]/div/div[2]')).then(function (result) { if (result) { return true; } }); }, 20000); element(by.xpath('/html/body/main/section/header/div/div/article[1]/div/div[2]')).click();
browser.wait(function () {
return browser.isElementPresent(by.repeater('menu in menuGroupOptions.menus')).then(function (result) {
if (result) {
return true;
}
});
}, 20000);
element.all(by.repeater('menu in menuGroupOptions.menus')).then(function (arrs) {
expect(arrs.length).toEqual(10);
arrs[5].click()
});
browser.wait(function () {
return browser.isElementPresent(by.xpath('/html/body/main/aside[1]/div/div/div[2]/div[1]/div/div/div/div/div[1]/div/div[2]/pl-tool-bar-header/div/div[3]/div[2]/div')).then(function (result) {
if (result) {
return true;
}
});
}, 20000);
element(by.xpath('/html/body/main/aside[1]/div/div/div[2]/div[1]/div/div/div/div/div[1]/div/div[2]/pl-tool-bar-header/div/div[3]/div[2]/div')).click();
browser.wait(function () {
return browser.isElementPresent(by.model('row.entity.label')).then(function (result) {
if (result) {
return true;
}
});
}, 20000);
element(by.model('row.entity.label')).sendKeys('ClientSideTestCase');
browser.wait(function () {
return browser.isElementPresent(by.css('.default-save')).then(function (result) {
if (result) {
return true;
}
});
}, 20000);
element.all(by.css('.default-save')).then(function (newarrs) {
newarrs[0].click()
});
browser.wait(function () {
return browser.isElementPresent(by.css('.pl-cancel-btn')).then(function (result) {
if (result) {
return true;
}
});
}, 20000);
element.all(by.css('.pl-cancel-btn')).then(function (newarrs) {
newarrs[2].click()
});
browser.wait(function () {
return browser.isElementPresent(by.css('.icon-remove')).then(function (result) {
if (result) {
return true;
}
});
}, 20000);
element(by.css('.icon-remove')).then(function (newarrs) {
expect(newarrs.length).toEqual(11);
newarrs[0].click()
});
I don't think, you do require to put browser.wait() for finding every element, only few of them require browser wait before going to interact and most of them will be present on page once page loaded completely.
So as your interacting with page, you will be aware of the things where does browser.wait() required and where doesn't...
OR Create one function for browser wait with two parameters and call it whenever you do require to wait by passing element locator(like: by.css(..) or by.xpath(..)) and amount of time(30000)
var waitForElement=function(locatior,waitTime) { waitTime = typeof waitTime != 'undefined' ? timeout : 10000; return browser.wait(function () { return browser.isElementPresent(locator).then(function (result) { if (result) return result; }); }, timeout).thenCatch(function (err) { assert.fail("Element '" + locator + "' has not found: ERROR is -" + err); }); }
Hi sallojusuresh,
Thanks, but I am really not understanding the behaviour of this, now I have remove all browser.wait and still everything is working as desire, but previously i have required to put ptor.sleep otherwise it gives me error NoSuchElementError: No element found using locator:
Amazing..
If your app is angular-based and has no onTimeout in it (okay to use $interval), you should never have to wait. That being said, the problem is probably from the fact that you're using ptor.findElement. This returns a raw webdriver webelement instead of a protractor-wrapped elementfinder.
Always use element(by...) instead of ptor.findElement(by...).
ok Thanks hankduan, I will take this into consideration
@sallojusuresh Currently am using browser.sleep in many place ex:deleting few lines, here we cant use isprsent,clickable and all. some times delete is taking 20 sec some times 2 sec in such case how to use browser wait please help me. thanks in advance.