Skip to content

MyUstaadG

Learn to Serve

  • Computer Science
    • Software
    • Computer Tips
    • Coding
      • Conditional Structures Exercises
      • Loops Exercises
      • Arrays Exercises
      • Python Exercises
    • Microsoft Office
      • MS Excel
      • MS Word
  • Videos
    • Funny Videos
    • Health and Fitness Videos
    • PUBG Videos
    • Technology Videos
  • News
  • Funny
  • Memes
  • Playwright Tutorials
  • Tutorials
  • Toggle search form
playwright Tutorial No. 06

Playwright Tutorial for Beginners 6 – Demo 1

Posted on February 20, 2022February 20, 2022 By myustaadg.com No Comments on Playwright Tutorial for Beginners 6 – Demo 1
Spread the love

Introduction:

In previous tutorials, we learnt some basic concepts of testing with Playwright.

So in this tutorial, we will put some of the concepts into practice by writing a basic test script for an online store.

Swaglabs

Setting up our tests:

  • installing Playwright via npm install --save-dev @playwright/test.
  • next, setup the configuration file in the root of the project directory:
  //playwright.config.js
  const { devices } = require('@playwright/dev');

  /** @type {import('@playwright/test').PlaywrightTestConfig} */
  const config = {
      projects: [
          name: 'chrome',
          use: { ...devices['Desktop Chrome'] }
      ],
      use: {
          launchOptions: {
              slowMo: 100,
              headless: false
          }
      }
  };
  module.exports = config;

This config file set Playwright to run the test on a desktop chrome browser.

Writing our test scripts

Using our demo, our test should perform the following, step-by-step:

  • fill out user details and login
  • select the Sauce Labs Bike Light product and add it to cart
  • view the cart and assert that the total price of the cart is $9.99

We will create a new file, demo-store.spec.js, and write the code below.

//demo-store.spec.js
const { test } = require('@playwright/test');

test('demo store cart store', async ({ page }) => {
    await page.goto('https://www.saucedemo.com/');
});

The code above opens the specified URL.

Next, we will fill up the Username and Password with the login details given for a standard user using the page.type() method:

const standardUser = {
    username: 'standard_user',
  password: 'secret_sauce'
};

await page.type('[placeholder=Username]', standardUser.username);
await page.type('[placeholder=Password]', standardUser.password);

await page.click('text=LOGIN');

We will click on the selected product name and add it to cart:

await page.click('text=Sauce Labs Bike Light');
await page.click('text=ADD TO CART');

Then assert the total price in the cart:

const totalPrice = page.locator('.inventory_item_price');
expect(await totalPrice.textContent()).toBe('$9.99');

Combining the whole scripts would look like this:

// demo-store.spec.js
const { test, expect } = require('@playwright/test');

test('demo store cart store', async ({ page }) => {
  await page.goto('https://www.saucedemo.com/');

  // login credentials
  const standardUser = {
    username: 'standard_user',
    password: 'secret_sauce'
  };

  await page.type('[placeholder=Username]', standardUser.username);
  await page.type('[placeholder=Password]', standardUser.password);
  await page.click('text=LOGIN');

  await page.click('text=Sauce Labs Bike Light');
  await page.click('text=ADD TO CART');

  await page.click('.shopping_cart_link');

  const totalPrice = page.locator('.inventory_item_price');
  expect(await totalPrice.textContent()).toBe('$9.99');
});

Running the test script

npx run playwright test demo-store.spec.js

Hope You will learn Something. Happy Coding.

Visit my YouTube Channel as Well https://www.youtube.com/watch?v=JuIHQ9cLSEw&list=PLbIhkHxfUIItTdcyCb34uRIrPJbXBndIl&index=4
Playwright Tutorials Tags:Everything about the Playwright framework, Getting started | Playwright, Getting started with Playwright, How do I run a playwright test in applitools?, How do I use playwright with Node JS?, how to install playwright, playwright automation tool, playwright documentation, playwright example, playwright python tutorial, playwright test, playwright test framework, playwright tutorial, Playwright tutorial example

Post navigation

Previous Post: Playwright Tutorial for Beginners 5 – Performing Actions
Next Post: Playwright Tutorial For Beginners 7 – Videos

More Related Articles

playwright Tutorial No. 02 Playwright Tutorial for Beginners 2 – Code Generator Playwright Tutorials
playwright Tutorial No. 03 Playwright Tutorial for Beginners 3 – Trace Viewer Playwright Tutorials
playwright Tutorial No. 08 Playwright Tutorial for Beginners 8 – Screenshots Playwright Tutorials
playwright Tutorial No. 04 Playwright Tutorial for Beginners 4 – Selectors and Locators Playwright Tutorials
playwright Tutorial No. 01 Playwright Tutorial for Beginners 1 – Getting Started Computer Tips
playwright Tutorial No. 05 Playwright Tutorial for Beginners 5 – Performing Actions Playwright Tutorials

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2022 MyUstaadG.

Powered by PressBook Blog WordPress theme