I'm trying to click a button, but I'm getting the following error:
Unable to find link or button "My Tasks" (Capybara::ElementNotFound)
This is what I get when I inspect it on Chrome:
<a class="btn btn-lg btn-success" href="/tasks" role="button">My Tasks</a>
And this is my code (my steps.rb):
# go to my tasks page
def visitTasksPage
page.has_content?('Signed in succesfully') do
clicK_link 'My Tasks'
end
self
end
I'm new to Ruby Capybara, can anyone help me please?
The problem might be that it's really a link that you're trying to click (an Some Text tag) not a button (a <button type="button">Some text</button> tag). To not have this problem, I recommend that you use the following method:
click_on('My Tasks') # clicks on either links or buttons
Or it could be that the link or button does not appear on the page before Capybara times out. Capybara by default waits 2 seconds before it times out, but you can extend it with
Capybara.default_max_wait_time = 9
Another thing you can do to debug this problem is add a call to binding.pry right before clicking the link/button and check if the button is actually there with the text you expect it to have:
page.has_content?('Signed in succesfully') do
binding.pry # at this point you can test if the link is really there with page.has_content?('My Tasks')
click_link 'My Tasks'
end
Note you would need to install pry by adding it to you Gemfile or with gem install pry.
Related
(Click for image) I am working on an project to write a scenario to test login feature. For some reason capybara is not accessing dynamic elements.
Steps to Reproduce:
1) visit Redfin.com(for example)
2) click on Sign in button
3) a dynamic popup dialog appears
4) click on "continue with email" and try and enter details and clicking on submit.
I am not able to find any of the elements with find(#) and inturn not able to click on submit or enter details.
Also I believe the webapp is build with React.
Please do let me know how to handle this.
<div class="emailSignInButtonWrapper" style="position: relative;">
<button class="button Button tertiary emailSignInButton v3" type="button" tabindex="0" data-rf-test-name="submitButton">
<span>
<span class="signInText">Continue with Email</span>
</span>
</button>
None of the elements you are talking about have ids so a CSS find using #<id> (find('#my_button').click) isn't going to work. However to click that button you should just be able to do
click_button('Continue with Email') # case of the text matters
or
click_button(class: 'emailSignInButton')
This all assumes you are using a driver with Capybara that supports JS - https://github.com/teamcapybara/capybara#drivers
Here's code that shows it works if using a JS capable driver
require 'capybara/dsl'
require 'selenium-webdriver'
session = Capybara::Session.new(:selenium_chrome)
session.visit "https://www.redfin.com"
session.click_link('Sign In', href: nil)
session.click_button('Continue with Email')
Here's what worked for me on the redfin site:
scenario "bring up signup form on redfin" do
visit 'https://www.redfin.com'
find('a', :text => 'Sign In').click
click_button('Continue with Email')
end
My system:
Windows 10 Pro 64-bit
ruby 2.1.9p490 (2016-03-30 revision 54437) [x64-mingw32]
FireFox 47.0.1
To start, here is the code I'm dealing with:
<div class="dz-style col-sm-7" is="null">
<div is="null">You can drag and drop your supporting document files here, or click to select files to upload.</div>
<input style="display: none;" multiple="" is="null" type="file"></div>
Here is my watir testing code:
Identify and confirm file is valid
local_file = '/Users/tom.feodoroff/Desktop/Charlie_Snoopy.jpg'
File.exists? local_file
raise "error" unless File.exists? local_file
Change the style display so I can interact with control
element = BROWSER.input(:type => 'file')
puts element.attribute_value('style') #display: none;
script = "return arguments[0].style = 'display: inline'"
BROWSER.execute_script(script, element)
puts element.attribute_value('style') #display: inline;
Use suggested syntax to add file to application
BROWSER.file_field(:type => 'file').set(local_file)
This doesn't generate any errors, but it also doesn't attach the file so that my Submit button becomes active. Do I need a different version of Ruby (Watir) to make this work or is there something I'm missing?
I don't understand why, but I added sleep(5) just before clicking the submit button, and now it works. My file icon now appears on the page and the submit button is active and successfully submitted the form. Things that make you go 'hmmmm' :)
Thanks for the responses. Hopefully this will help someone else with this problem?
How do I write a ruby watir script for switching to a modal dialog (iframe) of a third party company (payment services)? I always get element not found error message.
Given this HTML:
<frame id="foo">
link
</frame>
This snippet shows how to method-chain so that the element is found:
b.link(href: "http://www.example.org").exists?
#=> false
b.frame(id: "foo").link(href: "http://www.example.org").exists?
#=> true
If clicking on any element triggers a modal dialog (iframe) where you will have to enter texts in a textbox and click on Save button, then it will be like this-
#browser.iframe(index: 0).text_field(id: 'title').set 'Test Title'
#browser.iframe(index: 0).button(value: 'Save').click
How does capybara access/finds/matches html elements? I come from a watir background so for me capybara is a bit high level and I can't quite really get the grasp of it even with the tutorials. let's say for example we have element
<input id="submit-button" type="submit" value="Post Your Question" tabindex="120">
so how do I make capybara click that button? On watir-webdriver I could easily do a browser.button(:id => "submit-button").click what's the capybara equivalent?
click_button("Post Your Question") should work for you. Check out the Capybara Cheat Sheet.
EDIT: To expand on this when you want to click a button you will use the click_button method and pass the value as a parameter to this method.
I have a particularly difficult form that I am trying to click the search button and can't seem to do it. Here is the code for the form from the page source:
<input type="image" name="" src="http://images.example.com/WOKRS53B4/images/search.gif" align="absmiddle" border="0" onclick="return check_form_inputs('UA_GeneralSearch_input_form','search');" title="Search" alt="Search" class="">
I am trying to do the standard mechanize click action:
login_page = agent.click(homepage.link_with(:text => "Search"))
Is this because the button uses javascript? If so, any suggestions?
I struggled with this too, especially since my form had multiple buttons.
There are multiple ways to submit a form (with many using a 'form_with' block), but this helped me:
# get the form
form = agent.page.form_with(:name => "my-form")
# get the button you want from the form
button = form.button_with(:value => "Search")
# submit the form using that button
agent.submit(form, button)
See more info here
Also, make sure you upgrade to the latest mechanize. I was using mechanize 1.x, which was giving me "undefined method" errors for the code above.
It is not a link, it is a button. What you need to do is look for the form (for example, with form_with) and then look for the ImageButton and submit it.
button = form.button_with(value: 'Search')
form.click_button(button)