Capybara Unable To Find Input Field - ruby

I am using the Ruby gem Capybara for automation purposes (having read the instructions on this website). I have a simple script that shows the problem I am having:
require 'capybara'
session = Capybara::Session.new(:selenium)
session.visit 'https://service.ringcentral.com/'
session.fill_in 'LoginName', with: '555-555-5555'
I have tried many different suggestions here on SO, and from other websites, with no success. I think this simply has something to do with the RingCentral website itself, because I do not have as much issue with other websites (such as google.com).

Since the login field is invisible (opacity: 0 to allow the emptyText to show through from below) Capybara won't find it by default. You can pass visible: false to fill_in to find non-visible elements, and in this case that will then trigger focus on the element, which will trigger it to change to opacity: 1 and proceed to fill in the data.
require 'capybara'
session = Capybara::Session.new(:selenium)
session.visit 'https://service.ringcentral.com/'
session.fill_in 'login-form-username-field-LoginName', visible: false, with: '555-555-5555'

I found the solution here! Capybara by default ignores hidden elements. I wanted to post my answer because the other solution was so hard to find because I didn't know that the field I was trying to fill in was a hidden element, so I do not consider this post a duplicate. If you are having a difficult time getting Capybara to find and fill in fields, try setting the ignore_hidden_elements option to false, even if your field is completely visible.
require 'capybara'
session = Capybara::Session.new(:selenium)
Capybara.ignore_hidden_elements = false
session.visit 'https://service.ringcentral.com/'
session.fill_in 'login-form-username-field-LoginName', with: '555-555-5555'

Related

Cannot find link using Capybara and Rspec while writing an integration test

I am trying to find a link by CSS classes and ids but always getting an error: Capybara::ElementNotFound: Unable to find css ...
The actual piece of code is:
find('#bucket_resources_containers > #user_base_widget.widget >
div.widget_header > div.right.may-
edit.control.button.add.icon.add_options > a.tasksy.options').click
The page source is: enter image description here
You gave us the answer in your comment, the element was not visible.
Short answer: find_link(selector, visible: :all).click
As capybara shows in the documentation:
By default Capybara will only locate visible elements. This is because a real user would not be able to interact with non-visible elements.
Only locate visible elements is an smart design of capybara, it avoids thinking that a user would be able to find the element.
The find_link method documentation doesn't help much when finding for hidden links because it only show these options: wait, href, id, title, alt, class:
#find_link([locator], options = {}) ⇒ Capybara::Node::Element
But you see on the finding documentation there was a visible option:
find_link('Hello', :visible => :all).visible?
find_link(class: ['some_class', 'some_other_class'], :visible => :all).visible?
This option visible comes from #all method, where you can see here. It can have these values:
true - only finds visible elements.
false - finds invisible and visible elements.
:all - same as false; finds visible and invisible elements.
:hidden - only finds invisible elements.
:visible - same as true; only finds visible elements.
So, in your case, you could use visible: false, if you really mean it to be hidden, or visible: :all if you don't care about the visibility.

How to find a css element on the page if it is display: none?

I'm running automated testing in Ruby.
I'm trying to write a script that finds id with display:none (visible only in mobile form), and clicks it.
Here is my code:
def open_menu
page.find[:css, "a[id='mobile-nav-link']", visible: false].click
end
It gives me an error:
Selenium::WebDriver::Error::InvalidSelectorError:
invalid selector: No selector specified
A quick search reveals similar code using round brackets, not square. I.e.:
def open_menu
page.find(:css, "a[id='mobile-nav-link']", visible: false).click
end
Selenium only interacts with elements the way a user would.
If the link is only visible in mobile, I would suggest one of the following.
View the page via the mobile URL.
It's possible that you may be able to use Ruby's javascript execute to either update the control to be visible, or set the value that indicates you are viewing the page via a mobile device.
Updating the control to be visible
This link discusses how to set an element to visible using javascript execute: Selenium Webdriver - click on hidden elements
This link discusses how to use ruby's javascript execute: How to execute Javascript in Ruby written Webdriver test?
Put the two together, and you get something like this?
def open_menu
elem = page.find[:css, "a[id='mobile-nav-link']", visible: false]
js = "arguments[0].style.height='auto' arguments[0].style.visibility='visible';"
driver.execute_script(js , elem)
page.find[:css, "a[id='mobile-nav-link']", visible: true].click
end
I'm not entirely familiar with Ruby syntax, so I make no guarantees it will work copy/paste.

SitePrism: sometimes find elements, sometimes doesn't while Capabara can

I am using Capybara and Selenium for testing my website. I also use with Site Prism for Page Object model. I can make every thing work now, however, I don't understand why sometimes actions with page elements donot work, while using "natively" Capybara work.
For example, I have a Page object:
class MyPage < SitePrism::Page
element :sign_in_link, :css, 'a.signin-link'
element :join_link, :css, "a.join-link"
end
and its implementation:
#mypage = MyPage.new
#mypage.sign_in_link.click
# It works at first, then after some repeated test round, it doesn't work sometimes, with error: NoMethodError <br>
While I use:
find(:css, 'a.signin-link').click #=> always work, but not Page Object model
So, why it happens? Have anyone experienced this problem?
By default site_prism disables Capybaras implicit waiting behavior while finding elements. This means to have the same behavior as your capybara example you would need to do
#mypage = MyPage.new
#mypage.wait_for_sign_in_link
#mypage.sign_in_link.click
You can read more about this in the site_prism README under "Using Capybara Implicit Waits"
Another options is to use site prisms "Load Validations" feature to ensure pages are loaded before starting to click on their elements

Ruby Mechanize: Programmatically Clicking a Link Without Knowing the Name of the Link

I am writing a ruby script to search the web. Here is the code:
require 'mechanize'
mechanize = Mechanize.new
page = mechanize.get('http://www.example.com/)
example_page = page.link_with(:text => 'example').click
puts example_page.body
The code above works alright. The text 'example' ((:text => 'example') has to be a link on the page for the code to work correctly. The problem, however, is that when I do a web search (bing, yahoo, google, etc), hundreds of links show up. How can I programmatically click a link without knowing the exact name of the link? I want to be able to click a link if the name of the link partly (or fully) matches a text that I specify or click a link if it has a certain url. Any help would be appreciated.
Mechanize has regular expressions:
page.link_with(text: /foo/).click
page.link_with(href: /foo/).click
Here are the Mechanize criteria that generally work for links and forms:
name: name_matcher
id: id_matcher
class: class_matcher
search: search_expression
xpath: xpath_expression
css: css_expression
action: action_matcher
...
If you're curious, here's the Mechanize ElementMatcher code

Watir can't locate frame which houses an element in Firefox but works fine in IE

I have a script:
BROWSER.frame( :name, 'FRAME_NAVIGATION' ).span(:text=>'foo').fire_event('onmouseup')
which clicks on elements of a tree view (inside a frame) which works great in IE, but when I set:
require 'watir'
Watir::Browser.default = 'firefox'
and run it I get:
Failure/Error:
BROWSER.frame( :name, 'FRAME_NAVIGATION').span(:text=>'foo').fire_event('onmouseup')
Watir::Exception::UnknownFrameException:
Unable to locate a frame using name and FRAME_NAVIGATION.
I get this with any elements inside a frame on my page. Not just the tree view. I've tried requiring firewatir as well, or just on its own with no luck.
Any insight would be greatly appreciated!
Thanks,
-M
You're not using the frame method correctly. Try this way:
BROWSER.frame(:name => 'FRAME_NAVIGATION').span(:text => 'foo').fire_event('onmouseup')
You may be able to diagnose the problem by adding a BROWSER.show_frames call. That would tell you what Firefox thinks it can see.

Resources