Triggering Validations

Part of the reason why we use Selenium to interact with the site is because it is ‘like the user’. Except for the niggly little detail of it not being like a user at all.

For instance, typing into a field. What the Remote Control API will do it put the desired chunk of text into the DOM at that element. Which means that the JS validations get skipped. I’ve found that most validations can be triggered in one of two ways.

The first way is to tell the browser that you have moved away from the field (as a user would if they hit tab or moved the mouse to another field) which is useful if you want validate-on-complete logic to deal with. This is done using the fire_event function.

def __set__(self, obj, val):
    super(CityTextElement, self).__set__(obj, val)
    wrapper().connection.fire_event(self.locator, "blur")

In this particular page-object-y example, the text is put into the DOM and then we fire the blur event. And it is nicely hidden from the user.

The other validation form of validation is the as-you-type style that is often seen with things like password strength meters. These trigger off the key up event.

def __set__(self, obj, val):
    super(PasswordTextElement, self).__set__(obj, val)
    wrapper().connection.key_up(self.locator, val[-1])

Again, from a page object, we are putting the text in the telling the browser that we released the key that was at the final index of the text. It does not get duplicated on the screen because we do not do the associated key down.

In theory, if you used the WebDriver API this trick would not be needed, but don’t yet recommend to clients that they switch APIs yet.

Post a Comment

Your email is never published nor shared. Required fields are marked *