A post to be used as a reference for items/issues that has surfaced when using WebDriver with Java.
- Muting WebDriver logs
- Avoiding untrusted SSL cerificate
- FireFox in fullscreen mode, F11
- Relative xpaths from the current WebElement
- Switching between open windows
- Dismissing/accepting alert dialog windows
Muting WebDriver logs
At points where WebDriver is really verbose it is quite convenient to mute the output or at least certain levels of it.
// Java // Set it before creating the WebDriver instance to avoid having any messages generated from the // constructor call RemoteWebDriver.setLogLevel(Level.WARNING); // java.util.logging.Level WebDriver driver = new FirefoxDriver(profile);
Avoiding untrusted SSL cerificate errors when using Firefox
Typical test environment issue. A simple workaround that worked for us was to simply set the profile used to accept and assume any certificate issues.
// Java FirefoxProfile profile = new FirefoxProfile(); profile.setAcceptUntrustedCertificates(true); profile.setAssumeUntrustedCertificateIssuer(false); WebDriver driver = new FirefoxDriver(profile);
Running FireFox in full screen
Especially handy when combining Sikuli and WebDriver, you need to be absolutely sure that as much as possible is visible on the screen.
((FirefoxDriver)driver).getKeyboard().pressKey(Keys.F11);
Relative xpaths from the current WebElement
Very useful when working with complex HTML-pages whose elements are very deep down and can only be found using xpath. Maintaining these long xpaths in multiple places in the test code base is a pain. The solution is to do the xpathing in parts to minimize the effort needed to maintain the xpath-strings.
<html> <body> <table> <tbody> <th></th> <tr> <td>cell to find</td><td></td> </tr> <tr> <td></td><td></td> </tr> </tbody> </table> </body> </html>
Starting the xpath-string with the DOT is equivalent to starting the search from the current node.
WebElement tableElement = driver.findElement(By.xpath("//html/body/table")); // Using relative xpath from the beginning of the table to find the first cell. // Note that it has to start with the '.', DOT WebElement cell = driver.findElement(By.xpath("./tbody/tr[1]/td[1]"));
Switching between open windows
Based on words in the page title.
Set<String> windows = driver.getWindowHandles(); for (String window : windows) { driver.switchTo().window(window); if (driver.getTitle().contains(pageTitle)) { break; } }
Dismissing/accepting alert dialog windows
Whenever an alert window pops-up it has to be accepted or dismissed to avoid blocking the test execution, this is simply handled using Alert class (Selenium docs).
// WebDriver driver = new FirefoxDriver(); Alert alert = driver.switchTo().alert(); alert.accept(); // OR // alert.dismiss();