Using Capybara w/ Selenium, how would I click a table row? - ruby-on-rails

Would simply like to know if there's an alternative to click_link / click_button that can be used with any element as some of my clickable elements are not standard html clickables such as tr elements, but they still contain href attributes.
Javascript enabled.

Use Javascript then:
page.execute_script("$('whatever_you_want').click()");

I had the same situation with a html month view, I had to choose a day of month. I kept it as simple as I could and this is only one way of doing this.
# Choose July 22 (at this point in time)
assert page.has_css? '#calendar'
within '#calendar' do
find('td', :text => '22').click
end

Are you using cucumber? Not sure if it's any use to you, but here's a cucumber step definition for clicking anything with selenium:
Then /^I click "(.+)"$/ do |locator|
selenium.click locator
end

I have tried the javascript solution in past, it works in the sense that the button do gets clicked, but cucumber fails to recognise it as a completed step.

Related

Rails (/Rspec) (/Capibara): Test Icon is visible?

I'm currently using Rails, Rspec and Capybara.
How would I test that an Icon is actually visible and coming through on the client side? Something like this:
<i class="ionicons ion-man"></i>
I've searched stack overflow for "Rspec Icons" and "Rails Test Icons" and haven't found an existing question that can answer this.
edit:
I don't want to show that the css is present and visible on the page, but whether the icon is coming down the asset pipeline. I just tested this solution and a passing test after 'breaking' the asset pipeline so that the icons come through as boxes. Perhaps my request is out of the ability of capybara?
You can use the have_css selector.
expect(page).to have_css('i.ion-man')
You can check if a particular node (in your case li) with a specific class (in your case ionicons) exist and is also visible:
page.has_css?('li.ionicons', :visible => true)

Capybara - check for the existence of a data attribute on a link

I have a link with a specific attribute on my page. Using Rspec + Capybara how can I check for the existence of this link?
Foo
Does not work:
page.find(:css, 'a[data-content="This is a discrete bar chart."]')
Works:
page.find(:css, 'a#test')
I have the same query, but the quotes are different.
find("a[data-method='delete']").click
and works for me.
Here are some different ways:
expect(page).to have_selector("a[href='#'][data-content='This is a discrete bar chart.']")
page.has_selector?("a[href='#'][data-content='This is a discrete bar chart.']")
page.find("a[href='#'][data-content='This is a discrete bar chart.']") # returns the node found
If you don't have access to page but have access to rendered, then
expect(Capybara.string(rendered)).to have_selector("a[href='#'][data-content='This is a discrete bar chart.']")
It could be an issue of Capybara finding too many results. Maybe change 'find' to 'all' and see if you get an array of results that you could select from. Best of luck.
This worked for me when searching for a link's title attribute (tooltip):
Then(/^I should see tooltip "(.*?)" on link "(.*?)"$/) do |tip, link|
within('#results') do
link = find(:link, link)
expect(link['title']).to eql(tip)
end
end
NOTE: the within is optional and useful for reducing chances of ambiguity.
HTML Source code:
<a title="Owner: lfco Fire Department" href="/water_supplies/597df82566e76cfcae000018">Hydrant LFCO One</a>

Capybara not able to recognize ajax loaded elements.?

In my Rails 3 application, I have a "image" on my HTML page, which creates a "div" element in html page, when clicked.
I have to test the creation of this new "div" through RSpec (and i am using Capybara for views based testing).
I have written the following code :
it "clicks the extended details button" do
Capybara.default_wait_time = 5
within('.table_expand') do
find("#img_dealer_code_04039").click
should have_selector('#extended_details_04039')
end
end
yeah i have already added, :js=>true in corresponding describe.
I thought the problem would be of Ajax time , so i added Capybara.default_wait_time = 5 for it to load properly. (But it didnt work)
Also i tried putting :visible=>true in line should have_selector('#extended_details_04039') , but with no success..
Is there anything that i am missing..??
Please help me...I am stuck with this from quite a long time..!!
https://github.com/rspec/rspec-rails/issues/478
are you sure that you don't have duplicated ID on site?
Try to save it to file and check the source for duplications.
To see what's going on the page try
page.save_and_open_page method
Is img part of your ID or is it the tag type? I believe you shoul have
find("img#dealer_code_0409").click

How to select an option in Rails/Capybara/Selenium

The 'select' method (part of Capybara DSL) doesn't work under ':js => true', but it works otherwise. I am trying to test adding/removing items between two select boxes in Rspec, so need to trigger javascript associated with the clicking of 'Add' and 'Remove' buttons.
It's not stated anywhere in Capybara's document that its DSL will not work with ':js => true' (which enables a javascript driver - in my case the default Selenium). Another way to tackle the issue might be to do the clicking through Selenium, but I could not find any document, especially in the Rails/Rspec/Capybara context.
Can somebody share some examples/links? A complete syntax reference of Capybara/Selenium would be great. Many thanks!

How to test jQuery TokenInput field using Selenium

I'm unable to test a Tokeninput field in a form using selenium. The situation is when we type something, it gives a list to options to select but those options aren't part of the DOM. The text fills the field but doesn't select the item.
The code which I have written is:
Given admin user is on schedule interview page
And he select "obie[1]" and "DHH[1]" from the candidate name(s) auto sugget field
**step defination**
Given /^he select "([^"]*)" and "([^"]*)" from the candidate name\(s\) auto sugget field$/ do |arg1, arg2|
within(:css, "#interview_template_candidate_names_input") do
fill_in('tmp',:with => arg1) --tmp is name of the token input field
find("li:contains('obie[1])'").click
save_and_open_page
end
end
I finally succeeded in making this work. Here's the gist: https://gist.github.com/1229684
The list is part of the dom (div.token-input-dropdown), it's added as the last child of the body element, which is probably why you didn't see it.
If you understand what the tokeninput plugin is doing, you can get a better idea of what you need to do. For each tokeninput you create, the plugin:
creates a ul.token-input-list (immediately before input#your_input_id)
creates a ul.token-input-list input#token-input-your_input_id
hides the input#your_input_id
creates a div.token-input-dropdown
So the most challenging part is finding the correct ul.token-input-list, because you have to find it based on its position relative to the original input, and the selenium webdriver doesn't let you navigate the dom.
After that, you just fill in the input#token-input-your_input_id and "click" on the div.token-input-dropdown li option that matches what you're looking for.

Resources