Skip to content

Commit 4ffe337

Browse files
committed
Add sample tests to login without using storage states
1 parent ca1b4cd commit 4ffe337

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

tests/specs/login-using-storage-states.spec.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ test.describe('Saucedemo tests for successful, unsuccessful logins and add produ
1818
await productsPage.verifyProductsPageIsDisplayed();
1919
});
2020

21+
// To run tests with different user storageState in the same spec file then isolate them in to test.describe blocks
22+
test.describe(() => {
23+
test.use({ storageState: getUserAuthPath(visualTestUserCredentials) });
24+
test('Saucedemo test - Add product to cart', async ({ loginPage, productsPage, miniCartPage }) => {
25+
await loginPage.navigateToSauceDemoInventoryPage();
26+
await productsPage.verifyProductsPageIsDisplayed();
27+
await productsPage.addToCartByProductNumber(1);
28+
// verifying mini cart count is updated to 1
29+
await miniCartPage.verifyMiniCartCount('1');
30+
});
31+
});
32+
2133
// To run tests with out storageState in the same spec file then isolate them in to test.describe blocks
2234
test.describe(() => {
2335
test.use({ storageState: { cookies: [], origins: [] } });
@@ -30,16 +42,4 @@ test.describe('Saucedemo tests for successful, unsuccessful logins and add produ
3042
await productsPage.verifyProductsPageIsNotDisplayed();
3143
});
3244
});
33-
34-
// To run tests with different user storageState in the same spec file then isolate them in to test.describe blocks
35-
test.describe(() => {
36-
test.use({ storageState: getUserAuthPath(visualTestUserCredentials) });
37-
test('Saucedemo test - Add product to cart', async ({ loginPage, productsPage, miniCartPage }) => {
38-
await loginPage.navigateToSauceDemoInventoryPage();
39-
await productsPage.verifyProductsPageIsDisplayed();
40-
await productsPage.addToCartByProductNumber(1);
41-
// verifying mini cart count is updated to 1
42-
await miniCartPage.verifyMiniCartCount('1');
43-
});
44-
});
4545
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// @fixturesetup is the alias path set up for importing fixtures from testFixtures file.
2+
// Don't have to import individual page objects because they are initialized in fixtures and passed as test parameters.
3+
import { test } from '@fixturesetup';
4+
import { standardUserCredentials, visualTestUserCredentials } from '@testdata/sauce-demo-test-data';
5+
6+
test.describe.configure({ mode: 'parallel' });
7+
/**
8+
* To run tests with different user storageStates in the same spec file then isolate them in to test.describe blocks
9+
* See https://playwright.dev/docs/next/auth#multiple-signed-in-roles
10+
*/
11+
test.describe('Saucedemo tests for successful, unsuccessful logins and add products to cart @smoke', () => {
12+
// beforEach hook to navigate to home page in each test
13+
test.beforeEach('Navigating to sauce demo page', async ({ loginPage }) => {
14+
await loginPage.navigateToSauceDemoLoginPage();
15+
});
16+
test('Saucedemo tests - Successful login will display Products Page', async ({ loginPage, productsPage }) => {
17+
await loginPage.loginWithValidCredentials(standardUserCredentials);
18+
// verifying products page is displayed on successful login
19+
await productsPage.verifyProductsPageIsDisplayed();
20+
});
21+
22+
test('Saucedemo test - Add product to cart', async ({ loginPage, productsPage, miniCartPage }) => {
23+
await loginPage.loginWithValidCredentials(visualTestUserCredentials);
24+
await productsPage.verifyProductsPageIsDisplayed();
25+
await productsPage.addToCartByProductNumber(1);
26+
// verifying mini cart count is updated to 1
27+
await miniCartPage.verifyMiniCartCount('1');
28+
});
29+
30+
test('Saucedemo test - Products page is not displayed on failed login', async ({ loginPage, productsPage }) => {
31+
await loginPage.loginWithInvalidCredentials();
32+
// verifying Login is still displayed
33+
await loginPage.verifyLoginPageIsDisplayed();
34+
// verifying Products Page is not displayed
35+
await productsPage.verifyProductsPageIsNotDisplayed();
36+
});
37+
});

0 commit comments

Comments
 (0)