Send Keys or Set Text

Hello Team,

I have a question about the whats the best way to enter text in the text field for e.g First Name, Last Name

I discovered today that there is no consistency in my scripts and some fields are using Set Text or some are using Send Keys. I personally prefer Send Keys as they simulate entering key one by one.

Just looking for your thoughts about what we should try to use every time. I plan to bring consistency in my scripts for these fields.

Thanks,

That’s fine. However, be aware you’re testing the browser’s functionality as much as you’re testing your AUT. (And let’s be honest, testing the browser is not your job!)

To help you make a decision (and this is on a test by test basis) ask yourself this question:

Is there any key-handling code hooked up to an event on the control in question? If yes, probably best to use sendKeys. Otherwise, really, who cares?

One more thing: be pragmatic. If you are writing a test script that is for some reason giving you trouble AND if at that point in your workflow a genuine HUMAN user does NOT have the same problem, ANY workaround you apply to get past the problem is absolutely fine. IOW, if your sendKeys is giving you hell on some weird control and users don’t see an issue, use Set. Again, who cares? But DO apply the question above FIRST.

Hope that made sense.

3 Likes

Manpreet,

I just realized, that answer is a practical example of the general advice I offered you before. “Gatekeepers”, remember?

1 Like

@manpreet.mukkar and @Russ_Thomas i am struggling with these two options and trying to find one that can work for me.
i am trying to set a string like ‘all apples are fruits’ and i have tried both send keys and set text. but screen goes so fast that it does not type anything and nor is giving error.

please suggest something

@ravneet.boparai There might be many reasons around that.

Can you try adding a delay before entering the text?

Then enter the text in the field one by one? Use Send Keys for that.

I am not sure if it will work but if possible give it a try and also make sure the object selector is correct.

Yes I agree to @manpreet.mukkar, maybe you can try to add delay before the action. Hope that works

@manpreet.mukkar and @sarah2
My code is as follows and i have to use both sendKeys and setText to make it working. If i am using single of them then nothing works. I was already having wait of 20sec before using setText.
Don’t know how to move forward as i need to select this address column for many scripts and not sure of which keyword will work.

WebUI.waitForPageLoad(20, FailureHandling.STOP_ON_FAILURE)

WebUI.setText(findTestObject(‘New Folder2/Page_New Service Request - Cityworks/Address Field’), ‘2 wellington st w’)

WebUI.sendKeys(findTestObject(‘Object Repository/New Folder2/Page_New Service Request - Cityworks/Address Field’), ‘2 wellington st w’,
FailureHandling.STOP_ON_FAILURE)

WebUI.waitForElementVisible(findTestObject(‘New Folder2/Page_New Service Request - Cityworks/Address Field’), 10)

This is NOT a wait of 20 sec. waitForPageLoad not really waiting for the page to finish loading - #2 by Russ_Thomas

Try add a WebUI.delay(10) before the setText step for testing.
Also, move your waitForElementVisible before the setText

1 Like
WebUI.waitForPageLoad(20, FailureHandling.STOP_ON_FAILURE)

WebUI.waitForElementVisible(findTestObject('New Folder2/Page_New Service Request - Cityworks/Address Field'), 10)

WebUI.setText(findTestObject('New Folder2/Page_New Service Request - Cityworks/Address Field'), '2 wellington st w')

If that doesn’t work, we’ll try JavaScript.

Yeah! That’s working now
Thank You so much

:slight_smile: Perfect ! Now, we know it’s only a delay problem, so you can remove the delay step because you move the waitForElementVisible at the good position.
If the text disappears again, you only have to increase the timeout of the waitForElementVisible or add more waitFor[...] steps

yeahh it works this way too… thank You soo much:grinning:

@ravneet.boparai Adding delay/waits before interacting with an element after page loads is always a good practice. If you execute your scripts on both Chrome and Firefox you will notice that many problems on firefox will solve just by using delay/wait.

1 Like

From my experience using Katalon, I see that there something different from settext and sendkey.

  • If you use settext: Repeat 2 times settext for the same object -> only value in 2nd time will be set into
  • If you use sendkey: Repeat 2 time sendkey for the same object -> both value in 1st and 2nd time are set into
    So, base on your purposes which simulate users behaviors, you are able to choose suitable one.

FYI.

Hi, welcome to the forum…

If you have evidence that supports such a claim please post it.

If you have proved your claim on, say, 10 or 20 websites using two or three different browsers and got 100% the same results, you should report a bug to the selenium project.

Without that kind of evidence, it’s more likely you saw obscure behavior occurring on a couple of websites that happen to mess with field entry values that screwed with your tests – hardly what you would call supporting evidence.

Who knows, maybe I’m wrong. But before you make claims like this, you should make certain your reasoning is sound. :confused: :upside_down_face: