【Test】playwright Study Notes- 02

At the end of the last class, I finally downloaded the comprehensive driver of playwright. It is said that in the future, there is no need to find different drivers because of using different browsers, and to distinguish between various version numbers. Received unanimous praise....

    After the first session last time, I received a lot of updates and suggestions from my friends, so this tutorial, as a supplement to other official tutorials on the market, decided to use the test practice program to teach, that is, to actually implement some automation. Write automated scripts, continuously test out various knowledge points (guess-test-summary-retest confirmation), combined with the dictionary tutorial on the official website, everyone is not only easy to learn and vivid, but also impressed.

    I went to learn more about playwright before and found some new and useful information:

  • Playwright is not a product of a small workshop, but a new generation of lightweight automation framework launched by Microsoft. Therefore, the subsequent updates, development and ecological construction of this product can be guaranteed, and everyone can rest assured to learn without thinking.

  • From the description, playwright is quite friendly to python, and it can even be said that it is tailor-made for python, and it itself fully complies with python's low-threshold, fast and simple style. The friends who learn python have made a fortune~

  • Playwright supports recording operations. Compared with selenium's recording, this recording is more reliable, simpler and more stable, etc., but I need to test it myself to know the specific effect. Don't look at the advertisement to see the effect. However, the field of recording automation has always been discriminated against by big guys because of various garbage effects. I hope that playwright can change this phenomenon in one fell swoop.

Next, we have to experience the specific recording effect. Test website, a small promotional page of mine: woqurefan.cn

According to the official website tutorial: enter the command in cmd or terminal to start.

python3 -m playwright codegen -o 'test_526.py' -b chromium http://woqurefan.cn/

Among them, python3 -m playwright codegen is a fixed way of recording commands.

-o 'filename' means to save the code as a file.

-b drive URL means which browser is used to open which page, you don’t need to write the URL, and it will be recorded if you manually enter it.

After execution, the effect is that a Google browser is automatically opened and the website page is entered, and a code editing page pops up at the same time:

And the page automatically enters the debugging mode, and when the mouse hovers over any element, the acquisition and positioning code of the element can be displayed directly.

When I clicked this button, a magical scene happened. This line of code was automatically added to the generated code edit box:

Then I tried to click the interview question entry button again, and no surprises, this code was automatically generated again:

Then I randomly clicked on an interview question:

You can see that the code is generated again, but this time the positioning is to use text copywriting. As for why text copy is used instead of specific name and other attributes, please wait for the specific test in our next article.

After I close this page, I can see that the code just now has been saved:

After opening it with pycharm, let's study this demo script carefully.

Line 1 is imported from this module called sync_api. The name reads as a synchronous interface. Thinking about it this way, then palywright should also support asynchronous mode. It seems that we can study the asynchronous mode later.

Then there is a def run function and a main program code block with. The with code block is only responsible for calling the run function. If there is any code that needs to be set up at the beginning, you can try to put it in this with code block, such as calling the run function concurrently to start multiple browser concurrent tests.

Inside the run function, a variable broswer is defined first, the purpose should be to define the driver and headed mode. From this point of view, playwright should also support headless mode, and this line of code can be modified to start different drivers. Then if we want to do multi-browser concurrent compatibility testing in the future, we can start from the browser line (everyone should always learn and think by analogy like this)

Line 6 is a context variable declaration, and it is implemented by a certain function of the browser. Then I can assume that context is an instance of browser. It's as if the browser is a drawing, and the context is an actual product made according to this drawing. After that, we need to test, if there are multiple declarations, is it launching multiple browser windows or multiple tabs under one browser.

Line 7 is the page variable declaration, relying on the creation of context, it looks like a new page has been created by looking at the English name. And the following codes are all page.xxxxx, which looks a lot like the driver of selenium, but I have some doubts at the moment, the driver can control multiple pages and multiple tabs, and this page sounds like it can only be in the current tab page to operate. This will require our follow-up tests to know. I won't announce the answer in advance, otherwise I won't be impressed.

Line 8 is the page jump, goto function.

Lines 9-11 are specific positioning and operations, and the form is exactly the same as selenium. Two methods learned so far: get_by_role("tag name",name="") get_by_text("specific copywriting") One operation learned: .click() click.

Line 12 is the closing page

Line 15 is to close the context instance

Line 16 is to close the browser instance

The above three closures seem to be more troublesome. In our actual work, we need to choose the degree of closure according to our needs. In pytest or unitest, these closures should also be placed in appropriate positions to allow multiple use cases to execute normally. Especially in the mode of concurrent series mixed asynchronous, more attention should be paid to these three closures. Later we will write a separate article to test the specific effects and principles of the three closures.

Well, that's the end of today's lesson, you don't have to try it yourself, this tutorial is enough.

Guess you like

Origin blog.csdn.net/qq_22795513/article/details/131205427