Showing posts with label test automation best practices. Show all posts
Showing posts with label test automation best practices. Show all posts

Test Automation Tips - dont use WebDriver APIs in your test script

If your WebDriver test scripts look similar to the following script, you are not doing test automation correctly:

@Test
public void testFirstResult() {

driver.get("http://www.vpl.ca");   


WebElement searchField = driver.findElement(By.xpath("//input[@id='globalQuery']"));


searchField.click();           

searchField.sendKeys("java");
    
WebElement searchButton = driver.findElement(By.xpath("//input[@class='search_button']"));

searchButton.click();        
            
WebElement searchResultLink = driver.findElement(By.xpath("(//a[@testid='bib_link'])[1]"));
    
searchResultLink.click();
        
WebElement bookTitleElement = driver.findElement(By.xpath("//h1[@id='item_bib_title']"));    
String bookTitleValue = bookTitleElement.getText();
      
assertEquals(bookTitleElement.isDisplayed(), true); 
assertTrue(bookTitleValue.length() > 0);
    
WebElement bookAuthorElement = driver.findElement(By.xpath("//a[@testid='author_search']"));
String bookAuthorValue = bookAuthorElement.getText();
      
assertEquals(bookAuthorElement.isDisplayed(), true); 
assertTrue(bookAuthorValue.length() > 0);

}


The script includes many WebDriver API objects and methods:


  • WebElement objects
  • driver.findElement() method
  • isDisplayed() method
  • getText() method
  • click() and sendkeys() methods()
  • get() method



Even if this script works correctly, it is very difficult to change and maintain.

Imagine having 50 scripts similar to this and some of them failing one day because of site changes (UI and functional).

Modifying 50 scripts would be a very difficult and time consuming task.

To remove the WebDriver API objects and methods from the test script, you should start using the Page Object Model.

"A page object wraps an HTML page, or fragment, with an application-specific API, allowing you to manipulate page elements without digging around in the HTML." Martin Fowler

What you need to do is create classes that correspond to web pages or web page components and implement the interaction with the site inside of them.

These classes will be used then by the test scripts.

All WebDriver API methods will be moved from the test scripts to the page object classes so a test script looks as follows:

@Test
public void testResultsInfo() throws InterruptedException{        

HomePage home = new HomePage(driver);


assertTrue(home.correctTitle());          

  
ResultsPage results = home.search();

assertTrue(results.correctTitle());


assertTrue(results.keywordDisplayed());            


assertTrue(results.resultsCount() > 0);         


}  


No WebDriver API classes and methods are included in this script.

The script uses 2 page object classes (HomePage and ResultsPage) that implement all interactions with the web pages.



Read more about Page Object Model on these 2 links:

http://martinfowler.com/bliki/PageObject.html

https://code.google.com/p/selenium/wiki/PageObjects


Test Automation Tips - do not automate the regression test cases

Test automation projects are development projects.

For each test case to be automated, code needs to be written, maintained and debugged.

The effort for automating all regression test cases is very high and the benefits limited.

Instead, use the following strategies for test automation:

1. automate the scenarios from the MONEY PATH; automate all scenarios that confirm that the users can pay for the products

2. automate scenarios for NEW FEATURES

3. add test scripts for NEW BUGS to be sure that these bugs will not appear again

4. focus on positive scenarios; there is limited value for automating negative scenarios

5. focus on functional scenarios; there is limited value on automating UI scenarios


Test Automation tips - use a code source control system

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

Test Automation tips - store all locators in one class

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.

Test automation tips - create the correct project folder structure

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