Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload page if element does not show - Codeception

Tags:

codeception

So I'm trying to make codeception reload my page if it does not see an element.

if ($I->dontSeeElement('body > div.site > main > header > h1'))
{
    $I->reloadPage();
}

I have this set in the _after function.

But every time I run my test, it throws me an error. My logic is: If it does see that element, it should just move on. If not, then reload page.


1 Answers

You may want to extend your acceptance tester class with something like this:

public function reloadIfElementNotFound($selector)
{
    try {
        $this->seeElement($selector);
    } catch (\PHPUnit_Framework_ExpectationFailedException $e) {
        $this->reloadPage();
        $this->seeElement($selector);
    }
}

So, it will try to "see" an element, and if it's not found then a \PHPUnit_Framework_ExpectationFailedException will be thrown, which you're going to catch and then reload the page. Also, make sure that the element is then visible after the reload. However, if it's still not visible after the reload, then the exception will be thrown again and break the test, which - I guess - would be the desired behavior.

In your test you'd then simply use:

$I->reloadIfElementNotFound('body > div.site > main > header > h1');
like image 184
Oliver Maksimovic Avatar answered Feb 03 '26 00:02

Oliver Maksimovic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!