当前位置:网站首页>How to use mitmproxy to get data return in automated testing?

How to use mitmproxy to get data return in automated testing?

2022-07-24 00:43:00 TEST_ Two black

Background introduction

When we are at the interface or UI In automation projects , This phenomenon often occurs —— It's obviously a normal request , But can't get the desired data return .

such as :

  • scene A: Pages are dynamic data , The data obtained by entering the page for the first time , It is completely different from the data obtained next time you enter the page .

  • scene B: Page data has display restrictions , Some modules of the page or the entire page data are only displayed 3 Time , And the server returns the control .

What should we do at this time ?

If it is a manual test , We can use it directly Charles、Fiddler To simulate , After all, agent software can basically solve most of the testing needs .

What if it's in an automation project ? If you still use agent software —— In fact, it's not completely impossible , But we have better ways to practice .

That is to use MitmProxy Act as a forward proxy to intercept requests and return data .

How to use MitmProxy As a forward agent

Realization Maplocal

MitmProxy It's a tripartite library , We can regard it as Charles To use .

Here to achieve Maplocal Function description :

(1)Maplocal function , The simple understanding is when specific requests , Return specific data .

For example, request "www.baidu.com", Normal should return to :

 Insert picture description here
But we modify the returned data , As long as there is "www.baidu.com" Please come here , Just let it return automatically :
 Insert picture description here
This is the most basic maplocal Case study .

(2) stay MitmProxy How to realize Maplocal What about function? ?

Here we use get Request the implementation to explain :

get.json The configuration file , Record all that need to be intercepted here get request :

MitmProxy Script :

 Insert picture description here
In the executed script ( Like local shell or Other implemented py Script ):
 Insert picture description here
After starting the script , Connect MitmProxy All requests from devices will be monitored by scripts .

If appear "www.baidu.com" Request , It will come to get.json Find the corresponding in json, Automatically return to the device .

such , It realizes the simplest maplocal.

Of course, based on the current MitmProxy Script , We can also expand some functions .

For example, for the interface type get or post request , For request parameter type json or x-www-form-urlencoded, Can be extended more .

Send learning resources to

 Insert picture description here
These materials , For doing 【 software test 】 It should be the most comprehensive and complete war preparation warehouse for advanced friends , This warehouse also accompanied me through the most difficult journey , I hope it can help you ! Everything should be done as soon as possible , Especially in the technology industry , We must improve our technical skills . I hope that's helpful …….

原网站

版权声明
本文为[TEST_ Two black]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/205/202207240035048420.html