We will be discussing some of the practices (may not be THE best practices) which makes code base easier in terms of readability and maintainability.
- Package names should be in all lower case
package1 or com.gmail.webui
- Class names always begin with a capital letter. And if there are multiple words in the class name then each word must also begin with a capital letter
{
…..
}
- Method names should begin with lowercase and if there are multiple words in the name, then you need to use Uppercase for starting letters for the words except for the starting word
- If we are using methods to identify the objects(WebElements) on the webpage,
(mostly in pageobject model)
for buttons: btn_Cancel(WebDriver driver){…}
for links: lnk_HomePage(WebDriver driver){…}
for textbox/input box: txtBox_UserName(WebDriver driver){…}
for images: img_Logo(WebDriver driver){…}
for text content: txt_Message(WebDriver driver){…}
Include the locator details in a property file, for ease of modification.
- Variable names:
noOfRows, driver, excelWorkbook
If variable name represents a constant
EXCEL_PATH, NUMBER_OF_HOURS_IN_A_DAY
Boolean variables
isSet, isPresent
Variables in class should not be declared as public.
WebElement variables could be names as:
btn_Cancel, lnk_HomePage, txtBox_UserName, img_Logo, txt_Message
- Mostly avoid hard waits, use instead implicit or explicit waits.
- From the all available selenium locators, mostly use in the order id or CSS Selector or xpath.
- Give as much as comments to the variables, methods which creates interest for others and makes more maintainability.
/*
….
….
*/
- Record or take screenshot for the failed scenarios.
- Write test cases independent of each other, mostly each *.java file indicates one one test case.
- Create methods for every action (click on link, input text etc)
- Create as many as common methods so you can reuse them.
Login
- Implement page object model rather than data driven which makes code more readable and easily maintainable, this also helps to declare the WebElements once only in whole framework and it also reduces the redundancy.
- Use as much as oops concept.
- Create separate package as Utility for
excel operation,