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
  1. Open Eclipse
  2. Create a new Maven project
    1. File → New → Other → Maven Project
    2. Next
    3. Check “create a simple project”
    4. Next
    5. Give it a Group ID and Artifact ID
    6. Finish
  3. 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>.
  4. Under src/test/java, create a new package for the feature file
  5. In the new package, create a feature file
    1. Right-click on the package → New → File
    2. Give it a file name that ends in .feature
  6. Write the Feature File (click here for guidance)
  7. Run the feature file in Eclipse
  8. Copy the code snippets provided in the console into a step definitions file
    1. Create a stepdefinitions package in src/test/java
    2. In the stepdefinitions package, create a java class (Right-click on the package → New → Class)
    3. Paste code snippets into the java file (within the provided public class)
  9. Import Given, Then, and When (hover over the word, then choose import)
  10. Now write codes in your step definitions file (click here for guidance)
  11. 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
  12. 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.
  13. Add @RunWith(Cucumber.class) (you can do this on line 2) and import both Cucumber and RunWith.
  14. Now define Cucumber options: @CucumberOptions()
  15. Within the parentheses, give the location of the feature file, such as: features = {"src/test/java/apodsearch/features"}
    1. 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 /.
  16. 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"})
  17. Add another comma, then plugin = {"pretty",
  18. 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"
    }

  19. 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
Topic revision: r4 - 05 Sep 2020, SummerDale
© 2020 Ultranauts - 75 Broad Street, 2nd Floor, Suite 206, New York, NY 10004 - info@ultranauts.co