Conner
Conner

Reputation: 413

Text fields are not being filled in correctly in watir-webdriver

I've recently decided to switch from using watir to using watir-webdriver for my test scripts. It's working great with the exception of this odd behavior. When it comes to a form with multiple text fields, such as a first name field and a last name field, it fills these in incorrectly. It usually fills in the first name and then fills in a letter or two of the last name, then jumps back to the first name field and appends the rest of the last name onto the first name.

An example would be first name/last name text fields. I write the script to fill in Jim for the first name and Johnson for the last name. When I run the script I will end up with Jimhnson for the first name and Jo for the last name. It's like it decides to jump back to the first name field in the middle of the last name field. This is something I never experienced using Watir and have searched all around for similar instances but have had no luck. Hopefully someone can help. Here's a sample of the code I'm using. I don't want to use "sleep 1", but have found it's the only thing that prevents this odd behavior.

 def fill_in_applicant_name(fName, lName)
  puts "Fills in the applicant first name"
  @@b.text_field(:id, "ApplicantFirstName").set(fName)

  #sleep 1

  puts "Fills in the applicant last name"
  @@b.text_field(:id, "ApplicantLastName").set(lName)
end

Upvotes: 1

Views: 1666

Answers (2)

Conner
Conner

Reputation: 413

Well, it has been a week or so of creating and running watir-webdriver tests with firefox's native events turned off. This has definitely been the answer to the problem. Since, turning native events off, I have not had a single occurrence of this unusual behavior. The tests/checks are running well and I only have one little issue. One of the app's pages a pop-up message appears and never disappears as you go on to the next pages. It is a little annoying, but it doesn't affect the tests in any way. Thanks everyone, for your input. Here's the code again for turning native events off.

profile = Selenium::WebDriver::Firefox::Profile.from_name "webdriver"
profile.native_events = false
browser = Watir::Browser.new :firefox, :profile => profile

Upvotes: 1

adam reed
adam reed

Reputation: 2024

If you need a sleep statement, it sounds like there's some AJAX or Javascript firing off after the text_field has been populated? Since you are explicitly naming the text_fields that are being filled in there is really no other reason I can think of as to why the text should end up in one box.

The parentheses around your variable names are superfluous and not very Ruby-like. They can be removed.

My approach to this method would use when_present in order to make sure that the text field is accessible.

def fill_in_applicant_name(first, last)
 @browser.text_field(:id, "First").when_present.set first
 @browser.text_field(:id, "Last").when_present.set last
end

Using other wait methods as necessary if you receive an error

Watir::Watir.until(30) { @browser.text_field(:id, "Last").visble? }

If that doesn't work can you post some code from the page you're testing to help us out?

Upvotes: 2

Related Questions