如何给dusk套个代理嗅探网页中的长视频m3u8

Sometimes it is necessary to check the validity of HTTP requests that are sent in the background while a certain flow is being made in the application: Was the right data sent in the request? Did some services return errors? In this guide I will explain how to integrate network traffic tests into selenium tests. The guide is written for c# language but can be used in any other language you choose.

Prerequisites: Basic knowledge in Docker-Compose , Selenium and Linux.

First we need to run a container with a BrowserMob proxy server, our Selenium WebDriver instance will use it to establish a proxy connection:

  1. Open terminal and create a docker-compose.yml file:
nano docker-compose.yml

2. Add to the .yml file:

version: "3.3"

services:

  proxyserver:
    image: qautomatron/docker-browsermob-proxy
    ports:
     - "9090:9090"
     - "32000-33000:32000-33000"
    environment:
     - BMP_PORT=9090
     - PORT_RANGE=32000-33000

3. Save, Exit and the run following command:

docker-compose up -d

4. On your terminal, run the following command:

curl http://localhost:9090/proxy 

You should get following response :

{"proxyList":[]}

It shows us the available ports which we can use in order to set a proxy connection. It's empty since we haven't yet added any port to it.

5. Let's add a port using an empty POST request:

curl -X POST -d "port:32001" http://localhost:9090/proxy

You should get following response:

{"port":32001}

Now let's execute another GET request to our proxy server in order to validate our port was added:

curl http://localhost:9090/proxy 

We should get following response:

{"proxyList":[{"port":32001}]}

6. In your Selenium tests create an instance of WebDriver and connect to proxy:

ChromeOptions chromeOptions = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.HttpProxy = "http://localhost:32001";
chromeOptions.Proxy = proxy;
IWebDriver driver = new ChromeDriver(chromeOptions);

7. Execute an empty PUT request in order to create a new HAR file. All the network traffic will be saved in this HAR file. HAR or HTTP Archive format , is a JSON-formatted archive file format for logging of a web browser's interaction with a site. The common extension for these files is .har:

curl -X PUT http://localhost:9090/proxy/32001/har

8. In order to get the network traffic data execute a GET request to:

curl http://localhost:9090/proxy/32001/har

The above request returns the JSON/HAR content representing all the HTTP traffic passed through the proxy. The HAR file should look like this:

breeze 文档

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

赞赏支持
被以下专题收入,发现更多相似内容