

Inspect Network Traffic in Capybara Using the Poltergeist Driver
source link: https://www.tuicool.com/articles/hit/mERvE3F
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Ever tried inspecting the network traffic of any page and validating its response values? It's possible in Capybara if you are using the Poltergeist driver.
The Poltergeist driver is a PhantomJS driver for Capybara. It runs your tests in headless mode.
Network Tab in Developer Tools
If you are stuck up with any issues in any of the pages, you can open your dev tools and check the console errors or network traffic to understand the issue. This works if you are working on any browser.
What if You Are Running Your Capybara Tests?
All you need to do is add the below code:
page.driver.network_traffic
The above line will give you all network calls made during the page load with its headers that are difficult to read/manage.
Let's say you only want the list of URLs and their corresponding status. Just add two more lines of code as shown below:
page.driver.network_traffic.each do |request| request.response_parts.uniq(&:url).each do |response| puts "\n URL #{response.url}: \n Status #{response.status}" end end
The above code will give you the below output:
Get Response Headers
Suppose you want to get all the response headers of a page, the code looks like what I've got below:
page.driver.response_headers
You can also get the value of a specific header from the list. For example, if you want to validate the 'cache-control' header.
page.driver.response_headers['Cache-Control']
What Other Things Can We Do?
-
Check for any missing request/response strings.
-
Check for any 404s or any other errors in the page.
Code to Register Your Poltergeist Driver in Capybara
options = {} Capybara.register_driver :poltergeist do |app| Capybara::Poltergeist::Driver.new(app, options) end Capybara.javascript_driver = :poltergeist
Recommend
-
39
Post Syndicated fromJeff Barr original https://aws.amazon.com/blogs/aws/new-vpc-traffic-mirroring/ Running a complex network is not an e...
-
6
Poltergeist 1.0! 7 October 2012 Almost a year ago, on a complete whim, I decided to try to hook up the headless browser PhantomJS with Ruby’s full-stack acceptance t...
-
14
Poltergeist 0.6.0: Javascript error handling and remote debugging 15 April 2012 Recently I pushed version 0.6.0 of Poltergeist. This version brin...
-
7
Poltergeist: A PhantomJS driver for Capybara 3 January 2012 This announcement is coming way later than I had originally intended. Last October I started experimenting with the idea of writing a driver...
-
15
The following is an excerpt from my book, Rails Testing for Beginners. What we’re going to do One of the biggest challenges in getting started with testin...
-
6
Automated screenshots using Capybara and Selenium Thijs Cadier on Jul 21, 2015 “I absolutely love AppSignal.” Discover AppSignal
-
12
The rspec / capybara / poltergeist tests pass 100% locally, the random tests fail on the teamcity CI server advertisements I have a bunch of r...
-
8
Using the Figma Inspect panel to help engineers build your designsHow the Elastic UI design system (EUI) connects between its Design (Figma) and Code (React) libraries.One of the biggest c...
-
6
How to Edit Webpages on Safari Using Inspect Element By MUO Staff Published 10 hours ago Here's how you can tinker with text and images on a website through Safari's nifty Ins...
-
2
Inspect Ktor’s network traffic on Android with Chucker
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK