As soon as the test automation project has more than a few scripts, the test automation process should start including a source control system.
This allows having different versions of the code, rolling back to a previous version, having a history of all code changes, etc.
It makes sense to use source control for test automation projects as these are development projects.
For source control, typical options are:
- Subversion (https://subversion.apache.org/) if you want to keep the code changes locally or on a server
- GitHub (https://github.com/) if the code should be stored online
When starting on test automation, a frequent mistake is that
each page object class file has its own locators defined as class members:
This can be corrected easily by
moving the locators from all page object class files to a single locator class file.
Solution
A new class is created and the locators from all page object classes are moved into it:
The code from the test class is changed then to use the new locator class:
You can remove the locator members from the HomePageScripts class and just use the Locator class members directly in the WebDriver commands like
WebElement searchLabel = driver.findElement(By.xpath(Locators.searchLabelLocator));If both the Locators class and the page object classes are in the same package, nothing else is needed.
If they are in different packages, you will need to import the Locator class package in the page object class file.
When starting on test automation, a frequent mistake is that
all project files are created in the src folder of the Eclipse project:
The src folder includes
- test classes (HomePageScripts.java)
- page object classes (HomePage.java and ResultsPage.java)
- any other project files
This can be corrected easily by creating folders in the project for specific purposes:
Example
framework folder
page object class folder
locator class folder
test scripts folder
Solution
Before working on the project folder structure, lets investigate a bit the project:
- source java files are stored in the src folder
- class files (created when the source files are compiled; the class files are needed for executing the code) are stored in the bin folder
The classpath file has 2 rules for this:
- first rule says that the source files are in the src folder
- the second rule says that, by default, all class files are created in the bin folder
Multiple steps are needed for getting the correct project structure:
1. remove the existing classpath entries
2. create a test folder under the src folder: src/test
3. add the following classpath entry
This rule means that for all java files from src/test, the class files will be created under target/test-classes.
4. build the project.
the target/test-classes folders are created as a result of the project build:
5. create the com.testproject.java package under the src/test folder:
6. move the HomePageScripts.java file to src/test/com/testproject/java
7. build the project
8. the following folders are added under target/test-classes: com/testproject/java
the HomePageScript.class file is stored in target/test-classes/com/testproject/java:
9. similarly, create a main folder under src
10. add a new entry to classpath for the src/main folder
This rule says that for all java files stored under src/main, the class files will be created under target/classes.
11. build the project
12. the target/classes folder is created
13. add the following package to src/main: com.testproject.java.framework.pageobject
14. move the page object class (HomePage.java) to the src/main/com/testproject/java/framework/pageobject
15. build the project and confirm that the class file is created in target/classes/com/testproject/java/framework/pageobject:
16. add the following package to src/main: com.testproject.java.framework.locators
17. move the Locators.java file to src/main/com/testproject/java/framework/locators
18. build the project
19. confirm that the Locators.class file is created in target/classes/com/testproject/java/framework/locators
20. open all java files and add the packages where different classes are stored:
21. run the project
At this moment, the project structure is much better than when we started:
SRC
MAIN --> folder used for framework files
COM
TESTPROJECT
JAVA
FRAMEWORK
LOCATORS
Locators.java
PAGEOBJECTS
HomePage.java
TEST --> folder used for the test scripts
COM
TESTPROJECT
JAVA
HomePageScripts.java