Reputation: 1386
I'm trying to test an HTML
form using Selenium. The page authenticates the input data by using javascript's onFocus()
event on each text field (which is an input tag in HTML
). As such, my code needs to make sure onFocus()
get fired whenever I interact with the text field, just as it would if a real user were filling out the form.
I would expect the following code to fire this event:
this.textfield.clear();
if (value != null) {
this.textfield.sendKeys(value);
}
However, the onFocus()
event doesn't fire. I've tried manually clicking the element
this.textfield.click()
But that also doesn't work. I've also tried using the Actions Class
this.textfield.clear();
Actions action = new Actions(driver);
action.moveToElement(this.textfield).click().sendKeys(value.toString()).perform();
But this too hasn't given me any luck.
Is there any way in Selenium to ensure that the onFocus()
event is executed?
Upvotes: 1
Views: 824
Reputation: 106
You can execute JavaScript in your browser session to explicitly fire onFocus(). This code is in C#, but there must be something similar in Java bindings too.
((IJavaScriptExecutor)driver).ExecuteScript(string.Format("$('#{0}').onFocus()", elementId));
Upvotes: 2