So I have this Selenium-based test suite written in Java. We are doing functional/integration testing, not unit testing. The test suite is a string of operations and each operation depends on the state of the UI at the start. If the previous operation did not leave the UI in the correct state, recovery is needed, sometimes non-trivial.
Ideally, the logic of the test suite would ensure that the state is correct every time. However, with various error/exception execution paths this becomes tricky to ensure manually. If I put in Page Object Model (currently not used, because each page is only used by one place in the code anyway, but this might change), tracing the state manually might become even harder.
I'd like to store the current state somewhere, so that any code that changes the state (opens/closes a dialog, etc) could modify this state, and any code that needs a certain state might check whether it is correct (and recover if it is not). Of course this is still not error-proof, as I might set the state incorrectly in some code. But at least if NoSuchElement exceptions start, I can check if the stored state is real and chase that up.
But how do I store the state? The only thing I can think of now is a global variable, probably stored in the driver manager helper object, with an enum in it. But this looks somewhat ugly? Might there be a better way?
I did look into POM guides online but they just propose asserting the state based on autodetection, for example, the title. However, if the assertion fails, there is no way to find out the actual state and recover. In theory I could write a massive DetectState method with autodetection for every state I know, but I'm not sure this is a great idea.