Codeception now has tryTo...
, e.g, tryToSee()
trytoClick()
, etc., so there's no need for a Try/Catch block. I find it more readable than performOn()
.
You need to enable it with this in acceptance.suite.yml or codeception.yml:
# enable conditional $I actions like $I->tryToSee()
step_decorators:
- \Codeception\Step\TryTo
- \Codeception\Step\ConditionalAssertion`
You can click on something that may or may not be there with:
$I->tryToClick('#save_button`);
If there's no button, the code goes on with no error message. This could also be used to click on the node to expand a section of a tree before examining it, but only if that section is closed (there should always be a class that's only there when it's closed).
Another way to go is in an if
statement. The tryTo...
methods all return true on success and false on failure, so you can do this, which some might consider clearer than the above (no error will be thrown):
if ($I->tryToSee('some_locator')) {
$I->click('some_locator');
}
This form is also useful if there is a sequence of actions you want to perform based on a condition, the else
is optional.
if ($I->tryToSee('some_locator')) {
$I->fillField('username', 'myname');
$I->fillfield('password', 'mypassword);
$I->click('Submit');
} else {
/* Do something else */
}