Skip to content

Commit

Permalink
Add sample tests to login without using storage states
Browse files Browse the repository at this point in the history
  • Loading branch information
vasu31dev committed Mar 26, 2024
1 parent ca1b4cd commit 4ffe337
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
24 changes: 12 additions & 12 deletions tests/specs/login-using-storage-states.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ test.describe('Saucedemo tests for successful, unsuccessful logins and add produ
await productsPage.verifyProductsPageIsDisplayed();
});

// To run tests with different user storageState in the same spec file then isolate them in to test.describe blocks
test.describe(() => {
test.use({ storageState: getUserAuthPath(visualTestUserCredentials) });
test('Saucedemo test - Add product to cart', async ({ loginPage, productsPage, miniCartPage }) => {
await loginPage.navigateToSauceDemoInventoryPage();
await productsPage.verifyProductsPageIsDisplayed();
await productsPage.addToCartByProductNumber(1);
// verifying mini cart count is updated to 1
await miniCartPage.verifyMiniCartCount('1');
});
});

// To run tests with out storageState in the same spec file then isolate them in to test.describe blocks
test.describe(() => {
test.use({ storageState: { cookies: [], origins: [] } });
Expand All @@ -30,16 +42,4 @@ test.describe('Saucedemo tests for successful, unsuccessful logins and add produ
await productsPage.verifyProductsPageIsNotDisplayed();
});
});

// To run tests with different user storageState in the same spec file then isolate them in to test.describe blocks
test.describe(() => {
test.use({ storageState: getUserAuthPath(visualTestUserCredentials) });
test('Saucedemo test - Add product to cart', async ({ loginPage, productsPage, miniCartPage }) => {
await loginPage.navigateToSauceDemoInventoryPage();
await productsPage.verifyProductsPageIsDisplayed();
await productsPage.addToCartByProductNumber(1);
// verifying mini cart count is updated to 1
await miniCartPage.verifyMiniCartCount('1');
});
});
});
37 changes: 37 additions & 0 deletions tests/specs/login-without-storage-states.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @fixturesetup is the alias path set up for importing fixtures from testFixtures file.
// Don't have to import individual page objects because they are initialized in fixtures and passed as test parameters.
import { test } from '@fixturesetup';
import { standardUserCredentials, visualTestUserCredentials } from '@testdata/sauce-demo-test-data';

test.describe.configure({ mode: 'parallel' });
/**
* To run tests with different user storageStates in the same spec file then isolate them in to test.describe blocks
* See https://playwright.dev/docs/next/auth#multiple-signed-in-roles
*/
test.describe('Saucedemo tests for successful, unsuccessful logins and add products to cart @smoke', () => {
// beforEach hook to navigate to home page in each test
test.beforeEach('Navigating to sauce demo page', async ({ loginPage }) => {
await loginPage.navigateToSauceDemoLoginPage();
});
test('Saucedemo tests - Successful login will display Products Page', async ({ loginPage, productsPage }) => {
await loginPage.loginWithValidCredentials(standardUserCredentials);
// verifying products page is displayed on successful login
await productsPage.verifyProductsPageIsDisplayed();
});

test('Saucedemo test - Add product to cart', async ({ loginPage, productsPage, miniCartPage }) => {
await loginPage.loginWithValidCredentials(visualTestUserCredentials);
await productsPage.verifyProductsPageIsDisplayed();
await productsPage.addToCartByProductNumber(1);
// verifying mini cart count is updated to 1
await miniCartPage.verifyMiniCartCount('1');
});

test('Saucedemo test - Products page is not displayed on failed login', async ({ loginPage, productsPage }) => {
await loginPage.loginWithInvalidCredentials();
// verifying Login is still displayed
await loginPage.verifyLoginPageIsDisplayed();
// verifying Products Page is not displayed
await productsPage.verifyProductsPageIsNotDisplayed();
});
});

0 comments on commit 4ffe337

Please sign in to comment.