Java Basics:Switch Case example in Java Selenium
Switch Case example in Java Selenium
It is also one type of Conditional Statements, which is basically used to write for menu driven programs. When ever there are more number of options to select then we will go for Switch statement i.e., single expression with many possible options. The same thing can be done using if..else statements but it can get very confusing and if..else statement is difficult to change when it becomes highly nested.
Syntax:
switch(expression) {
Case value1: statement1;
Break;
Case value2: statement2;
Break;
Case value3:statement3;
Break;
……..
default: statement;
}
This expression is compared with each case value until the match found. If no case value is matched then default case will get executed. The expression value type may be of type byte, short, int, char. From 1.5v Wrapper classes and enum are allowed to take as conditional expression and from 1.7v Strings are also allowed.
Integer values are checked for equality using == operator and String value invoke equals() for checking equality.
Here is the simple example for switch :-
public class SwitchDemo {
public static void main(String[] args) {
int x=2;
switch (x) {
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
case 3:
System.out.println("three");
break;
default:
System.out.println("default");
}
}
}
Output: two
Break is optional in switch case. If we don’t mention break for the statement after the case, once the match case value found, the statements after next case also executes irrespective of value matches or not.
Let us look this Example:
public class SwitchDemo {
public static void main(String[] args) {
int x=2;
switch (x) {
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
case 3:
System.out.println("three");
break;
default:
System.out.println("default");
}
}
}
Output:
two
three
This is called fall through in switch. So it is necessary to use break for every case.
Now let us see the example in Selenium using Switch.
public void openBrowser(String browserType) {
switch (browserType) {
case "firefox":
driver = new FirefoxDriver();
break;
case "chrome":
driver = new ChromeDriver();
break;
case "IE":
driver = new InternetExplorerDriver();
break;
default:
System.out.println("browser : " + browserType + " is invalid, Launching Firefox as browser of choice..");
driver = new FirefoxDriver();
}
}
Now, based on the browser type value we pass, the driver will be initiated. We can also use the above script to run
1) selenium tests in multiple browsers using the same switch.
2) We can define it as Parameterized tests that execute based on each parameter we defined for each test in testng.xml file.
1) selenium tests in multiple browsers using the same switch.
Configure parallel execution of tests using TestNG selenium
In testing, it is always important to test application in different browsers. We can perform automation on multiple browsers using selenium and testng. If there are more number of tests that need to be executed in parallel on different browsers also, we can do this using testng. And there is an other challenging task such executing tests in different browser versions and also different Operating systems. This can be achieved with Selenium Grid.
We will look into the below examples in detail:
Below is the sample test which will only run one browser at a time and to run all the browsers in parallel, we need to add a parameter in testng.xml file which we will see below
package com.pack;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParallelTest {
private WebDriver driver;
String baseURL = "http://www.google.com/";
@Parameters({ "browser" })
@BeforeTest
public void openBrowser(String browser) {
try {
if (browser.equalsIgnoreCase("Firefox")) {
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver",
"D:/chromedriver.exe");
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("IE")) {
System.setProperty("webdriver.ie.driver",
"D:/IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
} catch (WebDriverException e) {
System.out.println(e.getMessage());
}
}
@Test
public void login_TestCase() {
driver.navigate().to(baseURL);
//do something
}
@Test
public void search_TestCase() {
driver.navigate().to(baseURL);
//do something
}
@AfterTest
public void closeBrowser() {
driver.quit();
}
}
In the above code, we have OpenBrowser method with BeforeTest annotation along with parameter 'browser'. In the xml we will define three tests tags to run each test with different browser. We will compare the browser value with the parameter value and based on that we will create the driver instance. We have now defined three tests with three browsers (Firefox, Google Chrome and Internet Explorer)
Below is the testng.xml file which will run all the tests which are defined. We are passing parameter value with browser name for each test. Tests will be executed in there browsers one by one.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite">
<test name="Firefox Test">
<parameter name="browser" value="Firefox"/>
<classes>
<class name="com.pack.ParallelTest"/>
</classes>
</test>
<test name="Chrome Test">
<parameter name="browser" value="chrome"/>
<classes>
<class name="com.pack.ParallelTest"/>
</classes>
</test>
<test name="Internet Explorer Test">
<parameter name="browser" value="IE"/>
<classes>
<class name="com.pack.ParallelTest"/>
</classes>
</test>
</suite>
We can also use TestNG to execute tests Simultaneously by defining the "parallel" attribute to "tests" to run all the tests in defined browser with the help of testng.xml configuration file.
<suite name="Parallel test suite" parallel="tests">
We just need to update the single line in above testng.xml. By defining parallel="tests" in suite tag, tests will get executed simultaneously. In order to execute to execute tests simultaneously it is recommended to have good system configuration, so that the execution time can be saved.
You can find more details about Executing multiple tests using testng
It looks like below:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="tests">
<test name="Firefox Test">
<parameter name="browser" value="Firefox"/>
<classes>
<class name="com.pack.ParallelTest"/>
</classes>
</test>
<test name="Chrome Test">
<parameter name="browser" value="chrome"/>
<classes>
<class name="com.pack.ParallelTest"/>
</classes>
</test>
<test name="Internet Explorer Test">
<parameter name="browser" value="IE"/>
<classes>
<class name="com.pack.ParallelTest"/>
</classes>
</test>
</suite>
After executing you can view the report, which will look like below:
2)Parameterized tests
Parameterization in TestNG using testng.xml
TestNG allows the user to pass values to test methods as arguments by using parameter annotations through testng.xml file.
Some times it may be required for us to pass values to test methods during run time. Like we can pass user name and password through testng.xml instead of hard coding it in testmethods. or we can pass browser name as parameter to execute in specific browser.
Let us now try to understand parameterization with a basic example.
package com.parameterization;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestParameters {
@Parameters({ "browser" })
@Test
public void testCaseOne(String browser) {
System.out.println("browser passed as :- " + browser);
}
@Parameters({ "username", "password" })
@Test
public void testCaseTwo(String username, String password) {
System.out.println("Parameter for User Name passed as :- " + username);
System.out.println("Parameter for Password passed as :- " + password);
}
}
In the above class, for Test Method 'testCaseOne', we are passing two parameters 'username' and 'password' as input to test method.
The below is the testng.xml file, in which we need to pass the parameter values for the test method
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parameterization Test Suite">
<test name="Testing Parameterization">
<parameter name="browser" value="Firefox"/>
<parameter name="username" value="testuser"/>
<parameter name="password" value="testpassword"/>
<classes>
<class name="com.parameterization.TestParameters" />
</classes>
</test>
</suite>
In the above testng.xml file, we have two attributes for parameter tag, the name attribute which defines name of
the parameter, and the value attribute defines the value of the parameter.
Now after running the above testng.xml file, the output will show as below:
We will get an error if we dont specify the parameter in testng.xml file to a test method.
In the above example, if you comment ant of the parameter, and try to see the error by executing it.
<!-- <parameter name="browser" value="Firefox"/> -->
The below is the exception that you see after execution:
org.testng.TESTNGException:
Parameter 'browser' is required by @Test on method 'testCaseOne' but has not been marked @Optional in testng.xml
The @Parameters annotation can be placed on any method that has a @Test, @Before/After or @Factory annotation.
The XML parameters are mapped to the Java parameters in the same order as they are found in the annotation, and TestNG will issue an error if the numbers don't match.
org.testng.TESTNGException:
Parameter 'browser' is required by @Test on method 'testCaseOne' but has not been marked @Optional or defined in testng.xml
In testng.xml, parameter values can be set at both suite and test level. If we have two parameters with the same name, the one defined in
Comments
Post a Comment