Java Reference
In-Depth Information
Let's look at this example. We open the form page, save the current page title, and click
the Submit button. This raises the alert because we didn't type in a value. Next, we call
getAlert b to check whether the code raised the correct alert. Finally, we make sure
we're still on the same page by comparing the new page title with the saved title.
Selenium tip
As of version Selenium 1.0 Beta 2, JavaScript alerts won't pop up a visible alert
dialog box. JavaScript alerts generated from a page's onload event handler aren't
supported. If this happens, JavaScript will open a visible dialog box, and Selenium
will wait until someone clicks the OK button.
We don't need to create a test to check whether an alert has taken place during nor-
mal operation of our page. If the test generates an alert but getAlert doesn't con-
sume it, the next Selenium action will throw a SeleniumException , for example:
com.thoughtworks.selenium.SeleniumException:
ERROR: There was an unexpected Alert! [Please enter a value.]
12.8.8
Capturing a screen shot for a JUnit 3 test failure
Selenium provides the ability to capture a screen shot at the time of failure to sub-
classes of SeleneseTestCase . Selenium disables this feature by default; to enable it,
call setCaptureScreenShotOnFailure(true) . By default, the screen shot is written to
a PNG file in the Selenium server directory with the same name as the test name given
to the SeleneseTestCase String constructor.
12.8.9
Capturing a screen shot for a JUnit 4 test failure
To access this feature from a JU nit 4 test case, you'll need to modify the search exam-
ple as shown in listing 12.17.
Listing 12.17
Capturing a screen shot on JUnit 4 test failure
B
private void captureScreenshot(Throwable t) throws Throwable {
if (selenium != null ) {
String filename = this.getClass().getName() + ".png";
try {
selenium.captureScreenshot(filename);
System.out.println("Saved screenshot " + filename + " for " +
t.toString());
} catch (Exception e) {
System.err.println("Exception saving screenshot " + filename +
" for " + t.toString() + ": " + e.toString());
e.printStackTrace();
}
throw t;
}
}
C
 
 
 
 
 
 
 
Search WWH ::




Custom Search