Latest / Talk Python To Me / #368: End-to-End Web Testing with Playwright
Transcript
- 0:00How do you test whether your websites are working well?
- 0:02Unit tests are great, but for web apps, the number of pieces that have to click together just so are many.
- 0:09You have databases, server code, such as your Flask app, server templates like Jinja, CSS, JavaScript, and even deployment topologies.
- 0:18Think Nginx and uvicorn.
- 0:20Unit tests won't cover all of that integration, but Playwright does.
- 0:24Playwright is a modern Pythonic take on testing web apps using code driving a browser core engine to interact with web apps the way real users and API clients do.
- 0:34I think you'll find a lot to like here.
- 0:36And we have Pandy Knight from Automation Panda here to break it down for us.
- 0:41This is Talk Python To Me, episode 369, recorded May 12, 2022.
- 0:47Welcome to Talk Python To Me, a weekly podcast on Python.
- 1:03This is your host, Michael Kennedy.
- 1:05Follow me on Twitter where I'm @mkennedy and keep up with the show and listen to past episodes at talkpython.fm.
- 1:11And follow the show on Twitter via at Talk Python.
- 1:14We've started streaming most of our episodes live on YouTube.
- 1:18Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified about upcoming shows and be part of that episode.
- 1:25This episode is sponsored by Microsoft for Startups Founders Hub.
- 1:30Check them out at talkpython.fm/founders hub to get early support for your startup.
- 1:35And it's brought to you by Compiler from Red Hat.
- 1:39Listen to an episode of their podcast as they demystify the tech industry over at talkpython.fm/compiler.
- 1:47Transcripts for this and all of our episodes are brought to you by Assembly AI.
- 1:51Do you need a great automatic speech to text API?
- 1:54Get human level accuracy in just a few lines of code.
- 1:56Visit talkpython.fm/assemblyai.
- 2:00Hello, Andy.
- 2:01Hey, what's going on?
- 2:02What's up?
- 2:02Man, it's great to catch up with you.
- 2:04Oh, thank you.
- 2:04You as well.
- 2:05It's been a while.
- 2:06Actually, the last time that we got to meet up, we were chilling at the Pi Bay, right?
- 2:11I believe so.
- 2:12Pretty far from both of our places.
- 2:13But what a cool place to meet.
- 2:14Outside at a food cart conference in San Francisco.
- 2:18That was one of the best days of 2021.
- 2:20It was incredible.
- 2:21It really was.
- 2:22And I got to tell you, I think more conferences should follow this style.
- 2:27I know if you got a conference in Minnesota in the winter, you probably can't have it outside.
- 2:31But boy, what a neat way to have a conference in the age of COVID where people felt comfortable.
- 2:37And honestly, forget COVID.
- 2:39I would have loved to go to this regardless, but it just works even better because of it.
- 2:42Oh, yeah.
- 2:43I'm looking forward to going back this year in September.
- 2:45Oh, yeah.
- 2:46It's going to be epic.
- 2:46Okay.
- 2:47Anyway, that was really great to meet you there.
- 2:50And now we're here to talk about testing.
- 2:52It's really, you know, what a surprise that you're on the show to talk testing in Python.
- 2:58Who would have thought of all people?
- 3:00Yeah.
- 3:01And people may know you from your Automation Panda work and blog and stuff like that, right?
- 3:07So let's get started with your story.
- 3:08How do you get into programming in Python?
- 3:10And maybe just kick it off by talking about Automation Panda.
- 3:12It's maybe a good place to set the stage.
- 3:14Sure.
- 3:14So I'll start with how Python, then I'll talk about why Automation Panda.
- 3:18So I first started programming Python in high school.
- 3:20This was around 2005.
- 3:24I think Python 2.3 was the new version.
- 3:28I was taking a survey of programming languages course.
- 3:32We did a whole bunch of things.
- 3:33And the instructor really liked Python.
- 3:34So I picked it up a little bit.
- 3:36Nice.
- 3:36Didn't do much other than toy around.
- 3:37Then I didn't do anything with Python until about 2015, when I joined a new company in my
- 3:43area called Max Point, and their three languages were Java, C#, and Python.
- 3:47And so I picked up Python and I really, really, really loved it again.
- 3:52And that's when I went heads down into it.
- 3:54I've spoken my first Python conference.
- 3:56It was PyData Carolinas 2016, right in my backyard in Durham.
- 4:00IBM was hosting it.
- 4:01Yeah.
- 4:01Then I attended PyCon 2018, and the rest was history.
- 4:04I think a lot of people out in the audience listening, many of them have never been to
- 4:09either a regional or near international conference, PyCon.
- 4:14And so many people I talked to are like, I can't, I didn't think I was really good enough
- 4:19to go or experienced enough to go or whatever.
- 4:21And then they say, but I went and it was amazing.
- 4:24And now I feel so much more part of the community, right?
- 4:26Like I just want to encourage people to, you know, like it sounds like you went there and
- 4:29you're like, oh my gosh, I forgot how cool this place was, this ecosystem was.
- 4:33And like, I'm in.
- 4:34Absolutely.
- 4:34I mean, that's one of the best ways to engage the Python community.
- 4:37Find your local meetup, find your regional conference or attend the biggins.
- 4:40Yeah.
- 4:41And unfortunately, there are people who are far away from any of those, but there's really
- 4:46good online meetups.
- 4:48Like I was just on the PyIndie one and they've, they're doing a hybrid now with the Six Feet
- 4:54Up folks.
- 4:54And it's like, they've got their, their real in-person meetup, but then you can participate
- 4:59and you're up on the screen with the group and whatnot.
- 5:02So there's still ways to do it.
- 5:03Even if you feel like, well, I live in small town outside of the small town that has no meetup,
- 5:09right?
- 5:09I'm the one Python person that I possibly know.
- 5:11It's still an option.
- 5:12Absolutely.
- 5:13Okay.
- 5:13So you dive in, get into it.
- 5:15And you've basically been, been working in it on and off in different aspects for quite
- 5:20a while now.
- 5:20Indeed.
- 5:21Indeed.
- 5:21I consider Python my favorite programming language.
- 5:23Funny fact, until my current job from about 2016 through 2021, my main programming language
- 5:31at work was actually C#.
- 5:33And so people would ask me, so what do you do with Python at work?
- 5:35I say nothing.
- 5:36And they think I'm joking.
- 5:37I'm like, I'm more of a polyglot.
- 5:40One can dream, but no, it is not true.
- 5:41I don't know.
- 5:42Yeah.
- 5:42I mean, I do Java, JavaScript, Python, C#, you name it.
- 5:46Way long ago, I did test automation in Perl for a couple of years.
- 5:49That was fun, quote unquote.
- 5:51I'm going to write a test regular expression to test the regular expression.
- 5:54Oh my gosh.
- 5:55Oh my gosh.
- 5:56Sorry.
- 5:57I didn't really give you flashbacks.
- 5:58It's all right.
- 5:59That is the one gem that's buried in Perl, I would say.
- 6:01I mean, Perl, every language has strengths and weaknesses.
- 6:04Perl strength is reg X by far.
- 6:06Yeah, sure.
- 6:07Yeah.
- 6:07Anytime I hear people talking about how much they use Perl or love Perl, it's got a really
- 6:12strong text understanding element to what they were doing.
- 6:15Mm-hmm.
- 6:16Mm-hmm.
- 6:17Nice.
- 6:17Well, you spoke about your jobs and doing the C# thing.
- 6:20And I don't think that that's necessarily that big.
- 6:23I don't think it's a negative.
- 6:24You know, there's tons of people who they go to work and work in one language and then
- 6:27they maybe come back and they love Python or vice versa.
- 6:30Maybe they do Python and they love JavaScript.
- 6:33I don't know.
- 6:34We could get them help.
- 6:34Just kidding.
- 6:36But I know that for a lot of people, the dream is like, how do I do Python full time?
- 6:40For now, it's a hobby.
- 6:42But how do I like, I'm writing a blog, I'm doing some projects, but I want to do that as
- 6:46my job.
- 6:46Right.
- 6:46So yeah, you kind of made that transition again, in a sense.
- 6:50Yep.
- 6:50Now being a developer advocate, I play with all the tools at the sandbox.
- 6:53Yeah, that's awesome.
- 6:56So tell us about what you're doing now.
- 6:58Sure.
- 6:58So I'm a developer advocate at a company called Apple tools.
- 7:01Apple tools focuses on automated visual testing.
- 7:05So if I got this right, there you go.
- 7:07You got it.
- 7:07Yeah.
- 7:07Some people think I work at Apple.
- 7:09It's like, no, no, no.
- 7:09Apple tools.
- 7:10Apple tools.
- 7:12A-P-P-L-I tools.
- 7:14I'll put it in the show notes.
- 7:15Sure, sure.
- 7:15And so the premise is a lot of traditional functional testing is either your manual tester
- 7:22banging on a keyboard, looking and clicking through.
- 7:24Or if you're doing automation, you're scripting it, right?
- 7:26With a tool like Playbrite or Cypress or Selenium WebDriver, whatever.
- 7:30They're all good tools.
- 7:31But when you're using a browser automation tool, typically you're faking like the clicks
- 7:36and the typing.
- 7:37And then you're scraping the DOM of the page to get things like text or attributes of HTML
- 7:43tags or classes.
- 7:44And you're doing some sort of assertions on those.
- 7:47Right.
- 7:47Sometimes as JavaScript, like a view front end, you just get like handlebars back.
- 7:52Oh, well.
- 7:52Nothing.
- 7:52Yeah.
- 7:53Or you got to do a little bit, something more complicated to scrape it.
- 7:56But still, you're just like comparing text, right?
- 7:59Exactly.
- 7:59Exactly.
- 8:00What about CSS?
- 8:00What about visuals?
- 8:01What about JavaScript events being linked up?
- 8:04Exactly.
- 8:04And so in that sense, the assertions you're doing are really a bare minimal validation of
- 8:10the page.
- 8:10The way I like to describe it is most traditional automated scripts will work if you rip the entire
- 8:16CSS off the page.
- 8:17It would look butt ugly, but your test scripts will go through.
- 8:20So the question is, is that really proving that it works?
- 8:23From a human aspect, no.
- 8:25You would expect to make sure that things like your layout are good, that your buttons are
- 8:29there, that your text is aligned.
- 8:31All the colors are right.
- 8:33And so that's where automated visual testing comes in, right?
- 8:36That you would take snapshots of different views of your web or mobile apps, whether it's
- 8:41a full page, whether it's a section of a page.
- 8:43And then every time you make changes, you run the tests again, and they'll take checkpoint
- 8:47comparisons to point out, here's what has changed.
- 8:50And then you as the tester can decide, is that good or bad?
- 8:53Kind of a visual diff, like an image diff, and then go from there.
- 8:57Yep.
- 8:57It's a little bit stronger than just like a pixel to pixel comparison, right?
- 9:01Because if you had a padding shift by two pixels, all of a sudden everything's blown up, right?
- 9:05Apple tools uses.
- 9:07Yeah, exactly.
- 9:07Like if it has the time of day up there and the time it goes from like nine to 10, it'll
- 9:12shift everything a little bit.
- 9:13Text changes like that too.
- 9:14So what Apple tools does is it uses visual AI to really pick out the things that would be
- 9:19noticeable to a human eye.
- 9:20So if you have a small shift, not a big deal.
- 9:22You can set ignore regions, like for certain text areas, like it's the time of day or those
- 9:26kinds of things.
- 9:27So you can really hone these visual assertions that you're making.
- 9:31Other thing you can do is because they're taking snapshots of the page, it's like a full
- 9:35DOM capture.
- 9:35It's not just a pixelated screenshot.
- 9:37You can take those snapshots and re-render them on different browser configurations.
- 9:41So, you know, for example, I'm using a Mac.
- 9:43I can test on Chrome and Firefox and Safari and Edge now, but I can't test IE.
- 9:50You know, well, you could take that snapshot, send it up to the Apple tools cloud.
- 9:54And then in there, you can test it on IE.
- 9:56There you can test it on mobile platforms.
- 9:57Or if you're a Windows person, like good luck with Safari.
- 10:00Oh, exactly.
- 10:01Right.
- 10:01That's the other one.
- 10:02If you're on Windows, you want to test Safari, but you can't.
- 10:04Boom.
- 10:04Apple tools ultra fast grid is a way to do that.
- 10:06So there's a lot of really cool stuff you can do with visual testing.
- 10:10That sounds fun.
- 10:11Yep.
- 10:11And so what I do at Apple tools, I'm a developer advocate.
- 10:14So I stand between company and community.
- 10:17I help the community understand good testing practices.
- 10:20What is visual testing?
- 10:21Help them get the most out of using Apple tools.
- 10:24And also to be in the community to advocate for them.
- 10:27Here are the problems.
- 10:28Here are the needs.
- 10:29Here's the struggles.
- 10:29How can Apple tools help me?
- 10:30Right.
- 10:31I tell people our tool does this.
- 10:32They say, we don't need that solved.
- 10:34We need this other slightly different thing solved.
- 10:36Yep.
- 10:37Yep.
- 10:37And you're like, all right, I'll talk to the team.
- 10:39That kind of stuff, right?
- 10:40Yeah, exactly.
- 10:40Yeah.
- 10:41I think the DevRel developer evangelist role is super fun.
- 10:44It sounds like a really neat.
- 10:46If you're a little bit extroverted, you like talking to people, you love code.
- 10:50Yeah.
- 10:50I mean, it's great.
- 10:51Yep.
- 10:52So one other thing I do want to mention with the whole Apple tools, DevRel thing, Apple tools run something called test automation university, which is a free platform full of online courses about testing automation and everything on that topic.
- 11:05So we got some of the best instructors in the world who have these courses.
- 11:08I've taught three of the Python courses.
- 11:12You know, I would recommend folks check it out.
- 11:14Test automation university, TAU.
- 11:16It's good stuff.
- 11:17All right.
- 11:18Automation Panda.
- 11:19Oh my goodness.
- 11:20So yes, I also happen to be called the automation Panda.
- 11:23That's just me.
- 11:24You know, there's no like company or anything.
- 11:26It's just my moniker.
- 11:28It's my Twitter handle.
- 11:28It's my blog.
- 11:29Or the reason I started the whole automation Panda thing back in 2016, I got my first position as a senior level engineer.
- 11:37And I thought, wow, this is great.
- 11:38I've achieved my career dream.
- 11:40I'm senior level.
- 11:41People going to listen to me now.
- 11:42What's next?
- 11:43Like, oh, hmm.
- 11:45Up until that point, all the work I'd ever done had been behind company proprietary walls.
- 11:50Even if that work had not necessarily been proprietary or secret or anything like that.
- 11:56You know, I got sick of rewriting the same thing, wiki pages across multiple teams.
- 12:00So I was like, you know what?
- 12:01Maybe I can do something more public facing.
- 12:03Maybe I can write those wiki pages on something like a blog that everyone can just Google for and find.
- 12:08Especially me, because I don't want to have to repeat the same things over and over.
- 12:11And so I was like, okay, I'll start a blog.
- 12:13And I needed a catchy name because you go to, you know, whatever website to register your domain.
- 12:19And I'm like, well, andrewknight.com is not available.
- 12:22And nobody's going to remember Andrew Knight anyway.
- 12:24Let me come up with something catchy.
- 12:26And I just landed on Automation Panda.
- 12:28Automation for test automation.
- 12:30Panda, because pandas are awesome.
- 12:32Yeah.
- 12:32And there we go.
- 12:33And pandas hate crashing web apps and desktop apps.
- 12:37As I think across the different animals throughout the animal kingdom, they are very finicky about a failed app.
- 12:46I suppose.
- 12:46Push or whatever.
- 12:48I'm just kidding.
- 12:49No, that's awesome.
- 12:50And I certainly know you through this.
- 12:52And you've done a lot of general advocacy for testing in the Python space there.
- 12:57Yeah.
- 12:57I advocated for good testing before I was a developer advocate.
- 13:00That is true.
- 13:02Exactly.
- 13:02Well, I mean, I think that that's often how people get into these roles.
- 13:06You think there's probably a lot of people that are thinking, like, I would love to have that role.
- 13:11So I'm going to try to apply so I can get that role.
- 13:13And, you know, you did a bunch of public speaking and presentations.
- 13:17And I'm sure they saw that.
- 13:18Like, this guy is great.
- 13:20We just need to have him on our team.
- 13:21You know?
- 13:22And I got hired doing in-person developer training because I was speaking at user groups and meetups and conferences and blogging.
- 13:31And they're just like, I see you doing this for free.
- 13:34Would you like a job to do it?
- 13:35I'm like, that sounds awesome.
- 13:37Let's do that.
- 13:37Indeed.
- 13:38So, yeah, it's definitely advice people can take sort of as a path forward.
- 13:42Oh, yeah, definitely.
- 13:43If they want to end up where you are, yeah.
- 13:44When you generate content, that becomes your electronic business card.
- 13:49That's how people get to know you.
- 13:50And so if you do the things you like, the things you like will start coming to you.
- 13:54Absolutely.
- 13:54All right.
- 13:55Now, how about we talk a little bit of testing?
- 13:59Let's do it.
- 13:59Yeah.
- 14:00So the kind of testing that we're talking about here is somewhat related, actually, to your current job.
- 14:07Yeah.
- 14:08Apple tools, right?
- 14:09Indeed.
- 14:09I don't know if you're using the same tooling exactly or anything, but it's the similar idea of not just, okay, I've got a stack data structure and I can push a thing onto it and I can pop it off.
- 14:21And if I pop two on and pop it off, it's going to be the second one.
- 14:24Okay.
- 14:25Our app is working, right?
- 14:26It's trying to understand more of an integration black box level of testing, would you say?
- 14:32Correct.
- 14:33Correct.
- 14:33I always say there is a big difference between testing code and testing features.
- 14:38Testing code is what we would think of as white box or unit testing or maybe sometimes subcutaneous testing where you're testing to make sure that the implementation of whatever code you wrote, be it a function or method or something else, is working according to specification.
- 14:53Like, okay, I have a function, an absolute value function.
- 14:55Let me make sure that, you know, I have a table of inputs that meet the expected table of outputs, right?
- 15:00Input, rank it up.
- 15:01Output doesn't match.
- 15:02You know, that would be unit testing.
- 15:04And so it's very good for pinpointing things quickly, right?
- 15:08Unit tests are very fast.
- 15:09It catches problems at their source.
- 15:11But it doesn't test things like, okay, can you actually complete the workflow that this whole big application is designed to do?
- 15:17That's where feature testing comes in.
- 15:19That's where you have to do something black box.
- 15:21Unit testing is good when you are focused in on a part of the app.
- 15:25Like, this part is the part that checks whether or not I can access this page, whether or not the user's logged in.
- 15:31So let me try to request it with a user that's not logged in.
- 15:34Let me try to request it with a user that is.
- 15:36How about now one with whose one's logged in but doesn't have access through some role thing?
- 15:40Right?
- 15:41Like, that is fine.
- 15:42But most applications, as they grow, become like Lego pieces that click together, right?
- 15:49And if you think about web apps or APIs or I don't even want to think about microservices, you know, they become these things like, well, here's the database with its schema and constraints.
- 16:02And here's the data layer.
- 16:04And here's my view method.
- 16:06And then, oh, here, these are the routes that are set up based on this view.
- 16:09And here's the static folder for the JavaScript.
- 16:11And then it renders and just here's the HTML template.
- 16:14Like, it'd be insanity to try to test those on a unit level and then be sure that, well, I tested the view and then I tested the template.
- 16:22And somehow I feel like that's OK because they could get out of sync.
- 16:25Exactly.
- 16:25Exactly.
- 16:26This portion of Talk Python To Me is brought to you by Microsoft for Startups Founders Hub.
- 16:33Starting a business is hard.
- 16:35By some estimates, over 90% of startups will go out of business in just their first year.
- 16:40With that in mind, Microsoft for Startups set out to understand what startups need to be successful and to create a digital platform to help them overcome those challenges.
- 16:49Microsoft for Startups Founders Hub was born.
- 16:52Founders Hub provides all founders at any stage with free resources to solve their startup challenges.
- 16:59The platform provides technology benefits, access to expert guidance and skilled resources, mentorship and networking connections, and much more.
- 17:07Unlike others in the industry, Microsoft for Startups Founders Hub doesn't require startups to be investor backed or third party validated to participate.
- 17:17Founders Hub is truly open to all.
- 17:20So what do you get if you join them?
- 17:21You speed up your development with free access to GitHub and Microsoft Cloud computing resources and the ability to unlock more credits over time.
- 17:29To help your startup innovate, Founders Hub is partnering with innovative companies like OpenAI, a global leader in AI research and development, to provide exclusive benefits and discounts.
- 17:39Through Microsoft for Startups Founders Hub, becoming a founder is no longer about who you know.
- 17:45You'll have access to their mentorship network, giving you a pool of hundreds of mentors across a range of disciplines and areas like idea validation, fundraising, management and coaching, sales and marketing, as well as specific technical stress points.
- 17:58You'll be able to book a one-on-one meeting with the mentors, many of whom are former founders themselves.
- 18:03Make your idea a reality today with the critical support you'll get from Founders Hub.
- 18:09To join the program, just visit talkpython.fm/founders hub, all one word, no links in your show notes.
- 18:14Thank you to Microsoft for supporting the show.
- 18:17Having that full application ready to go and you're testing it as if you were a user is very, very valuable.
- 18:26It can be painful to set up.
- 18:28It can be painful to think about all the scenarios to do, but it is also incredibly valuable in the results you get from it.
- 18:33Yeah.
- 18:34And I made the snarky comment about like a view or react front end as well.
- 18:38Yeah.
- 18:38Yeah.
- 18:38Well, which really requires a little bit more of a test that understands the behavior rather
- 18:44than just creating like a flask test app and requesting the view.
- 18:47Exactly.
- 18:48You're just going to get back where the binding bits of the JavaScript framework go.
- 18:52Exactly.
- 18:53You need something like a web browser, right?
- 18:54Exactly.
- 18:55Yes.
- 18:55Yes.
- 18:56You need to pull up the page, load it in a browser and see what it does.
- 18:59And, you know, you mentioned, I know this is the top Python podcast.
- 19:03And so we love Python.
- 19:04I love Python.
- 19:05But in the web space, it's inevitable, unless you're using HTML or something.
- 19:09Ha ha ha.
- 19:10That Python is going to test JavaScript at some point.
- 19:12Right?
- 19:13Yeah.
- 19:14They're going to crash together definitely in the web space for sure, unless you are purely
- 19:18on the API side.
- 19:19And who knows?
- 19:20Probably somehow still it'll worm its way in there.
- 19:23So traditionally, this kind of testing has been done by pulling up a browser.
- 19:28It could be several levels.
- 19:30It could be, okay, I get my feature implemented and I go click, clickety, clickety, click.
- 19:34Seems like it's working.
- 19:35Let's push it.
- 19:36Like that's, let's call that level zero testing.
- 19:39Actually, that's level like 0.1 because level zero is just make the feature change and push
- 19:43it.
- 19:44And then we'll see whether it crashes, right?
- 19:46No test.
- 19:47No test is level zero.
- 19:48But then just above that is, I kind of sort of clicked around in the area that I think I
- 19:53might have affected to see that it works, right?
- 19:55Maybe take it from there.
- 19:56Like, where do we go from there if we want to be better?
- 19:58Well, I would caveat to say maybe it's not necessarily layers of getting better.
- 20:01There is always a place for manual testing.
- 20:04The place for manual testing is what I would consider exploratory testing because it's
- 20:08still good to have a human go in there and try things and feel the experience.
- 20:13Because like we said before, a lot of test automation struggles to handle things like visuals, struggles
- 20:19to handle things like good design, judging design, judging user experience.
- 20:23Does it feel laggy or is it responsive?
- 20:26Exactly.
- 20:26Exactly.
- 20:27Performance issues and stuff.
- 20:28And so people, like they start clicking and typing.
- 20:31It's immediate, you know?
- 20:33Like if anything is laggier than like 0.4 seconds or something, I forget the name of
- 20:38that threshold.
- 20:38That's categorically bad.
- 20:40What we don't want to do is have the bulk of our testing coverage done that way.
- 20:47We don't want to have the, let's say like the majority of our functional test cases executed
- 20:52manually.
- 20:52Because the idea of a test case, it's a well-defined procedure that you step through one through
- 20:57and you do the steps and you make sure it works.
- 21:00That kind of testing is where we might want to quote unquote level up to something like
- 21:03automation, right?
- 21:04Where you have these complimentary practices of you have automation do the rote, repetitive,
- 21:10but still necessary checks along the way.
- 21:13And then have your humans be creative in exploring and be able to judge the things that a script
- 21:18can't necessarily judge for you.
- 21:20Right.
- 21:20That's kind of where that goes.
- 21:21It would be lovely to leverage continuous integration and tools like that all over the place.
- 21:26And if you don't have at least some basic level of automation, that's more or less inaccessible
- 21:32to you, right?
- 21:33Like, so let's take an example of, I've got a Django CMS that I built and it's open source.
- 21:38And I want to allow people to send PRs to me to contribute back to this general CMS like
- 21:44WordPress, but with Django press, I'll just make a random term.
- 21:48It probably exists.
- 21:49Who knows?
- 21:49But like, imagine my Django press, I want to allow contributors and they're going to submit
- 21:53stuff.
- 21:53If I don't have something a little bit like the testing you're describing, then every PR,
- 21:59I've got to go download it, try it out and do those things.
- 22:03And as opposed to, no, the test didn't pass because you broke this thing.
- 22:06First fix that, please.
- 22:07Exactly.
- 22:08Your automated test suites are your safety net and they should account for a very high
- 22:14percentage of the coverage that you would want in your testing, right?
- 22:19The idea is the automated tests will do almost all the kinds of things.
- 22:23And then you would just have yourself or someone else go in and just kind of poke around real
- 22:28quick, make sure everything is still good.
- 22:29There's an, let me try the, okay, good, you know, and then feel good because you spent a
- 22:34little bit of time, sanity check, making sure everything is still good.
- 22:37Good from a user experience perspective.
- 22:38While you had the battery of your automated suites batter the crap out of whatever changed
- 22:43us when it, and it's like, Oh, they all passed.
- 22:46Yeah.
- 22:46Yeah.
- 22:47Yeah.
- 22:47I'm 95% sure we're okay.
- 22:49Now let's go from there.
- 22:50Yeah, exactly.
- 22:50You can also hone in on the really important things, right?
- 22:53Like in your CMS thing, maybe there's a reporting section that reports page hits over time or if
- 22:59it goes down, that's not great.
- 23:00But if the main page or main site won't even load, that's worse than the reporting being
- 23:06screwed up.
- 23:06Right.
- 23:06So you can, you can focus in on those areas and really put the energy where people are going
- 23:12to notice and consider it fully broken.
- 23:14Basically.
- 23:14Indeed.
- 23:14Indeed.
- 23:15All right.
- 23:15So the tools of the trade traditionally have been Selenium for this type of thing, right?
- 23:20And not to bash on Selenium anyway, it's, it's pretty awesome, but we're going to talk about
- 23:24some sweet new stuff as well that also works with Python in nice ways.
- 23:28Indeed.
- 23:29Yeah.
- 23:29So maybe tell people real quick about Selenium and then we'll talk about Playwright and you
- 23:33can compare those.
- 23:34Absolutely.
- 23:34So as you said, Michael, Selenium has been the classic browser automation tool.
- 23:40It's been around for, oh gosh, well over a decade now.
- 23:44It's hard to believe.
- 23:45Almost like two decades inch.
- 23:48I mean, the early, very, very early, early things that ultimately became the Selenium
- 23:53projects.
- 23:53How Selenium works is that you will have your browser, whether that's Chrome.
- 23:59IE, Firefox, RE, what have you.
- 24:01There's the browser.
- 24:02Then there is this web driver executable that acts as a proxy between the browser and your
- 24:08test automation code.
- 24:09And then in your test automation code, you have language bindings that will send requests
- 24:14to the browser or the Selenium web driver executable.
- 24:17It used to use a JSON protocol.
- 24:19I think it's Selenium before they might've changed some things.
- 24:21So I don't remember exactly.
- 24:23Basically, you send, you send requests to the thing, its proxy goes to the browser, performs
- 24:27whatever thing you wanted.
- 24:28Did you want to click?
- 24:29Did you want to get text?
- 24:30Sends the response back through.
- 24:31And so that's how your automation can control the browser.
- 24:34This is back and forth between browser and your automation through this, this proxy executable.
- 24:40Selenium is an open source project.
- 24:42So everything is available online.
- 24:44They have multiple language bindings, not just Python, Java, JavaScript, Python, C#, Ruby.
- 24:51I don't know if there are any others, but those would be like the big five.
- 24:54What else?
- 24:55What else?
- 24:55Not only is Selenium open source, but it is also open standards and open governance.
- 25:00Those might be new things or new phrases for some people.
- 25:04So let's go over there real quick.
- 25:06Open source means that the source code for all the implementations for Selenium web driver,
- 25:13as well as the other tools, Selenium ID and grid, which I'll talk about in a moment.
- 25:17Those are all GitHub.
- 25:18You can check out the source.
- 25:20You can look at it.
- 25:20You can fork it.
- 25:21You can make changes.
- 25:22You want to contribute to the main project.
- 25:23You can submit pull requests.
- 25:25And so that is all open and free.
- 25:28Open standards.
- 25:29What does that mean?
- 25:30The way Selenium works is it uses the web driver protocol to communicate with browsers.
- 25:36Web driver as a protocol is a W3C standard.
- 25:40It's something that all major browser vendors are supposed to conform to.
- 25:44And the reason, and that was a hard fall battle to get that to be a W3C recommendation.
- 25:48What that means is that hopefully in perpetuity, web driver will work with every major browser
- 25:54out there, right?
- 25:55So that there's this guarantee.
- 25:56We get a Zerodium instead of a Chromium in the future.
- 25:59Theoretically.
- 26:00Exactly.
- 26:01Theoretically it'll come with that, right?
- 26:03So that we could use the same protocol, meaning we could use the same tools to automate the
- 26:06basic browser interactions.
- 26:07So that's open standards.
- 26:09Open governance means that Selenium is not controlled by any single party or private entity or single
- 26:16company.
- 26:16The way that the Selenium project is governed is by a consortium of open source contributors
- 26:23and collaborators.
- 26:23I know many of these folks, they span multiple different companies.
- 26:27And so that way it's not like one person can control it.
- 26:30One person can't kill it.
- 26:32And again, everything is open.
- 26:34So it's not like it's a secret little cabal that everybody's scheming together.
- 26:37No, you can go read the meeting notes or the monthly meetings of the Selenium team.
- 26:41That's Selenium in a nutshell.
- 26:42Most times when people talk about Selenium for browser automation, that means Selenium web
- 26:46driver.
- 26:46That's like when you pip install Selenium in Python, you're getting the Selenium web driver
- 26:51package.
- 26:51That's the Python language bindings for the web driver protocol that you can go communicate
- 26:56with the browser.
- 26:57The other two projects Selenium IDE is a basically like a visual builder.
- 27:03So instead of you trying to figure out your expats and everything, you can use Selenium IDE to kind of
- 27:07create a visual builder for your test case steps.
- 27:10And the other project they have is Selenium grid, which is this scale out solution.
- 27:15So rather than running all the browsers on your local machine for testing, because that will max out your system resources very quickly.
- 27:23If you parallelize, you can distribute that remotely.
- 27:26So you can have like a cluster of nodes that have IE, some of them have Chrome, some have Safari.
- 27:32And so then you make remote requests and do the automation that way.
- 27:36So that's Selenium.
- 27:37Okay.
- 27:38Very cool.
- 27:39And like you said, it's been around for a while.
- 27:41And then the new one on the block is Playwright.
- 27:46And it's conceptually people should think of it like similar to Selenium, I think is your mental model, I would guess.
- 27:52But then it has some differences and advantages.
- 27:55One of which is it had 10 years of experience to build upon.
- 28:00True, true.
- 28:01Maybe tell us about Playwright.
- 28:02Sure, sure.
- 28:02So Selenium and Playwright at their core, the highest level are both browser automation tools.
- 28:08They go in, they do the clicks, they do the scrapes, you know, they send back the answers.
- 28:14So in that sense, they're similar.
- 28:16And also the main, I should also just caveat this, the main use case for both Selenium and Playwright is test automation.
- 28:21You could use it for any kind of automation, but primarily people use it for testing.
- 28:25Right, okay.
- 28:25And I will talk mainly in the domain of testing.
- 28:28I mean, you could use this to like, if you wanted to, I don't know, like refresh a page every hour and scrape it using Scrappy or something.
- 28:36But anyway, let's talk, just keep the focus on testing.
- 28:39I kind of would have like, in my history, would have liked to automate registering for college classes so I could get the 10 a.m. classes, not the 7.30 a.m. calculus class.
- 28:49Yep, yep.
- 28:49If only, if only, right?
- 28:52So Playwright, Selenium, both browser automation tools.
- 28:55But the ways in which they go about that are different.
- 28:59Selenium uses that web driver protocol, which kind of operates outside the browser, right?
- 29:04Playwright uses debug protocols.
- 29:08So basically, Chrome dev tools and Firefox's equivalent and all that.
- 29:11That's what Playwright focuses on.
- 29:13In that case, Playwright can access things about a browser that either Selenium can't or Selenium struggles to.
- 29:21It also makes Playwright run a bit faster than Selenium.
- 29:26One of the big complaints about Selenium-based tests is that they are slow.
- 29:31Now, this is due to the nature of them being black box that you need to have the whole system up.
- 29:36The fact that...
- 29:38Right.
- 29:38Launch your entire browser and then we're going to send messages and then shut it down and then launch it again for the next test.
- 29:43Right, right.
- 29:44Like that kind of stuff, right?
- 29:45So there is, based on the nature of UI or black box testing, there is sluggishness there.
- 29:50But at the same time, like you just mentioned, you know, the whole browser setup thing can be kind of slow.
- 29:55Playwright has some optimizations there that are pretty...
- 29:57So it can be a little bit fast.
- 29:58Another big difference between Playwright and Selenium, Selenium WebDriver is meant to be more of a low-level interface.
- 30:08So the interactions that you would do with Selenium are very raw.
- 30:13Like when you do a click, it'll send a click event and that's it.
- 30:17It doesn't wait for the thing to be received.
- 30:19It doesn't wait for the page to load or do anything magic.
- 30:21It just goes.
- 30:22And so with Selenium, you have to do a lot of explicit waiting for things.
- 30:28Because if you click and you don't wait for the page to reflect and then you proceed to your next interaction, the page might not be ready.
- 30:35The element might not be loaded and your test will go kaboom.
- 30:39And then your test suite gets criticized for being quote-unquote flaky.
- 30:43Yeah, absolutely.
- 30:44Well, this is like what happens in the JavaScript world.
- 30:47It's not the instant the page shows up.
- 30:49It's got to then bring down the JavaScript.
- 30:51Then maybe the JavaScript talks to some API and then it comes back and it binds to some stuff.
- 30:55Like there's a bunch of network steps with built-in latency there.
- 30:59Exactly.
- 31:00That is the pain point of all types of black box testing.
- 31:04There are inherent race conditions.
- 31:06And when you do test automation in that type of space, then you need to handle appropriate waiting for those race conditions or else your test will be flaky.
- 31:16And so in that sense, Selenium WebDriver, the tool does get a bad rap for being flaky when it's not WebDriver itself.
- 31:2299% of the time, it's actually the tests written using WebDriver that don't have those explicit waits in there.
- 31:28But on the other hand, like the tool gets used in the way that the tool encourages itself to be used.
- 31:34And so, yeah.
- 31:35Exactly.
- 31:36And so that's exactly where I was going.
- 31:38Because when you see some method, driver.click, you as the programmer expect it to click and kind of wait for things, right?
- 31:47Like that's kind of what you think.
- 31:48Nope.
- 31:49No, no.
- 31:49And so from experience working on many of these types of test automation projects across all languages and tools, when people are automating their tests, waiting is something that a lot of times they just don't even think of, right?
- 32:01Especially when they're newer to it.
- 32:02Whether that is a manual tester who's learning automation for the first time or a developer who's crossing over to automate some tests, you know?
- 32:10Yeah.
- 32:11Well, I mean, maybe you're just testing your local dev machine running on a local dev server where there's no network latency.
- 32:18And then you go and test against your microservice deployed cloud CDN thing.
- 32:24And it just doesn't behave the same, right?
- 32:26Exactly.
- 32:26It could have been working and working and working and working.
- 32:28And now it's flaky.
- 32:29Exactly.
- 32:30So with that in mind, with Selenium being that low-level kind of tool, I mean, I've even talked and heard from the Selenium people on this.
- 32:37Their thought is WebDriver is meant to be a low-level tool that you have to handle all those things yourself.
- 32:43And they even encourage you, you have, you know, layers and frameworks on top of Selenium that will handle that kind of stuff for your needs.
- 32:50In fact, at my previous company, I created an open source project in C# called Boa Constrictor, which is a .NET implementation of the screenplay pattern to handle those kinds of things.
- 33:00Like in Java, you have Serenity BDD, which has a layer over it like that.
- 33:04There's something called Selenide.
- 33:05You know, there's tons of different little projects out there.
- 33:09Sure.
- 33:09Compared to Playwright.
- 33:10Playwright is not a low-level interaction tool.
- 33:16Playwright is meant to be more modern, more refined, more high-level, more do-things-for-you, where the framework is there to help you.
- 33:23And so when it comes to waiting, most of the time that you need to do waiting in Playwright, Playwright handles it for you automatically.
- 33:31So for any single interaction where you are interacting with an element, depending on that type of interaction, there is a specific kind of wait automatically built in for that element, that target element to be ready before it does anything with it.
- 33:45You know, so before you go to click an element, Playwright's going to be like, whoa, whoa, whoa, let's wait about five seconds until it appears on the page.
- 33:51And once that thing appears, it's a smart wait.
- 33:54So boom, it'll dive in and do it then.
- 33:56So you don't have that flakiness that perbades so many older test suites.
- 34:01It's so good.
- 34:01And when your tests are flaky, then you just, doesn't matter if it fails, whatever.
- 34:05It fails sometimes.
- 34:06Right?
- 34:07As opposed to, oh no, the test failed.
- 34:08What happened?
- 34:09What went wrong?
- 34:10Mm-hmm.
- 34:10Mm-hmm.
- 34:11Exactly.
- 34:11You want to have a lot of faith and trust that a broken build means something or a failed test means something.
- 34:17Yes.
- 34:18This portion of Talk Python To Me is brought to you by the Compiler Podcast from Red Hat.
- 34:25Just like you, I'm a big fan of podcasts.
- 34:28And I'm happy to share a new one from a highly respected and open source company, Compiler, an original podcast from Red Hat.
- 34:36With more and more of us working from home, it's important to keep our human connection with technology.
- 34:41With Compiler, you'll do just that.
- 34:43The Compiler Podcast unravels industry topics, trends, and things you've always wanted to know about tech through interviews with people who know it best.
- 34:51These conversations include answering big questions like, what is technical debt?
- 34:55What are hiring managers actually looking for?
- 34:58And do you have to know how to code to get started in open source?
- 35:02I was a guest on Red Hat's previous podcast, Command Line Heroes, and Compiler follows along in that excellent and polished style we came to expect from that show.
- 35:11I just listened to episode 12 of Compiler, How Should We Handle Failure?
- 35:15I really valued their conversation about making space for developers to fail so that they can learn and grow without fear of making mistakes or taking down the production website.
- 35:25It's a conversation we can all relate to, I'm sure.
- 35:27Listen to an episode of Compiler by visiting talkpython.fm/compiler.
- 35:32The link is in your podcast player's show notes.
- 35:34You can listen to Compiler on Apple Podcasts, Overcast, Spotify, Pocket Cast, or anywhere you listen to your podcasts.
- 35:41And yes, of course, you could subscribe by just searching for it in your podcast player,
- 35:45but do so by following talkpython.fm/compiler so that they know that you came from Talk Python To Me.
- 35:51My thanks to the Compiler Podcast for keeping this podcast going strong.
- 35:57Before we dive in too much to Playwright, just a couple of questions that I think there that are interesting and worth covering.
- 36:05Dave Sharp says, I use Selenium for some headless browser tests and automations.
- 36:10Is that possible with Playwright?
- 36:11Yes.
- 36:11Yeah.
- 36:12Yeah.
- 36:12So real similar, I think the API is probably a little cleaner as well, as we'll see in a minute.
- 36:16Then Micah also says, absolutely can recommend Playwright.
- 36:20And the robot framework side browser library is built on Playwright and Selenium library on top of Selenium.
- 36:27Faster and just makes sense.
- 36:29Boom.
- 36:29Very cool.
- 36:30All right.
- 36:31Well, we've talked about some of these differences and stuff.
- 36:34Let's maybe just highlight some of the, you've introduced it pretty well across browser because it does Chromium WebKit and Firefox.
- 36:42And when you say Chromium these days, you're like 96% of all the browsers, sadly.
- 36:47That covers a lot, right?
- 36:49That's not just testing for Chrome, but that's testing for Edge, Brave, Vivaldi, et cetera, et cetera, cross-platform.
- 36:57I want to go back to browser because there is an interesting difference here to note between Selenium and Playwright.
- 37:03When you use Selenium, you're testing the full browsers, right?
- 37:06You're testing Google Chrome.
- 37:07You're testing Mozilla Firefox, Microsoft Edge.
- 37:10Does it even load up like extensions and stuff like that?
- 37:13Or is it just, is it more bare?
- 37:14Is it like maybe just below your customizations?
- 37:16In Selenium, it's the full browser, whatever you've got.
- 37:18You can do things from a command line to kind of like play with certain things like what user profile use, what user data directory and all that.
- 37:25But essentially, the browser that's installed on your machine is what Selenium WebDriver would use.
- 37:30But in Playwright, it doesn't use those quote unquote stock browsers.
- 37:33Playwright uses browser projects.
- 37:35So with Playwright, you are not testing Google Chrome.
- 37:38You are literally testing the Chromium project.
- 37:41You're not testing Mozilla Firefox.
- 37:43You're testing the open source Firefox project.
- 37:45You're not testing Apple Safari.
- 37:47You're testing the WebKit project.
- 37:49And so when you go to set up and install Playwright, part of the Playwright install command is it will download the latest versions of these browser projects and install them on your machine.
- 37:58And so when Playwright runs, it's running against those.
- 38:01In a sense, it's a more pure stripped down, just the essence of the rendering engine, JavaScript.
- 38:07Exactly.
- 38:07VM and stuff.
- 38:08Exactly.
- 38:08So it is.
- 38:10That's really nice.
- 38:11It's an opinionated take.
- 38:12You seem to have a very positive view of that, Michael.
- 38:15I do.
- 38:15Yes.
- 38:16I think it's cool.
- 38:17I think it's awesome.
- 38:18I think it's lighter weight.
- 38:19But I would also caveat they may not be usable in all contexts.
- 38:23Like if you are a government contractor or you're working at some big bank or some of these older, more legacy shops, they may have very strict requirements on browsers and versions to test.
- 38:34It's out there.
- 38:35It exists.
- 38:36Maybe you want IE 11 and you've got to just run that because they're they're crummy SharePoint installation from 10 years ago.
- 38:43Stuff like that.
- 38:43Unwilling to upgrade like anything but that log into it or some weird thing like that.
- 38:48Right.
- 38:48Exactly.
- 38:48So anything like that may be a showstopper for folks who want to use Playwright.
- 38:52My gut tells me that's a very, very small sliver of the wider industry, but it is something to call it.
- 38:57So kind of modern web browsers is where you're going to end up.
- 39:01Yes, exactly.
- 39:02Exactly.
- 39:02Sure.
- 39:03Okay.
- 39:03Across platform, Windows, Linux, Mac plus CI and headless.
- 39:08That was sort of touched upon in Dave's question, but maybe discuss this headless versus headed idea of this, these types of frameworks.
- 39:17Sure, sure.
- 39:18So when you're the human and you open up your browser, that's headed mode.
- 39:21Why is it called headed mode?
- 39:23Because it's rendering in front of you and it's doing all that fun stuff and you can see it visibly on the screen or in whatever viewports you've got.
- 39:29Headless mode is a way that you can run browsers without having it pop up on the screen and render all that stuff.
- 39:35It'll still do the rendering.
- 39:36It just won't graphically display that to you.
- 39:38When you run automated tests, specifically like in a continuous integration environment, maybe in a GitHub action or something, you want to run headless mode.
- 39:46Why?
- 39:47Because it uses fewer resources.
- 39:48It's a little bit faster.
- 39:50Nobody is there to look at the screen when it pops up.
- 39:53It's got a button.
- 39:54Would you like to upgrade?
- 39:55Yeah.
- 39:56Yeah.
- 39:56Chrome is great, but you should try Edge.
- 39:59It has these new shopping.
- 40:00No, I don't want that.
- 40:00Go on.
- 40:01Yeah.
- 40:01Thanks.
- 40:03I know that wouldn't happen in the project.
- 40:04That might be more of a Selenium thing if you get like these weird, typically, you know, periodic pop ups, right?
- 40:09From the browser vendors.
- 40:10Indeed.
- 40:11When headless modes first became popular, I want to say about like five years ago, like it really became popular with PhantomJS.
- 40:18And then Chrome basically came along, did the same thing and killed the PhantomJS project.
- 40:22The dude just like, I'm done.
- 40:24Use Chrome instead.
- 40:25Like TLDR on the GitHub repo.
- 40:27I was like, wow.
- 40:27Now all the browsers do this.
- 40:29But it is a recommended practice that when you're running tests for credit, like in CI or something, you do headless mode.
- 40:35Also, if you try to do headed mode in certain CI tools or GitHub actions, sometimes you might get these walkie errors like, you couldn't render this thing.
- 40:42Yeah.
- 40:43Or maybe like the desktop UI tools or frameworks are not set up to actually show windows or some weird thing.
- 40:49But headed, you might want to see what happened.
- 40:52Like if something goes wrong.
- 40:53So you would use headed mode when you are developing the test automation on your workbench.
- 41:00Like, you know, I've got Visual Studio Code open and I just code my test.
- 41:04I want to run it real quick.
- 41:05I want to see it pop up and dance through the page.
- 41:08Right.
- 41:08That's when I would use headed mode.
- 41:09Also, interesting thing.
- 41:11I believe by default, Playwright uses headless mode.
- 41:15It's not something that you have to.
- 41:17Yes.
- 41:17I think you're right about that as well.
- 41:19Yeah.
- 41:19If you want a headed mode, you got a headless equals false or headed equals, I don't know.
- 41:23We'll see it in a second.
- 41:24We'll get to the examples.
- 41:25But you got to opt in.
- 41:27And then you already mentioned the Playwright API is TypeScript, JavaScript, Python, .NET, and Java.
- 41:32And people are like, ah, I'm not using it.
- 41:34It's not Python.
- 41:35Well, the browsers aren't Python anyway.
- 41:37So it's just like one more layer of who knows how many technologies are in there.
- 41:41But like NumPy has a C layer.
- 41:43This is like a layer to talk to the thing that does the work.
- 41:47And it's a pretty nice API, as we're going to see in a moment.
- 41:49And then you can also test mobile web, even if you don't have a mobile web thing, because it has native mobile emulation.
- 41:55Yeah.
- 41:56Correct.
- 41:56That's all, you know, viewport size, some of the little options on whatever request it has, just to kind of somewhat emulate what a mobile browser would look like.
- 42:05Yeah.
- 42:05Very cool.
- 42:05Test like responsiveness and like a bootstrap menu or something.
- 42:09Auto-wait.
- 42:10That sounds very promising.
- 42:12It waits for elements to be actionable prior to trying to click them.
- 42:16You already went into the flakiness and race conditions of all that, but that's pretty cool.
- 42:20Web-first assertions.
- 42:22What does that mean?
- 42:23So that sounds good.
- 42:24Yeah.
- 42:24Yeah.
- 42:24Yeah.
- 42:24Because you're testing the web.
- 42:25So that kind of goes hand in hand with auto-wait.
- 42:28For those of you who are familiar with unit testing in Python, whether that's with unit test or pytest.
- 42:34No, unit test has its like assert that library.
- 42:38pytest, you literally just use the assert command in Python, right?
- 42:42Assert this, assert that, all that kind of stuff.
- 42:44When it comes to web testing or really any sort of black box testing, the basic kind of assert statements can be limited.
- 42:52Again, because a lot of times you have to wait for the value to be ready before you can make a Boolean condition of an assertion on it.
- 42:59That's basically what these web-first assertions mean.
- 43:01In Playwright, there is an expect function.
- 43:04It's in JavaScript.
- 43:05It's in Python.
- 43:06I think it's in the others.
- 43:07But basically, you would say something like expect a certain locator to have a certain text value.
- 43:12And what it'll do is it'll wait until said phrase has that text value or wait until this locator appears on the screen.
- 43:19It's a more fluid kind of way of writing assertions.
- 43:22And it also has that built-in waiting.
- 43:25And they call it web-first because it's the idea that it's putting the emphasis of the condition on the web elements as we would naturally understand them.
- 43:32Not trying to shoehorn them into some fixed static Boolean condition.
- 43:36Yeah, that's very cool.
- 43:37Okay, and then the last selling point on resiliency here is tracing, configure tests, retry strategies, capture execution traces, videos, and screenshots.
- 43:48So, yeah, we'll see some of the tooling, the GUI tools and stuff for this.
- 43:52But, yeah, this sounds really nice.
- 43:54Mm-hmm.
- 43:55Mm-hmm.
- 43:55If your test fails in CI and you get a video, they click there and then that gave a, you know, 400 or something.
- 44:02In JavaScript instead of filling up this form, like, that sounds pretty nice.
- 44:06Oh, yeah.
- 44:06So, yeah, and that's a huge advantage that Playwright has over Selenium as well.
- 44:10Like, in Selenium, you can capture screenshots along the way, but there's no built-in video capture.
- 44:15In Playwright, you can not only get screenshots if you want explicitly through the code, implicitly you slap on the screenshot command line argument after every single failed test, it'll poop out a screenshot.
- 44:27Or you can just be like, give me the whole dang video so I can trace through a watch.
- 44:30I mean, screenshots are great.
- 44:33Video is lit.
- 44:34It's incredible.
- 44:36It's like, oh, my gosh, they did it.
- 44:38They planted the flag.
- 44:40Victory.
- 44:41This is amazing.
- 44:42Yeah, because it's one thing to see it where you have it in headed mode and you can interact.
- 44:45It's another when it's on a remote system, maybe a different platform, and you're like, what went wrong with this thing?
- 44:50Exactly.
- 44:50Because, like, that's the thing.
- 44:52Like, in olden times, I kid you not, when people did automation, they would literally push the button on the machine and just, like, watch it go.
- 44:59Wait for it to finish.
- 45:00And that was automation.
- 45:01No, no, no, no.
- 45:02Like, true automation is it's in some sort of CI system.
- 45:05You know, it runs on a, after every pull request, after every commit, or maybe if the test suite is too big, it runs nightly or, you know, a couple times a week.
- 45:13You are not there to watch it fail, right?
- 45:15So you need all the evidence you can get.
- 45:18What's the best evidence?
- 45:19Video.
- 45:19Video to see what's happening, and then you dig into logs.
- 45:22Yeah, I mean, your only visibility is spinning, spinning on CI and then failed.
- 45:26Exactly.
- 45:26And so, yeah, it's really good.
- 45:28I love it.
- 45:29Okay.
- 45:30Stuff about running on modern platforms, but then let's talk the isolation.
- 45:35I think that that's huge.
- 45:36There's two things about sort of authentication and cookies and stuff like that.
- 45:41It's pretty interesting.
- 45:43But then just also the isolation, right?
- 45:45You have this very much in testing, right?
- 45:48Yes.
- 45:49You don't want to depend on the order of tests, for example, at a unit test.
- 45:54And this, you kind of like don't want to depend upon the last page you visited for browser tests.
- 45:59And specifically with the isolation and Playwright and the browser context here.
- 46:04You had mentioned earlier, Michael, about in Selenium, how for every single test, you would have to open a new browser, open a new web driver process, and at the end of it, close it, close it.
- 46:14And the reason you do that, you open and close for every single test is because you want that isolation, right?
- 46:19You don't want to share web browsers across tests because things can go wrong.
- 46:23With Playwright, one of the optimizations is for the entirety of your test suite, you do only have one browser process.
- 46:30So you only have one Chromium process.
- 46:32It's at the beginning of everything you started up and it exists throughout all tests and you close it.
- 46:37You're like, well, how, wait, doesn't that violate independence and isolation?
- 46:41What Playwright does is it creates what's called browser contexts out of that.
- 46:44So the slow part is...
- 46:46It's like a new private incognito window sort of, right?
- 46:49Yes, exactly.
- 46:50It's basically like your own session, your own window, your own user account kind of thing.
- 46:55Browser contexts are very quick to create and close.
- 46:58So you don't have the like five second to 10 second startup and teardown time per test.
- 47:03It's like a split second.
- 47:04So that makes you be able to set up your test a lot faster.
- 47:06And it's basically, it's like an incognito session.
- 47:09So you have access to everything in your scope.
- 47:12But if there are other contexts running at the same time, e.g. you're running parallel tests,
- 47:18you can't access anything in the other sandboxes.
- 47:20So it's safe.
- 47:21And it's really a game changer for browser-based testing.
- 47:24And as you mentioned, just like I have a quick count, I think I have six browser tabs
- 47:30and a progressive web app open right now.
- 47:32They don't interact, right?
- 47:34I mean, that's just how browsers are built.
- 47:35That if I visit playwright.dev and then some other...
- 47:38Selenium, whatever, they don't get to spy on each other, right?
- 47:43That's just how it works.
- 47:45And so it leverages that to say, well, we can do more than one tab at once.
- 47:48Let's go.
- 47:49And all of your authentication or browser context, you know, any of the cookies you have protected by browser context.
- 47:55So you can do a lot of really cool stuff.
- 47:57It's not just you're on a different tab because that comes down to pages off of browser context, which is another thing.
- 48:03But it's full context of what's going on.
- 48:07Yeah.
- 48:07Yeah.
- 48:08Yeah.
- 48:08Very cool.
- 48:09And then the last sort of selling point that Playwright talks about is that they have a bunch of tooling to go along and help you, right?
- 48:16We're going to talk about the API next and go see that.
- 48:18And the API is really nice.
- 48:20But sometimes you're like, oh, what is that CSS selector?
- 48:23And I've been writing CSS selectors for many years, so it doesn't bother me.
- 48:27But I remember the beginning of like, what does it mean when there's the arrow versus not the arrow?
- 48:32Or does it need the space or not the space between the dot and the preceding element?
- 48:36You're just like, oh, I'd rather just go hit me with a stick.
- 48:39I've done more content.
- 48:39I just can't take this anymore.
- 48:41I'm pulling my hair out.
- 48:43So there's some tools like a code gen, sort of record my interactions and then generate Python or other language, I guess.
- 48:50For me, that'll help a lot there, right?
- 48:52Yep.
- 48:53Maybe we could talk a bit about really high level.
- 48:55Then we could like, we'll dive in.
- 48:56We'll pull some pictures about it.
- 48:57Sure, sure.
- 48:58Yeah.
- 48:58I mean, it's basically that like it fires up a browser.
- 49:01You click through.
- 49:02You say, I'm done.
- 49:02And then poops out all the stuff you did in Playwright code.
- 49:05Yeah.
- 49:06Well, you talked about it using the dev tools.
- 49:07It's very much like the inspect element of the dev tools.
- 49:10Yep.
- 49:10Okay.
- 49:11And then there's an inspector, a similar, but also kind of like lets you step through and a trace viewer as well.
- 49:18That's a little bit like the network tab, I guess, plus snapshots.
- 49:21Yeah, yeah.
- 49:22Screenshots.
- 49:22So trace viewer is something that you would kind of put on a test for monitoring and then it poops out all the logs and stuff.
- 49:29So you can kind of better trace through it after the test is done.
- 49:32Live DOM snapshots and all that good stuff.
- 49:34Okay, cool.
- 49:35Let's maybe talk through some of the code and how it works.
- 49:38Install.
- 49:39Easy.
- 49:40Know it.
- 49:41Love it.
- 49:41Pip install it.
- 49:42But it looks like there's two steps.
- 49:44I pip install Playwright and then I playwright install.
- 49:46Ah, yes, yes.
- 49:47So pip install Playwright is what's going to give you the Python Playwright package.
- 49:51I know that's a lot of P's.
- 49:52What Playwright install does is it installs those browser projects, Chromium, Firefox, and WebKit.
- 49:59Right, okay.
- 49:59You can also, I think you can do Playwright install Chromium if you want just one, right?
- 50:03Correct.
- 50:04You can focus in.
- 50:04Yep.
- 50:04You can pick the one to install.
- 50:06Yeah, but it's 2022.
- 50:07Just install them all.
- 50:08Let it have it.
- 50:09Yep.
- 50:10One other thing that if you're in Python, I would strongly recommend you include would
- 50:13be a pip install Playwright pytest, which is the pytest plugin for Playwright.
- 50:18Again, plop, plop, plop, plop.
- 50:21The Playwright package alone is going to be the browser automation tool.
- 50:25The pytest plugin for Playwright is what gives you all the tie-ins to pytest so you can make
- 50:32really nice tests.
- 50:34It gives you the command line options like the headed option or the video option.
- 50:40It gives you the fixtures for getting the context and the page.
- 50:43So if you're going to be using it for testing, definitely Python, Playwright, plus pytest.
- 50:48Amazing combo.
- 50:50Cool.
- 50:50If you're just doing pure automation, maybe you don't need the pytest plugins and so on.
- 50:57Yeah, cool.
- 50:57Okay.
- 50:57So we've installed it.
- 50:59And then immediately what might catch your eye is when I import, I import Playwright.either
- 51:07sync API or async API.
- 51:09And you're like, oh, wait, there's two APIs.
- 51:12How lovely.
- 51:12But maybe talk us through what the code, keep in mind this audio, but give us a sense of
- 51:17what does it feel like to write code for the sync API?
- 51:19And then we can see how it changes with async.
- 51:21Sure, sure.
- 51:21So with the sync API, of course, everything is step by step.
- 51:25It waits before it goes on to the next thing.
- 51:26I would say if you're doing test automation in Python, the sync API is the way to go.
- 51:31And in fact, that pytest fixture I mentioned gives you everything sync.
- 51:35You would use async if you're doing more just rogue automation.
- 51:39You know, you're trying to sign up for your college courses.
- 51:42That would be what you would use the async.
- 51:44So in this little snippet, what it's doing here is with sync API, you're saying browser
- 51:50equals p chromium launch.
- 51:52That's opening up a chromium browser instance for Playwright.
- 51:56So that's the full browser.
- 51:58That's the start of the browser process.
- 51:59Then off of that, the next line, page equals browser.newpage.
- 52:03What it's saying there is from that browser instance, create a under the hood.
- 52:09This is not included here, but create a new browser context.
- 52:12And from that browser context, create a new page and give me the page options.
- 52:17All of the interactions in Playwright are going to be going off of that page.
- 52:21If you want to click, if you want to scrape, if you want to send text, all that is going to be on the page.
- 52:27So following that, the next line is the first call we see page.go to, you know, some web address,
- 52:33http colon slash /playwright.dev.
- 52:36So that would be an example of a browser interaction on the page.
- 52:40That one, because that will load the webpage, it's directly off the page.
- 52:45A lot of times you might need to say page.locator with your CSS selector, expat ID to identify an element on the page
- 52:53and then do something on the element.
- 52:54So you would say page.locator, blah, blah, blah, dot click.
- 52:57Page.locator.fill with certain text.
- 53:01That would be like typing text.
- 53:02Right, okay.
- 53:02Then the next line we see here.
- 53:04Nice, then you can access like properties like page.title, right?
- 53:06Oh yeah, you can get page title, you can get page URL.
- 53:09I don't know the exact names of everything, but if you're using like Visual Studio Code
- 53:14and Python typing, then you can get on a complete and find the whole list.
- 53:18Beautiful.
- 53:18Yeah, yeah, that's awesome.
- 53:20So another thing it seems like I could do here is we've got p.chromium, p.webkit, I presume, and so on.
- 53:26I could use a parameterized pytest test and give it the browser for Chromium.
- 53:33Okay, so.
- 53:35No?
- 53:35So, okay.
- 53:36How do I just test all the browsers?
- 53:38Two things here.
- 53:38Two things here.
- 53:39First of all, what you just recommended is an antipatter.
- 53:42You never want to use pytest parameters to choose browser.
- 53:44Browser is a browser choice.
- 53:47Browser choice is a test control input.
- 53:49That's something that you want the user to specify, I want to target this browser.
- 53:52And the way you would want to treat that is to say, okay, I have my whole test suite.
- 53:56Honestly, all web UI tests should be able to run against any browser.
- 54:00There's no reason one would test on Chromium versus Firefox or something.
- 54:03So, you would want to say, hey, for this entire test run of these tests that I'm filtering on my suite, target Chromium.
- 54:11If you wanted to test on Firefox, you spin up another process and say, okay, run these and target Firefox.
- 54:15Test control input passed in and handle.
- 54:18Now you're probably thinking, oh, okay, so now I can use a pytest fixture to read an environment variable and pass it in that way.
- 54:24That's what the Playwright pytest plugin does for you.
- 54:27Okay, nice.
- 54:29Yeah, we'll get to that in a second.
- 54:30Yeah, yeah.
- 54:30That's another reason to use the plugin is because literally it's a command line option.
- 54:34You know, project Chromium, project Firefox.
- 54:38Or you can specify all of them and run three at a time.
- 54:40Got it.
- 54:40Okay, cool.
- 54:41And then there's a similar async API, but instead of using a with block to create the browser's sort of session up here, right?
- 54:51Instead of with sync playwright SP, we have, we create an async with block to create an async playwright.
- 54:57And then we await launching the browser or launching the process and then await getting the page.
- 55:02And there's a lot of await on this page, honestly.
- 55:04This might be the most await dense async thing I've ever seen in my life.
- 55:08Oh, you should see the JavaScript code.
- 55:09Which is not necessarily negative, but this is all await.
- 55:11Like six lines of await.
- 55:12Yeah, no, you're absolutely right.
- 55:14In fact, right before our chat today, I was doing playwright in TypeScript.
- 55:18And yeah, it's await, await, await, await, await, all the way down.
- 55:22At least in Python, you get to choose.
- 55:24Yeah, true.
- 55:25Yeah, JavaScript doesn't have the sync options.
- 55:28Yeah, so, but otherwise it's the same, right?
- 55:30Exactly.
- 55:31So when would I use, like when would, let me rephrase this.
- 55:33When would you use the async API?
- 55:35I would use the async API if I'm trying to do some sort of browser automation apart from test automation.
- 55:43Like if I'm trying to write a tool that's trying to log in and register for college course,
- 55:48or I'm trying to scrape numbers off of a dashboard on a website that change every five minutes or something.
- 55:55That would be the case where I would probably write like a pyscript and just puke everything in one file.
- 56:00And I would use the async API.
- 56:02I would use sync API for test automation.
- 56:04Got it.
- 56:04Okay.
- 56:05Would it make sense to use the async API if I was trying to run on all three browsers at once?
- 56:09I mean, yeah.
- 56:10Why would it still not go async there?
- 56:12Well.
- 56:12Maybe like an xdist or something like that on my PY test?
- 56:16That would not be the appropriate way to handle that.
- 56:18What you would do is you would code your test and it would be generic enough to run on any browser.
- 56:23And then when you would launch it using the PY test plugin, you would specify all three browsers to run at the same time, which would basically triple the test.
- 56:33And then you would use PY test xdist dash three.
- 56:36So it would parallelize that.
- 56:38Right.
- 56:38So you would write the test as if it's generic and then you would use control options to parallelize and cross browser tests.
- 56:44Yeah.
- 56:45Probably the xdist anyway.
- 56:46I don't know.
- 56:47You probably know.
- 56:48It's probably multi-process, not threaded anyway, right?
- 56:51Yes.
- 56:51Yeah.
- 56:52So it doesn't really matter if it's asynchronous.
- 56:53Correct.
- 56:54Okay.
- 56:54Got it.
- 56:55Got it.
- 56:55Got it.
- 56:55And then here's the example of like how you might launch it in headed or not headed mode.
- 57:00You would say like Firefox dot launch headless equals false.
- 57:04There's another interesting parameter here.
- 57:06Slow mo.
- 57:07Mm-hmm.
- 57:08Mm-hmm.
- 57:08Like don't you want your test to be fast?
- 57:10You went on and on about how it was good.
- 57:11So when you are developing tests on your local workbench, and like I said before, that's when you want to do headed because you want to see it.
- 57:17Playwright is so fast that sometimes you can't track what's going on.
- 57:20I kid you not.
- 57:21It's fast.
- 57:23And so what you would want to do during like your local development or debugging, you might want to inject a slowdown after every interaction.
- 57:31Usually I use like 1,000 or 2,000 milliseconds just to kind of.
- 57:35Yeah.
- 57:35Also, I would recommend just like with headless option, slow-mo option, you can use the command line.
- 57:41And I would even say that's the preferred way rather than injecting directly to code.
- 57:45Yeah.
- 57:45Probably also just to be nice.
- 57:47If you're not testing, but you're going around scraping on people's websites, don't hit it as fast as the network will allow you.
- 57:55Yeah, because some websites will identify you as a bot and be like, nope, I'm rejecting your request.
- 58:00So if you inject a slow-mo, you seem more like a human.
- 58:03You'll skirt under the radar.
- 58:05Ooh, I didn't even think of that, but that's right.
- 58:07Yeah.
- 58:08I'm sure that that actually, it may not be the intent.
- 58:10The intent is to make it human interactable, but it's still, it might be necessary.
- 58:16Get in that college course, get that 10 a.m. class, don't get banned.
- 58:20Okay.
- 58:20I actually did do something like that when I was in college, but using a dial-up modem because I had to talk to somebody.
- 58:28But it was glorious.
- 58:29I had the best class schedule of the year that year.
- 58:31Nice.
- 58:32Okay, so another thing you could do is you can record scripts using Playwright, CodeGen, and then give it a URL.
- 58:38And then it'll watch you interact with it and then generate Python, try to mimic those interactions, right?
- 58:44Mm-hmm.
- 58:44Okay, that's pretty awesome.
- 58:46We talked a lot about, we'll come back to the pytest in a second.
- 58:49There's also a REPL mode, which is pretty, I mean, I guess that's just saying like you can call Python code in a REPL.
- 58:55That's not super interesting.
- 58:57If you're coming to Python from another ecosystem, this might be a big deal for you.
- 59:02I think for us, we're like, oh yeah, you can do any Python in a REPL.
- 59:05No, it's simple, whatever.
- 59:06So somebody come from like the Java world, oh my gosh, this will blow their mind, you know, probably.
- 59:10Yeah, that's for sure.
- 59:11I guess I'd never really thought about creating an async REPL, but you can create an async REPL using Python-M asyncio and then you get a REPL, but it lets you write await.
- 59:21I've never done that.
- 59:23So I guess I learned something.
- 59:24Then also it has this interesting way to build itself into a standalone executable.
- 59:31So you don't need Python or anything else to run it using PyInstaller, but there's some integrated commands.
- 59:37Tell us about this.
- 59:38Like I'm searching for a way in which this might be relevant to me.
- 59:41Well, that would be like the tool that we mentioned versus a test automation suite, right?
- 59:46If you wanted to build your college course hacker scheduler thing, you could build it.
- 59:52I see.
- 59:52And then.
- 59:52Yep.
- 59:52You could build into it an executable and then it's a little easier to run.
- 59:55That's all.
- 59:56I see.
- 59:56Then you tell it, it takes these command line arguments and then it goes and it.
- 1:00:00Yep.
- 1:00:00Okay.
- 1:00:00Got it.
- 1:00:01If I want to share it.
- 1:00:01Sure.
- 1:00:01By the way, the instructions on the website, people are watching.
- 1:00:04There's a bash, a PowerShell and a batch version.
- 1:00:06The bash version like doesn't have backslash ends or something.
- 1:00:09I don't know.
- 1:00:10It's a little weird.
- 1:00:10Just look at the other ones.
- 1:00:12So realize that's three lines.
- 1:00:13Oops.
- 1:00:14Oops.
- 1:00:15All right.
- 1:00:16It does have some interesting ideas.
- 1:00:18You talked about the waiting and it says, don't use time.sleep.
- 1:00:22You might be able to get away with async.io.sleep, but don't use time.sleep.
- 1:00:25Use page.wait for timeout instead, which is interesting.
- 1:00:30Apparently the time.sleep messes with its internal asynchronous.
- 1:00:34Interesting.
- 1:00:34Or something.
- 1:00:35So yeah, just people be aware of that.
- 1:00:38All right.
- 1:00:38Let's talk pytest plugin real quick.
- 1:00:39Sure.
- 1:00:40Sure.
- 1:00:40So we could write a pytest test that takes a page object, which is.
- 1:00:46The browser process is launched.
- 1:00:48A session is created and a page has been created and then it's handed to you.
- 1:00:52Exactly.
- 1:00:53So like that opening code snippet we just talked about, you don't need to do that when you're
- 1:00:57doing pytest with the plugin.
- 1:00:59You basically say, I want to use a page.
- 1:01:01Fixture go.
- 1:01:02Boom.
- 1:01:02And it gives it to you.
- 1:01:03So you can immediately jump into things like page go to, page locator, give me this, page
- 1:01:08title, page click, you know, straight from the test function, which is really, really
- 1:01:13nice.
- 1:01:14It cuts down on all that boilerplate.
- 1:01:15Yeah.
- 1:01:15That's, that's very neat.
- 1:01:16And then here, right below that in the, it shows the CLI options, pytest --headed,
- 1:01:23--browser, Firefox or multiple browsers and so on.
- 1:01:26Mm-hmm.
- 1:01:27Mm-hmm.
- 1:01:27Yeah.
- 1:01:28Very nice.
- 1:01:29Very nice.
- 1:01:29What else should we talk about?
- 1:01:30Maybe we could talk like text input and stuff just really quick, because I feel like that
- 1:01:35goes a little bit beyond just CSS selectors, like the clicks and the inputs and the.
- 1:01:39Sure, sure, sure.
- 1:01:40So on.
- 1:01:40So which one do we talk about?
- 1:01:42Inputs, locators?
- 1:01:43There should be a section on selectors if you go down, which I would recommend.
- 1:01:47There you go.
- 1:01:47So the selectors are the starting point of finding things on the page.
- 1:01:53You can identify elements on the page from many different kinds of selectors.
- 1:01:56You can have an ID, boom, you nail it, you know, assuming it's your unique on the page,
- 1:02:01right?
- 1:02:02You can use a CSS selector.
- 1:02:03You can use an XPath.
- 1:02:05All those are things that are also selectors available with Selenium WebDriver and Cypress.
- 1:02:10But Playwright has a few other kinds of selectors that it supports.
- 1:02:14One of the most helpful ones is what they call a text selector.
- 1:02:17Oh, you've got it on the screen here.
- 1:02:18Yeah, there you go.
- 1:02:18So like I want the button that says login.
- 1:02:21Correct.
- 1:02:22Exactly.
- 1:02:23Because I don't care what kind of element it is.
- 1:02:26I just want the element that has a text that says login, that has submit.
- 1:02:31As long as it's the only one on the page, you're going to get a right.
- 1:02:34That lends to the developer experience, right?
- 1:02:37Because you're trying to figure out CSS selectors and XPaths.
- 1:02:41There's a cognitive load.
- 1:02:42Sometimes you have to do it.
- 1:02:43But if you don't have to and you can do something easier, it's better for you and for the test
- 1:02:47case.
- 1:02:48Simple is better than complex.
- 1:02:49And so here, right, yeah.
- 1:02:51Give me the text.
- 1:02:51Boom, I'll find the element.
- 1:02:52So that's one of the really, really cool selectors.
- 1:02:55Yeah, that's fantastic.
- 1:02:56And then you can come in here for input and like go and say, I would like to check the check
- 1:03:02box that has the ID agree.
- 1:03:04Or that has the text XL.
- 1:03:06Like what size of t-shirt do you want?
- 1:03:08I want XL.
- 1:03:08Check that.
- 1:03:09That radio button or that check box.
- 1:03:11That's super cool.
- 1:03:12Actually, I should comment that the commands we're seeing here are things like page check,
- 1:03:16page uncheck, page fill.
- 1:03:18That's actually no longer the recommended practice with Playwright.
- 1:03:21You can do it this way.
- 1:03:23What do they suggest?
- 1:03:23What they suggest is page.locator, pop in your selector, and then dot whatever the interaction
- 1:03:29is.
- 1:03:30So in these cases, instead of page dot check, agree will be page locator agree dot check.
- 1:03:35And the reason they recommend that, even though it's more verbose, it's ultimately supposed
- 1:03:41to lead you to a refactoring into things like page objects, right?
- 1:03:44Because you are separating the concern of page structure from interaction.
- 1:03:48Whereas when they're together, even though it's more concise, it is readable.
- 1:03:52You're combining those concerns.
- 1:03:54It doesn't really matter.
- 1:03:56You can use both ways.
- 1:03:57Sure.
- 1:03:58Okay.
- 1:03:59Yeah.
- 1:03:59These are nice.
- 1:04:00All right.
- 1:04:01So really, basically the takeaway, like for example, here I can go and find me the button
- 1:04:07that says text submit, and then I can do a hover over it and then click, which I think
- 1:04:10is interesting because often it doesn't matter.
- 1:04:13But sometimes if you hover over a thing, it exposes different DOM elements that when you
- 1:04:18click on them may have a different behavior.
- 1:04:19Indeed.
- 1:04:20So you have this really fine grain control.
- 1:04:21Indeed.
- 1:04:22Got a few.
- 1:04:23Go.
- 1:04:23No.
- 1:04:23Okay.
- 1:04:23Finish your thought.
- 1:04:24Sorry.
- 1:04:24Sorry.
- 1:04:24I was going to say like, also in that example, we just showed you created the locator object
- 1:04:29to one time, and then you perform multiple interactions on it.
- 1:04:32Whereas if we, if you had repeated or if you had combined the calls, you would have under
- 1:04:37the hood duplicated the creation of the same locator object.
- 1:04:40So there's a sense of reusability there as well.
- 1:04:43That's all.
- 1:04:43Got it.
- 1:04:43All right.
- 1:04:44Let's round out our conversation here just quickly with the tools.
- 1:04:48So I think that these are really nice.
- 1:04:50These visual tools here.
- 1:04:51All right.
- 1:04:51The first one I pulled up first is the playwright inspector, and it's kind of like, I don't
- 1:04:56even know how to describe it.
- 1:04:57So it's, it's as if you went through and you did a bunch of CSS stuff and then playwright
- 1:05:02wrote asynchronous Python code with the things like page.click and whatnot that we discussed.
- 1:05:08And then, then you can go and check it out.
- 1:05:10So this is kind of like a playback understanding type thing, right?
- 1:05:15Yeah.
- 1:05:15Yeah.
- 1:05:15How would you characterize this?
- 1:05:16So this is like saying, okay, this is what your code did.
- 1:05:19Here's more information on it.
- 1:05:21Very similar to Chrome dev tools.
- 1:05:24I will caveat to say, I have not used this much myself.
- 1:05:27This is also kind of new to me too.
- 1:05:29Also on top of that, I should say the playwright team keeps pumping out new, awesome stuff all
- 1:05:33the time.
- 1:05:33So hold on to your butts.
- 1:05:35They just keep making more awesome, cool stuff.
- 1:05:37Yeah.
- 1:05:37Apparently it has features for like debugging selectors for stepping through like one of
- 1:05:42these playback scripts to see how it goes and so on.
- 1:05:44It's really cool.
- 1:05:45Real time stuff here.
- 1:05:47Yeah.
- 1:05:47Yeah.
- 1:05:47This is super neat.
- 1:05:48Okay.
- 1:05:48Another one, the trace viewer.
- 1:05:50This, like I said, looks a little bit, it's a little bit like the dev tools of a browser.
- 1:05:54If you open up the network tab and then select, show me the response in that view for people
- 1:06:00who are not seeing this, but you can go to the playwright.dev site and check it out.
- 1:06:03That's a good way to describe it.
- 1:06:04And it bundles up like so many things.
- 1:06:07Like I think you can say like, oh, I want to capture the screenshots.
- 1:06:10I want to capture the video.
- 1:06:12I want to capture the DOMS of the pages.
- 1:06:13I want the stores.
- 1:06:14Like it all ended up like, I think it bundles it up all for you in one artifact.
- 1:06:18It's like, you're just tested done.
- 1:06:19Here you go.
- 1:06:20Have fun looking at it.
- 1:06:21So it's the kind of thing where you could, when you, let's say you're running this in continuous
- 1:06:25integration, you know, test fail, it bundles all this stuff up, saves it for you as an artifact
- 1:06:30and then you can go look at it later for the ones that fail and be like, oh, so here's
- 1:06:34all the context of what went wrong.
- 1:06:36And you can really get in there and figure out what happened.
- 1:06:38It's fantastic.
- 1:06:40It has all the actions.
- 1:06:41You click on them and it says, what was the action?
- 1:06:43What was it like before?
- 1:06:44What was it like after?
- 1:06:45What did the network do?
- 1:06:46Yeah.
- 1:06:46Really good.
- 1:06:47And then test generation because, hey, we don't need to write tests.
- 1:06:50We'll just tell it to go.
- 1:06:51No, I'm just kidding.
- 1:06:52Tell us about this.
- 1:06:53This is pretty cool though.
- 1:06:54This is like fire up the browser.
- 1:06:56Let me interact with it and then generate a script that I can go from there.
- 1:07:00Yeah.
- 1:07:00Basically it's like a screen recorder.
- 1:07:01Yeah.
- 1:07:02You start the recorder, you navigate through the webpage, you know, you do your workflow,
- 1:07:06you stop the recorder and then it turns all of the things that you did into a Playbride.
- 1:07:11Right.
- 1:07:11Instead of recording video, it records asynchronous Python.
- 1:07:14Basically.
- 1:07:16So that it generates.
- 1:07:17Now this is really cool and it's super easy.
- 1:07:19The other thing that's worth noting here is they talk about authenticated state.
- 1:07:24So if you've got to log in and set up some cookies and local storage, you know, like even if you've
- 1:07:28got a JavaScript app that needs to read and write stuff to local storage, you can sort
- 1:07:33of save and load that from a JavaScript, the JSON file rather.
- 1:07:36Yeah.
- 1:07:36So because that's another thing, particularly with test automation, how many times you have
- 1:07:40to log into your app under test, username, password, token or whatever.
- 1:07:44And so, I mean, you can do this with pretty much any browser automation tool.
- 1:07:48This is how Playbride does it.
- 1:07:49You're basically caching that authentication so that you can reuse it for different sessions
- 1:07:55or tests.
- 1:07:55So you don't have to burn that five seconds of log in every single time.
- 1:07:59Sure.
- 1:08:00If you're not trying to test the log in flow, it's just in your way.
- 1:08:03Correct.
- 1:08:03Yeah.
- 1:08:03Fantastic.
- 1:08:04I guess debugging tools.
- 1:08:05We could look real quick.
- 1:08:06I mean, I feel like we kind of touched a lot of them.
- 1:08:08Anything else you want to highlight?
- 1:08:09Maybe running in debug mode?
- 1:08:11I mean, it's all goodness.
- 1:08:14I would say go read the docs up on it.
- 1:08:15There's more than we can cover in a one hour discussion.
- 1:08:17Yeah.
- 1:08:18Well, I think we're down to minutes, not hours.
- 1:08:20So maybe leave it there.
- 1:08:23There's a bunch of guides about like auto-weighting, testing APIs, browser context, JavaScript events.
- 1:08:28Like there's a lot of stuff to go in here.
- 1:08:31And I'll check out even testing video.
- 1:08:33We're recording videos.
- 1:08:34Yeah.
- 1:08:35Cool.
- 1:08:35Fantastic.
- 1:08:36And for folks who want to learn more about Playwright, what I can recommend is that I have done a tutorial on Playwright in Python.
- 1:08:44It's on my GitHub account.
- 1:08:46It's playwright-python-tutorial.
- 1:08:49I have given this at PyCon.
- 1:08:51I gave this at Python Web Conference.
- 1:08:53I think I gave this at the TAU Homecoming last year.
- 1:08:56And in this repository, what it has, it has not only example code, but it has full instructions on how you build a test automation project with these tools.
- 1:09:04And it walks you through in glorious detail all the way.
- 1:09:07If this is something you want to get hands-on with, yep, there it is.
- 1:09:11Boom.
- 1:09:11Give that a try.
- 1:09:12Check it out.
- 1:09:13Follow the instructions in the tutorial folder.
- 1:09:15And let me know if you get stuck.
- 1:09:17Yeah, this is really great.
- 1:09:17People can follow along and play with it, right?
- 1:09:20Do a little DuckDuckGo.
- 1:09:21Basically.
- 1:09:22Yeah.
- 1:09:23No, but take some other public website like DuckDuckGo and figure out how you might go about testing it, right?
- 1:09:29Work your way through understanding what you should do to test it and then put that into action with Playwright.
- 1:09:34Yep.
- 1:09:34Yep.
- 1:09:35All right.
- 1:09:35Well, I think that pretty much is the time we've had to talk about this.
- 1:09:40Anything else you want to quickly touch on before we call it a wrap?
- 1:09:43No.
- 1:09:43I mean, have fun with it.
- 1:09:45I think Playwright is awesome.
- 1:09:46I really love what the team is doing.
- 1:09:47And if you ever have, if anyone ever has questions, all testing, automation, Playwright, Selenium, you name it, hit me up on Twitter, AutomationPanda.
- 1:09:55Right on.
- 1:09:56All right.
- 1:09:57Now, before we get out of here, I've got to ask the final two questions.
- 1:10:01If you're going to write some Python code, what editor do you use?
- 1:10:05Your tutorial may have given it away if people have seen that, but go ahead.
- 1:10:08VS Code.
- 1:10:09That's what I've been using recently.
- 1:10:10All right.
- 1:10:11Right on.
- 1:10:11And then notable PyPI package.
- 1:10:14I mean, pip install Playwrights, one of them.
- 1:10:16But something else you want to recommend, you're like, oh, this is cool.
- 1:10:18People should know about X.
- 1:10:19I mean, typically I would say pytest, right?
- 1:10:21But in fact, I just released a package on PyPI during PyCon.
- 1:10:27I got it done before the final closing keynote.
- 1:10:29Oh, nice.
- 1:10:29Okay.
- 1:10:30It's called Screenplay.
- 1:10:31It is a Pythonic implementation of the Screenplay pattern.
- 1:10:34It's very minimal right now.
- 1:10:36It doesn't even have documentation.
- 1:10:37But if anybody knows about Screenplay Pattern and wants to help contribute to that, check out Screenplay.
- 1:10:42Hold on.
- 1:10:42There it is.
- 1:10:43This one?
- 1:10:43Yeah.
- 1:10:45All right.
- 1:10:45I'll put that in.
- 1:10:46Put that in the show notes.
- 1:10:47Yeah.
- 1:10:47Very cool.
- 1:10:48Thank you.
- 1:10:48All right.
- 1:10:49Yeah, you bet.
- 1:10:50Well, it's been really great to have you here.
- 1:10:52Thanks for sharing all your experience with Playwright.
- 1:10:54It looks very exciting.
- 1:10:55It looks like something you just want to play with.
- 1:10:58Yeah, indeed.
- 1:10:58Well, thank you for inviting me to talk.
- 1:11:00I'd love to do it.
- 1:11:01Yeah, you bet.
- 1:11:01So people are excited to get started.
- 1:11:03Final call to action.
- 1:11:04What do you tell them?
- 1:11:05People are excited to get started with Playwright.
- 1:11:07Check out the website, playwright.dev.
- 1:11:10Check out my tutorial with the link.
- 1:11:12And also, if you ever get stuck, you can reach out to me or join the Playwright Slack.
- 1:11:17The folks in there are very helpful and very quick to respond.
- 1:11:20All right.
- 1:11:20Fantastic.
- 1:11:21Well, thanks again for being here.
- 1:11:23It's great to chat with you.
- 1:11:24Thanks, man.
- 1:11:24Appreciate it.
- 1:11:25Yeah.
- 1:11:25You bet.
- 1:11:25Bye.
- 1:11:26This has been another episode of Talk Python To Me.
- 1:11:31Thank you to our sponsors.
- 1:11:32Be sure to check out what they're offering.
- 1:11:34It really helps support the show.
- 1:11:35Starting a business is hard.
- 1:11:37Microsoft for Startups, Founders Hub, provides all founders at any stage with free resources
- 1:11:43and connections to solve startup challenges.
- 1:11:46Apply for free today at talkpython.fm/founders hub.
- 1:11:50Listen to an episode of Compiler, an original podcast from Red Hat.
- 1:11:55Compiler unravels industry topics, trends, and things you've always wanted to know about
- 1:12:00tech through interviews with the people who know it best.
- 1:12:03Subscribe today by following talkpython.fm/compiler.
- 1:12:07Want to level up your Python?
- 1:12:09We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:12:13Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:12:18And best of all, there's not a subscription in sight.
- 1:12:21Check it out for yourself at training.talkpython.fm.
- 1:12:24Be sure to subscribe to the show.
- 1:12:25Open your favorite podcast app and search for Python.
- 1:12:29We should be right at the top.
- 1:12:30You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:12:35and the direct RSS feed at /rss on talkpython.fm.
- 1:12:39We're live streaming most of our recordings these days.
- 1:12:42If you want to be part of the show and have your comments featured on the air,
- 1:12:46be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:12:51This is your host, Michael Kennedy.
- 1:12:52Thanks so much for listening.
- 1:12:53I really appreciate it.
- 1:12:54Now get out there and write some Python code.
- 1:12:56Thank you.