'Accessing browser dev tools and capturing network traffic using Watir
Have a requirement to capture network traffic from the browser while running automation test cases.
Came across below post but not sure if it has been included as part of Watir 7. Appreciate if there is any example for tapping on to devtools and capture network traffic like api calls using Watir. Thanks!!
Solution 1:[1]
Watir is not currently wrapping those features. You have access to everything by getting the selenium driver with the #wd method. So like this:
requests = []
browser.wd.intercept do |request, &continue|
requests << request
continue.call(request)
end
browser.goto page
expect(requests).not_to be_empty
There isn't a great guide in the selenium documentation for these, but here are other examples from the specs: https://github.com/SeleniumHQ/selenium/blob/trunk/rb/spec/integration/selenium/webdriver/devtools_spec.rb
Solution 2:[2]
This should work if you're interested in response as well
log_name = "/path/to/logs/network.log"
log_file = File.open(log_name, 'w')
browser.wd.intercept do |request, &continue|
continue.call(request) do |response|
log_file << "#{request.id} \t
#{request.method} \t
#{response.code} \t
#{request.url} \n"
end
end
and indeed it works, but something is exceptioning
undefined method `each_with_object' for nil:NilClass
/selenium-webdriver-4.1.0/lib/selenium/webdriver/devtools/response.rb:38:in `from'
/selenium-webdriver-4.1.0/lib/selenium/webdriver/common/driver_extensions/has_network_interception.rb:109:in `intercept_response'
/selenium-webdriver-4.1.0/lib/selenium/webdriver/common/driver_extensions/has_network_interception.rb:68:in `block in intercept'
/selenium-webdriver-4.1.0/lib/selenium/webdriver/devtools.rb:155:in `block in callback_thread'```
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | titusfortner |
| Solution 2 | Luka Vladika |
