0

Here I wanted to run this method properly when boolean values are false, but when running this method it always failed the test case saying "no such element: Unable to locate element".How can I handle the boolean = false here?

 public void loginToStrata(String email,String psw) throws InterruptedException {
    enterEmail(email);
    enterPassword(psw);
    clickLogin();
    Thread.sleep(1000);
    
    boolean emailErrorPresence=driver.findElement(emailError).isDisplayed();
    boolean pswErrorPresence=driver.findElement(pswError).isDisplayed();        
    
    
      if(emailErrorPresence==true){ 
          String emailErrorMessage = driver.findElement(emailError).getText();
          System.out.println(emailErrorMessage);
      }else if (pswErrorPresence==true) {              
          String pswErrorMessage = driver.findElement(pswError).getText(); 
          System.out.println(pswErrorMessage);
      }else if(emailErrorPresence==false && pswErrorPresence==false) {
          boolean sysHeadingPresence=driver.findElement(portalHeading).isDisplayed();
          System.out.println(sysHeadingPresence);
          String heading = driver.findElement(portalHeading).getText(); 
          System.out.println(heading);            
      }      
}
1
 public void loginToStrata(String email,String psw) throws InterruptedException {
    enterEmail(email);
    enterPassword(psw);
    clickLogin();
    Thread.sleep(1000);
    
    List<WebElement> emailErrorPresence=driver.findElements(emailError);
    List<WebElement> pswErrorPresence=driver.findElements(pswError);        
   
    
      if(emailErrorPresence.size()!= 0){ 
          String emailErrorMessage = driver.findElement(emailError).getText();
          System.out.println(emailErrorMessage);
      }else if (pswErrorPresence.size()!=0) {              
          String pswErrorMessage = driver.findElement(pswError).getText(); 
          System.out.println(pswErrorMessage);
      }else if(emailErrorPresence.size()==0 && pswErrorPresence.size()==0) {
          boolean sysHeadingPresence=driver.findElement(portalHeading).isDisplayed();
          System.out.println(sysHeadingPresence);
          String heading = driver.findElement(portalHeading).getText(); 
          System.out.println(heading);            
      }      
}

use findelements instead and check if size() of the returned list is zero if element not exists else != zero

3
  • Thank you so much PDHide,This is working Jan 26 at 7:29
  • can I know what is the reason for using ""List<WebElement>"" here? @PDHide Jan 28 at 4:34
  • 1
    Findelemtns returns a list , we are type casting it meaning we tell that it's a list of only webelements
    – PDHide
    Jan 28 at 9:00
1

Write a helper method TryFindElement that doesn't throw an exception but returns false if it isn't found?

Here's my C# example:

public bool TryFindElement(By locator)
{
    try
    {
        driver.FindElement(locator);
        return true;
    }
    catch (NoSuchElementException)
    {
        return false;
    }
    catch (TargetInvocationException)
    {
        return false;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.