Capybara provides some methods to navigate the application through links or buttons:
Click_link_or_button
Click_link
Click_on
Capybara do not uses the fixed strategy to locate elements. It uses its own “best guess” pattern to locate elements in the webpage. Capybara uses either of the element properties when trying to locate elements on the webpage. Text or Title or value or id attribute of input, button or anchor tag.
Let suppose we have the following HTML:
<div class="fields">
<input id="user_session_submit" class="submit signin" type="submit" value="" name="commit">
</div>
<div class="fields" style="text-align:center;margin-top:13px;">
<a id="forgotPswd" class="forgotPswdSignIn" href="javascript:void(0)">Forgot Password?</a>
</div>
In the above HTML, as you can see that there is a link for "Forgot Password" and a button to "Submit".
Given(/^I clicked on Forgot Password link through text/$)
click_link_or_button 'Forgot Password?'
Given(/^I clicked on Forgot Password link through class attribute/$)
click_link 'forgotPswdSignIn'
Given(/^I clicked on Forgot Password link through class attribute/$)
click_on 'forgotPswd'
So, in the above examples we have used all the three methods with different attributes.
Now if we want to click on the button in the above HTML then we can use the above method in the following manner:
Given(/^I clicked on Submit button through id/$)
click_link_or_button 'user_session_submit'
Given(/^I clicked on Forgot Password link through class attribute/$)
click_link 'submit signin'
So, this way we can click on buttons in Capybara.
0 Comment(s)