Web automation testing framework Selenium

What is automated testing

Automated testing is testing done automatically by machines assuming some preset values.

Web automated testing applies automated testing technology to web testing. It assumes some preset values and uses the program to drive the browser to complete the testing of web programs, as shown below:

Selenium
There are many technical means and frameworks for web automation testing. Here we mainly introduce Selenium.

Selenium official website 4

Selenium is a tool for automated testing of web applications. Selenium tests run directly in the browser, just as the user would.

What we usually call Selenium is the general term for Seleninum, which includes three projects, namely:

1. Selenium WebDriver
2. Selenium IDE
3. Seleninum Grid

In subsequent related articles we will only focus on Seleninum WebDriver

Selenium WebDriver
Selenium webdriver is actually a code base that can control the browser based on the implementation of the W3C WebDriver protocol in different languages. It provides a simple API interface so that testers can easily control various behaviors of the browser.

Several ways to run automation
Operating mechanism: webdriver sends execution commands to the browser through the driver (browser driver); the same browser returns information to the webdriver through the same channel after executing the play command.

The whole thing is a C/S architecture, and our webdriver exists as a client.

This way is Seleninum Webdriver and browser driver and browser are on the same host

Of course, we can also separate the webdriver to another server and then automate it through Remote.


We can also implement remote connections through Seleninum Server or Seleninum Grid to conduct distributed testing.

Environment installation

Python

Selenium

Browser driver: Related address 7

Simple example

from selenium import webdriver
import time
 
driver = webdriver.Chrome(executable_path='./chromedriver')
driver.maximize_window()
driver.get('https://www.baidu.com')
driver.find_element_by_id('kw').send_keys('python')
driver.find_element_by_id('su').click()
time.sleep(2)
driver.quit()

Code analysis:
In fact, the core code for instantiating the driver is this section. We can enter the source code through the IDE and see that the Service is first established. In fact, it is to run the browser driver through the command line.
self.service = Service( executable_path, port=port, service_args=service_args, log_path=service_log_path) self.service.start()
After the service is started, a connection is created to perform subsequent operations.
try: RemoteWebDriver.__init__( self, command_ executor=ChromeRemoteConnection( remote_server_addr=self.service.service_url, keep_alive=keep_alive), desired_ capabilities=desired_capabilities) except Exception: self.quit() raise

As we said when we introduced Selenium, Selenium Webdriver actually implements The WebDriver Wire Protocol, and the protocol is actually some HTTP-related requests. We can view the specific information here 1.

Next we use requests to achieve the same function. First we need to start the Service. Here we can use Selenium Server instead, or we can directly run browser drivers such as chromedriver. Here I use Selenium to complete.

First download selenium-server-standalone-{VERSION}.jar, download address 1

then start

java -jar selenium-server-standalone-{VERSION}.jar

After startup, the console will print us the relevant service address.

At this time, we go to the browser to visit this address: http://127.0.0.1:4444/wd/hub

I will be automatically redirected here

We click Create Session. After the creation is successful, we will find that a browser is launched.

In fact, if you open the chrome inspection panel and find that a Post request is actually sent, and the relevant information is the same as the protocol, then we will use postman to call these interfaces to see the effect.

When sending these requests, the browser is making corresponding changes

Summary:

Thank you to everyone who reads my article carefully! ! !

As someone who has experienced it, I also hope that everyone will avoid some detours. If you don’t want to experience the feeling of not being able to find information, no one to answer questions, and giving up after persisting for a few days while studying, here I will share with you some learning about automated testing. Resources, I hope they can help you on your way forward.

How to obtain documents:

Join my software testing exchange group: 632880530 Get it for free~ (Academic exchanges with fellow experts, and live broadcasts every night to share technical knowledge points)

This document should be the most comprehensive and complete preparation warehouse for friends who want to engage in [software testing]. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!

All of the above can be shared. You only need to search the vx official account: Programmer Hugo to get it for free