在Robot Framework中记录HTML请求

2 投票
2 回答
4860 浏览
提问于 2025-04-20 07:25

我一直在努力寻找相关的信息,所以我来这里寻求帮助。

我正在用机器人框架对一个网页应用进行用户界面测试。当测试失败时,我想要记录下HTML请求的日志,这样我可以回头看看哪里出问题了,比如说某些东西没有加载、500错误等等。

到目前为止,我还没有在机器人框架或selenium中找到相关的功能。

另一个选择是看看是否有Python库可以用来记录这些信息,或者自己创建一个这样的库是否可行。

我也考虑过使用autoit来利用浏览器的内部网络日志工具,但使用这些工具本身就是一个大工程,我不太确定效果如何。我相信我不是第一个想要这个功能的人。

我继续研究这个问题,发现一个可行的选项可能是使用pcapy这个数据包嗅探器,但我对网络编程一窍不通,也不知道如何处理数据包,只获取POST和GET请求及其响应。如果有人能提供帮助,我将非常感激。

谢谢!

2 个回答

2

我使用了BrowserMobProxy来实现相同的功能。它可以根据测试需求捕获网络流量。

第一个函数CaptureNetworkTraffic()会根据传入的参数打开浏览器。

第二个函数Parse_Request_Response()会从上面的函数获取HAR文件,并根据配置的参数返回相应的网络数据。

比如:

print Capture_Request_Response("g:\\har.txt","google.com",True,True,False,False,False)

在这个例子中,它会检查网址是否是"google.com",并返回该网址的请求和响应头信息。

    from browsermobproxy import Server
    from selenium import webdriver
    import json

    def CaptureNetworkTraffic(url,server_ip,headers,file_path):
    ''' 
    This function can be used to capture network traffic from the browser.                       Using this function we can capture header/cookies/http calls made from the      browser
url - Page url
    server_ip - remap host to for specific URL
    headers - this is a dictionary of the headers to be set
    file_path - File in which HAR gets stored
    '''
    port = {'port':9090}
    server = Server("G:\\browsermob\\bin\\browsermob-proxy",port) #Path to      the BrowserMobProxy
    server.start()
    proxy = server.create_proxy()
    proxy.remap_hosts("www.example.com",server_ip)
    proxy.remap_hosts("www.example1.com",server_ip)
    proxy.remap_hosts("www.example2.com",server_ip)
    proxy.headers(headers)
    profile  = webdriver.FirefoxProfile()
    profile.set_proxy(proxy.selenium_proxy())
    driver = webdriver.Firefox(firefox_profile=profile)
    new = {'captureHeaders':'True','captureContent':'True'}
    proxy.new_har("google",new)
    driver.get(url)
    proxy.har # returns a HAR JSON blob
    server.stop()
    driver.quit()
    file1 = open(file_path,'w')
    json.dump(proxy.har,file1)
    file1.close()


def Parse_Request_Response(filename,url,response=False,request_header=False,request_cookies=False,response_header=False,response_cookies=False):
resp ={}
har_data = open(filename, 'rb').read()
har = json.loads(har_data)
for i in har['log']['entries']:
    if url in i['request']['url']:
        resp['request'] = i['request']['url']
        if response:
            resp['response'] = i['response']['content']
        if request_header:
            resp['request_header'] = i['request']['headers']
        if request_cookies:
            resp['request_cookies'] = i['request']['cookies']
        if response_header:
            resp['response_header'] = i['response']['headers']
        if response_cookies:
            resp['response_cookies'] = i['response']['cookies']
return resp


  if (__name__=="__main__"):
  headers = {"User-Agent":"Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X)  AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3"}

    CaptureNetworkTraffic("http://www.google.com","192.168.1.1",headers,"g:\\har.txt")
    print Parse_Request_Response("g:\\har.txt","google.com",False,True,False,False,False)
2

Selenium 只是模拟用户的操作,所以在这个情况下它帮不了你。你可以使用一个代理工具,它可以记录所有的网络流量,让你查看这些流量。BrowserMob Proxy 就可以做到这一点。你可以查看 Selenium2Library 中的创建 Webdriver,了解如何为你的浏览器配置代理。

这样,当你在测试中发现问题时,可以让代理工具返回相关的网络流量。

撰写回答