Codeception / module-webdriver

WebDriver module for Codeception

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

There should be a direct programatic way to check whether any javascript error has occurred due to running some test(s).

HaseebLUMS opened this issue · comments

What are you trying to achieve?

What do you get instead?

Provide console output if related. Use -vvv mode for more details.

# paste output here

Provide test source code if related

// paste test

Details

  • Codeception version:
  • PHP Version:
  • Operating System:
  • Installation type: Phar || Composer
  • List of installed packages (composer show)
  • Suite configuration:
# paste suite config here

@HaseebLUMS I don't see such feature in php-webdriver library and W3C WebDriver protocol (Please double check).

I think that the most realistic option is to use executeJS method to setup window.onerror handler after each page load and then use another executeJS call to check if the handler captured any errors.
But this is error prone and it won't work if the error happened before onerror handler was setup or if the page was reloaded.
Codeception won't implement anything like that until there is such feature in WebDriver protocol.

This can be achieved.

Step: 1

Codeception Config:

File: acceptancejs.suite.yml (this can be different in your case)

actor: AcceptanceJsTester

modules:
    enabled:
        - Asserts
        # use WebDriver instead of PhpBrowser to enable javascript testing
        -   WebDriver:
                url: 'http://localhost:8116/'
                #url: https://web/
                #host: chrome
                debug_log_entries: 10 # <------ this is the most important one; it must be > 0 
                log_js_errors: true # <------ this won't work if `debug_log_entries` is 0 (the default value)
                browser: chrome
                window_size: 1900x950
                capabilities:
                    javascriptEnabled: true

Till now if you test fails for any reason, the client side JavaScript (browser devtool console) error will be displayed as comment in CLI. But if you tests passes, it won't be shown.

But we want to see errors and want to fail tests if there are any such errors.

Below steps are for that.

Step: 2

In your test/cest file:

use Codeception\Module\WebDriver;

class ProfileControllerCest 
{
    public $wd;

    protected function _inject(WebDriver $wd)
    {
        $this->wd = $wd;
    }

    public function testSomething(AcceptanceJsTester $I)
    {
        // your actual tests that may yield client side JS errors
    }

   public function _after(AcceptanceJsTester $I)
    {
        $this->failIfBrowserConsoleJavaScriptErrorsExists($I, $this->wd);
        parent::_after($I);
    }

    protected function failIfBrowserConsoleJavaScriptErrorsExists(AcceptanceJsTester $I)
    {
        // $I->wait(3); // depending on your tests, you may "wait" for JS operations to finish
        $clientSideErrors = $this->getClientSideErrors();
        if ($clientSideErrors) {
            // $I->fail('JavsScript (JS) error found in browser devtools console!');
            $I->assertNull($clientSideErrors);
        }
    }

    protected function getClientSideErrors()
    {
        $logs = $this->wd->webDriver->manage()->getAvailableLogTypes();
        $logType = 'browser';
        if (in_array($logType, $logs)) {
            return $this->wd->webDriver->manage()->getLog($logType);
        }
        return [];
    }

Also if you navigate from one page to another in test and if first one have errors and second does not then you have to handle this in your own way


Things to do in Codeception:

  • fix incorrect documentation regarding log_js_errors (submitted PR #129)
  • add public method to get all JS errors in tests
  • add a suite config to allow failure of tests if JS errors exist