Ruby equivalent call for verifyEditable selenium command - ruby

Using ruby selenium-webdirver, I want to check whether input element is editable or not. To check this, in IDE, we've selenium command - verifyEditable. In Ruby, is there any way(method), we verify whether element is editable?

Have you tried $driver.find_element(:id, "elementID").enabled?? This works as long as you're only checking on an element that you know to be an input field.
In my tests, I just declare a method in the same class/module that hosts the browser control:
def element_enabled?(how, what)
if $driver.find_element(how, what).displayed? && $driver.find_element(how, what).enabled? then
true
else
false
end
rescue Selenium::WebDriver::Error::NoSuchElementError
false
end
And then you can just use the method with:
element_enabled?(:id, "elementID")
Having the foo.displayed? && foo.enabled? prevents your script from running into errors if the element you're trying to enter input into isn't currently visible.
Including the rescue command just makes the method return false if you've asked for an element that does not exist on the page. If you leave out the rescue block, your script will exit and return the Selenium error message complaining that the element does not exist. My preference is to always get a true or false instead (like with verifyEditable), but you may find that you want your script to exit with an error if the element isn't present (like with assertEditable).

disabled = find('input_id')['disabled']
disabled.should_not == 'disabled'
disabled.should_not == 'true' # just to be safe
or with selenium driver you can just go ahead fill it in with some value
fill_in 'input_id_or_label', with: "some input value"
If the field is disabled selenium will complain (but some headless driver would not, for example capybara-webkit):
Selenium::WebDriver::Error::InvalidElementStateError: Element is disabled and so may not be used for actions
However I don't think it's a good idea to use selenium to perform such view test -- it's slow and can be flaky, and results may differ from driver to driver. If the view is rendered with input disabled by default, a view test should do the job; otherwise if it's some javascript that disables the input it should be a simple jasmine/mocha javascript test.

Related

How can I get Watir to make a fresh reference on a non-stale element?

A portion of some tests I am writing calls for checking if an option gets removed from a select list once that option has been used. I am inconsistently getting this error: timed out after 60 seconds, waiting for {:xpath=>"//select[#id = 'newIdentifierType']//option", :index=>31} to be located (Watir::Exception::UnknownObjectException)
It causes my test to fail maybe 2-3 times out of 10 runs and seems kind of random. I think Watir is looking for the "old" select list with this ID since it caches the element and may also include that it had 32 items, but it times out since a select list with this ID and 32 items no longer exists. The new select list has the same ID but only 31 items.
Is there a way to always get a new reference on this element even though it's not technically going stale? Am I experiencing this problem due to a different issue?
My current code for getting the options in the select list:
#browser.elements(:xpath => "//select[#id = 'newIdentifierType']//option")
I am using Ruby/Cucumber with Selenium and Watir Webdriver level. I first tried defining the element as a select_list in a page-object but moved it to the step definitions using #browser.element to see if that would stop the timeout. I thought it may ignore Watir's cached elements and get the most current one with the ID, but that does not appear to be the case.
Please avoid using XPath with Watir. Everything you can do with XPath, Watir has a much more readable API to handle.
To check for a specific option not being there, you should avoid collections and locate directly:
el = browser.select_list(id: "newIdentifierType").option(value: "31"))
# or
el = browser.select_list(id: "newIdentifierType").option(text: "This one"))
Then to see if it has gone away:
el.stale?
# or
el.wait_until(:stale?)
That won't test the right thing if the entire DOM has changed, though, so you might need to just relocate:
browser.select_list(id: "newIdentifierType").option(text: "This one")).present?
If you are intent on using a collection, the correct way to get the list of options is:
options = #browser.select(id: 'newIdentifierType').options
el = options.find { |o| o.text == 'This one' }
# Do things
el.stale?

Capybara can not find element by id

Capybara is not able to find a <p> tag by it's id in my cucumber test. I'm able to see the element when I save_and_open_page. But I can't locate it with has_css? or find:
pry(#<Object>)> page.html.scan(/notice_sent/).count
=> 1
pry(#<Object>)> page.html.scan(/id=\"notice_sent\"/).count
=> 1
pry(#<Object>)> page.find('#notice_sent')
Capybara::ElementNotFound: Unable to find css "#notice_sent"
from /Users/me/.gem/ruby/2.1.7/gems/capybara-2.4.4/lib/capybara/node/finders.rb:41:in 'block in find'
What am I missing?
By default Capybara doesn't find elements that are not visible on the page. You can try
page.find('#notice_sent', visible: :all)
to see if that's the case. If so, and you're testing an app, then you should perform whatever actions a user would perform that would make that element visible, and then check for its presence.
Though I am a newbie to Capybara, I think this might work for you !!
page.find('#notice_sent', :visible => false)
Also, add this code to your env.rb file.
Capybara.ignore_hidden_elements = false
The accepted answer is good in most cases, however, there are times that elements may dynamically present; and not just hidden. When using find and the element is not present we get an error very similar to when it is present, but hidden. How do we handle that case?
Capybara::Node::Finders#all Also known as: find_all, has the same visibility option as find, but will not throw an error when there are no matches.
page.all('#notice_sent', visible: :all)
This is very similar to where vs find in ActiveRecord, and there are uses for both approaches.
if you want to return the matching element or nil instead of an Array / collection, then you can append first and get a result that closer matches the behavior of find without the error.
page.all('#notice_sent', visible: :all).first
I think, it is time to update capybara for the very beginning.
And, I can not test it out now, but try page.html.find('#notice_sent')

Watir Check Text exists

I get it not the Watir in conjunction with rspec find my text.
The following code leads to this error.
Code:browser.text.include?("Coverberechnung").should == true
Error1: expected: true got: false (using ==)
Error2: Using should from rspec-expectations' old :should syntax
without explicitly enabling the syntax is deprecated. Use the new
:expect syntax or explicitly enable :should instead. Called from
Maybe I can have a help
URL for the Site: enter link description here
You're looking for an initially-capitalized string (i.e. Coverberechnung), but that string is all-capitalized on the test site (i.e. COVERBERECHNUNG).
Try:
browser.text.include?("COVERBERECHNUNG").should == true
or (using expect syntax)
expect(browser.text.include?("COVERBERECHNUNG")).to be true
I prefer to use
expect(browser.text).to include('coverberechnung')
If I wanted to be indifferent about case I would do something like this:
browser.text.upcase.include?("COVERBERECHNUNG").should == true
or
browser.text.downcase.include?("coverberechnung").should == true
this way you can avoid text comparison that may have varying cases.
also for you last problem #3:
use
expect(browser.text.downcase.include?("coverberechnung")).to be true
they deprecated that version some time ago. so you can give this one a try with no issue.
NOTE: only one caveat is that this will ignore case. As described above.
Or you can just do the following:
fail unless #browser.text.include? 'COVERBERECHNUNG'
Or if you want to target that exact string, you could do the following instead:
#browser.h1(text: 'COVERBERECHNUNG').wait_until_present
This code will raise an exception after 30 seconds (thus, failing your test in the process) if it can't find a header element with the text: 'COVERBERECHNUNG'. You can also override the waiting or polling process by doing the following:
#browser.h1(text: 'COVERBERECHNUNG').wait_until_present(10)
That code will check that h1 element within 10 seconds.

#driver.find_element(:id=>"body").text.include?(textcheck) not verifying the text only the id

I am using Selenium-WebDriver for Ruby and I am trying to verify that text is present on a page. I have done many searches and tried many things and the best answer I have found is to use something like
def check_page(textcheck)
if verify {#driver.find_element(:id=>"body").text.include?(textcheck)}
yield it_to "fail"
else
yield it_to "pass"
end
end
The expected outcome if the value of textcheck is present in the body would be pass and if the value of textcheck is not present in the body it would be fail. What is actually happening is if :id=>"body" is present then it is pass and if it is not present then it is fail regardless of .text.include?(textcheck)
If anyone could point me in the right direction for how to verify text is present on a page using Selenium-WebDriver in Ruby it would be greatly appreciated. I have found workarounds for certain cases where I can do
verify {#driver.find_element(:tag_name, 'h1').text!=(textcheck)}
but the element I am trying to verify I can't get to so easily. I looked into css locators and was very confused on how to simplify the tag so I could use it. Any help would be greatly appreciated. Thank you very much. If you require any more information from me please let me know and I will provide it as soon as possible.
I am using Ruby 1.93 with Selenium-WebDriver 2.25 testing in Firefox 14.0.1
I do it this way
#wait = Selenium::WebDriver::Wait.new(:timeout => 30)
begin
#wait.until { #driver.find_element(:tag_name => "body").text.include?("your text")}
rescue
puts "Failure! text is not present on the page"
#Or do one of the options below
#raise
#assert_match "true","false", "The text is not present"
end
UPDATE
Answer to your question in the comments section.
There are two kind of "waits", implicit wait and explicit wait. You can read more about it here. The reason your code failed was because you were searching by "id"=>"body" and not by "tag_name"=>"body". Usually all text is encompassed within the "body" HTML tags in your DOM.

How to handle security alert in Firefox with Selenium

I'm using selenium-webdriver with ruby to write automated tests.
Chrome and the chromedriver binary work really well, but I have an issue with Firefox that is related to the configuration of the browser and that's making my tests fail, whereas they pass with Chrome.
When executing the tests in Firefox, sometimes I get an alert with this message:
Although this page is encrypted, the information you have entered is
to be sent over an unencrypted connection and could easily be read by
a third party
And it breaks the execution. Is there a way of disabling this warning in recent Firefox versions (10+) or handling this behavior with Selenium?
In the process of finding a solution for this, I think I might have found a bug in Capybara (v1.1.2).
I managed to get around this problem using the following approach, instead of using the click from capybara (which would not allow me to capture an exception), I started using the click method from selenium-webdriver.
It seems that when Firefox triggers this alertbox, a string with the message
Although this page is encrypted, the information you have entered is
to be sent over an unencrypted connection and could easily be read by
a third party
is returned as a result of object.native.click, otherwise the string
ok
is returned.
# Define the click method in ruby and call it when you're executing a 'click'
# Public: Click the object.
#
# object - Capybara::Node::Element object to click.
def click object
return_string = object.native.click
check_firefox_alertbox if return_string == "ok"
end
def check_firefox_alertbox
if #current_browser == :firefox
#session.driver.browser.switch_to.alert.accept
end
rescue Exception => e
puts e
end
Here is what you can do. Type about:config in the firefox. You would be presented a number of options (once you pass through a warning message).
You have to look for security.warn_leaving_secure; and security.warn_leaving_insecure. Make both of them false. And you would be good to go.
Please note: This would work only on the FF instance you have made modification to, so you will need to use firefox profile to launch this instance.
Hope this helps.
Actually this meant to be a comment but I need to go above 50 in order to be able to comment..I suppose by 'breaking' the execution you mean that of the Ruby Script right? What happens to Firefox? Needs a click to proceed? If that is the case you can improvise by capturing the Ruby error after inserting the sensitive code (where it breaks) between a BEGIN and a RESCUE clause..Something like this..
BEGIN
.
.
Code that is giving you a headache
.
.
RESCUE
.
Capture the exception and give Ruby a chance to continue the script normally.
.
.
END
Alternatively if you don't fancy the above solution you can go to Firefox and then type in the address box about:config. Filter by 'security.warn' and set to false all the boolean variables you see there. Good riddance, fingers crossed ;)

Resources