Home > front end >  Handling Url Title with Wait in selenium with java
Handling Url Title with Wait in selenium with java

Time:11-05

I'm just trying to capture the title of the page using wait in selenium with java. The code seems to be working correctly for positive test case but for negative, the code in the else{} part does not seem to work. Not sure why I get a TimeOutException before the else part is executed.

Ex. 1. Positive TC: When I enter the value for title as "Log In", it works fine.

Ex. 2. Negative TC: When I enter the value for title as "Log in", the else part is not executed.

public class WaitForTitleURL {
    static WebDriver driver;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        driver = new ChromeDriver();
        driver.get("https://classic.crmpro.com/");
        
        Thread.sleep(5000);
        By forgotPwd = By.xpath("//a[text()='Forgot Password?']");
        
        
        driver.findElement(forgotPwd).click();
        
        String title = waitForTitleAndCapture("Log in", 5);
        System.out.println(title);
    }
    
    public static String waitForTitleAndCapture(String value, int timeOut) {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOut));
        if(wait.until(ExpectedConditions.titleContains(value))) {
            String title = driver.getTitle();
            return title;
        }else {
            System.out.println("Title is not present within the given timeout: "   timeOut);
            return null;
        }
    }

}

CodePudding user response:

I tried changing the code a bit with Try catch block and it seems to be working fine now.

Can anyone please let me know if this is an acceptable solution or do I need to do something else?

public static String waitForTitleAndCapture(String value, int timeOut) {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOut));
    
    try {
        boolean flag = wait.until(ExpectedConditions.titleContains(value));
        if(flag) {
            String title = driver.getTitle();
            return title;
        }
    }catch(Exception e) {
        System.out.println("Title is not present within the given timeout: "   timeOut);
    }
    return null;
}

CodePudding user response:

The issue you're experiencing is because of how the ExpectedConditions.titleContains(value) works. It checks if the page title contains the given value. If the title doesn't contain the value, it will continue to wait for the specified timeOut duration. If the title doesn't match the expected value within the timeout, a TimeoutException is raised, and your else block won't be executed.

In your negative test case, you are checking if the title contains "Log in," but it seems that the actual title is "Log In," with a capital "I". Since string matching is case-sensitive, it doesn't match, and you get a TimeoutException.

To handle this case, you can make your comparison case-insensitive by converting both the actual title and the expected value to lowercase or uppercase before comparing.

public static String waitForTitleAndCapture(String value, int timeOut) {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOut));
    if (wait.until(ExpectedConditions.titleContains(value))) {
        String title = driver.getTitle();
        return title;
    } else {
        System.out.println("Title is not present within the given timeout: "   timeOut);
        return null;
    }
}

if you don't want to use try/catch block. here is another way you can do this

public static String waitForTitleAndCapture(String value, int timeOut) {

  WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOut));

  if(!wait.until(ExpectedConditions.titleContains(value))) {
    System.out.println("Title is not present within the given timeout: "   timeOut);
    return null;
  } 
  
  String title = driver.getTitle();
  return title;
}
  • Related