How to have capybara see page change after ajax call to rails? - ruby-on-rails

I am writing a feature spec:
the steps are:
List item
I add a new record
I overwrite its verification_date to be nil
This makes it list with a 'verify' link
I click the verify link
If the url is valid the screen should (via ajax) update the 'verify' link to be '2014' text.
This is current working fine in the actual application, the problem is in trying to add some tests that will replicate the behaviour and reflect a working process.
I can't seem to get the test to recognize changed text from the ajax call.
I can do the steps manually but when I use the spec it doesn't see an updated page.
I make sure (in the spec) that I only have (and have added only) 1 record.
Whatever I do I get
1) verification lets me verify a link
Failure/Error: expect(page).to have_content(this_year)
expected to find text "2014" in "Test Linker Links New Link Gr...
The spec is:
...
describe "verification", :type => :feature do
before :all do
User.create(:username => 'x#google.com', :password => 'xyz')
end
before :each do
visit '/ladmin/login'
fill_in 'username', :with => 'x#google.com'
fill_in 'password', :with => 'xyz'
find('input[value="Login"]').click
end
it "lets me verify a link" do
Link.delete_all
expect(Link.count).to eq 0
this_year=Time.now.strftime('%Y') # This is what the screen gets after verification
visit links_path
expect(page).to_not have_content(this_year) # true
l=FactoryGirl.create(:valid_url_link)
l.save
l.update_column(:verified_date, nil) # Force condition that makes 'verify' appear
expect(Link.count).to eq 1
visit links_path
find('a', text: "verify") # This seems to work. The one record has a 'verify' link
click_link("verify", match: :first) # This should change the text to 2014
sleep(7)
expect(page).to have_content(this_year)
end
end
the link factory is:
FactoryGirl.define do
factory :link do
group {FactoryGirl.create(:group)} #:group
url_address {"http://test.com"+SecureRandom.uuid}
alt_text "examples of common situations amnd solutions"
end
factory :valid_url_link, parent: :link do
group {FactoryGirl.create(:group)} #:group
url_address {"http://www.google.com"}
alt_text "valid url"
end
...
end
The js (though probably not where the issue is imho) is:
$ cat app/assets/javascripts/verifying_link.js
$(function(){
$("a[data-verifying-link]='yes'").click(function(event){
event.preventDefault();
a=$(this).parent();
a.html('<img src="assets/ajax-loader.gif">');
var id= $(this).data("id");
var row = $(this).data("tableRow");
$.ajax({
url: "/verify_link/"+id+"&table_row="+row,
type: 'GET',
success: function(r) {
$("span#verify_link_"+row).html('<span class="done">Verified</span>');
},
error: function(r) {
$("span#verify_link_"+row).html('<span class="undone">Unverified</span>');
}
});
});
});
The code for the actual display of the field is a bit messy (but has worked for 378 links so far...) is:
%td.column_align
%span{id: "verify_link_#{index}"}
- if link.verified_date
- link_text = session[:full_details] == 'true' ? long_form(link.verified_date) : short_form(link.verified_date)
= render :partial => 'link_toggle', :locals => { :content => [long_form(link.verified_date), short_form(link.verified_date)], :url => verify_link_path(id: link.id, table_row: index) }
- else
- if session[:user_id]
%a{href: "#", :data => {verifying_link: 'yes', id: link.id, table_row: index}}
verify
- else
No
The partial involved is:
- if session[:full_details] == 'true'
%span{class: "show_hide shown"}
= link_to content[0], url, title: 'Reverify this link', remote: true
%span{class: "show_hide hidden"}
= link_to content[1], url, title: 'Reverify this link', remote: true
- else
%span{class: "show_hide shown"}
= link_to content[1], url, title: 'Reverify this link', remote: true
%span{class: "show_hide hidden"}
= link_to content[0], url, title: 'Reverify this link', remote: true
That needs work but has been working that way for a while in the live app.
Why does the test keeps showing the page with the links added but not verified and showing 2014?

The spec shown does not have the required :js => true
e.g.
describe "verification", :js => true, :type => :feature do
This will then require you (via an error) to add capybara-webkit to your Gemfie, e.g.
gem 'selenium-webdriver'# For rspec capybara javascript tests
and of course
bundle

Related

How to write jmeter test to submit a form within website (Jmeter, ruby -jmeter)

I am writing a load test for my application.
I would like to simulate the following steps:
login
visit several pages
submit a form within the website
I first wrote the login and visit several pages and run them successfully (no errors). When I added the code to submit form, I was getting '404/Not Found error' for the Submit Abstract transaction.
I am grateful to anyone who can provide me direction on how to solve this.
I wrote this test script using ruby then execute to convert it to .jmx file which I use to run headless test in cli.
code for login and visit several pages:
require 'ruby-jmeter'
test do
threads count: 100, rampup: 60, loops: 10, duration: 120 do
defaults domain: 'myapp.herokuapp.com', protocol: 'https'
cookies policy: 'rfc2109', clear_each_iteration: true
transaction 'Page Load Tests' do
user_defined_variables [{name: 'email', value: 'example#example.com'}, {name: 'password', value: 'Pass_w0rd'}]
visit name: 'Visit Login', url: '/users/sign_in' do
extract name: 'csrf-token', xpath: "//meta[#name='csrf-token']/#content", tolerant: true
extract name: 'csrf-param', xpath: "//meta[#name='csrf-param']/#content", tolerant: true
extract name: 'authenticity_token', regex: 'name="authenticity_token" value="(.+?)"'
end
end
http_header_manager name: 'X-CSRF-Token', value: '${csrf-token}'
submit name: 'Submit login', url: '/users/sign_in',
fill_in: {
'${csrf-param}' => '${csrf-token}',
'user[email]' => '${email}',
'user[password]' => '${password}',
'authenticity_token' => '${authenticity_token}'
}
visit name: 'Welcome Page', url: '/static_pages/welcome'
visit name: 'New Abstract Page', url: '/users/2/abstracts/new'
visit name: 'My Profile Page', url:'/users/2/participations/1/profile'
visit name: 'My Own Abstract Page', url:'/users/2/participations/1/abstracts/1'
view_results_in_table
aggregate_report
end.jmx
code for login, visit pages and submit form:
require 'ruby-jmeter'
test do
threads count: 100, rampup: 60, loops: 10, duration: 120 do
defaults domain: 'myapp.herokuapp.com', protocol: 'https'
cookies policy: 'rfc2109', clear_each_iteration: true
transaction 'Page Load Tests' do
user_defined_variables [{name: 'email', value: 'example#example.com'}, {name: 'password', value: 'Pass_w0rd'}]
visit name: 'Visit Login', url: '/users/sign_in' do
extract name: 'csrf-token', xpath: "//meta[#name='csrf-token']/#content", tolerant: true
extract name: 'csrf-param', xpath: "//meta[#name='csrf-param']/#content", tolerant: true
extract name: 'authenticity_token', regex: 'name="authenticity_token" value="(.+?)"'
end
end
http_header_manager name: 'X-CSRF-Token', value: '${csrf-token}'
submit name: 'Submit login', url: '/users/sign_in',
fill_in: {
'${csrf-param}' => '${csrf-token}',
'user[email]' => '${email}',
'user[password]' => '${password}',
'authenticity_token' => '${authenticity_token}'
}
visit name: 'Welcome Page', url: '/static_pages/welcome'
visit name: 'New Abstract Page', url: '/users/2/abstracts/new'
visit name: 'My Profile Page', url:'/users/2/participations/1/profile'
visit name: 'My Own Abstract Page', url:'/users/2/participations/1/abstracts/1'
transaction 'Submit Abstract' do
visit name: 'New Abstract Page', url: '/users/2/abstracts/new' do
extract name: 'csrf-token', xpath: "//meta[#name='csrf-token']/#content", tolerant: true
extract name: 'csrf-param', xpath: "//meta[#name='csrf-param']/#content", tolerant: true
extract name: 'authenticity_token', regex: 'name="authenticity_token" value="(.+?)"'
end
http_header_manager name: 'X-CSRF-Token', value: '${csrf-token}'
submit name: 'Submit Abstract', url: '/users/2/abstracts/new',
fill_in: {
'${csrf-param}' => '${csrf-token}',
'abstract[title]' => 'Lorem Ipsum',
'abstract[main_author]' => '2',
'abstract[co_authors][]' => ["", "1", "3"],
'abstract[corresponding_author_email]' => '${email}',
'abstract[keywords]' => 'word, words',
'abstract[body]' => 'The test directive is a root point, where all the magic starts. Then, using threads method we are telling JMeter what number of users we want to use. The defaults command allows us to specify default options for all our http requests. And, finally,cookies indicates that we need to store cookies and send them with each request.',
'abstract[references]' => '1\r\n2\r\n3',
'authenticity_token' => '${authenticity_token}'
} do
assert 'contains' => 'Abstract submission completed.'
assert 'contains' => 'Lorem Ipsum'
end
end
end
view_results_in_table
aggregate_report
end.jmx
UPDATE PER #DMITRI T SUGGESTION:
EDITED TEST
REQUEST HEADER:
Browser:
Jmeter:
REQUEST BODY:
Browser:
Jmeter:
RESULT: STILL 404 error for the Submit Abstract Transaction
HTTP Status 404 means that the server cannot find the requested resource so most probably your URL is wrong.
So double check that the URL of https://myapp.herokuapp.com/users/2/abstracts/new returns a valid response for the logged in user and if it does - capture the request for creating the new "abstract" using your browser developer tools
Then in JMeter GUI:
Change number of threads, ramp-up period and loop count to 1 in the https://jmeter.apache.org/usermanual/component_reference.html#Thread_Group
Add Debug Post-Processor to your Test Plan (it will allow you to see the values of JMeter Variables)
Add View Results Tree listener to your Test Plan (it will allow to see the request and response details)
Run your test in JMeter GUI, inspect Submit Abstract request details and cross-check it with what you see in the browser developer tools - the requests must be absolutely the same (apart from dynamic parameters)
Fix your JMeter configuration so it would send exactly the same request and it should resolve your issue.

Rails, Capybara - click_link on remote links doesnt work

I'm using Capybara to test my project. But i have a problem.
I have some remote forms on my project. They add records via ajax. When i'm testing with capybara it works well on development environment. It visits the page, fills in the form and submits. Booom, record has been added and test didnt fail.
But when i run rspec with test environments i'm getting unknown format exception.
1) add new address user adds new address
Failure/Error: find("input[value='Adres Ekle']").click
ActionController::UnknownFormat:
Account::AddressesController#create is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
# ./spec/features/user_add_new_address_spec.rb:28:in `block (2 levels) in <top (required)>'
I've also tried to respond via js from controller like;
def create
request.format = :js
end
Then it returns;
1) add new address user adds new address
Failure/Error: find("input[value='Adres Ekle']").click
ActionController::UnknownFormat:
Account::AddressesController#create is missing a template for this request format and variant.
request.formats: ["text/javascript"]
request.variant: []
# ./spec/features/user_add_new_address_spec.rb:28:in `block (2 levels) in <top (required)>'
And my scenario if u want more info;
scenario 'user adds new address' do
expect(page).to have_content 'Kayıtlı Adreslerim'
find("a[title='Adres Ekle']").click
expect(page).to have_content 'Yeni Adres Ekle'
expect(page).to have_content 'Adres Başlığı'
fill_in 'address[name]', with:'Izmir Ofisi'
select('Izmir', :from => 'address[city_id]')
fill_in 'address[address]', with: 'Lorem ipsum dolor sit amet.'
find("input[value='Adres Ekle']").click # It submits remote: true form.
expect(page).to have_content 'Success!'
end
PS: my create action doesnt render something like that.
its like;
def create
#new_address = Address.new
#address = #current_account.addresses.new(address_params)
if #address.save
#check = true
else
#check = false
end
end
it renders: create.js.erb
<% if #check %>
if($('.addresses').length) {
$('.addresses').append('<%= j(render('account/addresses/address', address: #address)) %>');
}
if($('#did-addresses').length){
$('#did-addresses').append("<%= "<option selected='true' value='#{#address.id}'>#{#address.name}</option>".html_safe %>").selectpicker('refresh');
}
$('#new-address').html('<%= j(render('account/addresses/form', new_address: #new_address)) %>');
swal({
type: 'success',
title: "<%= t('response.success') %>",
text: "<%= t('flash.actions.create.notice', resource_name: Address.model_name.human) %>",
timer: 2000
});
quickview.close('#new-address');
<% else %>
<% #address.errors.each do |error| %>
<% end %>
<% end %>
$('.preloader').fadeOut();
I was facing the same case in rails 6 but I fixed it be adding js: true to the scenario and it automatically worked well.
scenario 'Should delete the feature', js: true do
# Your logic
# Your expectations
end
Since copying your development config over your test config fixed your issue, it sounds like you probably an error in one of your JS files. Normally in the test and production environment all of your JS assets get concatenated into one file which means an error in any one of them can prevent the code in the others from being executed. In the development environment each JS file is loaded separately which means an error in any file can only affect the rest of the code in that file. Check your the console in your browser for any JS errors when going to the page in question and fix them.

How can I test a rails 4 confirm dialog with Capybara and Poltergeist?

I'm trying to test that a link to a destroy action throws a native browser confirm box with the correct message.
The link is being generated using rails' link_to:
link_to 'Delete', user_path, method: :delete, data: { confirm: "Are you sure?" }
And generates the following html:
<a data-confirm="Are you sure?" data-method="delete" href="/users/6" rel="nofollow">Delete</a>
The functionality is working correctly in the browser, but I want to test for it in my rspec feature spec.
I'm trying to stub out the browser's confirm function as described here and in this gist, however I can't get it to work.
it 'requests confirmation', js: true do
visit user_path(user)
page.execute_script "
window.confirmMsg = null;
window.confirm = function(msg) { window.confirmMsg = msg; return true; };"
click_link 'Delete'
expect(page.evaluate_script('window.confirmMsg')).to eq('Are you sure?')
end
Gives the following error from rspec:
Failure/Error: expect(page.evaluate_script('window.confirmMsg')).to eq('Are you sure?')
expected: "Are you sure?"
got: nil
(compared using ==)
However, if I call a confirm directly via page.execute_script:
it 'requests confirmation', js: true do
visit user_path(user)
page.execute_script "
window.confirmMsg = null;
window.confirm = function(msg) { window.confirmMsg = msg; return true; };
window.confirm('Are you sure?');"
expect(page.evaluate_script('window.confirmMsg')).to eq('Are you sure?')
end
Then the test passes.
Also clicking the Delete link will cause the test to fail, even if confirm has been called directly for page.execute_script:
it 'requests confirmation', js: true do
visit user_path(user)
page.execute_script "
window.confirmMsg = null;
window.confirm = function(msg) { window.confirmMsg = msg; return true; };
window.confirm('Are you sure?');"
click_link 'Delete'
expect(page.evaluate_script('window.confirmMsg')).to eq('Are you sure?')
end
Gives the same error from rspec:
Failure/Error: expect(page.evaluate_script('window.confirmMsg')).to eq('Are you sure?')
expected: "Are you sure?"
got: nil
(compared using ==)
Why is the test failing? And, how can I test confirm dialogues correctly?
Context:
I'm running my tests from a Vagrant virtual machine, which is Ubuntu 12.04.4 LTS and running ruby 2.1.2p95.
My Gemfile.lock shows that I have the following versions:
rails (4.1.4)
poltergeist (1.5.1)
capybara (2.4.1)
page.driver.browser.accept_js_confirms is deprecated. Instead use
page.accept_confirm do
click_link 'Delete'
end
Unfortunately, you cannot do this, because Poltergeist does work only in one window.
For that specific test you will need to use Selenium and this API:
page.driver.browser.switch_to.alert.accept
If you are concerned about wanting to run your tests headless, you can use Xvfb (X Virtual Framebuffer) like this:
Xvfb :1 -screen 0 1024x768x24+32
Alternatively you can also use capybara-webkit:
page.driver.browser.accept_js_confirms
page.driver.browser.reject_js_confirms
However, I have made the best experience using a mixture of (mostly) Poltergeist and Selenium where necessary.
To expand on the above, when using Selenium you can test the actual text of the confirm dialog using the following:
click_link 'Delete'
a = page.driver.browser.switch_to.alert
expect(a.text).to eq("Are you sure?")
a.accept
Also, just found a good test for whether the alert is present here: selenium 2.4.0, how to check for presence of an alert I slightly modified it, put in my spec_helper file as:
def alert_present?
begin
page.driver.browser.switch_to.alert
return true
rescue
return false
end
end
And then in your test just do:
click_link 'Delete'
expect(alert_present?).to eq(true)
Given this Javascript:
confirm('You have unsaved changes, do you want to continue?')
For Poltergiest I found the following to work:
expect(page.driver.browser.modal_message).eq 'You have unsaved changes, do you want to contine?'
page.driver.browser.dismiss_confirm
page.driver.browser.accept_confirm
This is how I am doing in Rails 3.2 and capybara (~> 2.18.0)
context 'when `All listing` overlaps with listing 1' do
it 'displays warning' do
set_weekday_times('.show_window','10:00 AM', '02:00 PM', listing1.address)
set_weekday_times('.show_window:last-child', '11:00 AM', '03:00 PM', 'All Listing')
# button click event that triggers the confirm to appear
submit_weekdays_form
# Get the message in the confirm dialog
confirm_text = page.driver.browser.switch_to.alert.text
expect(confirm_text).to include('overlapping show windows on Sunday')
end
end
It's tough to test JavaScript behavior. But if you want to check confirmation message, it might be okay to test link attribute only without Poltergeist:
it 'requests confirmation' do
visit user_path(user)
delete_link = find_link 'Delete', href: user_path(user)
expect(delete_link['data-confirm']).to eq 'Are you sure?'
end
Here is the alternative:
it 'requests confirmation' do
visit user_path(user)
expect(page).to have_selector "a[data-confirm='Are you sure?'][href='#{user_path(user)}']", text: 'Delete'
end
This test cannot check if JS is working correctly, but it might be enough for the most of cases. (And fast!)

Rspec/Capybara - Select Form should trigger Onchange event when testing

I'm new to TDD and I actually wrote the code before the tests. The code itself works fine and from the main page the onChange event applies. However, when testing, even though a different option is chosen as the selected option(so the option does change), it seems that the onchange event is not actualled trigger when testing and it stays on the same page.it says expected: "/ctscans/index", got: "/" (using ==)
welcome_spec.rb
require 'spec_helper'
describe "Welcome Page" do
before {visit root_path}
subject{page}
describe "with Ctscan selected" do
before {select("CT Scan", :from => "procedure")}
it {should have_select('procedure', :selected => 'CT Scan')}
it "redirects to" do
select 'MRI', :from => 'procedure'
current_path.should == ctscans_index_path
end
end
end
views/welcome/index.html.erb
<%=select_tag(:procedure, options_for_select(#paths, :selected => #paths[0]),:onchange => "gotoNewPage()")%>
<script type="text/javascript">
function gotoNewPage() {
if (document.getElementById('procedure').value){
window.location.href = document.getElementById('procedure').value;
}
}
</script>
You haven't indicated that the spec needs to use a javascript driver. Try
describe "with Ctscan selected", js: true do ...

Rails: Capybara doesn't find a div element

I'm using Capybara with RSpec to check my Rails project.
I'm testing errors when form fields are not correctly filled. Here is the form (using haml):
= form_tag '/objects', :class => 'objects-form' do
%ul
%li= select_tag 'object', options_for_select([['Select an object', ''], ['Car', 'CAR'], ['Keys', 'KEYS'], ['Ambrella', 'AMBRELLA']]), :id => 'select-object'
%li= text_field_tag :quantity, nil
%li= submit_tag 'Buy', :id => 'object-submit'
When an error occurs (in this case, not choosing an object but only a quantity), a flash message is displayed (with a .flash-error class name):
- flash.each do |type, msg|
= content_tag :div, msg, :class => "flash-#{type}", :id => 'flash-msg'
So, here is my Capybara test:
it 'Should return error when no object selected' do
within 'form.objects-form' do
fill_in 'quantity', :with => '1'
click_button 'object-submit'
current_path.should == objects_path
save_and_open_page
page.should have_css 'div.flash-error'
end
end
But I get the following Capybara error:
Failure/Error: page.should have_css 'div.flash-error'
expected #has_css?("div.flash-error") to return true, got false
The opened page (using save_and_open_page) shows the error message with the appropriate div:
div#flash-msg.flash-error
I'm using the latest version of Capybara (since many similar problems on Stackoverflow are relative to old versions of Capybara) using gem 'capybara', :git => 'https://github.com/jnicklas/capybara.git'
Any idea? Thank in advance.
EDIT:
Corrected typing mistake on Capybara code, as detected by Alex.
The problem is your selector you are using in capybara is div#flash-error but your div is div#flash-msg.flash-error or more simply div.flash-error.

Resources