Automation Using Selenium Webdriver

Thursday 27 October 2016

Decide What Test Cases to Automate

Decide What Test Cases to Automate

It is impossible to automate all testing, so it is important to determine what test cases should be automated first.
The benefit of automated testing is linked to how many times a given test can be repeated. Tests that are only performed a few times are better left for manual testing. Good test cases for automation are ones that are run frequently and require large amounts of data to perform the same action.
You can get the most benefit out of your automated testing efforts by automating:
  • Repetitive tests that run for multiple builds.
  • Tests that tend to cause human error.
  • Tests that require multiple data sets.
  • Frequently used functionality that introduces high risk conditions.
  • Tests that are impossible to perform manually.
  • Tests that run on several different hardware or software platforms and configurations.
  • Tests that take a lot of effort and time when manual testing.
Success in test automation requires careful planning and design work. Start out by creating an automation plan. This allows you to identify the initial set of tests to automate, and serve as a guide for future tests. First, you should define your goal for automated testing and determine which types of tests to automate. There are a few different types of testing, and each has its place in the testing process. For instance, unit testing is used to test a small part of the intended application. To test a certain piece of the application’s UI, you would use functional or GUI testing.
After determining your goal and which types of tests to automate, you should decide what actions your automated tests will perform. Don’t just create test steps that test various aspects of the application’s behavior at one time. Large, complex automated tests are difficult to edit and debug. It is best to divide your tests into several logical, smaller tests. It makes your test environment more coherent and manageable and allows you to share test code, test data and processes. You will get more opportunities to update your automated tests just by adding small tests that address new functionality. Test the functionality of your application as you add it, rather than waiting until the whole feature is implemented.
When creating tests, try to keep them small and focused on one objective. For example, separate tests for read-only versus read/write tests. This allows you to use these individual tests repeatedly without including them in every automated test.
Once you create several simple automated tests, you can group your tests into one, larger automated test. You can organize automated tests by the application’s functional area, major/minor division in the application, common functions or a base set of test data. If an automated test refers to other tests, you may need to create a test tree, where you can run tests in a specific order.

How to pass two strings (Username and Password) one by one in the two different text fields (Username and Password) in webdriver (Java)

I am having two different Strings by name username and password. And i want to pass these string values one by one (ONLY one value from username and one value from password) into username and password text fields. I tried in two different ways but not sure where i am doing mistake. Below are my codes and O/P result. Expected: Two combinations (1st: @ and @, 2nd: test and test).
Expected O/P: username password username password
String[] username={"@", "test"};
String[] password= {"@", "test"};
1st method:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TwoStrings {
public static void main(String[] args) {
String[] username={"@", "test"};
String[] password= {"@", "test"};
WebDriver d =new FirefoxDriver();
d.get("http://newtours.demoaut.com/mercurysignon.php");
d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
for(String j:username){
d.findElement(By.name("userName")).clear();
d.findElement(By.name("userName")).sendKeys(j);
System.out.println("userName");
for(int i =0; i<password.length; i++){
d.findElement(By.name("password")).clear();
d.findElement(By.name("password")).sendKeys(password[i]);
System.out.println("password");
}
}
d.findElement(By.name("login")).click();
}
}
O/P: userName password password userName password password
2nd method which i tried:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TwoStrings {
public static void main(String[] args) {
String[] username={"@", "test"};
String[] password= {"@", "test"};
WebDriver d =new FirefoxDriver();
d.get("http://newtours.demoaut.com/mercurysignon.php");
d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
for(String j:username){
for(String k:password){
d.findElement(By.name("userName")).clear();
d.findElement(By.name("userName")).sendKeys(j);
System.out.println("userName");
d.findElement(By.name("password")).clear();
d.findElement(By.name("password")).sendKeys(k);
System.out.println("password");
}
}
d.findElement(By.name("login")).click();
}
}
O/P: userName password userName password userName password userName password

Exception Handling in Java

Exception Handling in Java

The process of converting system error messages into user friendly error message is known asException handling. This is one of the powerful feature of Java to handle run time error and maintain normal flow of java application.

Exception

An Exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's Instructions.

Why use Exception Handling

Handling the exception is nothing but converting system error generated message into user friendly error message. Whenever an exception occurs in the java application, JVM will create an object of appropriate exception of sub class and generates system error message, these system generated messages are not understandable by user so need to convert it into user friendly error message. You can convert system error message into user friendly error message by using exception handling feature of java.
For Example: when you divide any number by zero then system generate / by zero so this is not understandable by user so you can convert this message into user friendly error message like Don't enter zero for denominator.

Hierarchy of Exception classes



Type of Exception

  • Checked Exception
  • Un-Checked Exception

Checked Exception

Checked Exception are the exception which checked at compile-time. These exception are directly sub-class of java.lang.Exception class.
Only for remember: Checked means checked by compiler so checked exception are checked at compile-time.



Un-Checked Exception

Un-Checked Exception are the exception both identifies or raised at run time. These exception are directly sub-class of java.lang.RuntimeException class.
Note: In real time application mostly we can handle un-checked exception.
Only for remember: Un-checked means not checked by compiler so un-checked exception are checked at run-time not compile time.


Difference between checked Exception and un-checked Exception

Checked ExceptionUn-Checked Exception
1checked Exception are checked at compile timeun-checked Exception are checked at run time
3e.g.
FileNotFoundException, NumberNotFoundException etc.
e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.

Difference between Error and Exception

ErrorException
1Can't be handle.Can be handle.
2Example:
NoSuchMethodError
OutOfMemoryError
Example:
ClassNotFoundException
NumberFormateException

Handling the Exception

Handling the exception is nothing but converting system error generated message into user friendly error message in others word whenever an exception occurs in the java application, JVM will create an object of appropriate exception of sub class and generates system error message, these system generated messages are not understandable by user so need to convert it into user-friendly error message. You can convert system error message into user-friendly error message by using exception handling feature of java.

Use Five keywords for Handling the Exception

  • try
  • catch
  • finally
  • throws
  • throw
Syntax for handling the exception

Syntax

try
{
  // statements causes problem at run time 
}
catch(type of exception-1 object-1)
{
  // statements provides user friendly error message 
}
catch(type of exception-2 object-2)
{
  // statements provides user friendly error message
}
finally
{
  // statements which will execute compulsory 
}

Example without Exception Handling

Syntax

class ExceptionDemo 
{
public static void main(String[] args) 
{
int a=10, ans=0;
ans=a/0;
System.out.println("Denominator not be zero");  
}
}
Abnormally terminate program and give a message like below, this error message is not understandable by user so we convert this error message into user friendly error message, like "denominator not be zero".

Example of Exception Handling

Example

class ExceptionDemo 
{
public static void main(String[] args) 
{
int a=10, ans=0;
try
{
ans=a/0;
}
catch (Exception e)
{
System.out.println("Denominator not be zero");
} 
}
}

Output

Denominator not be zero

Next -- try and catch block







try and catch block

try block

Inside try block we write the block of statements which causes executions at run time in other words try block always contains problematic statements.

Important points about try block

  • If any exception occurs in try block then CPU controls comes out to the try block and executes appropriate catch block.
  • After executing appropriate catch block, even through we use run time statement, CPU control never goes to try block to execute the rest of the statements.
  • Each and every try block must be immediately followed by catch block that is no intermediate statements are allowed between try and catch block.

Syntax

try
{
  .....
}
/* Here no other statements are allowed 
between try and catch block */
catch()
{
  ....
}
  • Each and every try block must contains at least one catch block. But it is highly recommended to write multiple catch blocks for generating multiple user friendly error messages.
  • One try block can contains another try block that is nested or inner try block can be possible.

Syntax

try
{
.......
try
{
.......
}
}

catch block

Inside catch block we write the block of statements which will generates user friendly error messages.

catch block important points

  • Catch block will execute exception occurs in try block.
  • You can write multiple catch blocks for generating multiple user friendly error messages to make your application strong. You can see below example.
  • At a time only one catch block will execute out of multiple catch blocks.
  • in catch block you declare an object of sub class and it will be internally referenced by JVM.

Example without Exception Handling

Example

class ExceptionDemo 
{
public static void main(String[] args) 
{
int a=10, ans=0;
ans=a/0;
System.out.println("Denominator not be zero");  
}
}
Abnormally terminate program and give a message like below, this error message is not understandable by user so we convert this error message into user friendly error message, like "denominator not be zero".

Example of Exception Handling

Example

class ExceptionDemo 
{
public static void main(String[] args) 
{
int a=10, ans=0;
try
{
ans=a/0;
}
catch (Exception e)
{
System.out.println("Denominator not be zero");
} 
}
}

Output

Denominator not be zero

Multiple catch block

You can write multiple catch blocks for generating multiple user friendly error messages to make your application strong. You can see below example.

Example

import java.util.*;
class ExceptionDemo 
{
public static void main(String[] args) 
{
int a, b, ans=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter any two numbers: ");
try
{
 a=s.nextInt();
 b=s.nextInt();
 ans=a/b;
 System.out.println("Result: "+ans);
}
catch(ArithmeticException ae)
{
System.out.println("Denominator not be zero");
} 
catch(Exception e)
{
System.out.println("Enter valid number");
} 
}
}

Output

Enter any two number: 5 0
Denominator not be zero

finally Block in Exception Handling

Inside finallyblock we write the block of statements which will relinquish (released or close or terminate) the resource (file or database) where data store permanently.

finally block important points

  • Finally block will execute compulsory
  • Writing finally block is optional.
  • You can write finally block for the entire java program
  • In some of the circumstances one can also write try and catch block in finally block.

Example

class ExceptionDemo 
{
public static void main(String[] args) 
{
int a=10, ans=0;
try
{
ans=a/0;
}
catch (Exception e)
{
System.out.println("Denominator not be zero");
} 
finally
{
System.out.println("I am from finally block");
}
}
}

Output

Denominator not be zero
I am from finally block