Exception Handling with Selenium

955 views
Skip to first unread message

Tarun K

unread,
Jul 21, 2009, 8:05:28 AM7/21/09
to selenium-developers
Hello Dev Team,

Can you guys please have a look at this thread -

http://clearspace.openqa.org/message/65614#65614

and let doc team know of you thoughts about this approach and better
possibilities?

Thanks in advance
Tarun K

Tarun K

unread,
Jul 21, 2009, 11:33:05 PM7/21/09
to selenium-developers
Hello there,

I am keenly waiting to hear from dev like there are better options
than described in thread above.

~ T

jcmeyrignac

unread,
Jul 22, 2009, 8:38:20 AM7/22/09
to selenium-developers
Your link doesn't work. I get a "Not authorized" error.

Tarun Kumar Bhadauria

unread,
Jul 22, 2009, 8:42:14 AM7/22/09
to selenium-...@googlegroups.com
This may ne cause forum is not public... Let me check if I could invite you...

Tarun K

unread,
Jul 22, 2009, 8:43:36 AM7/22/09
to selenium-developers
check your gmail account... if u would have got invitation..

jcmeyrignac

unread,
Jul 24, 2009, 5:51:38 AM7/24/09
to selenium-developers
Sorry, but I didn't receive anything.

Why not copy/paste here ?

BTW, I added exception handling in our user-extensions, this is why
I'm interested in your post.

JC

Tarun K

unread,
Jul 24, 2009, 5:58:55 AM7/24/09
to selenium-developers
Let me keep it here, though it is not exactly how it was in my initial
post in forum.
(Let us know also know what you have done in user extension)

Problems:

Selenium doesn’t provide any indication to user as to what was carried
out during execution of a method. Like typing in a text box. This sort
of message would be highly comprehensible to non technical QA to know
what was done during exe of a method

Selenium doesn’t provide exception handling hence if element locator
is not found then execution of a method would be aborted, which is
highly Inconvenient in case of non mandatory / least significant
objects in a page.

So this is what we have come up with –

Exception handling –

SafeSelenium class will add exception handling to most frequently used
selenium methods.

Below mentioned class uses the type method [by calling type
(textBoxLocator, testData, webObject, logMessage);] selenium to
demonstrate it.

************************************************************************************
public class SafeSelenium extends SelTestCase {

private static ScreenshotTaker screenshotTaker = new ScreenshotTaker
();;
private static String failureImageFolder = "/reportng/
failureimages/";


/**
* Returns image file path for failure images
*
* @param elementLocator HTML Identifier of object
* @param webObject User Identifier for object Should <b>NOT</b>
contain spaces
* @return imageFilePath
*/
public String getImageFilePath (String elementLocator, String
webObject) {

String imageFilePath = null;

try {
imageFilePath = new StringBuilder(new java.io.File
(".").getCanonicalPath())
.append(failureImageFolder).append(this.getClass().getName())
.append(webObject).append(elementLocator)
.append(UUID.randomUUID()).append(".png").toString();

} catch (IOException e) {
Reporter.log(e.getMessage());
}
return imageFilePath;
}

public StringBuilder getErrorMessage (String webObject,
SeleniumException se) {

StringBuilder errorMessage = null;

try {

errorMessage = new StringBuilder("<span style=\"background: pink;
color:blue;\">")
.append(webObject).append(" is not available.")
.append(se.getMessage()).append("</span> ");

} catch (FilesException fe) {
Reporter.log(fe.getMessage());
}
return errorMessage;
}


/**
* Types in text box.
* Exception is raised in test report if element locator is not
found.
*
* @param textBoxLocator Text box locator
* @param testData Data to be keyed in
* @param webObject User Identifier for object Should <b>NOT</b>
contain spaces
* @param Boolean flag to determine logging
*/
public void safeType(String textBoxLocator, String testData, String
webObject, boolean logMessage) {

// Path for image for failed selenium commands.
String imageFilePath = getImageFilePath(textBoxLocator, webObject);

try {
// Type in text box.
LogSelenium.type(textBoxLocator, testData, webObject,
logMessage);

} catch (SeleniumException se) {
// Prepare error message.
StringBuilder errorMsessage =
getErrorMessage(webObject, se);

try {
// Capture Desktop screen shot.
screenshotTaker.saveDesktopAsPng (imageFilePath);

// Add screen shot to error message.
errorMsessage.append("<a href=file:///")
.append(imageFilePath).append(">ScreenShot</a> <br>" );

} catch (FilesException fe) {

// Report failure of screen shot capture.
Reporter.log(fe.getMessage());
}

// Log failure in test report along with screen shot when type
operation was unsuccessful.
Reporter.log(errorMsessage.toString());
}
}
}
**************************************************************************************************************************************
**************************************************************************************************************************************

Now:
Logging selenium – which enables logging of selenium operation in test
report. Type method which is used above in try block is actually
available here.

**************************************************************************************************************************************
**************************************************************************************************************************************
public class LogSelenium extends SelTestCase {

/**
* Provides logging for <code>type</code> method of
* <code>SeleneseTestBase</code> class
*
* @param textBoxLocator Text box locator
* @param testData Data to be keyed in
* @param webObject User Identifier for object Should <b>NOT</b>
contain spaces
* @param logMessage Boolean flag to determine logging
*/
public static void type(String textBoxLocator, String testData,
String webObject,
Boolean logMessage) {

// Type in text box.
selenium.type(textBoxLocator, testData);

if (logMessage) {

// Log the success message if type operation was successful.
if(testData.equals(selenium.getValue(textBoxLocator))) {
Reporter.log(new StringBuilder("<span style=\"color:green;\">")
.append(testData).append(" is entered in ")
.append(webObject).append("</span> <br>").toString());

} else {
Reporter.log(new StringBuilder("<span style=\"background: pink; "
+
"color:blue\">").append("Entered test data doesn't appear in ")
.append(webObject).append(" Entered value was: ")
.append(testData).append(" while text box value is: ")
.append(selenium.getValue(textBoxLocator))
.append("</span> <br>").toString());
}
}
}
}
**************************************************************************************************************************************
**************************************************************************************************************************************

Here Boolean flag is to enable or disable logging as one may not
always like to have these messages available in report.

So now one can perform type operation in different ways –



// Will do exception handling, but will not raise success
message in report

safeSelenium.safeType("q1", "Test", "Search Text Box",
false);



// Will do exception handling, and will also raise
success message in report

safeSelenium.safeType("q", "Test1", "Search Text Box",
true);



// Will perform type and will raise success message in test report
with out any exception

// handling

type("q", "Test4", "Search Text Box", true);



// Will just perform type with out any exception handling
and will not raise success

// message is test report

type("q2", "Test3", "Search Text Box", false);
**************************************************************************************************************************************
**************************************************************************************************************************************

I can not attached test report to demo this, but I guess u would
understand what I am trying to do

~ T


jcmeyrignac

unread,
Jul 25, 2009, 6:41:20 AM7/25/09
to selenium-developers
I don't see the interest of keeping even more logs.

> Selenium doesn’t provide any indication to user as to what was carried
> out during execution of a method. Like typing in a text box. This sort
> of message would be highly comprehensible to non technical QA to know
> what was done during exe of a method

If your goal is to help non technical QA to understand what's behind
every function, adding more information will only confuse them.
Instead, you should try to improve the readability of your functions,
by using locators that speak to them.
For example, replacing "type id input" by "type text_locator input",
where text_locator describes where you type.
Name: <input type=text id=whatever>
you should be able to use 'Name' as the locator, not whatever

> Selenium doesn’t provide exception handling hence if element locator
> is not found then execution of a method would be aborted, which is
> highly Inconvenient in case of non mandatory / least significant
> objects in a page.

This is a different need !

In my opinion, a test is a test, and must not have any optional path.
If an element is not present, then it fails, you must not have a way
to continue the test.
We see this difference of behavior in verify/assert, where verify
simply logs an error and continues, contrary to assert.

Your safetype introduces a second behavior, and it's dangerous in a
test, since people will probably start using safetype instead of type.

BTW, your safeType is very verbose, and boring to type.
If you use it once or twice, then you should add it in user-
extensions.js.

Finally, last point: saving a snapshot of your page is not the best
way to save an error.
You should instead save the HTML content of the page, so that you can
locate your error (to my knowledge, having a picture will not help you
finding the problem).
It's a million time easier to locate an error in an HTML page than in
a picture !
In our case, we have some non technical QA who maintain script tests
with Selenium IDE, and when an error appears, a snapshot of the page
is saved on the build server.
The build server contains the whole CSS/Javascript/pictures so they
can check in just one click where the error lies !
When the error is harder to locate, they simply load Selenium IDE,
load the test suite, place a breakpoint where the error is, and run
the script until this point.

So, after all this text, I think that if you have several non
technical QA working on your project, I strongly recommend you to use
Selenium IDE, instead of using the scripting language. It's a lot more
easy to understand and especially to MAINTAIN !
In our case, the application is still moving a lot, and the QA fixes
the problems in 99% of the cases (since executing our tests take 4
hours, it's impossible to fix the problems BEFORE committing).

Another interesting effect of Selenium IDE is that it's very easy to
add tests, compared to scripting them manually.
Note that Selenium IDE needs some manual tweaking to test certain
things (like Flash elements, etc...).

JC
Reply all
Reply to author
Forward
0 new messages