What is a page object
Simply put, a page object is an object that Selenium uses as a representation of an HTML element. Selenium tests will not interact with HTML code directly, but with objects that use selectors to refer to particular bits of the HTML code.
Defining page objects
You will need to create independent classes for declaring your page objects. Such a class should group together all the page objects that belong to the same page, or to the same module that is about to be tested. They should be grouped logically and naturally. The tests and page objects should not belong to the same Java class (they should be independent one of the other). This has a great number of advantages, amongst them: avoiding redundant code (having a page object in only one place), availability of a page object to every test class that needs it (every test class that needs an object will access it from the same location), changing of the selector will be done in only one place if the HTML code changes.
A recommended way of declaring a page object is presented here: Continue reading Creating the page objects
Identifying HTML elements in order to interact with them within you tests can be done by using CSS Selectors, which use pattern matching to easily find these elements. Below are the most used patterns to identify the elements on a page and examples of their usage: Continue reading CSS Selectors
To select HTML elements from your page, you can use XPath selectors, which are basically a set of expressions that will extract the nodes you require. The nodes are obtained by following a path in the HTML document, either downwards from a known node, or upwards (it searches for descendants or ancestors of a known element). To find elements using XPATH, find below what suits your search: Continue reading XPATH selectors
An HTML element has the following syntax:
<element [attribute="value"|...] />
An HTML element in enclosed within a preceding ‘<‘ and a trailing ‘/>’ characters. An HTML element can have none, one or many attributes, depending on its’ type. The attribute has pairs of keys and values to define additional information about the element.
There are a few attributes that can be assigned to any HTML element. Two of them are described here:
- ID – to specify a unique identifier for the element.
- CLASS – an element can have one or more classes.
There are a great number of HTML elements, but for the purpose of Selenium testing, the most commonly used ones are presented below.
Common HTML elements
Continue reading HTML and Selenium. An introduction
After the project is created, you need to setup the Selenium dependency, in order to use the library’s functionality. Make sure you always have the latest Selenium libraries available. The constant upgrade of the modern browsers might make some Selenium features unavailable or not working properly with older library versions.
To see what browser releases are scheduled in the future, you can check out these links:
Continue reading Setting up the Selenium bits
After the project has been created, you will need to decide how you want your automated tests to run. Keeping in mind that developers write unit tests, which by definition will validate of the code by itself, without interaction with other components, they are suitable to validate that the code commited satisfies the requirements in isolation. They should run fast, and not need interaction with browsers for example.
On the other hand, acceptance tests, which are the tests written by QAs should validate the code in the actual environment where it will reside, having contact with all the components around it. These tests validate that the code still acts properly when it runs in the system that it is built in. For that reason, these tests might take a long time to run, use browser instances, and might be rather fragile when it comes to succeeding. For example, Selenium tests are some of the most fragile, since, if you run tests in some sluggish environment, they might fail because of the slow responsiveness of the environment, pages not loading on time, and so on. Hence, running these tests for every project compilation phase is not feasible, as the build might never compile successfully. Continue reading Create the Maven profile for running tests.
The central and most essential part of a Maven project is its’ pom.xml file. Among other information (like the project’s defining artifactID and groupID), it stores the list of dependencies your project has and the plugins the project will use. Dependencies that are declared within the pom.xml file will be downloaded from the Maven repository configured for the project, into the project, as external libraries or dependencies. The default repository that is configured is the Maven central repository (http://search.maven.org/#search|ga|1|), and except for the situation where you explicitly configure a local repository, this is the place to look for the latest versions of the libraries you will import into your project. Continue reading Import the testing dependencies