Setting a text field that has a JQuery mask on it

2020-05-03 09:50发布

问题:

Using watir-webdriver, I am trying to set the value for a text field.

browser.text_field(:id, "phoneNumbers_value_input").set("5555551234")

When I run that command, I can see that watir found the field because the cursor is set on that field however no text is entered. I have also tried the send_keys and append commands but nothing seemed to work. No exception is thrown from these methods.

The only difference I could find between this field and the other fields on the page(which all work fine) is that it has this JQuery mask on it.

$(selector).mask("(999) 999-9999");

Any ideas on how to set the text field?

Edit:

Some more of the Javascript:

selector = '#' + id(field.id) + '_input';
if (field.format == 'phone') {
  $(selector).mask("(999) 999-9999");
}

HTML of the field:

<div id="phoneNumbers_value_form_item" class="form_item validated no_focus">
  <label for="phoneNumbers_value" class="form_label">phone</label>
  <input type="text" value="" name="phoneNumbers[][value]" id="phoneNumbers_value_input" class="text">
  <div class="tip">&nbsp;</div>
  <div class="tip_validating">

  </div>
  <div class="tip_error">

  </div>
</div>

回答1:

I don't see any element in the HTML you provided that would match the text input field being addressed in the ruby code at the top of your posting. e.g. there is nothing there with an ID of 'phone'

Based on the HTML in your question, I would expect the following to work

browser.text_field(:id, "phoneNumbers_value_input").set("5555551234")

Looking at the sample page you linked in the comments, when I use google chrome and the "Inspect Element" function on the input field with the ID of 'phone' I see that there are a number of event listeners associated with the field (blur, focus, input, keydown, keypress, unmask)

The 'focus' one gets my attention in particular, and seeing it there on that field makes me think that you might then need to first fire an event against the same element, such as the onfocus event, in order to activate the field, then try to set the value.

You'll notice that when you manipulate things manually, the field starts out blank, but as soon as it gets focus it displays the format for the input mask to the user, it may well be that this needs to happen first, before it see's any kind of input.

EDIT: In this case, based on feedback from the questioner, the answer turned out to be that they needed to first fire the 'unmask' event against the text field, and THEN .set the value they wanted, in order for things to work correctly when automating the testing. This doesn't exercise the field masking functionality, but then again I doubt the test mandate in this instance is to extensively test a 3rd party (JQuery) addin, and they are more concerned with the business logic etc in the back end, thus simply being able to set the value without the masking code getting in the way is what is needed.



回答2:

The problem has to do with focusing on the text field. Even with a .click() somehow webdriver ends up with the cursor at the end if the input field (see issue 2377). Pressing the HOME key moves the cursor to the beginning and allows you to type in the input field, and still have the mask functionality.

In Java:

WebElement txtPhone = getDriver().findElement(By.id("phoneNumbers_value_input"));
txtPhone.sendKeys(org.openqa.selenium.Keys.HOME);
txtPhone.sendKeys(phoneNumber);