Use 3000 words to realize Postman automated testing from 0 to 1UI

"It takes about 4 minutes to read this article.
Isn't Postman doing interface testing? Why can it still do UI automation testing?

In fact, as long as you understand the operating principle of Selenium, you can understand why Postman can also implement UI automation testing.

The underlying principle of Selenium
runs the code. After the browser is started, the webdriver will bind the browser to a specific port as the remote server (remote server) of the webdriver, and the client (client, that is, the test script, can be Python or Java code) will create a sessionId with the help of CommandExecutor, send an http request to the remote server, and after the remote server receives the http request, it will call webdriver to complete the operation, and return the http response result to the client.

Therefore, it is essentially the process of calling http requests, so it can be understood why Postman can be used to implement UI automation testing.

Postman implements UI automation testing
Above we know that the underlying principle of Selenium is actually the process of calling http requests, then we need to know the interface information if we want to call the interface, including request method, request address, request parameters, request format, etc.

We can get these interface information by analyzing the source code.

run chromedriver.exe

Selenium script:

  1. from selenium import webdriver

  2. driver = webdriver.Chrome()

Execute the above code, and the program will open the Chrome browser. (Prerequisite: The Chrome driver and the corresponding version have been correctly configured)

 

So, how does Selenium implement this process?

Source code analysis:

D:\Python3\Lib\site-packages\selenium\webdriver\chrome\webdriver.py

We can see that it executes a cmd command, which is mainly to start the chromedriver.exe browser driver. Before we execute the script every time, the program will automatically start the browser driver for us.

Since we skipped the code script, the browser driver needs to be started manually.

Address and port number:127.0.0.1:9515

New browser session

D:\Python3\Lib\site-packages\selenium\webdriver\remote\webdriver.py

Continue to view the source code, here is an important line of code:

 

The start_session() method http://127.0.0.1:9515/sessionsends a post request to the address, the parameter is in JSON format, and then returns a specific response message to the program, mainly creating a new sessionId.

Interface information:

url: /session
method: POST
content_type: application/json

Request parameters:

{
    "capabilities": {
        "browserName": "chrome"
    }
}

 

Call interface:

visit target site

Selenium script:

driver.get("https://www.baidu.com")

 

Execute the above code to access the target website.

Source code analysis:

D:\Python3\Lib\site-packages\selenium\webdriver\remote\remote_connection.py

In the RemoteConnection class, the interface addresses required for all selenium operations are defined (these interface addresses are all encapsulated in the browser driver).

The Command.GET: ("POST", "/session/$sessionId/url")address is the URL to access a website.

Next, it can be seen that the _request() method is mainly called through the execute() method to send the corresponding operation request address to the server through the urllib3 standard library, and then various operations of the browser are realized.

Opening the browser and operating the browser to implement various actions are associated through the sessionId returned by the newly created browser session in the previous step. You will also find that there is a $sessionId in the various interface addresses for subsequent operations, so as to be able to operate in the same browser.

 

Interface information:

url: /session/$sessionId/url

method: POST

content_type: application/json

Request parameters:

 

{
    "url": "目标网站地址"
}

 

maximize window

Selenium script:

driver.maximize_window()

Source code analysis:

Interface information:

url: /session/$sessionId/window/maximize
method: POST
content_type: application/json

 

Call interface:

element positioning

Selenium script: 

 

driver.find_element(By.XPATH, "//input[@id='kw']")

 

Source code analysis:

Interface information:

url: /session/$sessionId/element
method: POST
content_type: application/json


Request parameters:

{
    "using": "xpath", // 定位方式
    "value": "//input[@id='kw']" // 值
}


Interface call:

enter text

Selenium script:

driver.find_element(By.XPATH, '//input[@type="text"]').send_keys("测试蔡坨坨")

Source code analysis:

 

Interface information:

  1. url: /session/$sessionId/element/$id/value
    
    method: POST
    
    content_type: application/json

Request parameters:

  1. {
    
        "text": "测试蔡坨坨"
    
    }

Interface call:

 

click element

Selenium script:

driver.find_element(By.XPATH, "//input[@id='su']").click()

Source code analysis:

Interface information:

  1. url: /session/$sessionId/element/$id/click
    
    method: POST
    
    content_type: application/json

Interface call:

 

close browser

Selenium script:

driver.quit() 

 

Source code analysis:

Interface information:

url: /session/$sessionId
method: DELETE
content_type: application/json

 

Interface call:

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive 

 

Guess you like

Origin blog.csdn.net/kk_lzvvkpj/article/details/130093235