15 minutes from installation to automating using Playwright

15 minutes from installation to automating using Playwright

Test automation, as it stands today, often feels like an intimidating endeavor—an imposing beast lurking in the shadows of your development pipeline, waiting to be tamed. Yet, in Microsoft’s Playwright, we find something refreshingly accessible, robust, and, frankly, a bit exhilarating once the wheels start turning. Want to go from zero to a fully automated testing machine before your coffee turns cold? Let’s chart the journey.

Prelude: Prepping Your Arsenal


Install Node.js: Swing by the official Node.js site and scoop up the latest LTS version. The installation wizard is pretty much “Next, Next, Done,” so don’t fret. Once Node.js graces your machine, you’ve got a rock-solid foundation for the ecosystem you’re about to cultivate.


Grab VS Code: Next, head to Visual Studio Code’s homepage—no secret handshake required. It’s a famously approachable IDE, a place where seasoned devs and first-timers mingle without judgment. Download, launch, and let that sleek interface guide your next moves. Sure, there are countless editors out there, but VS Code feels like a versatile toolkit wrapped in a cozy blanket.

Please note that I will walk you through the installation process on a Microsoft Windows OS.


Planting the Seeds: Setting Up Playwright

There are two main ways to install Playwright:

  1. Using NPM (Node Package Manager): This method allows you to install Playwright through the command-line interface. It's a traditional approach that provides you with greater control over the installation process.
  2. Utilizing the Playwright Visual Studio Code Extension: This convenient approach lets you install and manage Playwright directly within VS Code. It offers a user-friendly interface, making it easy to run Playwright commands without leaving your development environment.

I highly recommend installing the Playwright Visual Studio Code extension. It offers the advantage of a graphical user interface, which simplifies the execution of many common Playwright tasks.


Follow the steps below to install the VS Code extension:

Step 1: Using the Windows File Explorer. Create a new folder

Step 2: Open VS Code

Step 3: Navigate to File > Open Folder

Step 4: Navigate to the playwright folder that you created in Step 1 and click Select Folder.

Step 5: Click “Yes", I trust the authors button if it appears.

Step 6: Click on Extensions section.?

Step 7: Search for Playwright Test for VSCode by Microsoft.?

Step 8: Click on Install.


We will set up a Playwright project in the folder we created in the above section.

Step 1.?Click on the Explorer section.?

Step 2: Go to View > Command Palette.?

Step 3: Type playwright in the search.?

Step 4:??Click on the command Test: Install Playwright. Choose your options and click OK.


Playwright Project Structure

Once you have configured Playwright in your folder, the structure of your folder should resemble the one shown below. Note: replace Playwright-Sample with the name of the folder that you recently created.



You should save any tests you create under the tests folder. See highlight above. The naming convention is

{test name}.spec.ts  (ts=typescript.)        


Capturing the Drama: Recording a Test

If imagining test writing as a manual hunt for selectors leaves you queasy, fear not. Playwright’s “codegen” feature is like having a personal stenographer who records your every on-screen interaction. Try this:


Step 1: Click on the tests folder and click on the new file icon to the right.


Step 2: Add the new file as shown below.


Step 3: Using the terminal, type the command below to launch recording a test.

npx playwright test codegen        


A browser and Playwright Inspector springs forth. Click around—tap links, fill out forms, and press buttons with abandon. When you’re done, click the record button to stop the recording, and copy your code to the new file you added. Close the browser. Behold! A test script materializes as if conjured by digital sorcery, capturing your journey in code. Instead of wrestling with CSS selectors and timing idiosyncrasies, you now refine a script born from your natural actions. It’s like conducting an orchestra by just humming a tune—Playwright does the transcription, leaving you to enhance the melody afterward.




Save your test by clicking on File > Save.

Unleashing the Tests

The script is ready. Run it:


To run the test, input the following commands into the terminal window.


npx playwright test        



Playwright will execute your test across Chromium, Webkit, and Firefox browsers. You will not see your test run because, by default, Playwright runs your tests headless (behind the scenes).

Watch your terminal come alive; each test performance on a miniature stage, passing or failing with elegant transparency. Before long, you’ll dabble in retries for flaky tests, tweak timeouts, and maybe even integrate test fixtures. Each run reveals another facet of the Playwright’s personality—predictable, calm, and quietly powerful.

For test results, type the command in the terminal.

npx playwright show-report        


Closing the Loop: A Strong Finish

We started with a blank slate and ended up orchestrating a full-blown test automation fiesta. You learned how to set up Node.js and VS Code, spin up Playwright, record a test with Codegen, and run those tests like a fearless conductor. This journey, from timid first steps to confident automation, highlights the flexibility and approachability of Playwright.


Don’t let your excitement fade—take action now. Learn more about Playwright by going to the Playwright documentation at https://playwright.dev/docs/intro. Playwright is a robust test automation tool. This article only provides a brief overview. Happy testing!



This article was thoughtfully crafted with the collaboration of AI assistance to enhance clarity and precision.

Olga Steele

Mentorship Community Coordinator

2 个月

Hi Alden Mallare, I saw that you liked Ganesh Ariyur's post on mentoring on Upnotch. I would love to have you join Upnotch as a mentor, as a mentee or both! Upnotch is a free mentorship platform for professionals. Please let me know if you’re interested. I’ll send you a connection request. Thank you ??

回复
Alden Mallare

Senior Transformational Quality Assurance Leader Specialized in Global & Strategic Leadership

2 个月

Have you used Playwright? What other test automation tools have you used or are using?

要查看或添加评论,请登录

Alden Mallare的更多文章

社区洞察

其他会员也浏览了