shared.ts 3.53 KB
Newer Older
1
import { browser, by, element } from "protractor";
Nishanth Mane's avatar
Nishanth Mane committed
2
import { ExpectedConditions as EC } from "protractor";
3
import { ProtractorBrowser } from "protractor/built/browser";
Natalie Liles's avatar
Natalie Liles committed
4 5 6 7 8 9
import {
  ConfirmEmailPage,
  isExtension,
  LoginPage,
  RegisterPage
} from "./app.po";
Nishanth Mane's avatar
Nishanth Mane committed
10

11
const SERVER_URL = "http://api:8000";
12
export const DEFAULT_WAIT = 37000;
Nishanth Mane's avatar
Nishanth Mane committed
13

Natalie Liles's avatar
Natalie Liles committed
14 15 16 17 18 19 20
export async function register(
  testBrowser: ProtractorBrowser,
  username: string,
  password: string,
  passwordConfirm: string,
  url = SERVER_URL
) {
21
  await testBrowser.sleep(50);
22
  const loginPage = new LoginPage();
Natalie Liles's avatar
Natalie Liles committed
23
  await loginPage.navigateTo(testBrowser);
Natalie Liles's avatar
Natalie Liles committed
24
  expect(await loginPage.getParagraphText(testBrowser)).toEqual("Log In");
Natalie Liles's avatar
Natalie Liles committed
25
  await loginPage.getRegisterLink(testBrowser).click();
Natalie Liles's avatar
Natalie Liles committed
26
  await browser.sleep(1000);
Nishanth Mane's avatar
Nishanth Mane committed
27

28
  const registerPage = new RegisterPage();
Natalie Liles's avatar
Natalie Liles committed
29
  expect(await registerPage.getParagraphText(testBrowser)).toEqual("Sign Up");
Natalie Liles's avatar
Natalie Liles committed
30
  await registerPage.enterEmail(testBrowser, username);
David Burke's avatar
David Burke committed
31
  await registerPage.clickEmailNextButton(testBrowser);
Natalie Liles's avatar
Natalie Liles committed
32 33
  await registerPage.enterPassword(testBrowser, password);
  await registerPage.enterPasswordConfirm(testBrowser, passwordConfirm);
David Burke's avatar
David Burke committed
34
  await registerPage.clickPasswordNextButton(testBrowser);
35 36 37
  if (isExtension) {
    await registerPage.enterServerURL(testBrowser, SERVER_URL);
  }
Natalie Liles's avatar
Natalie Liles committed
38 39 40 41 42 43
  const newsletterText = registerPage.getNewsletterText(testBrowser);
  await EC.browser.wait(
    EC.presenceOf(newsletterText),
    DEFAULT_WAIT,
    "Newsletter text should be shown"
  );
David Burke's avatar
David Burke committed
44
  await registerPage.clickNewsletterNextButton(testBrowser);
45
  const confirmEmailPage = new ConfirmEmailPage();
Natalie Liles's avatar
Natalie Liles committed
46
  const codeElem = confirmEmailPage.getCodeElem(testBrowser);
Natalie Liles's avatar
Natalie Liles committed
47 48 49 50 51
  await EC.browser.wait(
    EC.presenceOf(codeElem),
    DEFAULT_WAIT,
    "Confirm code page should be shown"
  );
52
  await codeElem.isDisplayed();
53
}
Nishanth Mane's avatar
Nishanth Mane committed
54

Natalie Liles's avatar
Natalie Liles committed
55 56 57 58 59 60
async function doLogin(
  testBrowser: ProtractorBrowser,
  username: string,
  password: string,
  url = SERVER_URL
) {
61
  await testBrowser.sleep(50);
62
  const loginPage = new LoginPage();
Natalie Liles's avatar
Natalie Liles committed
63
  await loginPage.navigateTo(testBrowser);
David Burke's avatar
David Burke committed
64
  await testBrowser.sleep(50);
Natalie Liles's avatar
Natalie Liles committed
65 66
  await loginPage.enterEmail(testBrowser, username);
  await loginPage.enterPassword(testBrowser, password);
David Burke's avatar
David Burke committed
67
  await loginPage.enterRememberMe(testBrowser);
David Burke's avatar
David Burke committed
68
  await browser.sleep(50);
69 70
  if (isExtension) {
    await loginPage.enterServerURL(testBrowser, SERVER_URL);
Natalie Liles's avatar
Natalie Liles committed
71
  }
David Burke's avatar
David Burke committed
72 73 74 75
  return await loginPage.submitByEnter(testBrowser);
}

/** This login will wait for success */
Natalie Liles's avatar
Natalie Liles committed
76 77 78 79 80 81
export async function login(
  testBrowser: ProtractorBrowser,
  username: string,
  password: string,
  url = SERVER_URL
) {
David Burke's avatar
David Burke committed
82
  await doLogin(testBrowser, username, password, url);
Natalie Liles's avatar
Natalie Liles committed
83 84 85 86
  return await browser.wait(
    EC.presenceOf(testBrowser.element(by.id("search"))),
    DEFAULT_WAIT
  );
David Burke's avatar
David Burke committed
87 88 89
}

/** This login will not wait for success - intended for testing for errors */
Natalie Liles's avatar
Natalie Liles committed
90 91 92 93 94 95
export async function failLogin(
  testBrowser: ProtractorBrowser,
  username: string,
  password: string,
  url = SERVER_URL
) {
David Burke's avatar
David Burke committed
96
  return await doLogin(testBrowser, username, password, url);
97
}
Nishanth Mane's avatar
Nishanth Mane committed
98

Natalie Liles's avatar
Natalie Liles committed
99
export const checkAndLogin = async (url: string) => {
Natalie Liles's avatar
Natalie Liles committed
100
  await browser.get(url);
Natalie Liles's avatar
Natalie Liles committed
101 102 103 104
  const USERNAME =
    Math.random()
      .toString(36)
      .slice(2) + "@example.com";
Natalie Liles's avatar
Natalie Liles committed
105
  const PASSWORD = "hunterhunter22";
106
  browser.controlFlow().execute(() => {
Natalie Liles's avatar
Natalie Liles committed
107 108 109 110 111 112 113 114 115 116 117
    browser.getCurrentUrl().then(currentUrl => {
      if (currentUrl === browser.baseUrl + "login") {
        register(browser, USERNAME, PASSWORD, PASSWORD);
        login(browser, USERNAME, PASSWORD);
        browser.wait(
          EC.stalenessOf(element(by.css("auth-form__heading"))),
          DEFAULT_WAIT,
          "Login wait failed"
        );
      }
    });
118
  });
Nishanth Mane's avatar
Nishanth Mane committed
119
};