Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeception test fails even though try-catch

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.

like image 907
harryfonda Avatar asked Feb 04 '16 16:02

harryfonda


1 Answers

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');
}
like image 139
the-noob Avatar answered Oct 26 '22 03:10

the-noob