3

I have a modal window with some textboxes in it. I used to input text using className to get all the form field and indexing the textboxes. Nowadays, the modal has changed and dev provided me with an ID for each box. What I'm trying to do it's just to input text and click a Submit button, but what happens is that sometimes, the String is inserted partially.

Let's say I want to input "System1" string, but sometimes the WebDriver inputs "Sys", or "Sy"etc.. and then goes for the click

I've tried to locate Element via ID, XPATH... but the point is that I can see how the text is on the field so the element is located properly

There is another issue as well. To make sure what the textbox contains after the .senKeys() method, I put and assert which supposedly gets the text from the textbox via .getText() method and compares it with the String I'm passing to the main method, but I'm getting null at this point, and I can see how functionally the WebDrivers input something, complete or not Please note that I'm using the same WebElement object to both .sendKeys() and .getText() methods

Here's the code I'm running, simple AF:

//Locating the TextBox
@FindBy(how = How.ID, using = "text")
public WebElement nameField;


public void introducirEnCampoNombre(String name) {
   //nameField.clear();
    nameField.sendKeys(name);
    
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    String xpectedString = nameField.getText();
    Assert.assertEquals(name, xpectedString);
}

Here is the related HTML code:

<form id="systemForm" action="/ik-conf/systemAction/save" method="POST">
            <input id="id" name="id" type="hidden" value="">
            <input id="parentNode.id" name="parentNode.id" type="hidden" value="1">

            <div class="row">
                <div class="left lv-text-label">
                    <label for="text" class="right-label inline">Nombre(*):</label>
                </div>
                <div class="right lv-text-input">
                    <input id="text" name="text" autofocus="autofocus" validation="/ik-conf/system/nameValidation" class="ikconf-fieldset width_100x100" type="text" value="">
                </div>
            </div>
            <div class="row">
                    <div class="left">
                        <label for="description" class="right-label inline">Descripción:</label>
                    </div>
                    <div class="right">
                        <textarea id="description" name="description" style="margin-bottom:0px;" class="width_100x100 z-label" rows="3" cols="40"></textarea>
                    </div>
                </div>

So as I said above, the code goes for the submit button (which is in the next method) before the string is inserted properly. When debugging, I can check that the string is correct, and then is the second issue. I try to locate again the same element with another name INSIDE the method but same result, .getText() returns me null

Hope someone could bring some light over here.

Thanks in advance

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
SeekanDestroy
  • 511
  • 1
  • 5
  • 12
  • can we see the HTML for the page you are testing? – C. Peck Mar 22 '19 at 11:44
  • For sure. There u go. Thanks – SeekanDestroy Mar 22 '19 at 11:51
  • I'd say this must be something on driver or selenium side here. What is the validation rule on input, and is it possible that it works slowly enough to introduce flakiness? Will it work fine if you remove validation? Have you tried using selenium Waits instead of locking the thread with `sleep()`? – M. Prokhorov Mar 22 '19 at 12:01
  • Possible duplicate of [Selenium - send\_keys() sending incomplete string](https://stackoverflow.com/questions/40985765/selenium-send-keys-sending-incomplete-string) – Alexis Mar 22 '19 at 15:43

2 Answers2

1

The line you are using now,

String xpectedString = nameField.getText();

Will return the innerHTML of that input element. I’m not sure if the text typed into that input field goes in the innerHTML, probably in the ‘value’ attribute. So try defining it as

String xpectedString = nameField.getAttribute(“value”);
C. Peck
  • 3,641
  • 3
  • 19
  • 36
0

I've seen this before. I think it's due to a page load issue... like the page loads to a point and then finishes loading causing a cut off of the text depending on the timing of things. You can potentially do a couple of things.

  1. You can see if the INPUT goes stale. If it does, wait for stale and then enter the text.
  2. Another option is to put the .sendKeys() and validation in a loop and remove the assert. Loop say up to 3 times, each time reentering the text until it succeeds. If the max attempts is hit, fail the test.
JeffC
  • 22,180
  • 5
  • 32
  • 55