I'm new both for PHP and Codeception, I've been trying to write some basic tests using page object.
Here's an example of a function in my page class. Ideally, it should click a button and if there's no button just log a comment.
try {
$I->click(self::$buttonAddNewAddress);
} catch (Expection $e) {
$I->comment('This address will be the first one');
}
I get «Fail Link or Button or CSS or XPath element with '//div[@class="buttons-set"]/button' was not found.» every time I try to run this code.
In AcceptanceTester.php I have a method that looks similar to me:
try {
$I->click('//a[contains(.,"No Thanks")]');
} catch (Exception $e) {
$I->comment('No email capture visible');
}
And it works correctly if link is not visible. I don't see any difference besides first junk of code being inside page class and the second one being inside AcceptanceTester.php.
Please help me with that or give me an advice how to rewrite this part of code.
If you have your tests in a namespace (and you should) the code will try to catch an exception from that namespace. I.e.
namespace MyTest;
try {
$I->click(self::$buttonAddNewAddress);
} catch (Exception $e) {
$I->comment('This address will be the first one');
}
This will only catch a \MyTest\Exception
.
You need to add \
to your code.
namespace MyTest;
try {
$I->click(self::$buttonAddNewAddress);
} catch (\Exception $e) {
$I->comment('This address will be the first one');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With