info@pragmatictesters.com +94 11 253 8512


Introduction

 

 

As a test automation engineer, you are moving into software development from manual testing  and must follow the best practices used by the software developers to build a successful career in software testing. Following a coding conventions is one of them.
 
In this post we will discuss Java coding conventions and other naming conventions to be used within a test automation projects suggested for Selenium Test Automation.
 
We have referred to a  few online posts (included in the reference section) as this post was created.


Naming conventions

We shall use Camel cased for naming conventions. Camel case (stylized as camelCase or CamelCase; also known as camel caps or more formally as medial capitals) is the practice of writing compound words or phrases such that each word or abbreviation in the middle of the phrase begins with a capital letter, with no intervening spaces or punctuation.
There are two alternatives of camel case namely upper camel case ((initial upper case letter, also known as Pascal case) ) and lower camel case  (initial lower case letter).

 

We shall lower camel case for directory naming. Sample directory structure for a typical Selenium project.

 

 

 

 

 


Directory Naming

 

 

 

 

Directory names shall follow lower camel case.

The directory names shall be short and meaningful.

 

 

 

 


Filename suffixes

 


Java naming conventions

Package names

Reverse of the organization’s domain name should be used as the prefix. All letters should be in lower case. Subsequent directory name shall be used according to the need of the project.Meaningful directory name shall be used to indicate the content within the package.
com.pragmatic.selenium 
com.pragmatic.selenium.pages
com.pragmatic.selenium.tests 
com.pragmatic.selenium.utils 

 

Class names

Upper camel case should be used for the class names. Class name should be a noun. Try to make the name simple and descriptive.
LoginTest.java : All the tests shall have suffix Test
LoginPage.java : All the pages shall have suffix Page
TestBase.java : Class to include all common methods to be used by tests and pages 

Method names

Lower camel case should be used for the method names. Method name should be a verb. Try to make the name simple and descriptive. Use whole word instead of abbreviations.
initialiseBrowsers()
initialiseProperties()
waitForTextToBePresent(WebElement webElement, String textToBeVerified)
testLoginwithValidCredentials() :All test methods shall start with prefix test 

Variable names

Lower camel case should be used for the variable names. Intent of the variable shall be indicated with the given name. The variable name should be a noun. Try to make the name simple and descriptive.

webDriver 
intEmployeeCount 
Use following prefix for the web elements to indicate the type of the element

 

Following naming convention shall be used to indicate the data type of the variable

 

Constants

All letters should be in uppercase. Words shall be separated with underscore _ .
BASE_URL
All the global constants shall be defined in a property file and loaded into the constants defined in Constant class.
Constants shall be defined as public static in the Constant class
public static String BASE_URL;

 Comments

Comments shall be used to explain the code to make the test scripts more readable for others.
Use consistent commenting styles.

Comments in Java

Single line comments

// This is comment

Multiple line

/* This is comment */

Documentation Comments

Documentation comments shall be used if you wish to generate Java Doc from your test suite. This could be useful for test automation framework.
/**
 * @param username the login username
*/

Comments in feature files (Cucumber)

You can add your comments in the lines followed by the keywords Feature, Scenario, Scenario Outline or Examples.
Also you can use #  for line comments  

 

 

Comments in property files

Comment lines in .properties files are denoted by the number sign (#) or the exclamation mark (!) as the first non blank character

 

Comments in XML

The syntax for writing comments in XML is similar to that of HTML.
<!-- This is a comment -->

Property File and properties

 

All the global properties and values that can be defined by the user shall be defined in the property file. This will allow you to run your tests with different configurations without re-compiling your code.

 

base.url=http://demo.pragmatictestlabs.com
user.name=admin
user.password=admin

 


Why should we use coding convention?

It is not compulsory to follow any coding conventions in your test automation projects.

 

  1. To reduce the effort needed to read and understand source code
  2. To enable code reviews to focus on more important issues than arguing over syntax and naming standards.
  3. To enable code quality review tools to focus their reporting mainly on significant issues other than syntax and style preferences.


Validating the coding conventions

Code analysis tools can be used for automating the verification of coding conventions. We shall list few tools that can be used with IntelliJ IDEA


Google Java Style Guide

The complete definition of Google’s coding standards for source code in the Java. A Java source file is described as being in Google Style if and only if it adheres to the rules herein.

 


References

1.Java Naming Conventions – Javatpoint.[ONLINE] Available at: https://www.javatpoint.com/java-naming-conventions. [Accessed 05 March 2018].
2.Code Conventions for the Java Programming Language: 9. Naming Conventions. [ONLINE] Available at: http://www.oracle.com/technetwork/java/codeconventions-135099.html[Accessed 05 March 2018].
3.Java Naming Conventions – GeeksforGeeks. [ONLINE] Available at: https://www.geeksforgeeks.org/java-naming-conventions. [Accessed 05 February 2018].
4.Camel case – En.wikipedia.org.  [ONLINE] Available at: https://en.wikipedia.org/wiki/Camel_case [Accessed 05 March 2018].
5.Object Naming Conventions – Msdn.microsoft.com [ONLINE] Available at:https://msdn.microsoft.com/en-us/library/aa263493(v=vs.60).aspx [Accessed 05 March 2018].
5.Cucumber Limited.Reference·Cucumber.[ONLINE] Available at:https://cucumber.io/docs/reference [Accessed 05 March 2018].
 

 

 

Author: Janesh Kodikara

Performance Tester | JMeter Trainer | Software Testing Service Provider

Your Turn To Talk

Leave a reply:

Your email address will not be published.