A new generation of automated testing tool Playwright

When it comes to WebUI automated testing, Selenium is the first to bear the brunt. For a long time, Selenium has dominated Web automation. Selenium has actually gone through four stages, from Selenium 1.0 released in 2006 to the latest Selenium 4.8.3.

  • In 2006, Selenium 1.0 was released

    Selenium 1.0 includes Selenium IDE (browser plug-in, which can help us record and generate scripts. It is a very good start for students who do not know how to code, but it is basically eliminated now), Selenium Grid (distributed, will A set of scripts are distributed to different machines for execution. They are very powerful and are still in use today.

    In the same year, Google engineer Simon Stewart launched a project called WebDriver, which was also an automated testing tool. It was just starting out at that time, and later it became one of Selenium's competitors.

  • In 2009, Selenium 2.0 was released

    In 2009, at the Google Test Automation Conference, developers from the Selenium and WebDriver teams decided to merge the two projects after communicating. The new project was named Selenium Web Driver, which is Selenium 2.0. Many people come into contact with Selenium, and they started with Selenium 2.0. The implementation principle of WebDriver is actually to have a WebDriver between Web浏览器and 我们的脚本to drive and operate the browser through the WebDriver protocol

    The author of WebDriver explained the reason for the merger of the two: "On the one hand, WebDriver solves the shortcomings of Selenium (for example: it can bypass the JavaScript sandbox, and WebDriver has an excellent API). On the other hand, Selenium solves the problems of WebDriver. Also, the main contributors to Selenium and the authors of WebDriver both believe that merging the projects is the best way to provide users with the best framework."

    Selenium 2.x (WebDriver) really started to rise in 2014 and became the most popular framework for web automation by around 2016. When it comes to web automation, it's Selenium. It is not only popular in the field of web automation testing, but also very popular in the field of crawlers.

  • In 2016, Selenium 3.0 was released

    This version does not introduce new tools, but mainly strengthens browser support.

    Selenium RC is completely removed; WebDriver exposes an API for browser access, which is accessed through the Driver provided by each browser manufacturer; the built-in Firefox Driver is eliminated; Firefox is supported to access Selenium through the GRCKO Driver; provided by Apple Safari Driver to access Safari; support IE access through Edge Driver.

    Therefore, there is actually not much difference between 3.x and 2.x.

  • In 2021, Selenium 4.0 is released

    In Selenium 3.x, communication with the browser is based on the JSON-wire protocol, so Selenium needs to encode and decode the API. Selenium 4 follows the W3C standard protocol, and the standardization of communication between the Driver and the browser allows them to communicate directly.

Since Selenium has not changed much in the two iterations of 3.x and 4.x, Selenium's dominance has become less stable due to the emergence of new frameworks.

Cypress, TestCafe, and Puppeteer, which appeared around 2020, are known in the technology radar as the troika of Web UI automation in the post-Selenium era. However, since these three frameworks are all developed based on JavaScript, testers need to be familiar with JavaScript, and none of them support Python, so the usage rate is not very high.

Introduction to Playwright

In 2020, Microsoft open sourced a tool called Playwright . It is as easy to get started as Selenium and supports multiple languages ​​(Python, Java, Node.js, .NET). It is produced by a major manufacturer and must be a high-quality product. When it first appeared, it was not very popular. As time went by, by around 2022, Playwright had been noticed and accepted by more people, and it was even better and more powerful than Selenium.

Playwright is a testing technology positioned end-to-end (End-to-End), which is used to test whether the entire application process meets expectations, simulating real user usage scenarios. To achieve this goal, the system usually A testing tool that is considered a black box, also known as black box testing, which operates mainly through public interfaces such as GUI and API).

Although Selenium is still the leader in domestic web automation testing, many foreign companies are already using Playwright, such as Adobe, some of Microsoft's own projects (Visual Studio Code, Bing), Disney Hostar, etc.

Since there is already a very mature and easy-to-use Selenium framework on the market, and it is continuously updated, and the community is also very active, why does Microsoft need to develop an additional automated testing tool?

The positioning of the two tools is different. Since Playwright is a later tool, it can handle some details better.

  • cross browser

    Although Selenium provides compatibility and support for almost all browsers, you need to download an additional driver for the corresponding browser version, otherwise Selenium WebDriver may not start. Playwright has built-in various browsers and browser drivers (supports all modern rendering engines, including Chromium, Firefox and WebKit), and does not require additional upgrades and management, making the entire automated testing project more stable.

  • automatically wait

    When using Selenium, we usually add various waits to the code based on test requirements. However, there is no so-called implicit wait in Playwright, and the explicit wait has been encapsulated and can be called directly.

  • Web first assertion

    Playwright will automatically make assertions based on the network environment until a certain condition is met.

  • track

    Playwright can easily configure retry strategies, automatically track results, and record them in the form of screenshots and screen recordings.

  • Parallel execution

    In Selenium, the execution of test cases is single-threaded, but in Playwright, due to the different running mechanisms (Selenium's instructions are sent through the http protocol, while Playwright uses the socket protocol; Playwright manages the browser through context, which is equivalent to Each test case will create an independent context. The browser context is actually a brand new browser. The advantage of this method is that it can speed up while also achieving isolation between tests, making the test results more accurate). So parallel execution can be achieved.

  • Powerful toolset

    Playwright provides us with a powerful set of tools, such as: script recording tool codegen, script writing and debugging tool playwright, etc.

First experience with Playwright

Take the Python language as an example to write the first Playwright script.

PS: Playwright requires Python >=3.7 version

Install playwright

pip install playwright

Install built-in browser

Use the following commands to install the various browsers that come with Playwright:

playwright install

Since there is a lot of content to install, this step will take some time.

First Playwright script

1. Start using the with mode
# author: 测试蔡坨坨
# datetime: 2023/4/8 2:18
# function: 第一个playwright脚本,使用with写法

from playwright.sync_api import sync_playwright, expect


def run(playwright):
    chromium = playwright.chromium  # or "firefox" or "webkit".
    browser = chromium.launch(headless=False)  # headless表示是否使用无头浏览器(也就是无GUI模式)
    page = browser.new_page()
    page.goto("https://caituotuo.top")
    # other actions...
    print(page.title())
    browser.close()


with sync_playwright() as playwright:
    run(playwright)

# 这里使用的是with方法,Python中的with方法可以很方便处理一些需要提前设置,事后需要清理的工作
# playwright正好有上下文处理,所以使用with写法会使代码更加简洁
# 比如:
# with open("/caituotuo.txt") as f:
#     f.read()
#
# 非with方式可能存在问题:1.可能忘记关闭文件句柄 2.文件读取数据时发生异常,但是没有进行任何处理
# f = open("/caituotuo.txt")
# data = f.read()
# f.close()
2. Start using start() method
# author: 测试蔡坨坨
# datetime: 2023/4/8 2:20
# function: 使用start()写法,直接实例化playwright同步方法

from playwright.sync_api import sync_playwright

playwright = sync_playwright().start()
chromium = playwright.chromium  # or "firefox" or "webkit".
browser = chromium.launch(headless=False)  # headless表示是否使用无头浏览器(也就是无GUI模式)
page = browser.new_page()
page.goto("https://caituotuo.top")
# other actions...
print(page.title())
browser.close()

Summarize

Selenium and Playwright are both popular automated testing tools. Both have rich functions and APIs, and both can be used for automated testing of web applications. When choosing a testing framework, you must consider the scenarios in daily work and the current team, and then choose the appropriate automated testing tool for testing. Rather than using a new fancy framework and then discarding it after a while because it doesn't meet our needs.

Of course, there is nothing better than mastering multiple technology stacks at the same time. As the saying goes, "when soldiers come, there will be obstacles, and when water comes, soil will cover up." No matter which tool is chosen in the enterprise, you can get started quickly, which is also a reflection of your own ability.

The most important thing is to take action, write more and practice more, and over time, you will naturally become like a fish in water, and truly implement automated testing in the enterprise, bringing about corresponding improvements in efficiency and quality assurance.

 


Finally, I will share with you the documents and learning materials that I have accumulated and truthful. If necessary, you can just pick them up. The above content should

be the most comprehensive and complete preparation warehouse for software testing friends. In order to better organize it For each module, I also referred to many high-quality blog posts and projects on the Internet, trying not to miss any knowledge point. Many friends relied on these contents to review and got offers from major manufacturers such as BATJ. This warehouse has also helped a lot. As a learner of software testing, I hope it can also help you.

Follow my WeChat official account below to get it for free! ↓ ↓ ↓ ↓ ↓

Guess you like

Origin blog.csdn.net/m0_53918927/article/details/132840793