即使try-catch,Codeception测试也会失败

har*_*nda 3 php selenium codeception

我是PHP和Codeception的新手,我一直在尝试使用页面对象编写一些基本的测试.

这是我的页面类中的一个函数示例.理想情况下,它应该单击一个按钮,如果没有按钮,只需记录注释.

try {
    $I->click(self::$buttonAddNewAddress);
} catch (Expection $e) {
    $I->comment('This address will be the first one');
}
Run Code Online (Sandbox Code Playgroud)

我得到«失败链接或按钮或CSS或XPath元素'找不到'// div [@ class ="buttons-set"] /按钮.»每次我尝试运行此代码.

在AcceptanceTester.php中,我有一个与我类似的方法:

try {
    $I->click('//a[contains(.,"No Thanks")]');
} catch (Exception $e) {
    $I->comment('No email capture visible');
}
Run Code Online (Sandbox Code Playgroud)

如果链接不可见,它可以正常工作.除了第一个代码是在页面类中,第二个代码在AcceptanceTester.php中,我没有看到任何区别.

请帮助我,或者给我一个如何重写这部分代码的建议.

the*_*oob 6

如果您在命名空间中进行测试(并且您应该),则代码将尝试从该命名空间捕获异常.即

namespace MyTest;

try {
    $I->click(self::$buttonAddNewAddress);
} catch (Exception $e) {
    $I->comment('This address will be the first one');
}
Run Code Online (Sandbox Code Playgroud)

这只会抓到一个\MyTest\Exception.

您需要添加\到您的代码中.

namespace MyTest;

try {
    $I->click(self::$buttonAddNewAddress);
} catch (\Exception $e) {
    $I->comment('This address will be the first one');
}
Run Code Online (Sandbox Code Playgroud)