basic-selenium-project
This project is written in java and will serve an example of implementing a Selenium test project with Selenium3 and Maven.
Everything is set up and tests can be added straight away.
Used Testrunner is the Failsafe plugin.
To execute the tests just browse to the path where the basic-selenium-project is located in your terminal and type mvn clean verify -Pbrowser-chrome
or execute the tests in your IDE. maven profiles for all browsers exists in the pom.xml.
If you want to make template tests that doesn't need browser interactions like clicking or execution of Javascript just have a look at Paco Framework. It's much much faster then Selenium and less fragile.
Implemented Browsers
Thanks to the awesome webdrivermanager it supports the following browsers and automatically downloads OS specific binaries for:
- Chrome
- Firefox
- PhantomJS
- Opera
- Internet Explorer
- Edge
The Webdriver Setup
The webdriver setup is based on the WebDriverBuilder and the DesiredCapabilitiesFactory to have a separation between driver instantiation and browser specific settings.
Page Objects Pattern
page object pattern is used to have reusable WebElements/small helper methods seperated from actual test classes and give the opportunity to have nice structured and easily readable tests (without the overhead of BDD-Frameworks like Cucumber or JBehave).
Annotations
Beside a bunch of [Convenient Methods] the basic-selenium-project provides some nice custom annotations to comfortably set some test conditions and/or assumptions Example test that uses several annotations: SearchIT.java
@Browser
The @Browser
annotation includes or excludes certain browsers from the test execution
skip test if browser equals firefox:
@Browser(skip = FIREFOX)
it also supports list of browsers, e.g. skip test if browser equals firefox or phantomjs
@Browser(skip = { FIREFOX, PHANTOMJS })
it can be used the other way around as well e.g. if you want a certain test just to be executed with phantomjs you can do something like this:
@Browser(require = PHANTOMJS)
The browser require option is working equivalent to the skip option and also supports list of browsers
@BrowserDimension
If you want to test a responsive website it can be handy to set the browsers to some specific viewports. To configure your breakpoints just edit them in the test_data.properties
Resizing the browser window for specific tests can be done by e.g.:
@BrowserDimension(LARGE)
@UserAgent
UserAgents can be overwritten and give the possibility to emulate the behaviour of an website if special devices visiting it. For Example if you want to test a mobile switch for devices like smartphones and/or tablets etc.
@UserAgent(IPHONE_I_OS_9)
There already is a list of user agents in the UserAgents Enum. All user agents located in that file will can be used with @UserAgent
Convenient Methods
you can find a couple of convenient methods like waits, isElementPresent/Visible, hover, dragAndDrop, etc. in SeleniumFunctions.java as well as helper methods for cookie, localstorage and sessionstorage handling. Example tests that do some stuff with cookies and do some stuff with localstorage/sessionstorage are added and should show the the usage self-evident.
How To run the tests
- Install browser docker images
chmod u+x install.sh
./install.sh
-
Edit the proxy address
settings.xml
. Replacelocalhost
with the ip address of the corporate proxy. -
Edit the
BROWSER_PROXY
environment variable (uncomment it and set the host address) in thedocker-compose.yaml
file -
Start selenoid using docker-compose
docker-compose up --build
- Open http://localhost:9000 in a browser and watch the progress
Note
You can run the maven project locally by using
mvn clean verify -Pbrowser-chrome
Though you will have to set the remote_webdriver
property in the driver_config.properites
file to point to the address where selenoid is running.