Remove the focus from an element after setText or sendKeys

Hello everyone,

I am testing a text field set with setText() but after its execution, the focus is still on the element I wrote in and then the web page doesn’t react (I am trying to trigger an error message with a field that I fill with the string “”).
I’ve tried to use sendKeys(myTestObject, Keys.chord("", Keys.TAB)) but a strange character appears in the next field and I don’t want to.

47

Is it a good way to do this kind of task? Is there a proper way to do it?

In advance, thank you :slight_smile:

WebUI.setText(to, "something")
WebUI.focus(otherTo)
1 Like

Thank you very much :wink:

Hello @Russ_Thomas
I found the alternative to focus() : WebUI.executeJavaScript("document.activeElement.blur()", null)
To focus an object that is not a a text field doesn’t blur the text field that is in focus and I don’t want to interact with a text field I am not actually testing to not pollute its test. Don’t know if it is right?

That is an admirable approach (but pragmatism sometimes demands that we find a reasonable way forward). And please understand, I gave you an answer based on the phrasing of your question - I would certainly use JavaScript in my code.

That said however, I need to comment on this part of your answer…

Possibly. You can focus on anything that can be “tabbed to” via the keyboard. Perhaps you chose an “otherTo” that cannot receive focus?

Yes, it seems that except the three text field set I want to test, no element can’t be “tabbed to” in the page I am working on.
At first I was passing the focus to the next text field set, and when I reached the last one I was passing the focus to the first, a little bit like in a cyclic permutation. As I was passing the focus to another text field, I thought why not write in it as well and test error message when all of them were filled. But with my colleagues we changed our minds and decided to test only one text field at once. And passing the focus, which I interpret as interacting with another field then became weird to me. That’s why I groped for a different way to proceed.
Code a line in a language I don’t know to call an other language that is also unknown to me was such a funny idea :joy:
Do you think this way is simple enough to be used?

It’s “reasonable” and you have reasoned it out very well.

But, there are caveats (see link below), as you’ve already hinted at. If your reasoning stands above all other possible solutions, then by all means use it (you might want to document that reasoning using // comments in the source).

That’s a great mantra/philosophy to work with. But since you are NOT modifying the AUT other than injecting JavaScript, in my view, it’s completely fine (I do it all the time!).