Maurizio Pozzobon
Maurizio Pozzobon

Reputation: 3084

Verify input text element is not empty in Selenium IDE

How can I verify that an input element has any text in it. It is loaded with random text so I can't verify a specific value, but there must be some text in it. I'm using Selenium IDE

Upvotes: 4

Views: 22653

Answers (6)

Laura
Laura

Reputation: 151

This is 12 years later, but I was having this same issue and I couldn't find the answer, so I did it in a slightly more brute force way since it doesn't seem that there is a single command with an elegant solution. This is for Selenium IDE 4.0.1-alpha.78

This is a 4 command solution, but it works.

Format is (Command ---> Target ---> Value)

 1. execute script ---> return $('selector').get(0).value != '' ---> valExists
 2. if ---> !${valExists}
 3.   assert ---> your error message that will stop test execution
 4. end

Line 1 returns the result of the logical comparison into the variable ${valExists} so that holds boolean true or false after execution.

Line 2 just checks the boolean value. Since you can't execute a script in an "if" statement, it has to be executed in the line before.

I'm sure this would work with anything that you need a value but you don't care what that value is. In my script, I also have an echo after the execute script to just log the result.

Hopefully this can help people from the future.

Edited for clarity.

Upvotes: 0

Hossain Mahmood Tuhin
Hossain Mahmood Tuhin

Reputation: 806

It was part of a sign up form assertion. The firstName field was provided with data. Then i used the following code to verify whether the field was filled up with data.

if(driver.findElement(By.cssSelector("#FirstName")).getText()==""){
        System.out.println("field didn't fillup with data");
    } 
else {
        System.out.println("field filled up with data");
    }

Upvotes: 0

robert
robert

Reputation: 31

I just use

verifyNotValue|//input[@id='email']|

the 3rd param is empty

Upvotes: 3

djibril
djibril

Reputation: 83

Try this:

driver.find_element_by_id("email").get_attribute("value") != ''

Upvotes: 0

Jeffrey D. Hoffman
Jeffrey D. Hoffman

Reputation: 793

In Selenium IDE you can use the verifyEval command and a bit of JavaScript to verify that a field is populated, or not.

Command: verifyEval
Target:  this.page().findElement("//input[@id='email']").value != ''
Value:   true

In this case, I'm testing that the <input type="email" id="email" value='[email protected]"> is not an empty field on my HTML page.

When this command is run, the JavaScript code in the target is evaluated. Here it is testing the current value of the email field against an empty string. If they don't match (e.g. the field isn't empty) then it true is returned. This return value is then compared to the expected Value which is also true and so the test passes.

Upvotes: 5

rs79
rs79

Reputation: 2321

This should be a cinch if you use Selenium-RC with any programming language. However, given that you are using the IDE, I would recommend adding this user extension. This will give you some looping constructs. You can use storeText and then take a decision based on the value of the variable in which the retrieved text is stored. The Stored Vars is another useful IDE firefox extension for analyzing the contents of the storeText variable.

Upvotes: 0

Related Questions