Get a new iPhone with help from Selenium

Posted: June 30th, 2010 | Author: Dave | Filed under: Examples | Tags: , , , , , , | No Comments »

I’ve been looking forward to the iPhone 4 ever since the 3GS was released, as I was in contract at the time and decided to wait another year for whatever Apple decided to release next. Due to unexpected (but very cool) circumstances, I was out of the country on the day Apple released the iPhone 4 and so was unable to queue up for one of the devices, and now due to the demand it’s very difficult to get hold of one. Since getting back to the UK I have been visiting my two nearest O2 stores to check if they have had a delivery, and today they pointed me towards their online stock checker…

So rather than check this manually, I decided to write a couple of small Selenium 2 scripts to check the stock levels at my ‘local’ stores, and to find out all stores that do have the devices. These examples use Java with TestNG (for dataproviders), and Selenium 2.

Read the rest of this entry »


Construct unique safe filenames in TestNG

Posted: October 22nd, 2009 | Author: Dave | Filed under: Examples | Tags: , , | No Comments »

In order to save files for investigating failures from within TestNG it’s important to have a safe filename that is unique to the test – otherwise you may overwrite important files. I have written the following simple method in Java that is called from a listener with an ITestResult parameter to construct a unique file name that should be safe on most file systems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private static String fileNameFrom(ITestResult result, String fileExtension) {
  List<String> parts = new ArrayList<String>();
  parts.add(result.getTestClass().getName());
  parts.add(result.getMethod().getMethodName());
 
  //add parameters
  Object[] parameters = result.getParameters();
  for (Object parameter: parameters)
    parts.add(parameter != null ? parameter.toString() : NULL_PARAMETER_VALUE);
 
  String fileName = StringUtils.join(parts.toArray(new String[parts.size()]), ".");
  fileName = fileName.replaceAll("[\\^\\\\.\\-:;#_]", "_");
  return fileName + "." + fileExtension;
}

Using soft assertions in TestNG

Posted: October 8th, 2009 | Author: Dave | Filed under: Guide | Tags: , , | 24 Comments »

One of the big differences between Selenium IDE and a Selenium RC solution is the ability to perform ‘soft’ assertions. Selenium IDE users can append commands with verify or assert to determine whether the test execution should stop when a failure is observed. A popular use for this is to first assert that you are on the correct page (assertTitle) and then verify elements on the page. If you were only able to assert then your tests may fail early on, not revealing further failures that may exist.

In Selenium RC you are limited by your test framework, and from my experience using the Java client library there isn’t a satisfactory equivalent to the soft assertion feature of Selenium IDE. Some solutions propose that you catch the assertions, log the occurrence of the failure, and check that there are no failures at the end of the test. The problem here is remembering to check for these verification failures.

Another solution suggests putting the check for verification failures in a method that is run by the test framework after every test, however when these fail (in TestNG) they are marked as configuration failures, and the default HTML report can still report your test suites as passed.

Cédric Beust (creator of TestNG) has discussed soft assertions on his blog, but most of the proposed solutions differ from the simple implementation that would encourage more Selenium IDE users to adopt Selenium RC.

TestNG has support for custom listeners, which can run when tests pass/fail/skip, as well as before and after invocation. By adding a custom listener to check for verification failures after invocation, we can get the details of all verification failures that have occurred, and report them at the same time as we report our hard failure, or if there are no hard failures we can change the result to a failure and report the verification failures.

This solution uses part of the TestNG soft failures patch by Dan Fabulich in order to combine the stack traces of multiple failures. Details of the patch are available here.

I have created a simple Eclipse project that can be downloaded, extracted and run. To keep it simple, this project does not use Selenium. You will need to add these files into your own project.

Read the rest of this entry »