Scala Enthusiast
Scala Enthusiast

Reputation: 468

Angular TypeScript Protractror end-2-end testing - remove need for browser.sleep() calls in code

I am writing in interface in Angular with TypeScript. We use Protractor for end-2-end testing. Problem we have is that our screens have quite a few sections that fold/unfold, or modals that appear and close when buttons are clicked.

On these we have to add a browser.sleep(500) after the click to open them so that they will be open before the next part of the test runs.

Is this the best way? or should we be adding a call-back of sorts when the window/section is open/closed and only continuing when it is called?

if('the confirmation modal opens, I can accept', () => {
  element(by.id('openPopUpButton')).click();
  browser.sleep(500);
  expect(element(by.id('confirmationTextString')).isPresent()).toBeTruthy();
});

I'm hesitant to add code that is specifically and solely to enable tests to run correctly. I would prefer if we could rely on async/await or promises in some way, but is that possible?

Upvotes: 0

Views: 145

Answers (1)

Ben Mohorc
Ben Mohorc

Reputation: 694

browser.wait() is a more nuanced version of sleep. Using Expected Conditions you can make your code wait until the desired condition. For example (Using your code)

let EC = protractor.ExpectedConditions;
element(by.id('openPopUpButton')).click();
let confirm = element(by.id('confirmationTextString'));
browser.wait(EC.presenceOf(confirm),15000); //Timeout of 15 seconds
expect(confirm.getText()).toBe('Random Text') //Checked for text since wait basically checks for isPresent

If the element takes 1 second to become present, it will only wait 1 second, but if you are on a slow browser and it takes 10 seconds to load, your code will wait 10 seconds.

Upvotes: 1

Related Questions