A final post (i guess) on Recovery scenario. This post is just the improvement of the Program that I demonstrated in my last post. The difficulty in getting the webdriver to identify the web elements in Gmail caused the delay … Continue reading
Monthly Archives: August 2012
junk post
To skip the 13th post
Recovery Scenario contd…. part 3
As discussed in my previous blog, in this script i’m going to further discuss how the Exception handling can be useful to implement Recovery scenario in Selenium webdriver.
I’m going to make use of same Gmail-login-functionality example here. As told in my previous blog, this code is going to do the following –
- Accept the user id and pw from excel sheet
- if there is a error (incorrect credentials), the login-testing function should identify that (error message) and throw that exception to Test method.
- The function (inside the tester class) has to handle that exception and proceed with next set of user data.
The below program is same as the one i had put in my earlier post except that i’ve added the functions to launch application, to read data from excel, to pass the data to the test function.
In the below example, i have shown only one (unchecked) exception handling in the program. But there is no restriction on adding any number of exception handlers for a test function (we need to have as many catch blocks within the test function to handle each type of exception that any calling function will throw).
class IncorrectPwException extends Exception
{
private static final long serialVersionUID = 1L;
public IncorrectPwException ()
{
System.out.println(” Oops!! Incorrect pw”);
}
}
class Gmail
{
WebDriver d = new InternetExplorerDriver ();
public void launchApp ()
{
d.get(“gmail.com”);
}
public void gtest(String id, String pw) throws IncorrectPwException
{
d.findElement(By.name(“Email”)).sendKeys(id);
d.findElement(By.name(“Passwd”)).sendKeys(pw);
d.findElement(By.name(“signIn”)).click();
d.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
List<WebElement> WrongPwMsg = d.findElements(By.className(“errormsg”));
if (WrongPwMsg.size()>0)
{
System.out.println(“Yes caught”);
throw new IncorrectPwException();
}
}
}
public class GmailTest2
{
Gmail g = new Gmail ();
@BeforeTest
public void setEnv()
{
g.launchApp();
}
@Test (dataProvider = “dp”)
public void testGmail (String uid, String pwd)
{
try
{
g.gtest(uid,pwd);
}
catch (IncorrectPwException i)
{
g.d.findElement(By.name(“Email”)).clear();
i.printStackTrace();
//clear the username field so that we can pass the next parameter
}
}
@DataProvider(name = “dp”)
public Object[][] sendData ()throws Exception
{
Object[][] retData = getArrayTable (“C:\\selDataFolder\\gmailTest.xls”);
return (retData);
}
public static String[][] getArrayTable (String xlPath) throws Exception
{
int r = 0, c =0;
InputStream inputStream = new FileInputStream(xlPath);
POIFSFileSystem fileSystem = new POIFSFileSystem (inputStream);
HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
HSSFSheet sheet = workBook.getSheetAt(0);
Iterator<Row> rows = sheet.rowIterator(); // create a Iterator object of Row type collection class – Rows which is nothing but the collection having all the rows from sheet. The rowIterator() returns the Iterator to start of the collection.
int totalRows = sheet.getPhysicalNumberOfRows();
HSSFRow row1 = sheet.getRow(1);
int totalCols = row1.getLastCellNum();
String[][] tabArray = new String[totalRows][totalCols];
while (rows.hasNext())
{
Row row = rows.next();
Iterator<Cell> cells = row.cellIterator();
while (cells.hasNext())
{
Cell cell = cells.next ();
tabArray[r][c] = cell.getStringCellValue ();
++c;
}
c=0;
++r;
}
return tabArray;
}
}
The snap shot of excel data
Recovery Scenario contd….. part 2
In my last blog, i gave the simple example of using the Exceptions as a means to achieve Recovery scenario in Selenium Webdriver. In this post, I am going to discuss the next level of Exception handling in Java.
As we know, the simple exceptions (checked exceptions) like the “unable to locate element” kinds are fairly simple to handle as discussed in my previous blog. But, what if we encounter other types of business case failures (unchecked Exceptions) which the JVM cannot interpret as exception? We need to create our own exception handlers in such cases.
Let me discuss this with one example here (I am not sure if it’s a best example). Let’s assume that we need to test that for different sets of user id and pw combination in gmail, we want to test the login functionality.
- Provide the data for user id and pw fields
- Click on Sign in button
- If success, just for verification, click on compose button.
In the design of above test case in selenium, we need to keep in mind that if either the user id or pw is not correct, the login will fail and a message appears just above the sign in button. If we don’t want the script to fail (you may have intentionally given the wrong combination – negative test case) then –
- we need to identify that error message
- Define a exception handler for it
- Throw the exception.
- Clear the user id/pw field
- Accept the next set of data and proceed.
In the below code snippet I have done the following –
- Created a class in which there is function to do the logging in into the gmail.
- In the same function, I’ve added lines of code to check if error message is displayed.
- If yes, then it will throw the IncorrectPwExceptionwhich I’ve created.
- This will be thrown to the function inside the Test class (main method).
- The catch block will catch it and then clears the user id field.
I will work to improve this code to do the following –
- Accept the user id and pw from excel sheet
- if there is a error, it should catch that and clear the user id/pw field.
- The function (inside the tester class) has to proceed with next set of user data.
class IncorrectPwException extends Exception {
public IncorrectPwException ()
{
System.out.println(” Oops!! Incorrect pw”);
}
}
class Gmail {
WebDriver d = new InternetExplorerDriver ();
public void gtest() throws IncorrectPwException
{
d.get(“gmail.com”);
d.findElement(By.name(“Email”)).sendKeys(“saina.nehwal”);
d.findElement(By.name(“Passwd”)).sendKeys(“bronzeMedal”);
d.findElement(By.name(“signIn”)).click();
d.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
List<WebElement> WrongPwMsg = d.findElements(By.classNam(“errormsg”));
if (WrongPwMsg.size()>0)
{
System.out.println(“Yes caught”);
throw new IncorrectPwException();
}
}
}
public class GmailTest2{
public static void main (String a[])
{
Gmail g = new Gmail ();
try
{
g.gtest();
}
catch (IncorrectPwException i)
{
i.printStackTrace();
//clear the username field so that we can pass the next parameter
g.d.findElement(By.name(“Email”)).clear();
}
}
}
Recovery Scenario in Selenium Webdriver
Those who are used to the Recovery Scenario function in QTP will find it difficult to work with selenium webdriver without such kind of option in it. From the day I started my journey with Selenium, I have found that the successful usage of it depends on the expertise in the programming language that we use.
If you are using Java, the one way you can implement Recovery scenario is by using Exception Handling.
To demonstrate this, I use Gmail website.
- In the try block, I intentionally made mistake in the webelement-property-name of sign-in button.
- in catch block, I used the correct property.
- The program worked without stopping after throwing “Element Not Found” error.
- Also, the console didnt show any error. Only when we print the stacktrace, the”Element Not Found” error was printed in console.
try
{
d.findElement(By.name(“Email”)).sendKeys(“vishwas.shenoy”);
d.findElement(By.name(“signI”)).click();
}
catch (Exception e)
{
e.printStackTrace();
d.findElement(By.name(“Passwd”)).sendKeys”password”);
d.findElement(By.name(“signIn”)).click();
}
