Problem with isElementPresent method

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with isElementPresent method

tulsi.tester
Hi Tarun,

I am facing the problem with isElementPresent() method. Indeed when the locator is not found this method has to return false. But it is throwing exception and coming out of the loop. Please find the scenario

1. I am trying to validate the error message for an invalid data to a field
2. I am giving Invalid data to that field, then only it populates the error message.
3. I am checking whether the error message is present or not using isElementPresent()
4. Since I am looping this logic, whenever i give valid data, the error message will not populates
5. Then also i am checking whether the error message is present or not by using isElementPresent()
6. Since there is no error message for valid data, the corresponding 'if' loop will not execute indeed.
7. But my script is throwing to exception saying that locator not found instead of returning false


I opened firebug and find some text.

The general xpath for an invalid error message is like  //*[@id='registrationContent']/div[20]/div[2]/span/span

But when there is no validation message, the text for the second span disappears in the firebug.
Please find the attachment for before and after validation messages.

Xpath.jpg












Reply | Threaded
Open this post in threaded view
|

Re: Problem with isElementPresent method

softwaretestingforum
Administrator
Source code please.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Problem with isElementPresent method

tulsi.tester
Hi Tarun,

The source code is


 <div class="separatedRow"></div>
            <div class="row">
                <div class="leftColumn">

                    <label for="How_would_you_like_us_to_contact_you_">How would you like us to contact you?</label><br />
                    <select data-val="true" data-val-required="The Method of Contact field is required." id="MethodOfContact" name="MethodOfContact"><option value=""></option>
<option selected="selected" value="Phone">Phone</option>
<option value="Email">Email</option>
<option value="Fax">Fax</option>
</select><br />
                   
                </div>
            </div>
Reply | Threaded
Open this post in threaded view
|

Re: Problem with isElementPresent method

softwaretestingforum
Administrator
sorry, I meant to ask your Selenium Test code which you are using for this scenario.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Problem with isElementPresent method

tulsi.tester
Hi,

Here you go

if(selenium.isElementPresent("//select[@id='MethodOfContact' and @class='input-validation-error']"))
                                {
                                        String text = selenium.getText(//select[@id='MethodOfContact' and @class='input-validation-error']");
                                }
Reply | Threaded
Open this post in threaded view
|

Re: Problem with isElementPresent method

softwaretestingforum
Administrator
Strange, are you sure exception is thrown by line -

if(selenium.isElementPresent("//select[@id='MethodOfContact' and @class='input-validation-error']"))
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Problem with isElementPresent method

tulsi.tester
Yes tarun, exception is throwing at if condition. Now i changed the xpath to following

//span[@data-valmsg-for='MethodOfContact' and contains(@class,'field-validation')]

and hence working fine.  Thanks for your replies