Beware the XPath // trap

Beware the XPath // trap

Beware the XPath // trap

Let’s understand Beware the XPath // trap. In XPath the expression // means something very specific, and it might not be what you think. Contrary to common belief, // means “anywhere in the document” not “anywhere in the current context”. As an example:

page.find(:xpath, ‘//body’).all(:xpath, ‘//script’)

You might expect this to find all script tags in the body, but actually, it finds all script tags in the entire document, not only those in the body! What you’re looking for is the .// expression which means “any descendant of the current node”:

page.find(:xpath, ‘//body’).all(:xpath, ‘.//script’)

The same thing goes for within:

within(:xpath, ‘//body’) do

page.find(:xpath, ‘.//script’)

within(:xpath, ‘.//table/tbody’) do

end

end

Web Form

Capybara has user-friendly API for it as

<form id=”myform”>

<input type=”text” name=”Forename” value=”” />

<input type=”text” name=”Surname” value=”” />

<input type=”submit” value=”Go” />

</form>

The following Cucumber step definition would fill in and submit the form

When(/^I complete and submit the form$/) do

fill_in ‘Forename’, :with => ‘Matthew’

fill_in ‘Surname’, :with => ‘Robbins’

click_on ‘Go’

end

The above example clearly explains working with windows. We hope this is section is now clear to you. But, in case you need more assistance, we’re always here to help. So, try the links below for further assistance.

Make your resume stand out and become a Certified Capybara Testing Professional. Try free practice tests here!

A great career is just a certification away. So, practice and validate your skills to become a Certified Capybara Testing Professional.

Understanding Selectors
Integration Testing

Get industry recognized certification – Contact us

keyboard_arrow_up