Dinu
Dinu

Reputation: 933

Else condition not executed due to try catch

I have a scenario in which, when we click on save two process can occur. It can show some messages or else the page can crash. I have added an if condition for the crash and mentioned the other process if the application does not crash as else condition. I have written a code like this to handle the crash.

try {
    if (driver.findElement(By.xpath("/html/body/form/div[4]/div[3]/div[1]/div[1]/div[1]/span[1]")).isDisplayed()){
        System.out.println("Critical Error Occured.");
        driver.close();
    } else{
    String msg = driver.findElement(By.xpath("/html/body/form/div[4]/div[3]/div[1]/div[1]/div/div/div[1]/div/div[2]/div/div[1]/span")).getText();
    if (msg.equals("User already registered")){
        driver.findElement(By.name("dnn$ctr5995$View$btnOk")).click();
        System.out.println(msg);
        driver.close();
    } else if (msg.equals("Admission number already exist.")) {
        driver.findElement(By.name("dnn$ctr5995$View$btnOk")).click();
        System.out.println(msg);
        System.out.println("Please change the admission number.");
        driver.quit();          
    } else if (msg.equals("Saved Successfully.")){
        driver.findElement(By.name("dnn$ctr5995$View$btnOk")).click();
        System.out.println(msg);
    }
    }
}catch ( org.openqa.selenium.NoSuchElementException e){
    System.out.println("No Such Element Exception.");
}

Since the page crashes rarely, while executing the control goes to catch and so the else conditions for the first if condition is not executed. Is there any solution for this?

Upvotes: 0

Views: 1096

Answers (3)

Abhishek Saxena
Abhishek Saxena

Reputation: 292

Finally clause can solve your problem. Its best practice to include must execute code in Finally clause

Upvotes: 0

Tim B
Tim B

Reputation: 41208

Do separate try-catch blocks inside the sections of the if or use a finally block for code you always want to execute.

You have two separate issues, the first is the findElement within the conditional. To handle that you need to do the find separately then do the if.

Element elem = null
try {
    elem = driver.findElement(By.xpath("/html/body/form/div[4]/div[3]/div[1]/div[1]/div[1]/span[1]"));
} catch (org.openqa.selenium.NoSuchElementException e) {
    System.out.println("No Such Element Exception.");
}

Then after that you need to handle it within the else

if (elem != null && elem.isDisplayed()){
    System.out.println("Critical Error Occured.");
    driver.close();
} else{
  try {
    String msg = driver.findElement(By.xpath("/html/body/form/div[4]/div[3]/div[1]/div[1]/div/div/div[1]/div/div[2]/div/div[1]/span")).getText();
    if (msg.equals("User already registered")){
        driver.findElement(By.name("dnn$ctr5995$View$btnOk")).click();
        System.out.println(msg);
        driver.close();
    } else if (msg.equals("Admission number already exist.")) {
        driver.findElement(By.name("dnn$ctr5995$View$btnOk")).click();
        System.out.println(msg);
        System.out.println("Please change the admission number.");
        driver.quit();          
    } else if (msg.equals("Saved Successfully.")){
        driver.findElement(By.name("dnn$ctr5995$View$btnOk")).click();
        System.out.println(msg);
    }
  } catch ( org.openqa.selenium.NoSuchElementException e){

  }
}

Upvotes: 4

ChoiBedal
ChoiBedal

Reputation: 111

http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html

May-be you're looking for the finally clause, aren't you ?

Upvotes: 0

Related Questions