isDisplayed is not returning false when element is not present.

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

isDisplayed is not returning false when element is not present.

Shekhar
Here is what I am trying to do..
this works when customer is not present... however it does not work if customer is present.

Referred http://code.google.com/p/selenium/issues/detail?id=1880

test case:
If customer dost not exist, create and sale.
if exist, sale.
//if(isPresentAndDisplayed("create_new"))

if(isPresentAndDisplayed(driver.findElement(By.id("create_new"))))
                               
                        {
                                driver.findElement(By.id("create_new")).click();
                                NewCustomer.registerNewCustomer(cuEnterData);
                        }
                        else

                                selecteItems.ItemSelection(cuitemsDates);
                                CreditCardInfo.CreditCardInfo(CCInfo);
                       
                       
        }
       
//find element to return boolean value.
       
        public  boolean isPresentAndDisplayed(final String string) {
                  try {
                    return driver.findElement(By.id(string)).isDisplayed();
                  } catch (NoSuchElementException e) {
                    return false;
                  }
                }

        public static boolean isPresentAndDisplayed(final WebElement element) {
                  try {
                    return element.isDisplayed();
                  } catch (NoSuchElementException e) {
                    return false;
                  }
                }
-SK
Reply | Threaded
Open this post in threaded view
|

Re: isDisplayed is not returning false when element is not present.

Shekhar
Seems issue is, it is not throwing exception if element is not present. However it works fine (return true) if element is present.
During debug, it does go in try block.. but then does not go forward once element is not present. Tried giving wrong name in string, and still does not throw exception.

How to find why try/catch is not throwing exception, even if element is not present?
-SK
Reply | Threaded
Open this post in threaded view
|

Re: isDisplayed is not returning false when element is not present.

softwaretestingforum
Administrator
Could you catch "Exception" instead of "NoSuchElementException" and try?
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: isDisplayed is not returning false when element is not present.

Shekhar
Happy New year :-)

Yes.. I tired that too. No luck

public  boolean isPresentAndDisplayed(String string) {
                  try {
                   
                          driver.findElement(By.id(string));
                          return true;
                  } catch (Exception e) {
                    return false;
                  }
                }
-SK
Reply | Threaded
Open this post in threaded view
|

Re: isDisplayed is not returning false when element is not present.

softwaretestingforum
Administrator
Happy new year to you as well :)

Could you pass By object to method instead of String and use that By object in try block?
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: isDisplayed is not returning false when element is not present.

Shekhar
do you mean (no luck in this too).


public static boolean isPresentAndDisplayed(final WebElement element) {
                  try {
                    return element.isDisplayed();
                  } catch (Exception e) {
                    return false;
                  }
                }

Basically I am trying to find if element is preset or not. If present flow1 else flow2.
As work around, i am passing flag from data. So user who will running the script to decide if he is running for new customer or existing customer. however I want it "smart" to check if it is existing or not.
-SK
Reply | Threaded
Open this post in threaded view
|

Re: isDisplayed is not returning false when element is not present.

softwaretestingforum
Administrator
No, the By object -

public static boolean isPresentAndDisplayed(By locator) {
                  try {
                    return driver.findElement(locator).isDisplayed();
                  } catch (Exception e) {
                    return false;
                  }
                }
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: isDisplayed is not returning false when element is not present.

Shekhar
bu then what should I pass... out of this... By.id("create_new")

-SK
Reply | Threaded
Open this post in threaded view
|

Re: isDisplayed is not returning false when element is not present.

Shekhar
        if(isPresentAndDisplayed(By.id("create_new")))
                        {
                                driver.findElement(By.id("create_new")).click();
                                NewCustomer.registerNewCustomer(cuEnterData);
                        }
-SK
Reply | Threaded
Open this post in threaded view
|

Re: isDisplayed is not returning false when element is not present.

Shekhar
No luck with this also. I does go in method (call by locator), however it is goes till
return driver.findElement(locator).isDisplayed();

I tried having boolean variable at this place and return this variable. However in debug, it is not showing value of this variable.

boolean isTrue=driver.findElement(locator).isDisplayed();
return isTrue;

Not sure if this right way to debug, as it (should) be throwing exception at driver.findElement(locator) itself.
                         
-SK
Reply | Threaded
Open this post in threaded view
|

Re: isDisplayed is not returning false when element is not present.

softwaretestingforum
Administrator
This should have worked.
Now I need you to send me a small reproducible test case, that is one class which I can execute and check.
This is the only way left for me to debug it.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: isDisplayed is not returning false when element is not present.

Shekhar
In example you posted , I added following under webdriver class. this is not throwing exception.

        if(isPresentAndDisplayed(By.id("create_new"))){
                registration.registerNewUser(registrationData);
                assert driver
                                .findElement(By.tagName("body"))
                                .getText()
                                .contains(
                                                "Thank you for registering. "
                                                                + "You may now sign-in using the user name and password you've just entered.");
        }
       
        }


public  boolean isPresentAndDisplayed(By locator) {
        try {
       driver.findElement(locator);
       return true;
        } catch (Exception e) {
          return false;
        }
      }
-SK