Change default Capybara browser window size - ruby

So, with respect to integration testing using Capybara and RSpec, I know I can do this:
page.driver.browser.manage.window.resize_to(x,y)
per How to set Browser Window size in Rspec (Selenium) for specific RSpec tests, but is there a way to do this globally so that every test that is affected by media queries doesn't have to define this?

A proper way to do it for all js tests is to add following inside spec_helper.rb RSpec.configure block
config.before(:each, js: true) do
Capybara.page.driver.browser.manage.window.maximize
end
to maximize the window. Change to resize_to(x,y) to set any window size.
EDIT: If you happen to be using Poltergeist the correct way to do it is
config.before(:each, js: true) do
Capybara.page.driver.browser.resize(x,y)
end

You could define that under before(:all)
describe "Test" do
before(:all) do
...
...
page.driver.browser.manage.window.resize_to(x,y) #Mention it here
end
it "should find everything" do
...
end
after(:all) do
...
end
end

Perhaps due to a recent change in Capybara, what worked for me was:
before do
Capybara.page.current_window.resize_to(x, y)
end

For test runtime in Capybara 2.2.4 version you can achieve this by doing
before do
handle = Capybara.page.driver.current_window_handle
Capybara.page.driver.resize_window_to(handle, width, height)
end
Or
before do
Capybara.page.current_window.resize_to(width, height)
end
If you get Capybara::NotSupportedByDriverError: Capybara::Driver::Base#current_window_handle YOU MUST CHANGE YOUR DRIVER FOR EXAMPLE USE JAVASCRIPT DRIVER!
before do
Capybara.page.current_window.resize_to(width, height)
end
scenario js: true do
# your test here
end

#tirdadc if you're using Poltergeist, you can add something like this to your rails_helper.rb file:
Capybara.register_driver :poltergeist do |app|
options = {
# js_errors: true,
# cookies: true,
window_size: [320, 568] # iphone 5
}
Capybara::Poltergeist::Driver.new(app, options)
end

Related

Can I eliminate multiple within_frame calls when writing rspec tests for my site?

I'm writing tests for a legacy app using Capybara + Rspec, and I'm finding that all of my tests need to be run within a specific frame. I've written up a little rspec helper that I run before every test to confirm I'm getting the session set correctly, and then visit the URL:
def set_session
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(
app,
window_size: [1280, 1024] ,
timeout: 60
)
end
Capybara.javascript_driver = :poltergeist
#sess = Capybara::Session.new :poltergeist
#authentication stuff here...
end
describe 'initiate_requests' do
before(:all) do
set_session
end
before(:each) do
#sess.visit(URL)
end
it 'shows error if not degree seeking student' do
#sess.within_frame(FRAME) {
set_test_vars(FAILID)
expect(#sess).to have_selector("#NO_PROGRAM_ERROR")
expect(#sess).not_to have_selector("#submitButton")
}
end
it 'works for a degree seeking student' do
#sess.within_frame(FRAME) {
set_test_vars(VALIDID)
expect(#sess).not_to have_selector("#NO_PROGRAM_ERROR")
expect(#sess).to have_selector("#submitButton")
}
end
end
This works just fine. However, every single test I need to write in the form:
it 'works for a degree seeking student' do
#sess.within_frame(FRAME) {
#do tests..
}
end
Is there some way I can in reset the #sess to just work within the frame all the time? Lacking that, is there some way I can declare the #sess.within_frame(FRAME) just once and have all of the tests follow that declaration?
I haven't specifically tried it, but you can probably use an around hook rather than your before(:each) hook
around(:each) do |example|
#sess.visit(URL)
#sess.within_frame(FRAME) do
example.run
end
end

How to ignore or skip a test method using RSpec?

please guide how to disable one of the below test methods using RSpec. I am using Selenuim WebDriver + RSpec combinations to run tests.
require 'rspec'
require 'selenium-webdriver'
describe 'Automation System' do
before(:each) do
###
end
after(:each) do
#driver.quit
end
it 'Test01' do
#positive test case
end
it 'Test02' do
#negative test case
end
end
You can use pending() or change it to xit or wrap assert in pending block for wait implementation:
describe 'Automation System' do
# some code here
it 'Test01' do
pending("is implemented but waiting")
end
it 'Test02' do
# or without message
pending
end
pending do
"string".reverse.should == "gnirts"
end
xit 'Test03' do
true.should be(true)
end
end
Another way to skip tests:
# feature test
scenario 'having js driver enabled', skip: true do
expect(page).to have_content 'a very slow test'
end
# controller spec
it 'renders a view very slow', skip: true do
expect(response).to be_very_slow
end
source: rspec 3.4 documentation
Here is an alternate solution to ignore (skip) the above test method (say, Test01) from sample script.
describe 'Automation System' do
# some code here
it 'Test01' do
skip "is skipped" do
###CODE###
end
end
it 'Test02' do
###CODE###
end
end
Pending and skip are nice but I've always used this for larger describe/context blocks that I needed to ignore/skip.
describe Foo do
describe '#bar' do
it 'should do something' do
...
end
it 'should do something else' do
...
end
end
end if false
There are a number of alternatives for this. Mainly marking it as pending or skipped and there is a subtle difference between them. From the docs
An example can either be marked as skipped, in which is it not executed, or pending in which it is executed but failure will not cause a failure of the entire suite.
Refer the docs here:
https://relishapp.com/rspec/rspec-core/v/3-4/docs/pending-and-skipped-examples/pending-examples
https://relishapp.com/rspec/rspec-core/v/3-4/docs/pending-and-skipped-examples/skip-examples
There are two ways to skip a specific block of code from being running while testing.
Example : Using xit in place of it.
it "redirects to the index page on success" do
visit "/events"
end
Change the above block of code to below.
xit "redirects to the index page on success" do #Adding x before it will skip this test.
visit "/event"
end
Second way: By calling pending inside the block.
Example:
it "should redirects to the index page on success" do
pending #this will be skipped
visit "/events"
end

Embed RSpec test in a Ruby class

I often build little single-purpose Ruby scripts like this:
#!/usr/bin/env ruby
class Widget
def end_data
DATA.read
end
def render_data source_data
source_data.upcase
end
end
w = Widget.new
puts w.render_data(w.end_data)
__END__
data set to work on.
I'd like to include RSpec tests directly inside the file while I'm working on it. Something like this (which doesn't work but illustrates what I'm trying to do):
#!/usr/bin/env ruby
class Widget
def end_data
DATA.read
end
def render_data source_data
source_data.upcase
end
def self_test
# This doesn't work but shows what I'm trying to
# accomplish. The goal is to have RSpec run these type
# of test when self_test is called.
describe "Widget" do
it "should render data properly" do
#w = Widget.new
expect(#w.render_data('test string')).to eq 'TEST STRING'
end
end
end
end
w = Widget.new
w.self_test
__END__
data set to work on.
I understand this is not the normal way to work with RSpec and isn't appropriate in most cases. That said, there are times when it would be nice. So, I'd like to know, is it possible?
There are two things. First off rspec by default won't pollute the global namespace with methods like describe and so on. The second thing is that you need to tell rspec to run the specs after they've been declared.
If you change your self_test method to be
RSpec.describe "Widget" do
it "should render data properly" do
#w = Widget.new
expect(#w.render_data('test string')).to eq 'TEST STRING'
end
end
RSpec::Core::Runner.invoke
(having of course done require 'rspec' then that will run your specs).
The invoke methods exits the process after running the specs. If you don't want to do that, or need more control over where output goes etc. you might want to drop down to the run method which allows you to control these things.

Model not being saved using Selenium but saved using Rack::Test

I have the following test that works in Rack::Test but not using Selenium. I.e. if I add , js: true to the describe block, I get an error message in Firefox saying that it couldn't find the License with id=(the id of #l)
describe "should hide allocation rule # for pdf & clickthrough licenses" do
it "reads current state and shows/hides fields appropriately" do
#l = FactoryGirl.create(:license:,
way: License::CLICK_WAY)
visit edit_admin_license_path(#l)
end
end
Why? I must be missing something. I can verify with Sequel Pro that the record is not getting saved when using js: true.
I need this spec to run in Selenium because I have javascript to test.
The simple solution was to turn transactional fixtures off.
Why does my Cucumber test fail when run with Selenium?
in spec/spec_helper.rb:
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before :each do
if Capybara.current_driver == :rack_test
DatabaseCleaner.strategy = :transaction
else
DatabaseCleaner.strategy = :truncation
end
DatabaseCleaner.start
end
config.after do
DatabaseCleaner.clean
end
end
and in the Gemfile, test section
gem 'database_cleaner'

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