com.appfoundation.automation.framework
Class BaseSeleniumTest

java.lang.Object
  extended by com.appfoundation.automation.framework.BaseSeleniumTest

public class BaseSeleniumTest
extends java.lang.Object

Base Selenium Test

The basic structure of a test, which handles the details of working with specific browser drivers in an abstract way. This handles working with external services for browsers that require it.

Author:
john.valentino

Field Summary
private static Browser currentBrowser
          Keeps track of the browser for the current running test method
protected  org.openqa.selenium.WebDriver driver
          The web driver used for browser interaction
private  BrowserVersion emulatedVersion
          For HTML unit based test classes, allow the specification of a version to emulate
private static org.apache.log4j.Logger log
           
 org.junit.rules.TestName name
          This JUnit rule is used for determining the name calling test class
private  Browser overrideBrowser
           
private  RuntimeProperties properties
          Runtime properties specified in the properties file on the root level of the classpath
private  int screenShotCount
          Current number of screenshots that have been taken in the currently running test case method
private static org.openqa.selenium.remote.service.DriverService service
          The driver service is only used for browsers that are not natively supported by the selenium web driver
 
Constructor Summary
BaseSeleniumTest()
           
 
Method Summary
static void afterClass()
          This method should be called after all the test methods in a given test class have run.
static void beforeClass()
           
 void beforeEachTestCase()
          This method should be called before each test method is run.
protected  void click(java.lang.String xpath)
          Clicks the web element matching the given xpath
protected  void clickAt(org.openqa.selenium.By by)
          This is something that was present in Selenium prior to Web Driver, which forces clicking on the element of the matching expression whether that element is clickable or whether that click would be at some other location on the screen.
protected  void createDriver()
          Handles creating the appropriate web driver
protected  org.openqa.selenium.remote.service.DriverService createDriverService(Browser currentBrowser)
          Instantiates the driver service (if needed) for the given browser type
protected  org.openqa.selenium.remote.service.DriverService createDriverServiceForChrome()
          Creates and starts the driver service for Chrome
protected  org.openqa.selenium.WebDriver createWebDriverForChrome()
          Creates the Web Driver implementation for Chrome.
protected  org.openqa.selenium.WebDriver createWebDriverForFirefox()
          Creates the Web Driver implementation for Firefox.
protected  org.openqa.selenium.WebDriver createWebDriverForHtmlUnit(BrowserVersion version)
          Creates the Web Driver implementation for HTML unit.
protected  org.openqa.selenium.WebDriver createWebDriverForInternetExplorer()
          Creates the Web Driver implementation for Internet Explorer.
protected  org.openqa.selenium.WebDriver createWebDriverForSafari()
          Creates the Web Driver implementation for Safari.
 void doubleClick(java.lang.String xpath)
          Double-clicks on the element that matches the given xpath
protected  void dragAndDrop(org.openqa.selenium.WebElement element, int x, int y)
          Performs a drag and drop
 java.lang.String getBaseUrl()
          Returns the base URL for all test locations
 org.openqa.selenium.WebDriver getDriver()
          Returns the current web driver
 int getScreenShotCount()
           
 void outputScreenShot()
          Takes a screenshot of the current browser and outputs it as a PNG to the specified output directory specified in the application properties file on the root level of the classpath.
 void screenshot(java.lang.String fileName, java.io.File outputDir)
           
 void setOverrideBrowser(Browser browser)
           
protected  java.io.File takeScreenShot(Browser currentBrowser, org.openqa.selenium.WebDriver driver)
          Handles selecting the browser implementation for getting a screenshot
protected  java.io.File takeScreenShotWithChrome(org.openqa.selenium.WebDriver driver)
          Takes a screen shot using Chrome
protected  java.io.File takeScreenShotWithFirefox(org.openqa.selenium.WebDriver driver)
          Takes a screen shot using Firefox
 void tearDown()
          Handles stopping the web driver after each test case method
protected  void waitForTextValueToBePresent(java.lang.String text, java.lang.String xpath)
          Waits for the specified text to be present
protected  void waitToBeClickable(org.openqa.selenium.By by)
          Waits for up to 1 second for the given element to be click-able
protected  void waitToBeClickable(org.openqa.selenium.By by, int timeoutInSeconds)
          Waits up until the specified timeout in seconds for given element to be click-able
protected  void waitToBeClickable(java.lang.String xpath)
          Waits up to 1 second for the given element by XPath to be click-able
protected  void waitToBeClickableAndThenClick(java.lang.String xpath)
          Waits for the element matching the given xpath to be clickable and then clicks in
protected  void waitToBeClickableAndThenClick(java.lang.String xpath, int time)
          Waits for the element matching the given xpath to be clickable and then clicks in
protected  void waitToBeClickableAndThenDoubleClick(java.lang.String xpath, int time)
          Waits for the element of the given xpath to be clickable, and then double-clicks it
protected  void waitUntilElementDoesNotExist(java.lang.String xpath)
          Waits until the given element no longer exists
protected  void waitUntilElementExists(java.lang.String xpath)
          Waits until the specified element exists
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static org.apache.log4j.Logger log

name

public org.junit.rules.TestName name
This JUnit rule is used for determining the name calling test class


service

private static org.openqa.selenium.remote.service.DriverService service
The driver service is only used for browsers that are not natively supported by the selenium web driver


driver

protected org.openqa.selenium.WebDriver driver
The web driver used for browser interaction


screenShotCount

private int screenShotCount
Current number of screenshots that have been taken in the currently running test case method


currentBrowser

private static Browser currentBrowser
Keeps track of the browser for the current running test method


properties

private RuntimeProperties properties
Runtime properties specified in the properties file on the root level of the classpath


emulatedVersion

private BrowserVersion emulatedVersion
For HTML unit based test classes, allow the specification of a version to emulate


overrideBrowser

private Browser overrideBrowser
Constructor Detail

BaseSeleniumTest

public BaseSeleniumTest()
Method Detail

setOverrideBrowser

public void setOverrideBrowser(Browser browser)

getDriver

public org.openqa.selenium.WebDriver getDriver()
Returns the current web driver

Returns:

getBaseUrl

public java.lang.String getBaseUrl()
Returns the base URL for all test locations

Returns:

getScreenShotCount

public int getScreenShotCount()

outputScreenShot

public void outputScreenShot()
Takes a screenshot of the current browser and outputs it as a PNG to the specified output directory specified in the application properties file on the root level of the classpath.


screenshot

public void screenshot(java.lang.String fileName,
                       java.io.File outputDir)

takeScreenShot

protected java.io.File takeScreenShot(Browser currentBrowser,
                                      org.openqa.selenium.WebDriver driver)
Handles selecting the browser implementation for getting a screenshot

Parameters:
currentBrowser -
Returns:

takeScreenShotWithChrome

protected java.io.File takeScreenShotWithChrome(org.openqa.selenium.WebDriver driver)
Takes a screen shot using Chrome

Parameters:
driver -
Returns:

takeScreenShotWithFirefox

protected java.io.File takeScreenShotWithFirefox(org.openqa.selenium.WebDriver driver)
Takes a screen shot using Firefox

Parameters:
driver -
Returns:

beforeClass

public static void beforeClass()

afterClass

public static void afterClass()
This method should be called after all the test methods in a given test class have run. For example if test class MyTestClass has test methods A, B, and C, when all of those tests have completed this method is run. The function of this method is to stop the Driver Service if it is currently in use.


createDriver

protected void createDriver()
                     throws java.lang.Exception
Handles creating the appropriate web driver

Throws:
java.lang.Exception

createWebDriverForChrome

protected org.openqa.selenium.WebDriver createWebDriverForChrome()
Creates the Web Driver implementation for Chrome. This is called whenever the framework needs to create a web driver to run a particular test case method.

Returns:

createWebDriverForFirefox

protected org.openqa.selenium.WebDriver createWebDriverForFirefox()
Creates the Web Driver implementation for Firefox. This is called whenever the framework needs to create a web driver to run a particular test case method.

Returns:

createWebDriverForInternetExplorer

protected org.openqa.selenium.WebDriver createWebDriverForInternetExplorer()
Creates the Web Driver implementation for Internet Explorer. This is called whenever the framework needs to create a web driver to run a particular test case method.

Returns:

createWebDriverForSafari

protected org.openqa.selenium.WebDriver createWebDriverForSafari()
Creates the Web Driver implementation for Safari. This is called whenever the framework needs to create a web driver to run a particular test case method.

Returns:

createWebDriverForHtmlUnit

protected org.openqa.selenium.WebDriver createWebDriverForHtmlUnit(BrowserVersion version)
Creates the Web Driver implementation for HTML unit. This is called whenever the framework needs to create a web driver to run a particular test case method.

Returns:

beforeEachTestCase

public void beforeEachTestCase()
                        throws java.lang.Exception
This method should be called before each test method is run. For example if test class MyTestClass has test methods A, B, and C, this method is run before A, then before B, and then before C. The purpose of this method is to determine the type of browser in use, to handle creating a driver service if needed, and to create the web driver for the determined browser.

Throws:
java.lang.Exception

createDriverService

protected org.openqa.selenium.remote.service.DriverService createDriverService(Browser currentBrowser)
                                                                        throws java.lang.Exception
Instantiates the driver service (if needed) for the given browser type

Parameters:
currentBrowser -
Returns:
Throws:
java.lang.Exception

createDriverServiceForChrome

protected org.openqa.selenium.remote.service.DriverService createDriverServiceForChrome()
                                                                                 throws java.lang.Exception
Creates and starts the driver service for Chrome

Returns:
Throws:
java.lang.Exception

tearDown

public void tearDown()
Handles stopping the web driver after each test case method


waitToBeClickable

protected void waitToBeClickable(java.lang.String xpath)
Waits up to 1 second for the given element by XPath to be click-able

Parameters:
xpath -

waitToBeClickable

protected void waitToBeClickable(org.openqa.selenium.By by)
Waits for up to 1 second for the given element to be click-able

Parameters:
by -

waitToBeClickable

protected void waitToBeClickable(org.openqa.selenium.By by,
                                 int timeoutInSeconds)
Waits up until the specified timeout in seconds for given element to be click-able

Parameters:
by -
timeoutInSeconds -

waitToBeClickableAndThenClick

protected void waitToBeClickableAndThenClick(java.lang.String xpath)
Waits for the element matching the given xpath to be clickable and then clicks in

Parameters:
time -

waitToBeClickableAndThenClick

protected void waitToBeClickableAndThenClick(java.lang.String xpath,
                                             int time)
Waits for the element matching the given xpath to be clickable and then clicks in

Parameters:
xpath -
time -

click

protected void click(java.lang.String xpath)
Clicks the web element matching the given xpath

Parameters:
xpath -

doubleClick

public void doubleClick(java.lang.String xpath)
Double-clicks on the element that matches the given xpath

Parameters:
xpath -

waitToBeClickableAndThenDoubleClick

protected void waitToBeClickableAndThenDoubleClick(java.lang.String xpath,
                                                   int time)
Waits for the element of the given xpath to be clickable, and then double-clicks it

Parameters:
xpath -
time -

clickAt

protected void clickAt(org.openqa.selenium.By by)
This is something that was present in Selenium prior to Web Driver, which forces clicking on the element of the matching expression whether that element is clickable or whether that click would be at some other location on the screen.

Parameters:
by -

waitForTextValueToBePresent

protected void waitForTextValueToBePresent(java.lang.String text,
                                           java.lang.String xpath)
Waits for the specified text to be present

Parameters:
text -
xpath -

waitUntilElementDoesNotExist

protected void waitUntilElementDoesNotExist(java.lang.String xpath)
                                     throws java.lang.Exception
Waits until the given element no longer exists

Parameters:
xpath -
Throws:
java.lang.Exception

waitUntilElementExists

protected void waitUntilElementExists(java.lang.String xpath)
                               throws java.lang.Exception
Waits until the specified element exists

Parameters:
xpath -
Throws:
java.lang.Exception

dragAndDrop

protected void dragAndDrop(org.openqa.selenium.WebElement element,
                           int x,
                           int y)
                    throws java.lang.Exception
Performs a drag and drop

Parameters:
element -
x -
y -
Throws:
java.lang.Exception