【发布时间】:2019-05-07 17:47:10
【问题描述】:
我使用 Watir 正确设置了 BrowserMob 代理,它正在捕获流量并保存 HAR 文件;但是,它没有做的是它没有连续捕获流量。所以以下是我想要实现的目标:
- 转到主页
- 点击链接转到另一个页面,我需要等待一些事件发生
- 进入第二页后,在事件发生后开始捕获流量并等待特定调用发生并捕获其内容。
然而,我注意到的是,它遵循了上述所有步骤,但在第 3 步中,代理甚至在该页面上进行调用之前就停止了捕获流量。返回的 HAR 中没有该调用,因此测试在它完成工作之前就失败了。以下是代码的样子。
class BMP
attr_accessor :server, :proxy, :net_har, :sel_proxy
def initialize
bm_path = File.path(Support::Paths.cucumber_root + "/browsermob-
proxy-2.1.4/bin/browsermob-proxy")
@server = BrowserMob::Proxy::Server.new(bm_path, {:port => 9999,
:log => false, :use_little_proxy => true, :timeout => 100})
@server.start
@proxy = @server.create_proxy
@sel_proxy = @proxy.selenium_proxy
@proxy.timeouts(:read => 50000, :request => 50000, :dns_cache =>
50000)
@net_har = @proxy.new_har("new_har", :capture_binary_content =>
true, :capture_headers => true, :capture_content => true)
end
def fetch_har_entries(target_url)
har_logs = File.join(Support::Paths.har_logs, "har_file # .
{Time.now.strftime("%m%d%y_%H%M%S")} .har")
@net_har.save_to har_logs
index = 0
while (@net_har.entries.count > index) do
if @net_har.entries[index].request.url.include?(target_url) &&
entry.request.method.eql?("GET")
logs = JSON.parse(entry.response.content.text) if not
entry.response.content.text.nil?
har_logs = File.join(Support::Paths.har_logs, "json_file_# .
{Time.now.strftime("%m%d%y_%H%M%S")}.json")
File.open(har_logs, "w") do |json|
json.write(logs)
end
break
end
index += 1
end
end
end
在我的测试文件中,我有以下内容
Then("I navigate to the homepage") do
visit(HomePage) do |page|
page.element.click
end
end
And("I should wait for event to capture traffic") do
visit(SecondPage) do |page|
page.wait_until{page.element2.present?)
BMP.fetch_har_entries("target/url")
end
end
我遗漏了什么导致代理无法完整捕获流量?
【问题讨论】:
标签: ruby selenium watir browsermob-proxy har