-3

how am I ever going to write any selenium automation code that can have the same outcome everytime and could ever ever ever be trusted? as most of you should know sendKeys is simply shit.

-it doesnt write the complete input

-sometimes it doesnt write at all

-its skips to other elements and messes up the input

-it cant write longer character sequences

-you cant check it correctly

-you cant make it wait.until everything is sent

and this all is simply randomized into a clusterfuck of one-to-a-billion my at this moment examples (I probably changed my approach more than 10 times)

public static void sendKeys(By by, String input) {
    if (by == null) {
        throw new IllegalArgumentException("Parameter 'by' is null.");
    }
    if (input == null) {
        throw new IllegalArgumentException("Parameter 'input' is null.");
    }
    LOGGER.info("sendKeys() - By locator '{}'", by.toString());
    WebElement elem = WebElementFinder.getElement(by);
    elem.clear();
    elem.sendKeys(input);
    checkKeySendResult(by, input);
    }

public static void checkKeySendResult(By by, String input) {
    WebElement elem = WebElementFinder.getElement(by);
    String value = elem.getAttribute("value");
    if (!value.equals(input)) {
        LOGGER.info("sendKeys() - didn't work");
        elem.clear();
        elem.sendKeys(input);
        checkKeySendResult(by, input);
    } else {
        LOGGER.info("sendKeys() - sent all characters");
    }
}

you know what I expect;

-all keys should be sent

-sendKeys shouldn't stop midway of typing and then go somewhere else

-I should be able to correctly check if the input is right

-if incorrect, I should be able to correct it easily and unwaveringly

and please dont tell me to use a waiter, cause I obviously did try out waiters in every single step...

I'll test out any kind of advice offered here as I do hope everyone does and hopefully someone can open my eyes to a steady sendKeys method.

if you cant help me, please do use this thread to ragingly describe your hatred towards sendKeys and how frustrating it is to be working with it.

Thank you all for your input.

obs: sorry for my french

  • Kind of hard to help you out here without a specific question that provides the HTML that is causing problems (even better, point us to the page with the problem). You've mentioned lots of issues above, too many in fact. I would tend to believe that the problem is web site behavior, not your automation code. e.g., if sendKeys() is sending too many characters to a field that limits the number of characters, you won't see all of the ones that you sent to it. that's expected behavior for the field, and not a bug with sendKeys() – Breaks Software Dec 27 '18 at 16:37

2 Answers2

1

I don't think the problem is sendKeys itself. A quick check of our own repo shows some 600 usages of sendKeys. We do have some issues with the command, but that is more as a result of the implementation of the website more than sendKeys.

As a workaround, we have implemented a method similar to yours, but rather than using an if-statement, we use while

public void sendKeys(WebElement element, String keys){
   while(!element.getAttribute("value").equal(keys){
        element.clear();
        element.sendKeys();
   }
}

so while the text does not match, clear and try again. You can also put in a logger and/or a max attempts to limit the tries it takes.

0

I faced so many problems with selenium , not just with sendkeys other options like clicking a button as well. Had to change my approach also so many times. To avoid these problems, you can try using the Java Script Executer in Selenium, that what i did, to do whatever you want to do with your DOM. You can set value for a field, click a button, and it provides you most of the functionality you can achieve using Java Script. It simply feels like, you have automated the Selenium to execute Java Script on the page you desire.

Go ahead, with that, its worth giving it a try .

Ankur Goel
  • 311
  • 1
  • 11