Skip to content

Commit

Permalink
fix: logins now work
Browse files Browse the repository at this point in the history
  • Loading branch information
dgattey committed Mar 22, 2022
1 parent a0cd161 commit 8930a2c
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 38 deletions.
49 changes: 24 additions & 25 deletions helpers/login.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import type {
BrowserContext,
Page,
PlaywrightTestArgs,
PlaywrightTestOptions,
PlaywrightWorkerArgs,
PlaywrightWorkerOptions,
TestType,
} from '@playwright/test';
import type { BrowserContext, Page } from '@playwright/test';
import { expect } from '@playwright/test';
import { LOGIN_PAGE, SHOPKEEP_ROUTES } from './routes';
import { LOGIN_PAGE, SHOPKEEP_ROUTES, SUPPLIER_ROUTES } from './routes';

/**
* Selectors used for logging in
Expand All @@ -19,13 +11,13 @@ export const LOGIN_BUTTON_SELECTOR = 'button#login';

const E2E_ACCOUNT_LOGIN = 'e2e_tester@shopcanal.com';

export const logInSuccessfully = async (
/**
* Use this to log a user in and make sure they land on a given page.
*/
const logIn = async (
page: Page,
context: BrowserContext,
test?: TestType<
PlaywrightTestArgs & PlaywrightTestOptions,
PlaywrightWorkerArgs & PlaywrightWorkerOptions
>,
firstLoggedInPageUrl: string,
): Promise<void> => {
if (process.env.APP_TEST_PASSWORD) {
// Make sure browser is logged out before attempting to go through login flow
Expand All @@ -41,22 +33,29 @@ export const logInSuccessfully = async (

// Then click the login button
await page.click(LOGIN_BUTTON_SELECTOR);

// Wait 10 seconds to give time for the next page to load
await page.waitForTimeout(10000);

// See if the nav has loaded - if it hasn't, then skip the test since login was flaky
const overviewButton = await page.$('a#navOverview');
if (test) test.skip(!overviewButton, 'Login was not successful - skipping test');

// Ensure that the URL is now the URL of the Shopkeep Inventory page
expect(page.url().includes(SHOPKEEP_ROUTES.INVENTORY)).toBeTruthy();
await page.waitForURL(firstLoggedInPageUrl);
} else {
console.warn('Could not log in because no APP_TEST_PASSWORD was provided. Failing test.');
expect(true).toBe(false);
}
};

/**
* Logs a user into the shopkeep landing page
*/
export const logIntoShopkeep = async (page: Page, context: BrowserContext): Promise<void> =>
logIn(page, context, SHOPKEEP_ROUTES.INVENTORY);

/**
* Logs a user into the supplier landing page
*/
export const logIntoSupplier = async (page: Page, context: BrowserContext): Promise<void> =>
logIn(page, context, SUPPLIER_ROUTES.OVERVIEW);

/**
* Simulates a log out by clearing cookies. With a refresh, this should be enough
* to fully log out.
*/
export const logout = async (context: BrowserContext): Promise<void> => {
await context.clearCookies();
};
5 changes: 3 additions & 2 deletions tests/login.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from '@playwright/test';
import {
logInSuccessfully,
logIntoShopkeep,
LOGIN_BUTTON_SELECTOR,
LOGIN_EMAIL_INPUT_SELECTOR,
LOGIN_PASSWORD_INPUT_SELECTOR,
Expand All @@ -18,6 +18,7 @@ test.describe('Login', () => {
*/
test.beforeEach(async ({ page }) => {
await page.goto(LOGIN_PAGE);
await page.waitForLoadState('networkidle');
});

/**
Expand All @@ -28,7 +29,7 @@ test.describe('Login', () => {
context,
page,
}) => {
await logInSuccessfully(page, context, test);
await logIntoShopkeep(page, context);
});

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/shopkeep/discover.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from '@playwright/test';
import { logInSuccessfully } from '../../helpers/login';
import { logIntoShopkeep } from '../../helpers/login';
import { SHOPKEEP_ROUTES } from '../../helpers/routes';

/**
Expand All @@ -12,10 +12,12 @@ test.describe('Shopkeep Discover', () => {
* and then navigate to the Discover page
*/
test.beforeEach(async ({ context, page }) => {
await logInSuccessfully(page, context, test);
await logIntoShopkeep(page, context);
await page.goto(SHOPKEEP_ROUTES.DISCOVER);

expect(page.url().includes(SHOPKEEP_ROUTES.DISCOVER)).toBeTruthy();

await page.waitForLoadState('networkidle');
});

// TODO: add tests once this page is ready to be launched
Expand Down
5 changes: 2 additions & 3 deletions tests/shopkeep/inventory-management.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from '@playwright/test';
import { logInSuccessfully } from '../../helpers/login';
import { logIntoShopkeep } from '../../helpers/login';

/**
* This file contains tests that confirm we can add and modify Shopify products
Expand All @@ -10,8 +10,7 @@ test.describe('Shopkeep Inventory Management', () => {
* We need to be logged in for each test, so we should log in before this test suite runs.
*/
test.beforeEach(async ({ context, page }) => {
await logInSuccessfully(page, context, test);

await logIntoShopkeep(page, context);
await page.waitForLoadState('networkidle');
});

Expand Down
5 changes: 3 additions & 2 deletions tests/shopkeep/navigation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from '@playwright/test';
import { logInSuccessfully, logout } from '../../helpers/login';
import { logIntoShopkeep, logout } from '../../helpers/login';
import { SHOPKEEP_ROUTES } from '../../helpers/routes';

/**
Expand All @@ -12,7 +12,8 @@ test.describe('Shopkeep Navigation', () => {
* We need to be logged in for each test, so we should log in before this test suite runs.
*/
test.beforeEach(async ({ context, page }) => {
await logInSuccessfully(page, context, test);
await logIntoShopkeep(page, context);
await page.waitForLoadState('networkidle');
});

test.afterEach(async ({ context }) => {
Expand Down
4 changes: 2 additions & 2 deletions tests/shopkeep/requests.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from '@playwright/test';
import { logInSuccessfully, logout } from '../../helpers/login';
import { logIntoShopkeep, logout } from '../../helpers/login';
import { SHOPKEEP_ROUTES } from '../../helpers/routes';

/**
Expand All @@ -19,7 +19,7 @@ test.describe('Shopkeep Requests', () => {
* and then navigate to the Discover page
*/
test.beforeEach(async ({ context, page }) => {
await logInSuccessfully(page, context, test);
await logIntoShopkeep(page, context);
await page.goto(SHOPKEEP_ROUTES.REQUESTS);

expect(page.url().includes(SHOPKEEP_ROUTES.REQUESTS)).toBeTruthy();
Expand Down
5 changes: 3 additions & 2 deletions tests/supplier/navigation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from '@playwright/test';
import { logInSuccessfully, logout } from '../../helpers/login';
import { logIntoSupplier, logout } from '../../helpers/login';
import { SUPPLIER_ROUTES } from '../../helpers/routes';

/**
Expand All @@ -12,11 +12,12 @@ test.describe('Supplier Navigation', () => {
* We need to be logged in for each test, so we should log in before this test suite runs.
*/
test.beforeEach(async ({ page, context }) => {
await logInSuccessfully(page, context, test);
await logIntoSupplier(page, context);

// Navigate to the overview page of the Supplier app
await page.goto(SUPPLIER_ROUTES.OVERVIEW);
await page.waitForSelector('text=Welcome to Canal');
await page.waitForLoadState('networkidle');
});

test.afterEach(async ({ context }) => {
Expand Down

0 comments on commit 8930a2c

Please sign in to comment.