Control Flow Statements:

Management flow statements, commands the regular flow of performance and runs based on conditions set by the programmer.

You will find three kinds of Control-Flow Statements :
Decision Making Statements (Selection Statements)
Looping Statements (Iterative Statements)
Branching Statements (Transfer Statements)

Decision Making Statements:

If , If – otherwise and Change Claims are decisionmaking claims. In this post we are going to look in to ‘straightforward If’, ‘if-else’, ‘if-else-if’ and ‘stacked if’ illustrations with selenium s/w.

1. Straightforward if Assertion:

If statement is the fundamental of control-flow assertions. It empowers block of code to be executed according to given state.

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

‘Statement1’ within if block may be executed when the Boolean phrase (State) is accurate. If the Boolean expression is bogus then, claims in if block is not going to be ran and it runs the remainder of signal after if prevent.

Depending appearance of if statement should be a ‘Boolean sort’ (Both True / False). If we all make an effort to offer another kind then it is going to provide compile-time error as ‘Type mismatch: can not change to boolean’

Why don’t we look in to below straightforward illustration:

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 preceding illustration, we’re checking site name. Here assertions in the if block may be executed JUST when ‘driver.getTitle()’ is ‘Yahoo’. In if block we’re putting ‘hole’ worth with ‘Correct’, thus the Declare declaration may be ran.

2. If – otherwise Declaration:

Format:

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

In if – otherwise declaration, if the given condition is a fact, then assertions in if prevent may be executed , if it’s untrue, then claims in otherwise prevent may be executed. If the state in if is accurate then otherwise prevent WOn’t ever get ran

Why don’t we appear in to an easy Illustration:

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”);
}
}
}

Why don’t we today consider instance with selenium s/w utilizing If-Else. Previously we’ve authenticated Site name utilizing if prevent, just as, we’ve utilized ‘driver.getTitle()’ and check always if it fits with anticipated name. If it’s accurate, claims in if prevent will be executed. If it’s PERHAPS NOT, assertions in otherwise prevent may 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’s just an individual assertion in if prevent or otherwise prevent, then we no must establish curly braces as well as the assertion in if prevent shouldn’t be declarative declaration.

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
}
}
Why don’t we seem in to the below Instance:

3. If-else if Assertion:

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

At any time there are mo-Re than two states, then we’ll move for if-else if assertion.

Format:

If the Boolean_exp1 is accurate, then the signal in if prevent may get executed, if it’s untrue, then it’s going to check for ‘otherwise if’ state i.e, Boolean_exp2 may be assessed and now if it’s accurate, then code in otherwise if prevent may be executed , when it is also untrue, then it’ll assess if you can find another otherwise if prevent accessible , if ‘yes’, it is going to check for the ‘otherwise if’ states and if all of them are Untrue, subsequently otherwise component may be ran.

Why don’t we seem at below instance:

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. Stacked If Assertion:

Stacked if declaration is just when- otherwise statement with still another if or if-else declaration .
Format: in case that(Boolean_exp)// outside if
If(Boolean_exp1)// interior if
statement1;
// internal if shut
Otherwise
statement2;
// interior otherwise shut
// outside if shut
Otherwise
statement3;
// external otherwise shut

Statement1 may be ran when equally external if and interior if state is evaluated to true. Assertion 2 may be ran when outside if state is evaluated to true and internal if state as untrue. and Statement3 may be ran when outside if state is evaluated to false.

Whenever the outside if state is evaluated to false, interior if state WOn’t ever be assessed.

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”);
}
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *