Testing Web Applications with Playwright

Rate this content
Bookmark

Testing is hard, testing takes time to learn and to write, and time is money. As developers we want to test. We know we should but we don't have time. So how can we get more developers to do testing? We can create better tools.

Let me introduce you to Playwright - Reliable end-to-end cross browser testing for modern web apps, by Microsoft and fully open source. Playwright's codegen generates tests for you in JavaScript, TypeScript, Dot Net, Java or Python. Now you really have no excuses. It's time to play your tests wright.

FAQ

Playwright is a tool for reliable end-to-end testing of modern web applications that works across all browsers and platforms. It is suitable for developers and testers who want to automate testing in multiple programming languages such as C#, Java, Python, JavaScript, and TypeScript.

To install Playwright in VSCode, look for the 'Playwright Test for VSCode by Microsoft' extension. Use the command panel and write 'Install Playwright'. Follow the prompts to configure browsers and other settings.

Yes, during the installation of Playwright, you can select which browsers to test on, including Chromium, Firefox, and WebKit. You can choose one or all of them, and modify these selections later if needed.

Yes, Playwright supports testing in full isolation where tests run in separate browser contexts, ensuring they do not interfere with each other.

Playwright supports testing in multiple programming languages including C#, Java, Python, JavaScript, and TypeScript, making it a versatile choice for diverse development teams.

Playwright tests run in parallel by default, which speeds up execution. It also has built-in auto-wait features to manage dynamic content without manual timeouts.

Playwright offers powerful tooling including Code Gen, which auto-generates tests from actions, and Trace viewer for detailed debugging. It also integrates with GitHub Actions for CI/CD pipelines and supports live debugging in VSCode.

Yes, Playwright can emulate mobile devices and test mobile-specific functionalities, allowing comprehensive testing across device types.

Locators in Playwright are methods used to find elements on the page with built-in auto-waiting and retry ability. They are strict by default and can be used with various selectors like getByText, getByRole, and more.

Playwright allows you to view test executions and failures through the Trace viewer, which provides detailed snapshots and logs of the test. You can analyze actions, network requests, and other elements to debug issues.

Debbie O'Brien
Debbie O'Brien
20 min
03 Nov, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Testing web applications with Playwright, a reliable end-to-end testing tool. Playwright offers fast execution, powerful tooling, and support for multiple languages. It provides precise selectors, web-first assertions, and code generation for easy testing. Playwright also offers features like live debugging, tracing, and running tests on CI. The future of Playwright aims to make testing easy and fun, with a focus on creating frustration-free web experiences.

1. Introduction to Playwright Testing

Short description:

Testing web applications with Playwright. Playwright testing is reliable end-to-end testing for modern web apps. It works on any browser and any platform. Tests run in full isolation, with fast execution and powerful tooling. You can run your tests in multiple languages, such as C#, Java, Python, JavaScript, or TypeScript. The VSCode extension for Playwright provides seamless integration and allows you to choose browsers, configure options, and use GitHub Actions for automated testing.

♪♪♪ Testing web applications with Playwright. It's time to play your tests right. Hi, everyone. My name is Debbie O'Brien. I'm a Senior Program Manager at Microsoft, and I'm basically working on advocating for Playwright. I'm an international speaker, teacher, YouTuber, open-source contributor, and you can follow me on Twitter, devs underscore o'brien, to see me running and cycling and doing all sorts of sports across the island of Majorca in Spain.

So we're here to talk about testing, so let's get straight to it. Playwright testing, reliable end-to-end testing for modern web apps, works on any browser. Doesn't matter what platform you're on. Any browser, any platform. One API. Tests run in full isolation. That means one test is here with a browser context, another test is here. They don't leak things to each other, right? So everything here is in full isolation from the other test here. Fast execution. We've got powerful tooling, which I'm gonna talk about a lot in this talk, and multi-language, which means you can run your tests in C Sharp, in Java, in Python, or in JavaScript and TypeScript. I'm gonna show you TypeScript today, but you can choose any of the other languages that you want.

I'm gonna show you the VSCode extension because I actually love the integration with VSCode. It's very, very cool, but don't worry, you can still use Visual Studio. You can still just use a terminal or whatever you prefer. But let me show you the extension. So look for the Playwright test for VSCode by Microsoft extension, and then basically in your command panel, just write Install Playwright, and click on that. And what you get is, now you get to select the browsers, and you can choose Chromium, Firefox, or WebKit. You can choose all of them or just one of them. And later you can configure it, so don't worry too much if you're not sure which you want. There's another option there, use JavaScript. If you don't wanna use TypeScript, it's TypeScript by default, but you can use JavaScript if you prefer. And then also the GitHub Actions. By clicking that box, we're gonna get a GitHub Actions, and it's gonna basically work out of the box without you having to configure anything. You'll be able to run your tests on GitHub, on CI in every push and pull request.

2. Running Tests and Live Debugging with Playwright

Short description:

Once you've installed Playwright, you'll have access to the GitHub folder with actions, a test folder with example files, and the Playwright config. Running tests is easy with the VS Code extension, which provides live debugging and error messaging. By setting breakpoints, you can analyze and debug your tests to resolve any issues. Playwright also offers parallel test execution and the option to show or hide the browser window during testing.

So once you've installed, you're gonna get this. You'll get that GitHub folder there with the actions inside it, and you get a test folder, when you example.spec.ts file. That will have a very small test in there, which we're gonna go through today, and then there's a test example file, which has a to-do demo app, and you can have a look at that in more detail.

And then the PlayWrite config, in case you wanted to later add or remove some of those browsers, or put testing for mobile Safari, or set up a dev server to run your application on localhost 3000 before you run your tests, et cetera. You can do all that in the config, but you very rarely need to touch the config, because everything just works.

So when you wanna run tests, you can run all tests. You can run a set of tests, a single test, and tests run in parallel. Without you having to do anything, automatically runs in parallel, which makes them super fast. So I'm gonna run the tests using the VS Code extension. Again, you could use the terminal and just put npx.playwritetest, but here on line three, you'll see this lovely green triangle. So I'm gonna click on that, and then basically, it's gonna go down through each line, and it's gonna run the test. Now, you can see it opened up a browser window there for me, and I was able to see everything that's going on. I'll just play that again. You can see super fast, right? That's because I have this show browser ticked, and you can uncheck that if you want to. If you do not wanna see the browser, if you wanna see the browser, make sure that that is checked.

So live debugging, if you have any errors in your test, I mean, you never have errors, right? But if you do have any bugs or anything, you have live debugging and error messaging in VS Code itself. So again, we have our test here in VS Code, and I'm just gonna like break this here, right? So getByText, I'm gonna run that test, it's gonna break, and it says, error, strict mode violation. So playwright's saying, Hey, look, there's one, two, three elements there, and you can have the whole log. Now, if you don't wanna read the log or don't understand the log, you could basically just set a break point, and you could run that test again, so you can kind of see it. So running it in debug mode, and we can now go through that and see what's happening. So at this point in time, playwright's saying, Hey, look, getByText, resolved to three elements. Here's one of them. Here's the other one. And down the bottom, we've got another one that has the word star inside that getting started text. So playwright doesn't know which one you want it to choose. So it's saying, Hey, this is breaking the strict violation. So you can play around and live debug this and say, Okay, what if I put get star? Get star only resolves to one, so that would be okay. That would work. Again, we can kind of like go back into here and play around and put something like, imagine we put playwright, right? We put playwright. There's like way too many, okay? Hey, there's like 12 of these.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Full-Circle Testing With Cypress
TestJS Summit 2022TestJS Summit 2022
27 min
Full-Circle Testing With Cypress
Top Content
Cypress has taken the world by storm by brining an easy to use tool for end to end testing. It’s capabilities have proven to be be useful for creating stable tests for frontend applications. But end to end testing is just a small part of testing efforts. What about your API? What about your components? Well, in my talk I would like to show you how we can start with end-to-end tests, go deeper with component testing and then move up to testing our API, circ
Everyone Can Easily Write Tests
TestJS Summit 2023TestJS Summit 2023
21 min
Everyone Can Easily Write Tests
Let’s take a look at how Playwright can help you get your end to end tests written with tools like Codegen that generate tests on user interaction. Let’s explore UI mode for a better developer experience and then go over some tips to make sure you don’t have flakey tests. Then let’s talk about how to get your tests up and running on CI, debugging on CI and scaling using shards.
Tiny Tests, Large Results
TestJS Summit 2022TestJS Summit 2022
21 min
Tiny Tests, Large Results
Yes, Big things do come in small packages. For example, isn’t a unit test’s speed, feedback, and reliability fantastic? Did you know we can also have fast, focused, and reliable feedback from our functional e2e tests? Atomic e2e tests are those that are targeted and focused. They’re tiny in size but large in their impact. This tutorial will teach you how to create atomic e2e tests with several code examples. First, we will use Cypress.io to authenticate by setting a cookie. Instead of using a UI. Second, we will use Cypress.io to set a JSON Web Token for authentication. Join me, and let’s write tiny tests for large results.
We May Not Need Component Testing
Vue.js Live 2024Vue.js Live 2024
26 min
We May Not Need Component Testing
Testings are mandatory and unit tests are the foundation for building a good testing system for our project. But for front end projects which involve components, how many unit tests are considered efficient and not overkill? Should we use additional libraries like Testing Library or Vue Test Utils with Vitest to test a component, when we can perform the same with just Playwright? Whether a component test using an E2E framework like Playwright is really a kill for? Let's find out in my talk.
Testing Mail Service With Playwright
TestJS Summit 2022TestJS Summit 2022
17 min
Testing Mail Service With Playwright
Top Content
We send emails to our users - account verification and newsletters. We allow the user to contact us by sending an email via inbuild form. Do we? Does the user receive an account verification email or exactly what notification they signed up for? We can cover this functionality as part of E2E tests: get an email and open it to check what is in it. We will need Playwright and a fake SMTP server to capture emails sent by the app.
E2E Tests for Web3 Applications
TestJS Summit 2022TestJS Summit 2022
21 min
E2E Tests for Web3 Applications
Top Content
We will go through a brief explanation of what is Web3 and the architecture of a web3 application. Then we will talk about how to end-to-end test, its challenges, some test tools that are available, and a demo using cypress and metamask.Agenda: What is Web3; The Architecture of a Web3 Application; Web3 E2E Tests Introduction; Web3 E2E Tests Challenges; E2E Test Tools; Demo.

Workshops on related topic

Detox 101: How to write stable end-to-end tests for your React Native application
React Summit 2022React Summit 2022
117 min
Detox 101: How to write stable end-to-end tests for your React Native application
Top Content
WorkshopFree
Yevheniia Hlovatska
Yevheniia Hlovatska
Compared to unit testing, end-to-end testing aims to interact with your application just like a real user. And as we all know it can be pretty challenging. Especially when we talk about Mobile applications.
Tests rely on many conditions and are considered to be slow and flaky. On the other hand - end-to-end tests can give the greatest confidence that your app is working. And if done right - can become an amazing tool for boosting developer velocity.
Detox is a gray-box end-to-end testing framework for mobile apps. Developed by Wix to solve the problem of slowness and flakiness and used by React Native itself as its E2E testing tool.
Join me on this workshop to learn how to make your mobile end-to-end tests with Detox rock.
Prerequisites- iOS/Android: MacOS Catalina or newer- Android only: Linux- Install before the workshop
Testing Web Applications Using Cypress
TestJS Summit - January, 2021TestJS Summit - January, 2021
173 min
Testing Web Applications Using Cypress
WorkshopFree
Gleb Bahmutov
Gleb Bahmutov
This workshop will teach you the basics of writing useful end-to-end tests using Cypress Test Runner.
We will cover writing tests, covering every application feature, structuring tests, intercepting network requests, and setting up the backend data.
Anyone who knows JavaScript programming language and has NPM installed would be able to follow along.
Building out a meaningful test suite that's not all E2E
TestJS Summit 2023TestJS Summit 2023
89 min
Building out a meaningful test suite that's not all E2E
Workshop
David Burns
David Burns
We're all taught to follow the Testing Pyramid but the reality is that we build out the Testing Christmas Tree. In this workshop, David will talk you through how to break down projects and put the tests where they need to be. By the end of the workshop you will be able to update your projects so that anyone and everyone can start contributing and truly living up to "Quality is everyone job".
He will walk you through:- Component Testing- API Testing- Visual Regression Testing- A11Y testing
He will also talk you through how to get these all setup in your CI/CD pipeline so that you can get shorter and faster feedback loops.
Live e2e test debugging for a distributed serverless application
TestJS Summit 2021TestJS Summit 2021
146 min
Live e2e test debugging for a distributed serverless application
WorkshopFree
Serkan Ozal
Oguzhan Ozdemir
2 authors
In this workshop, we will be building a testing environment for a pre-built application, then we will write and automate end-to-end tests for our serverless application. And in the final step, we will demonstrate how easy it is to understand the root cause of an erroneous test using distributed testing and how to debug it in our CI/CD pipeline with Thundra Foresight.

Table of contents:
- How to set up and test your cloud infrastructure
- How to write and automate end-to-end tests for your serverless workloads
- How to debug, trace, and troubleshot test failures with Thundra Foresight in your CI/CD pipelines
Uniform Browser Automation Infrastructure
TestJS Summit - January, 2021TestJS Summit - January, 2021
127 min
Uniform Browser Automation Infrastructure
Workshop
Ivan Krutov
Ivan Krutov
In this workshop, I will show you how to quickly deploy and use browser automation infrastructure with Moon solution. We will start deploying everything on your workstation and will soon be able to run Selenium, Playwright and Puppeteer tests in parallel in the same cluster. Then I will demonstrate how to easily deliver the same experience for your team using a remote cluster in the cloud platform.