Setting a text field that has a JQuery mask on it

前端 未结 2 1758
长情又很酷
长情又很酷 2021-01-27 11:05

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

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

相关标签:
2条回答
  • 2021-01-27 11:19

    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);
    
    0 讨论(0)
  • 2021-01-27 11:23

    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.

    0 讨论(0)
提交回复
热议问题