9 Selenium WebDriver Best Practices1.Use Java Coding Standards
Following coding standards make code easy to read, understand & change. Hence you must follow coding standard. Click here for Java Coding Standard.
2.Use Designing Pattern of Selenium
Page Object Model or Page Factory Model makes Class for Each Web Page. Hence it is quite easy to trace for changes (easy to maintain ).
3.Use Base Class
A Class which take cares about browser setUp, Loading configuration, lots of reusable code for capturing screenshot, explicit wait initialization etc. It’s main objective to avoid code duplication by creating reusable method in base class.
4.Not to Hard Code Values
Avoid passing url, username ,password, timeout time, file path directly into tests since it makes scripts difficult to change or maintain. Pass these things either by property file or excel file.
Speed of Selenium & AUT(Application Under Test i.e. facebook) might not be same everytime.F
A point where comparison between expected & actual results happens is called as Checkpoint or Assertion. Without Assertion selenium will just interact with browser without any testing. Hence Script must have checkpoints. In case of assertion failure proper error massage should be logged to have better traceability for root cause analysis.
7.Use Separate Object Repository
Object repository is container which is having properties of all web objects/elements. If object repository is used then it will be very easy to change any objects/elements property. Thus need to change at place to update any objects/elements property
8.Taking Screenshot on Error
There should be logic in your script to take screenshot on error with timestamp automatically.
9.Generate Better Report
Reports generated by TestNG is less attractive & Clients likes reports in attractive format like bar graphs & pie chart etc. So try to use Allure Reports or Extent Reports with CustomListner.