Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
HaseebLUMS opened this issue Oct 22, 2020 · 2 comments

Comments

@HaseebLUMS
Copy link

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 HaseebLUMS changed the title There should be a direct programatic way to check whether any javascript error has occurred due to running the some test(s). There should be a direct programatic way to check whether any javascript error has occurred due to running some test(s). Oct 22, 2020
@Naktibalda
Copy link
Member

@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.

@Naktibalda Naktibalda transferred this issue from Codeception/Codeception Oct 22, 2020
@SOHELAHMED7
Copy link

SOHELAHMED7 commented May 8, 2024

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants