List guidelines and recommendations when working with UI tests

These items are extracted from great posts below about automation testing best practices. I only picks items related to UI testing and also adds some from my experience.

https://www.selenium.dev/documentation/guidelines/

https://ultimateqa.com/automation-patterns-antipatterns/

I. Tests Should Be Atomic (small, isolated/independent, fast)

you can get more details what is atomic automation test and its benefit here

II. UI Test should be run in parallel to save time and give your team quick feedback

III. Follow Page Object Pattern to reduce code duplication and help your tests more maintainable.

UI tests should not expose interactions with web elements. Call Page Object methods instead.

Note: Do not use Page Factory Pattern. read this post to know why

IV. Design locators that are resistant to UI changes, focus on more unique locators like ID, Name first.

V. Choose appropriate hooks to cleanup/reset state of test data

For cleanup test data created in tests (delete users, delete user entities), afterEach/afterAll is good option. However, Avoid using afterEach of afterAll hooks for cleaning up test state (reset state in DB for example). Instead, we want to use beforeEach for cleaning up test state to make sure state will be reset.

VI. Do not try to automate the Captcha

There are two primary strategies to get around CAPTCHA checks:

  • Disable CAPTCHAs in your test environment
  • Add a hook to allow tests to bypass the CAPTCHA

VII. Handle download files test properly

find solution here

VIII. Handle Facebooks or Gmail login

The ideal practice is to use the APIs that email providers offer, or in the case of Facebook the developer tools service which exposes an API for creating test accounts, friends and so forth

read here for more detail

IX. Utilize API calls to make your test faster and more reliable

You should not do everything in UI tests using Selenium commands. Instead If API is available, use it to setup or teardown UI tests. For example, you can call API login to get the token and then inject it to browser cookies, it is faster than performing login action on UI. Or you can call APIs to prepare the test data or clean up.

You can also consider DB access to set up state of the data if needed.

X. Navigating to the page by go to (open) the URL directly instead of click some elements/links/menu to access the page.

for example, to navigate to Account Billing page, instead of clicking Home Page > click User Profile > click Account > click Billing Page, you can open URL to get there : https://app.com/thachhoang/account/billing

Leave a comment