在条件(if)语句中使用CodeCeption断言


Use CodeCeption assertion in conditional (if) statement

我对CodeCeption完全陌生。

我想根据另一个断言结果进行操作/断言,如下所示:

if ($I->see('message')){
    $I->click('button_close');
}

这样的事情可能发生吗?我试过了,但没用。断言结果可能不适用于IF,但有其他选择吗?

提前感谢!

重要更新:

最后,Codeception现在有了函数performOn!!http://codeception.com/docs/modules/WebDriver#performOn

我也遇到了同样的问题。虽然这并不理想,但你可以这样做:

try {
    $I->see('message');
    // Continue to do this if it's present
    // ...
} catch (Exception $e) {
    // Do this if it's not present.
    // ...
}

tests/_support/AcceptanceHelper.php中添加附加方法

function seePageHasElement($element)
{
    try {
        $this->getModule('WebDriver')->_findElements($element);
    } catch ('PHPUnit_Framework_AssertionFailedError $f) {
        return false;
    }
    return true;
}

然后在验收测试中测试使用:

if ($I->seePageHasElement("input[name=address]")) {
    $I->fillField("input[name=address]", "IM");
}

您可以使用类似的解决方法或类似的组合:

$tmp = $I->grabTextFrom('SELECTOR');
if ($tmp == 'your text') {
$I->click('button_close');
}

终极解决方案

最后,Codeception现在有了函数performOn,它完全符合我的要求!!

【2.2.9版】

http://codeception.com/docs/modules/WebDriver#performOn

回答我的例子:

$I->performOn('.message', ['click' => '#button_close'], 30);

等待30秒以查看class="message"的元素,然后单击id="button_close"的元素。

Codeception现在有tryTo...,例如tryToSee() trytoClick()等,因此不需要Try/Catch块。我发现它比performOn()更可读。

你需要在acceptance.suite.yml或codeception.yml:中启用它

# enable conditional $I actions like $I->tryToSee()
step_decorators:
    - 'Codeception'Step'TryTo
    - 'Codeception'Step'ConditionalAssertion`

你可以点击一些可能存在或不存在的东西:

$I->tryToClick('#save_button`);

如果没有按钮,代码将继续运行,不会出现错误消息。这也可以用于在检查树之前单击节点以展开树的一个部分,但如果该部分是关闭的,则(应该总是有一个类只有在它关闭时才在那里)。

另一种方法是使用if语句。tryTo...方法在成功时都返回true,在失败时都返回false,所以你可以这样做,有些人可能认为这比上面的更清楚(不会抛出错误):

if ($I->tryToSee('some_locator')) {
    $I->click('some_locator');
}

如果要根据条件执行一系列操作,则此表单也很有用,else是可选的。

if ($I->tryToSee('some_locator')) {
    $I->fillField('username', 'myname');
    $I->fillfield('password', 'mypassword);
    $I->click('Submit');
} else {
   /* Do something else */
}

我的项目在周发布

/**
 * https://stackoverflow.com/questions/26183792/use-codeception-assertion-in-conditional-if-statement
 * @param $element
 * @return bool
 * @throws 'Codeception'Exception'ModuleException
 */
public function seePageHasElement($element)
{
    $findElement = $this->getModule('WebDriver')->_findElements($element);
    return count($findElement) > 0;
}

断言不适用于条件语句的原因是Codeception首先在IF括号内执行断言,如果它不是真的,它会立即失败测试。我克服这一问题的方法是使用SOFT断言TryTo,如果失败,Codeception将忽略它:https://codeception.com/docs/08-Customization#Step-装饰

if ($I->tryToSee('message')){
    $I->click('button_close');
}```

100%工作解决方案!!稍后感谢我;)

在测试/''usupport/AcceptanceHelper.php中添加其他方法

public function seePageHasElement($element)
{
    try {
        $this->getModule('WebDriver')->_findElements($element);
    } catch ('PHPUnit_Framework_AssertionFailedError $f) {
        return false;
    }
    return true;
}

然后在验收测试中测试使用:

if ($I->seePageHasElement($element)) {
    $I->fillField($element);
}

WebDriver的‘seeElement’函数在这种情况下不起作用,因此几乎不需要修改函数,因为我使用了一个"_findElements"。请不要忘记在完成任何更改后构建您的演员。