0

I have a simple method for sending keys to a textbox, but sometimes the end letters are missing, or the whole text, yet selenium returns 'Passed' for this test step. This happens only in a specific dialog window which has 4 elements: 1. Select 2. Input text 3. Save button 4. Cancel button

The test case selects a value from the select list, then types the given string to the textbox and clicks save.

Can I somehow verify right after the text is typed that it equals to the expected text? I tried with textboxelement.getText() but it returned null, probably because the cursor was still in the textbox and the text was not sent. I'd like to solve it without pressing a key like ENTER because it might close this dialog window.

My send text method:

public final void sendText(final String value, final String id) {
    final By locator = By.id(textOrVariableValue(id));
        final WebElement element = getElementByLocator(locator, ExpectedConditions.elementToBeClickable(locator), 10, true);
        element.clear();
        element.sendKeys(textOrVariableValue(value));

}

Thanks in advance!

2
  • is there any child element present for text box where you trying to add text? Commented Dec 29, 2017 at 8:31
  • it looks like this: <div class="col-sm-8"> <input id="newelementkey" type="text" class="form-control input-sm" data-bind="value: newElementKey, valueUpdate: 'afterkeydown'"> <div class="alert alert-danger" data-bind="validationMessage: newElementKey" style="display: none;"></div> </div> The alert div is shown if the validation for the input fails. Commented Dec 29, 2017 at 9:26

1 Answer 1

2

On sending keys to a textbox and trying to verify right after the text is typed if it equals to the expected text will always return null and that is the Expected Behavior.

Reason

When you invoke sendKeys(), it simply pastes the Character String within the textbox. No other action is performed and nothing is returned. So no change in the HTML DOM.

So, when you try to do :

textboxelement.getText()

null is returned.

Incase when you click on Save button the onClick() event of Save button performs the intended functions (calls to JavaScript / AJAX Scripts) which inturn changes the HTML DOM and the text is gets accomodated with the DOM Tree. Now if you try to :

textboxelement.getAttribute("attribute_name");

You would be able to retrive the text.

Finally, as you mentioned without pressing a key like ENTER because it might close this dialog window that is how the text would be accomodated in the DOM Tree for Selenium and your Automation Script to find out through findElement() or findElements()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.