If, If Else and Nested If examples with Selenium

Control Flow Statements:

Control flow statements, controls the normal flow of execution and executes based on conditions specified by the developer.

There are three types of Control Flow Statements :
Decision Making Statements (Selection Statements)
Looping Statements (Iterative Statements)
Branching Statements (Transfer Statements)

Decision Making Statements:

If , If – else and Switch Statements are Decision making statements. In this article we will look into 'simple If', 'if-else', 'if-else-if' and 'nested if' examples with selenium

1. Simple if Statement:

If statement is the basic of all control flow statements. It enables block of code to be executed based on specified condition.

Syntax:

if(Boolean_exp) {
             Statement 1; //Statements will be executed if Boolean expression is true
}
Statement 2://Statements will be executed always

'Statement1' inside if block will be executed if the Boolean expression (Condition) is true. If the Boolean expression is false then, statements inside if block will not be executed and it executes the rest of code after if block.

Conditional expression of if statement should always be a 'Boolean type' (Either True / False). If we try to give any other type then it will give compile time error as 'Type mismatch: cannot convert to boolean'

Let us look into below simple example:

public class IfExample {
	public static void main(String[] args) {
		int a=10; // declaring and initializing a with 10
		if(a==10) {
		//Will get executed only when the condition is true.
		System.out.println("Inside If block");	
	}
}

<b>Now, let us look an example using if statements in selenium :</b>

<code>
package com.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class IfExampleSelenium {
	@Test
	public void testPageTitle() {
		System.out.println("Launching Firefox browser..");
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.navigate().to("http://google.com");

		// verifying the page title
		String expPageTitle = "Google";
		boolean flag = false;
		if (driver.getTitle().equalsIgnoreCase(expPageTitle)) {
			flag = true;
			// This method will return True when the page title matches with specified string
			System.out.println("Yeah... Page title matched");
		}
		Assert.assertTrue(flag, "Page title is not matching with expected");
	}
}

In the above example, we are verifying page title. Here statements in the if block will be executed ONLY when 'driver.getTitle()' is 'Google'. In if block we are assigning 'flag' value with 'True', hence the Assert statement will be executed

2. If – else Statement:

Syntax:

if(Boolean_exp) {
         Statement1;
}
else {
statement2:
}

In if – else statement, if the specified condition is true, then statements in if block will be executed , if it is false, then statements in else block will be executed.If the condition in if is true then else block will never get executed

Let us look into a simple Example:

public class IfElseExample {
	public static void main(String[] args) {
		int a, b;
		a = 10;
		b = 20;
		if (a > b) {
			//Below statement will be executed ONLY when 'a' is greater than b
			System.out.println("a is greater than b");
		} else {
			//Below statement will be executed ONLY when 'b' is greater than 'a' or Equal to 'a'
			System.out.println("b is greater than a");
		}
	}
}

Let us now look into example with selenium using If Else. Earlier we have validated Page title using if block, in the same way, we have used 'driver.getTitle()' and check if it matches with expected title. If it is true, statements in if block will be executed. If it is NOT, statements in else block will be executed

if(driver.getTitle().equalsIgnoreCase("some expected text"))
    //Pass
    System.out.println("Page title contains expected text ");
else
    //Fail
    System.out.println("Page title doesn't contains expected text");

When there is only a single statement in if block or else block, then we no need to define curly braces and the statement inside if block should not be declarative statement.

Let us look into the below Example:

public class IfElseExample {
	public static void main(String[] args) {
		int test=10;
		if(test==10)
			int test1=20; // compile time error - as statement here is a declarative statement
	}
}
3. If else if Statement:

When ever there are more than two conditions, then we will go for if else if statement.

Syntax:

If(Boolean_exp1){
Statement1:
}
else if(Boolean_exp2){
statement2:
}
else{
statement3:
}

If the Boolean_exp1 is true, then the code inside if block will get executed, if it is false, then it will check for 'else if' condition i.e, Boolean_exp2 will be evaluated and now if it is true, then code inside else if block will be executed , if it is also false, then it will check if there are any other else if block available , if 'yes', it will check for all the 'else if' conditions and if they all are False, then else part will be executed.

Let us look at below example:

public class IfElseIfExample {
	public static void main(String[] args) {
		int age;
		System.out.println("Enter age");
		//Ask the user to enter the age
		Scanner in = newScanner(System.in);
		age=in.nextInt();
		if(age<13)
			System.out.println("child");
		else if(age>13 && age<19)
			System.out.println("teenager");
		else
			System.out.println("adult");
	}
}
4. Nested If Statement:

Nested if statement is nothing but if- else statement with another if or if else statement .
Syntax: if(Boolean_exp){// outer if
If(Boolean_exp1){// inner if
statement1;
}// inner if closed
else{
statement2;
}// inner else closed
}// outer if closed
else{
statement3;
}// outer else closed

Statement1 will be executed when both outer if and inner if condition is evaluated to true. Statement 2 will be executed when outer if condition is evaluated to true and inner if condition as false. and Statement3 will be executed when outer if condition is evaluated to false.

Whenever the outer if condition is evaluated to false, inner if condition will never be checked.

Example:
public class NestedIfDemo {
public static void main(String[] args) {
int a=55;
int b=50, c=60;
if(a>b){
if(a>c) {
System.out.println("a is big");
}
else{
System.out.println("c is big");
}
}
else if(b>c){
System.out.println("b is big");
}
else{
System.out.println("c is big");
}
System.out.println("out side outer if and else");
}
}

Nested if statements are basically used whenever we want to check a condition based on the result of another condition.

Let us look into below example with nested if using selenium :

@Test
	public void testNestedIfExample() {

		System.out.println("Launching Firefox browser..");
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.navigate().to("http://facebook.com");
		By locator = By.name("firstname");
		if (driver.findElements(locator).size() > 0) {
			if (driver.findElement(locator).isDisplayed()) {
				driver.findElement(locator).sendKeys("Hello");
			}
		}

	}
Java Tutorials for Selenium: 

Add new comment