-1

My problem is that I have non-angular login page but sometimes when clicks Login button it stops on loading (login page still visible).

What I want is that after click Login button I give it ex. 5 sec to finish load, if not - I want to click Login button again.

Whats the best way to do that? My current part of code looks like:

browser.waitForAngularEnabled(false);
await browser.driver.wait(EC.visibilityOf(this.getLoginForm()), 10000);
await this.usernameInput.sendKeys(username);
await this.passwordInput.sendKeys(password);
await this.clickLogInButton();
//here I want condition with timeout for page loading with repeat clickLogInButton if necessary
browser.waitForAngularEnabled(true);
MichalM
  • 11
  • 4

1 Answers1

0

the most optimal way in my opinion would be try/catch even though I rarely suggest to use it

// since you may wait for home page twice, you want to make it a function to avoid duplicate code
let waitForHomePage = async function () {
  await browser.wait(
    EC.visibilityOf(welcomeMessage), <-- give it a home page specific element
    10000
  )
}

await browser.waitForAngularEnabled(false); // <-- don't forget await here
await browser.driver.wait(EC.visibilityOf(this.getLoginForm()), 10000);
await this.usernameInput.sendKeys(username);
await this.passwordInput.sendKeys(password);
await this.clickLogInButton();

try {
  await waitForHomePage();
} catch (e) {
  await this.clickLogInButton();
  await waitForHomePage();
}

await browser.waitForAngularEnabled(true); // <-- don't forget await here

I'll wait maximum 10 sec, and if in 10 seconds the home page is still not present it will click and wait again

Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40
  • or you can actually put click and wait in one function to save extra line of your code – Sergey Pleshakov Oct 14 '20 at 12:46
  • Hmm, but it freezes on first line with clickLogInButton, page is trying to load and load and finally its too late to make try-catch part of code. Maybe there should not be 'await'? – MichalM Oct 14 '20 at 13:28
  • I just answered the question you asked. I'm not aware of what your `clickLogInButton` is, I assume you tested it and it works. So that's on you – Sergey Pleshakov Oct 14 '20 at 14:25
  • if you're still having problems, accept this question and open another one for `await this.clickLogInButton();` problem – Sergey Pleshakov Oct 14 '20 at 14:26
  • Ok, thanks but you should understand that try-catch part of code does not resolve problem, because I am stuck at "await clickLogInButton" while its loading what causes timeout, after that its trying to reach try-catch part :/ I just need to click button, start counting while its loading, if it goes to ex. 10s -> please repeat click button – MichalM Oct 14 '20 at 21:42
  • you may have dozen of problems in your code, I just helped you with the question you asked. I can't make blind guesses about what `this.clickLogInButton();` is it can be another 3000 lines of code – Sergey Pleshakov Oct 15 '20 at 00:13
  • + you didn't read my first comment. I bet it will solve your problem – Sergey Pleshakov Oct 15 '20 at 00:13
  • but then you will find that you didn't need to click second/third time and just needed to figure out why the button didn't get the click the first time and realize that you need to force click it or wait until its clickable. But you never asked this question. We can have this conversation forever, unless you will be more specific in your questions – Sergey Pleshakov Oct 15 '20 at 00:33