wfmn17
wfmn17

Reputation: 204

Verify typed text in input text - Selenium

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!

Upvotes: 0

Views: 3045

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193298

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()

Upvotes: 2

Related Questions