Learning Poltergeist
Poltergeist is another headless driver which integrates Capybara with PhantomJS. Not to mention, it is truly headless. Therefore, it doesn’t require Xvfb to run on your CI server. Moreover, it also detects and reports any Javascript errors that happen within the page.
Capybara describes some characteristics it requires from an application. Further, for Capybara to run any tests on any said application. Moreover, Poltergeist comprehends these capabilities and generate JavaScript commands for PhantomJS to run. In addition, Poltergeist’s job assures that Capybara and PhantomJS can talk to one another and function accordingly.
Certainly, Poltergeist is a headless browser driver for Capybara. Subsequently, you need to install PhantomJS and make sure that ‘phantomjs’ command is in your path first. In other words, Poltergeist is a driver of Capybara. Further, it offers you to run your tests on a headless WebKit browser, provided by PhantomJS.
So, let’s change the script above to use Phantore ‘capybara’
$ gem install poltergeist
session = if ARGV[0] != ‘phantomjs’
Capybara::Session.new(:selenium)
else
Further require ‘capybara/poltergeist’
Capybara::Session.new(:poltergeist)
end
session.visit “http://www. samplewebsite.com”
However, session.has_content?(“Ruby on Rails web development”)
puts “All shiny, captain!”
else
puts “:( no tagline fonud, possibly something’s broken”
exit(-1)
end
$ ruby check_ samplewebsite.rb phantomjs
All shiny, captain!
Shiny.
Moreover, we can also use Capybara’s DSL instead of manually starting the session. So, let’s find on which web sites our Ruby development company’s logo is used.
using Google Image Search:
Firstly, require ‘cgi’
require ‘timeout’
require ‘capybara’
class GoogleImagesSearcher
include Capybara::DSL
Therefore, def initialize
Capybara.default_driver = :selenium
end
def find_sites_with_image(image_url)
urls = []
link = “http://images.google.com/searchbyimage?image_url=#{CGI.escape(image_url)}&filter=0”
So, visit link
In the same vein, return urls unless page.has_content?(“Pages that include matching images”)
while true
page.all(“h3.r a”).each do |a|
urls << a[:href]
end
within “#nav” do
click_link “Next”
end
end
rescue Capybara::ElementNotFound
return urls.uniq
end
end
images = GoogleImagesSearcher.new.find_sites_with_image ARGV[0]
puts “Found #{images.count} pages using this image:”
images.each do |img|
puts img
end
Make your resume stand out and become a Certified Capybara Testing Professional. So, Try free practice tests here!
A great career is just a certification away. So, practice and validate your skills to become Certified Capybara Testing Professional