Minimal Capybara / Poltergeist test returning empty page - ruby

It appears I'm retracing the steps taken in the SO post: Capybara, Poltergeist and Phantomjs and giving an empty response in body. (Mark this as a duplicate if you want, but I'm including a minimal standalone test case and version numbers.)
questions
Am I doing anything obviously wrong? Is there another minimal test I can run that might help isolate the problem?
file: pgtest.rb
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'capybara/poltergeist'
module PGTest
include Capybara::DSL
extend self
def test
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app)
end
Capybara.current_driver = :poltergeist
session = Capybara::Session.new(:poltergeist)
visit "http://www.google.com"
sleep 5
puts session.html
end
end
PGTest.test
When invoked as follows, it prints an empty page:
$ ruby pgtest.rb
<html><head></head><body></body></html>
environment
OS X 10.8.5 (12F45)
ruby 2.0.0p247 (2013-06-27) [x86_64-darwin12.4.0]
phantomjs 1.9.2
capybara (2.1.0)
poltergeist (1.4.1)
absolving phantomjs
It's worth noting that I can use phantomjs extract the html from www.google.com:
file: pjs_dump.js
var page = require('webpage').create();
page.open("http://www.google.com", function () {
var html = page.evaluate(function () {
return document.documentElement.outerHTML;
});
console.log(html);
phantom.exit();
});
When I run 'phantomjs pjs_dump.js', it prints the html from www.google.com, so phantomjs appears to be working properly.

This does the trick for me:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'capybara/poltergeist'
Capybara.run_server = false
Capybara.current_driver = :poltergeist
class PGTest
include Capybara::DSL
def test
visit "http://www.google.com"
puts page.body
end
end
PGTest.new.test

I had similar issue when testing rails application with rspec, capybara and poltergeist. Spec failed when trying to find element when the body was empty.
It turned out, that the problem was in my rails code. It caused an error, the page did not render, and capybara received an empty page. And I had nothing in stack trace.

Related

Launching IE and Chrome browser using Capybara, Selenium, Ruby

I am new to Cucumber and Capybara. I am trying to launch IE and Chrome browser.
I have downloaded drivers of both and copied them to bin folder of Ruby in C drive.
I have set Path in Env var.
Below is my support/env.rb file code
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
require 'selenium-webdriver'
require 'capybara/cucumber'
Capybara.run_server = false
#Set default driver as Selenium
Capybara.default_driver = :selenium
#Set default selector as css
Capybara.default_selector = :css
#Syncronization related settings
module Helpers
def without_resynchronize
page.driver.options[:resynchronize] = false
yield
page.driver.options[:resynchronize] = true
end
end
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => chrome) #Getting error at this line
end
World(Capybara::DSL, Helpers)
I am getting compilation Error at line with comment is as below.
method calls where the number of arguments passed to the method does not match the number of method parameters.
Here is my environment:
cucumber (2.4.0)
selenium-webdriver (3.0.3)
capybara (2.11.0)
rspec (3.5.0)
Ruby 2.3
Capybara::Selenium::Driver.new(app, :browser => chrome) try changing it to Capybara::Selenium::Driver.new(app, :browser => :chrome)

Watir with Ruby: undefined local variable or method `doc' for main:Object

I was following this tutorial for screen scraping with Ruby and Watir.
I tried to write a simple script to return text from Wikipedia:
require "selenium-webdriver"
browser = Selenium::WebDriver.for :chrome
browser.get "https://wikipedia.org"
require "nokogiri"
puts doc.xpath(".//*[#id='langsearch-input']/p").inner_text
But when I run the script, I get this error in my terminal:
$ ruby app/views/layouts/scraper.rb
app/views/layouts/scraper.rb:7:in `<main>': undefined local variable or method `doc' for main:Object (NameError)
I have nokogiri 1.6.7.2, watir-webdriver 0.9.1, and watir 4.0.2 installed.
What am I doing wrong?
You are missing a line to that converts the browser HTML into a Nokogiri document. In other words, you have not defined what doc is.
require "selenium-webdriver"
browser = Selenium::WebDriver.for :chrome
browser.get "https://wikipedia.org"
require "nokogiri"
doc = Nokogiri::HTML.parse(browser.page_source)
puts doc.xpath(".//*[#id='langsearch-input']/p").inner_text
#=> ""
Note that while this will address the exception, the inner_text will return an empty string - ie "". The element with id "langsearch-input" is an input field, which dos not have a child p element or a text node.
Also note that you are not actually using Watir at all. To use Watir, it would look like:
require 'watir-webdriver'
browser = Watir::Browser.new :chrome
browser.goto "https://wikipedia.org"
require 'nokogiri'
doc = Nokogiri::HTML.parse(browser.html)
puts doc.xpath(".//*[#id='langsearch-input']/p").inner_text
#=> ""
However, unless you are doing a lot of parsing of a single large HTML chunk, using Watir without Nokogiri might be easier:
require 'watir-webdriver'
browser = Watir::Browser.new :chrome
browser.goto "https://wikipedia.org"
puts browser.text_field(id: 'langsearch-input').value

YAML + Ruby usage

I'm trying to make testcase using Selenium WebDriver and Ruby. I started learning Ruby a few times ago.
I created the testcase:
require "test/unit"
require "selenium-webdriver"
require "yaml"
thing = YAML.load_file('config.yaml')
puts thing.inspect
class Test < Test::Unit::TestCase
def setup
browser = thing('browser')
#driver = Selenium::WebDriver.for browser
#driver.get 'http://google.com'
#driver.manage.delete_all_cookies
end
def teardown
#driver.close
end
def test_page_search
end
end
I decided to use YAML for config file where I will can change and for WebDriver.
config.yaml:
# Set browser (firefox, ie, chrome, opera)
browser: ":firefox"
# Search query
search_query: "ios testing"
But when I'm running the testcase I'm getting the error:
"test_yaml.rb:11:in `setup'"
You have:
browser = thing('browser')
Did you mean:
browser = thing['browser']
If you're trying to access the browser key, that should take care of it.
Thank you!
I resolved the issue:
require "test/unit"
require "selenium-webdriver"
require "yaml"
class Test < Test::Unit::TestCase
def setup
thing = YAML.load_file('config.yaml')
puts thing.inspect
browser = thing['browser'].to_sym
#driver = Selenium::WebDriver.for browser
#driver.get 'http://google.com'
#driver.manage.delete_all_cookies
end
def teardown
# #driver.close
end
def test_page_search
end
end

Ruby Page object Gem - Unable to pick a platform for the provided browser (RuntimeError)

I get this error on running my feature file.
Unable to pick a platform for the provided browser (RuntimeError)
Help required, please.
Here is the code;
class GooglePage
include PageObject
def self.visitor
visit("http://www.google.com")
end
end
env.rb
require 'selenium-webdriver'
require 'page-object'
require 'rubygems'
require 'page-object/page_factory'
World (PageObject::PageFactory)
#browser = Selenium::WebDriver.for :firefox
Step-Definitions
require_relative 'GooglePage'
Given(/^I am on the Google home page$/) do
visit(GooglePage)
# visit('http://www.google.com')
on(GooglePage).visitor
end
This won't work:
visit(GooglePage)
because you haven't called page_url in GooglePage (ln 4 below)
class GooglePage
include PageObject
page_url "http://www.google.com"
def self.visitor
visit("http://www.google.com")
end
end
Move the line #browser = Selenium::WebDriver.for :firefox to the Before method in hooks.rb
Before do
#browser = Selenium::WebDriver.for :firefox
end
After do
#browser.close
end
what if you make your rake file pass on tags and BROWSER and URL,e.g.
rake my_task BROWSER=chrome URL=http://google.com.au
and hooks will check if the BROWSER is chrome, then will use the specific Webdriver for chrome. But how to pass the URL in the PageObject?
Currently from hooks I have the ff:
when "chrome" then
caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps.version = "40.0.2214.115 m"
caps.native_events = false
caps.javascript_enabled= true
# This is for increasing the default timeout to 180
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 180
browser = Selenium::WebDriver.for :chrome
browser.manage.window.maximize
end
Before do
browser.cookies.clear
#browser = browser
end
After do
unless ENV["BROWSER"].eql? "ie"
browser.close
sleep 2
end
end
Then on my HomePage pageobject I have the ff:
class HomePage
include PageObject
page_url("#{ENV['URL']}")
#opens the url defined in ENV['URL']
def goto_homepage
visit(HomePage)
end
end
Using Watir, the URL got opened, so trying this out in Selenium and it won't work.

Using Capybara to test pure JavaScript application

I'm having some problems using Sinatra with Capybara.
I want to test a pure javascript application. It's just a plain index.html that is being served by Sinatra.
require "sinatra"
get "/" do
File.read("public/index.html")
end
Let's say for example that I want to test this code.
$("a.link").click(function(){
$(this).replaceWith("New String");
});
Click me!
Then the test would look something like this.
describe "requests", js: true do
it "should display a message" do
visit "/"
click_link "Click me!"
page.should have_content("New String")
end
end
The problem is that nothing happens. According to Ryan Bates screencast Firefox should start and run the test if js: true is added to the describe block.
Here is my spec_helper file.
require "rspec"
require "capybara"
require "capybara/dsl"
Capybara.javascript_driver = :selenium
require_relative "./../server"
Capybara.app = Sinatra::Application
Capybara.javascript_driver = :selenium
Capybara.default_wait_time = 10
RSpec.configure do |config|
config.mock_with :rspec
config.include Capybara
end
Here is the output when running rspec rspec/request_spec.rb.
requests
should display a message (FAILED - 1)
Failures:
1) requests should display a message
Failure/Error: page.should have_content("New String")
expected #has_content?("New String") to return true, got false
# ./spec/request_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 4.38 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/request_spec.rb:2 # requests should display a message
I created an complete example project on Github that can be found here:
https://github.com/oleander/capybara-js-fails
Anyone knows why it fails?
Here is the original answer from Jonas Nicklas.
You need to require 'capybara/rspec' and set :type => :request.
See the Capybara README section on "Using Capybara with RSpec".
/Jonas
Here is a working example on Github.

Resources