Reputation: 5
I will first forewarn I am brand new to Watir & Ruby and i'm kind of flying by the seat of my pants with use of google to figure things out.
So my issue is that I am going to a site for work and entering a username and password. After that I am trying to ideally verify the username and password are there and entered correctly. Now the password is being hidden so I thought id handle that once i'm more advanced but the username field no matter what i've tried I always get a failing test "browser.text.include?" seems like it should work and I have tried different kinds of waits & pauses and that doesn't seem to be the issue.
I have watched the test run a few times and it doesn't seem like a pausing issue of any sort. Now I am trying to view an input inside of a Div so I wasn't sure if that could be why it's failing.
Lastly most of the resources i've found for watir and ruby tutorials seem rather old and many of the early examples don't work. If there are any recommendations from users that would be greatly appreciated.
#require needed gems for the test case
require 'rubygems'
require 'watir-webdriver'
require 'rspec'
require "watir-webdriver/wait"
#Open a browswer and wait 60 seconds
browser = Watir::Browser.new :ie
browser.driver.manage.timeouts.implicit_wait = 60 #60 seconds
#Enter a Username
browser.text_field(:id => 'username').when_present.set("superuser")
#Enter a Password
browser.text_field(:id => 'password').when_present.set("password")
#If username of superuser is not found display the test passes. If username of superuser is found then the test failed
puts " Actual Results:"
browser.text_field(text: 'superuser').wait_until_present
if browser.text.include? 'superuser'
puts " Test Passed! Username & Password have not been cleared since the user has never clicked the clear button Actual Results match Expected Results. "
else
puts " Test Failed! Username has been cleared even though the clear button was never pressed. "
end
Upvotes: 0
Views: 1110
Reputation: 46836
Text fields, specifically input
elements, do not have a text node. As a result, you will get an empty string when using the text
method on a text field. Similarly, there will be no text when calling browser.text
.
The text you see in the text field is actually the value of the value
attribute. You have to check that attribute instead. For example, you can use the value
method:
browser.text_field(text: 'superuser').value.include? 'superuser'
Upvotes: 1