This article is coming up as an effort from the ABH testing research team in the light of using new technologies and tools in testing processes.
The main goal is to shortly describe all components/steps needed to automate your test scenario and run it. It should be clear, at the very beginning, that there is no “magic button” behind which will do the things for us. Test scripts should be written as well as structured and easily readable. But that’s a beauty, at the end, and opportunity to make the client and yourself happier. And, of course, valuable “escape” from the everyday manual testing world.
Article covered themes as follow:
- What is Selenium? (*)
- Selenium WebDriver (*)
- Create your first project
- Convert to Maven project
- Running Java from the command prompt
- TestNG framework (*)
- Conclusion
- Online resources
Note: (descriptive parts (*) are mostly copied and filtered from the resources sites)
There are some prerequisites:
- Core Java programming skills
Before you start, it is essential to go through core java programming and learn basic concepts; this was the most challenging obstacle, in my case, while working on a project. Avoid copy/paste coding and ask your colleagues, java developers, for advice, if needed.
- Java is installed (and the path is set)
- Java IDE – Eclipse is installed
What is Selenium?
Selenium is a set of different software tools each with a different approach to supporting test automation, and in particular to verify that they work as expected. Most Selenium QA Engineers focus on one or two tools that most meet the needs of their project, however learning all the tools will give us many different options for approaching different test automation problems. The entire suite of tools results in a rich set of testing functions specifically geared to the needs of testing of web applications of all types.
Selenium contains 4 tools:
- Selenium IDE
- Selenium RC (Selenium 1)
- Selenium WebDriver (Selenium 2)
- Selenium Grid
Selenium WebDriver
Selenium WebDriver (Selenium 2) is the successor of Selenium RC (Selenium 1) which has been officially deprecated.
While Selenium RC worked the same way for each supported browser (‘injected’ javascript functions into the browser), Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation.
WebDriver API is designed to provide a simpler, more concise programming interface in addition to addressing some limitations in the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded.
Create your first project
Download the latest Selenium Java binaries (Selenium Client & WebDriver Language Bindings) from the location: http://www.seleniumhq.org/download/ and unpack them into a directory.
WebDriver acts just as a normal Java library does: it’s entirely self-contained, and you don’t need to remember to start any additional processes or run any installers before using it.
Start a new Java project in Eclipse
Add downloaded Selenium .jars to your project (both: selenium-java-x.y.z-srcs.jar, selenium-java-x.y.z.jar, as well as the content of ‘libs’ folder)
Write some code and run it from Eclipse
This is only a part of “Sign up” script – Open a new browser window, fill in the form (first name):
package org.openqa.selenium.smoke; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; public class FirstName { @Test public static void main() { // Create a new instance of firefox driver WebDriver driver = new FirefoxDriver(); //Maximaze the window since some elements are not displayed on the default screen driver.manage().window().maximize(); // Visit AgileHats site driver.get("http://agilehatsplay.elasticbeanstalk.com/ register"); // Find the element and populate firstname field WebElement FirstName = driver.findElement(By.id(“firstname")); FirstName.sendKeys("Test"); //Get current value of the element String FirstNameValue = FirstName.getAttribute("value"); assertEquals("Test", FirstNameValue); driver.quit(); … } }
Convert to Maven project
Maven is a Java tool; can be used for building and managing any Java-based project. It manages all dependencies and different flows for building a project.
The beauty of Maven is that it keeps and maintains all dependencies that we use in our project (i.e. org.seleniumhq.selenium). It makes it much easier to set up the project on a new machine, and also makes a lot easier to run the project on CI.
Download m2e-eclipse plugin for Eclipse (http://download.eclipse.org/technology/m2e/releases)
In your Eclipse just right click on Java Project and click Configure and you should see the “Convert to Maven Project” option.
Add your custom dependencies in pom.xml file:
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.47.1</version> </dependency>
Running Java from the command prompt
There are several ways how to run a Java program from the command prompt. Once the project is converted to Maven, there is an option to use ‘exec-maven’ plugin (http://www.mojohaus.org/exec-maven-plugin/usage.html) with Java goal.
You can either use the command line version:
mvn exec:java -Dexec.mainClass="org.openqa.selenium.smoke.FirstName"
or you can configure the plugin in your POM:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>org.openqa.selenium.smoke.FirstName</mainClass> <arguments> <argument>foo</argument> <argument>bar</argument> </arguments> </configuration> </plugin>
and run it with the command:
mvn exec:java
TestNG
TestNG is a testing framework. It structures, groups, and launches tests. It also generates testing reports. It’s capable of a range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages, and even several external frameworks, such as application servers).
Here is described how to run our (adapted) basic test in Eclipse:
Install Eclipse plug-in
The TestNG Eclipse plug-in allows you to run your TestNG tests from Eclipse and easily monitor their execution and their output. For Eclipse 3.4 and above: http://beust.com/eclipse.
Convert project to TestNG
Right-click on Project / Configure / Convert to TestNG
Insert TestNG annotation in your code:
… @Test public static void main() { … } …
Add class tag to testng.xml
… <classes> <class name="org.openqa.selenium.smoke.FirstName" /> </classes> …
Run the program from Eclipse
Right-click on Project / Run as / TestNG test
Result should be something like:
[TestNG] Running: /private/var/folders/j4/tfvgn4yx5l773jlxjghb62tc0000gp/T/testng- eclipse--807252744/testng-customsuite.xml PASSED: main =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 …
Conclusion
This should be only a starting point on a way of further researching and learning. These are the project’s directions that will be taken:
Improve Java skills
Selenium Design Patterns and Development Strategies
(https://code.google.com/p/selenium/wiki/DesignPatterns)
Run the project on CI
Online resources
Selenium HQ – http://www.seleniumhq.org
Maven – http://maven.apache.org
TestNG – http://testng.org/doc/download.html
Selenium wiki – https://code.google.com/p/selenium/w/list
Free online training – https://www.udemy.com/selenium-training/