Hi, I’m Boy.
Those of you who have ever played with a crawler know Selenium, a powerful tool for automated testing. Writing Python automated scripts to free your hands is pretty much routine. If a crawler can’t crawl, use automated tests.
Although Selenium is well documented, it still requires a certain learning cost, which is a bit of a threshold for a pure beginner.
Microsoft recently opened source a project called “Play-Python,” which is awesome. This project is a pure automation tool for Python language, even without writing code, can achieve automation functions.
You might think it’s crazy, but it’s amazing. Let’s take a look at this artifact.
1. The Playwright is introduced
Ourselves is a powerful Python library that automates Chromium, Firefox, WebKit and other major browser automation with a single API, and supports both headless and header modes.
Offender’s automation technology is green, powerful, reliable and fast, and supports Linux, Mac and Windows operating systems.
2. The Playwright
The installation
The offender’s installation is simple, a two-step process.
The offender may be the same ourselves. The offender may be the same ourselvesCopy the code
The above two PIP operations are installed separately:
-
Python3.7+ is required to install the Ourselves Dependent library
-
Install drivers for Chromium, Firefox, and WebKit browsers
The recording
Instead of writing a line of code, we manually manipulated the browser, which recorded our actions and then automatically generated a code script.
Here is the recorded codeGen command, just one line.
Type --help to see all options
python -m playwright codegen
Copy the code
Codegen can be seen using –help, or simply add the url to the command, or add options if necessary.
python -m playwright codegen --help
Usage: index codegen [options] [url]
open page and generate code for user actions
Options:
-o, --output <file name> saves the generated script to a file
--target <language> language to use, one of javascript, python, python-async, csharp (default: "python")
-h, --help display help for command
Examples:
$ codegen
$ codegen --target=python
$ -b webkit codegen https://example.com
Copy the code
The options:
-
-o: Saves the recorded script to a file
-
–target: specifies the language in which the script is generated. It can be JS or Python. The default language is Python
-
-b: Specifies the browser driver
For example, I’m going to search baidu.com, use Chromium drive, and save the results as my.py python file.
python -m playwright codegen --target python -o 'my.py' -b chromium https://www.baidu.com
Copy the code
Command line input automatically opens the browser, and you can see that every action in the browser is automatically translated into code, as shown below.
Automatically close the browser and save the generated automatic script to the py file.
from playwright import sync_playwright def run(playwright): browser = playwright.chromium.launch(headless=False) context = browser.newContext() # Open new page page = context.newPage() page.goto("https://www.baidu.com/") page.click("input[name=\"wd\"]") page.fill("input[name=\"wd\"]", # click //a[normalize-space(.)=' jingdong JD.COM '] with "jingdong") page.click("text=\" jingdong" ") # click //a[normalize-space(.)=' jingdong JD.COM '] with page.expect_navigation(): with page.expect_popup() as popup_info: Page. Click (" / / a [normalize - space (.) = 'jingdong how fast JD.COM website to save Only for quality life '] ") page1. = popup_info value # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - context.close() browser.close() with sync_playwright() as playwright: run(playwright)Copy the code
In addition, the offender provides both synchronous and asynchronous apis, as documented below.
Link: Microsoft. Making. IO/playwright -…
synchronous
The following sample code: open three browsers in turn, go to Baidu to search, screenshot and exit.
from playwright import sync_playwright
with sync_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = browser_type.launch()
page = browser.newPage()
page.goto('https://baidu.com/')
page.screenshot(path=f'example-{browser_type.name}.png')
browser.close()
Copy the code
asynchronous
Asynchronous operations can be combined with asyncio to perform three browser operations simultaneously.
import asyncio
from playwright import async_playwright
async def main() :
async with async_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = await browser_type.launch()
page = await browser.newPage()
await page.goto('http://baidu.com/')
await page.screenshot(path=f'example-{browser_type.name}.png')
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
Copy the code
The mobile terminal
Better yet, the offender can also be used for mobile browser emulation. Here’s a snippet of code from the official document that simulates Safari on the iPhone 11 Pro in a given geographic location, first navigating to maps.google.com, then performing the location and taking a screenshot.
from playwright import sync_playwright
with sync_playwright() as p:
iphone_11 = p.devices['iPhone 11 Pro']
browser = p.webkit.launch(headless=False)
context = browser.newContext(
**iphone_11,
locale='en-US',
geolocation={ 'longitude': 12.492507.'latitude': 41.889938 },
permissions=['geolocation']
)
page = context.newPage()
page.goto('https://maps.google.com')
page.click('text="Your location"')
page.screenshot(path='colosseum-iphone.png')
browser.close()
Copy the code
In addition, it can also be used with the PyTest plug-in, you can try it yourself.
3. Summary
Ourselves has many advantages over established automated testing tools, such as:
- Supports Chromium, Firefox, and WebKit across browsers
- Supports Linux, Mac, and Windows operating systems
- Can provide recording code generation function, free hands
- It can be used on the mobile end
The current disadvantages are that the ecology and documentation are not very complete, such as no API Chinese documentation, no good tutorials and examples to learn. However, I believe that as more and more people know, the future will be better and better.
GitHub link: github.com/microsoft/p…
Open source organization: Microsoft
Feel free to like, view and share.
Open source project selection, follow my original public account: GitHuboy