I'm working with Ruby on Rails 3, Cucumber, and Capybara
I've been searching for quite some time, and I can't figure out how to find a specific page element within a css tag. In my case, I need to make sure that a name is found inside of a table, and not in the "Welcome [Name]".
I tried something like:
within('table') do
page.body.index("[Name]")
end
And I have a table with id='table'.
But I'd like to know how to do this for any css element, such as:
within('h2') do
page.body.should have_content ('stuff')
end
I think my problem has to do with page.body, but I'm not sure how to contain it to a particular css tag.
Thanks in advance!
Capybara's within matcher only matches the first result, so if you have multiple h2 tags, it'll only look in the first one.
Instead, try have_css with the :text option.
page.should have_css("#table", :text => "[Name]")
page.should have_css('h2', :text => 'stuff')
To find a specific element:
page.find('#table').should have_text('stuff')
I guess all answers should work but now Capybara doesn't use should anymore it uses expect
expect(page).to have_css("#table", :text => "[Name]")
expect(page).to have_css('h2', :text => 'stuff')
I've done stuff like this:
page.all(:css, 'button.btn-primary.days.active').size.should == 1
To check if there are any elements that contain a specific set of classes.
I didn't need to look for a particular text value of the element though.
I just wanted to ensure the existence of the element, and how many of them there were.
Related
I have a field whcih I am auto populating based on a dropdown on the page.
i.e. I select task_type and the task_name has task_type populated.
I can't work out how to test this. I basically just want to test what the current content of the field is.
Everything I try seems to be able to find the field but not check the content of it.
Then(/^"(.*?)" should contain "(.*?)"$/) do |field, text|
page.should have_field(field, :text => value)
end
I'm assuming I need to do some js trickery to get the info from the browser, but can't seem to work it out.
And I should see "Annual Accounts" in "task[name]"
Have tried task_name aswell and both fail with
find(field).should have_text(text)
With message
Unable to find css "task[name]"
if I use
page.should have_field(field, :text => text)
Then they fail with:
expected to find field "task_name" with text "Annual Accounts" but there were no matches. Also found "",
You can use the have_text helper
find(field).should have_text(text)
Right, both of these work:
Not sure what the difference between them is.
I also think this does an exact match. If anyone could let me know how to do a "contains" type match that would be really handy.
Then(/^I should see "(.*?)" in "(.*?)"$/) do |text, field|
# page.has_field?(field, :with => text)
page.should have_field(field, :with => text)
end
Two questions from a beginner.
Q1- Is it possible to assert the existence of an HTML node by ID and class?
For example, to see if the following element exists:
<div class="drawer" id="first"....>
I've seen you can use something like:
page.should have_css('div.drawer')
page.should have_css('div#first')
but can we somehow query for the existence of both parameters, I've tried the following and didn't work:
page.should have_selector("div", :class => "drawer", :id => "first")
Q2- Is it possible to add 2 selectors to the 'within' capybara method, ie, I've seen you can limit the scope by doing:
within("//div[#id='first']") do
but can we filter that DIV by adding id='first' and class='drawer' somehow?
Many thanks!
You can combine the selectors.
For your first question, the following checks for a div with id "first" and class "drawer":
page.should have_css('div#first.drawer')
For your second question, the within block can use the same css-selector as above:
within('div#first.drawer') do
Or if you really prefer xpath, you can do:
within("//div[#id='first' and #class='drawer']") do
A good reference for css-selectors: http://www.w3.org/TR/CSS2/selector.html
I have a page with several links like that <a href='/bla/bla/bla/?page=xxx>text</a>
I want to match certains xxx values links using Capybara and RSpec, i don't care about bla/bla/bla part of href attribute.
page.should have_selector("div.class ul li a", :href => "page=2")
doesn't work,
also
page.should have_xpath("//a[#href='page=2']")
is not an option because i don't know the full href attribute value.
PS: also didn't find any complete Capybara API documentation just to get all available methods and parameters' description. I there such thing?
Try using contains:
page.should have_xpath "//a[contains(#href,'page=2')]"
Try this:
link = page.find('div.class ul li a')
link[:href].should match(/page=2/)
More information here.
I use rspec 2.6.0 and Capybara 1.1.1 for acceptance testing.
With a view like the following:
<tr >
<td>Team 3 Name</td>
<td>true</td>
<td>Show</td>
<td>Edit</td>
<td>Deactivate</td>
</tr>
<tr >
<td>Team 4 Name</td>
<td>true</td>
<td>Show</td>
<td>Edit</td>
<td>Deactivate</td>
</tr>
I want to write an acceptance test that states: "Team 3 does NOT have the 'Deactivate' link." I expect the following to fail:
within('tr', :text => 'Team 3 Name') do |ref|
page.should_not have_selector('a', :text => 'Deactivate')
end
But it passes. To further test what is going on, I wrote the absurd:
lock = false
within('tr', :text => 'Team 3 Name') do |ref|
page.should have_selector('a', :text => 'Deactivate')
page.should_not have_selector('a', :text => 'Deactivate')
lock = true
end
lock.should be_true
Which passes as well.
I am assuming from this that the scope the have_selector() call is using is not limited by the within() block, but I am not sure why this is. The capybara documentation uses this pattern and does not seem to mention any gotchas.
What is the correct way to use within to limit the scope of my select?
Thank you.
/Salernost
Still learning Capybara myself, but have you tried have_link instead of have_selector? Also I don't think you need |ref|. For example:
lock = false
within('tr', :text => 'Team 3 Name') do # omit |ref|
page.should have_link('Deactivate')
page.should_not have_link('Deactivate')
lock = true
end
lock.should be_true
Update October 13, 2012
Having come a little further with Capybara, I see several potential issues here:
within may silently ignore the text field. You'll notice that the examples only show CSS or XPath finders without additional arguments.
If within does use text, it may not work here because you are asking it to look at the <tr>, but the text is in the <td>.
It's quite possible that the page subject still targets the entire page even if you are in a within block. The within examples are mostly about using fill_in or click. The exception is the example under Beware the XPath // trap.
As for creating a within block, you can either give your table rows unique ids and search for them using CSS, or you may be able to write a specific XPath targeting the first matching row.
The problem with the latter is that you want use the within on the <tr>, but the text you are using for your targeting is inside a <td> subelement. So for example, this XPath should find the table cell containing the text Team 3 Name but then you are only working within that first cell, not the whole row.
within(:xpath, "//tr/td[normalize-space(text())='Team 3 Name'") do
There are ways to "back up" to a parent element using XPath but I don't know how to do it and I've read that it's not good practice. I think your best bet here might be to just generate ids so your rows start like this:
<tr id="team_3">
then target them with a simple
within("tr#team_3")
I would also recommend Mark Berry's final approach he mentioned of adding id's to each of your table elements.
<tr id="team_3">
then target with
within("tr#team_3")
Capybara has given me issues when selecting by xpath in that it doesn't seem to work consistently, especially with CI services.
I also want to note on the same answer this section:
It's quite possible that the page subject still targets the entire page even if you are in a within block. The within examples are mostly about using fill_in or click. The exception is the example under Beware the XPath // trap.
This may have been the case in an older version, but in the current version of Capybara, calling page inside of a within block only inspects the part of the page targeted. So, using Mark's above example:
within("tr#team_3") do
expect(page).to have_content 'Team 3 Name'
# => true
expect(page).to have_content 'Team 4 Name'
# => false
end
have_selector seems to ignore :text and :content options. I had to use something like this instead:
within 'a' do
page.should have_content 'Deactivate'
end
The solution is to not use within method:
expect(page).to have_css('tr#team_3') do
without_tag('a', text: 'Deactivate')
end
I'm fairly new to Rspec so apologies if this is a really simple question:
I want to check that there is some link on the page that has a link to a pdf.
Essentially something along the lines of:
rendered.should have_selector :a[href=~/\.pdf/]
but I'm getting the error undefined variable or method href.
Is there someway to do:
attribute.should =~ /regex/
You can use the CSS3 Substring matching attribute selectors for this case.
rendered.should have_selector 'a[href$=.pdf]'
using has_selector should do a substring match for you, so you could always try doing something like this:
response.should have_selector("a", :href => ".pdf")