Overview
TestNG-Selenium takes care of all the boilerplate involved in writing
selenium tests.
Project Goals
- Configuration through System properties and annotations.
- Facilitate running tests in parallel.
- Remove typical boilerplate, such as taking screenshots on test
failures, configuring WebDriver, and instantiating page objects.
- Declarative way of defining page objects with PageFactory.
- Declarative way of configuring tests individually.
Maven Dependency
Writing Suites
In each of your suite classes, extend
AbstractSuite and pass
your PageFactory as a generic type argument.
AbstractSuite provides a
getPageFactory
method to create the page factories passed in as generic type arguments. In
this example, the page factory passed as a generic type on line 7 is
returned by
getPageFactory.
Writing a PageFactory
A PageFactory is nothing more than an interface:
Each method declared in a
PageFactory should return a sub class
of
Page. This allows TestNG-Selenium to do some cool things
when initializing your page objects, like navigating to them when
WebDriver has been created, wiring up annotated fields using
Selenium's PageFactory initializer, and validating that the URL currently
being viewed by WebDriver is valid for the requested page. This approach
also allows you to avoid boilerplate by letting TestNG-Selenium manage your
page factory's lifecycle.
Writing a Page object.
- Create a class I.E. GoogleHomePage
- Extend com.github.jsdevel.testng.selenium.AbstractPage.
- Pass your page object's type, and the page factory used to create it
as generic type arguments to AbstractPage.
- Optionally add WebElement fields annotated with @FindBy (see
Google's
PageFactory pattern.).
If you need to do something before validation occurs, such as wait for
requests, or poll a global javascript variable, you can override
AbstractPage#handlePageInitialized().