Rummaging over some old Java test code yesterday, refactoring to change project name, clean up dependencies and such, in order to create a nice clean demonstrable Selenium WebDriver test sample. After tinkering with various helper Java classes – and fixing dependencies and code breakage – realized the test did not actually do what I needed yet. Too much time spent tinkering, not enough time spent actually doing. Common problem.
Decided to try the bottom-up approach. Was wondering how long it would take to build a working test from scratch, since I haven’t done that for a few years. As it turns out, not too long at all… fifteen minutes, maybe less if you type faster than I do.
There’s plenty of sample Selenium code out there – Google search proves that quickly and nicely – but I wanted to re-cement some of the key steps in my head. So, after picking someone else’s nice clean example, I then distilled some reference notes – in case my brain decides not to remember the basics at some point in the near future (because that never happens… :P)
Sharing in case someone finds them useful, or enlightening. To learn more, check out the original example on SeleniumEasy.com, at the link shown below.
(Very) Minimal Java / Selenium Test
- Create Maven project
- In Maven pom.xml file, add dependencies for testng, selenium-java
- In Maven pom.xml file, add plugin entries for maven-surefire-plugin, maven-compiler-plugin
- In Maven pom.xml file, add suiteXmlFiles entry in surefire, point to testng.xml file
- Create Java Test Class with:
- — Imports: org.openqa.selenium (WebDriver, FirefoxDriver) and org.testng (Assert, Test, BeforeClass, AfterClass)
- — @BeforeClass – setup, instantiate WebDriver
- — @Test – navigate to URL, perform Assert test
- — @AfterClass – teardown, quit WebDriver
- Create testng.xml file to specify suites, tests, classes
- Compile and/or update dependencies as needed
- Execute via mvn test
Remember to match Selenium versions with supported Browser versions
Mostly distilled from: SeleniumEasy.com