Besides well-organized code in a scenario, it is necessary to manage values going through that scenario, especially in End-2-End scenario. Thus, this post will suggest an flow of those values inside an automation test case.
There are 2 main levels:
- Test: includes all steps that are behavior(s) and verifications. And the values will be call as
entities or DAO. - Page Object: as defined in Martin Fowler's post, it includes methods inside a page or fragment/layout of a page.
State
The term state is related to status of application during execution. It can be represented by individual values or combined properties to form object(s).
In a test case, it is usually created, transferred, updated for business actions and checked for verification.
Example 01
Let's take a simple example:
| |
As can be seen from above code, the variables a, b and result are considered as states.
aandbare created and initialized in Arrange stage.- Then these variables are passed to
sum()method ofCalculationPage - After method consumes variables, it will return the
result. - The returned values is compared with the expected value, so
expectedvariable is created and instantiated but it is not a state.
Example 02
Let's create a more complicated example.
Assume that we have to create a test case for making payment flow. So it is required to create some classes below:
| |
Then we have test like this:
| |
Now state contains properties of TransactionInfo object. As you can see:
- Some properties of
txnare defined first, in arrange stage. - Then
txnvariable are passed to methodmake()to get back transaction id and set to theid. - Next is getting a new instance of transaction info in
txtResultPage.getTransaction(). - Finally, compare state
txnwithactualTxn.
Because putting assert command in page object is not good practice, so we will get transaction from page object level to test level.
Summary
After 2 examples above, hope that you can have clear view of state management in automation test. There are some principles that you can follow:
- State management is just within only 1 test case, because all test cases should be independent each other.
- State should be created from the beginning of test stage.
- State can be shared between steps in a scenario, but should not share between pages directly. If you want to share stage from page 1 (of step 1) to page 2 (of step 2), page 1 should return to step 1, and then step 2 will take state and pass to page 2.
- Don't: page 1 -> page 2
- Do: page 1 -> step 1 -> step 2 > page 2
- Stage is normally provided by steps and consumed by pages. Sometimes, the state comes from returned values of page object like Example 2 getTransaction().