Web UI Testing with Java/Selenium - Astronomy Picture of the Day Search Page
For this tutorial, we will be testing the functionality of the buttons on the search page for Astronomy Picture of the Day:
https://apod.nasa.gov/cgi-bin/apod/apod_search
- Open Eclipse
- Create a new Maven project
- File → New → Other → Maven Project
- Next
- Check “create a simple project”
- Next
- Give it a Group ID and Artifact ID
- Finish
- Add dependencies to the pom file for cucumber-java, cucumber-junit, junit, selenium-java, and selenium-chrome (or whichever browser you wish to test with). The code for dependencies can be found at https://mvnrepository.com/ (get the ones from io.cucmber, not cukes). Note that you will need to surround the group of dependencies with <dependencies></dependencies>.
- Under src/test/java, create a new package for the feature file
- In the new package, create a feature file
- Right-click on the package → New → File
- Give it a file name that ends in .feature
- Write the Feature File (click here for guidance)
- Run the feature file in Eclipse
- Copy the code snippets provided in the console into a step definitions file
- Create a stepdefinitions package in src/test/java
- In the stepdefinitions package, create a java class (Right-click on the package → New → Class)
- Paste code snippets into the java file (within the provided public class)
- Import Given, Then, and When (hover over the word, then choose import)
- Now write codes in your step definitions file (click here for guidance)
- To configure it to run from the command line (via Maven) and to generate nice reports, first add a new package called "testrunners" to
src/test/java
- Then, add a new class to testrunners. I called mine APODTest. Note: Make sure the filename starts or ends with “Test,” with a capital T, or maven will not know how to find it.
- Add
@RunWith(Cucumber.class)
(you can do this on line 2) and import both Cucumber and RunWith.
- Now define Cucumber options:
@CucumberOptions()
- Within the parentheses, give the location of the feature file, such as:
features = {"src/test/java/apodsearch/features"}
- Find the location by right-clicking on the feature file in Package Explorer (in Eclipse) and choosing Properties. Copy the location, starting from src and ending just before the file name. If your slashes come in as
\\
, change them to /
.
- Add a comma after the closing curly brace, and define glue the same way, providing the location of your step definitions. (e.g.
glue = {"step_definitions"}
)
- Add another comma, then
plugin = {"pretty",
- Specify the format of the report, followed by a colon and the location where you want that report written. For example:
"html:target/SystemTestReports/report.html"
for an HTML report, "json:target/SystemTestReports/report.json"
for a JSON report, and/or "junit:target/SystemTestReports/report.xml"
for an xml report. You can output all three by separating them with commas, such as: plugin = {"pretty",
"html:target/SystemTestReports/report.html",
"json:target/SystemTestReports/report.json",
"junit:target/SystemTestReports/report.xml"
}
- Save, and you're done. It can now be run from the command prompt by navigating to the starting folder (the one that contains pom.xml) and running “mvn test.” It will generate reports in the folder .../target/SystemTestReports/
All done!
Your Test file should look something like this:
package testrunners;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(
features = {"src/test/java/apodsearch/"},
glue = {"step_definitions"},
plugin = {"pretty",
"html:target/SystemTestReports/report.html",
"json:target/SystemTestReports/report.json",
"junit:target/SystemTestReports/report.xml"
}
)
public class APODTest {
}
--
SummerDale - 04 Sep 2020