<commit_before><?php

namespace Oro\Bundle\TestFrameworkBundle\Pages;

use Oro\Bundle\UserBundle\Tests\Selenium\Pages\Login;
use PHPUnit_Framework_Assert;

/**
 * Class AbstractPage
 *
 * @package Oro\Bundle\TestFrameworkBundle\Pages
 *
 */
abstract class AbstractPage
{
    const URL = null;
    protected $redirectUrl = null;

    /** @var \PHPUnit_Extensions_Selenium2TestCase */
    protected $test;

    /**
     * @param $testCase
     * @param bool $redirect
     */
    public function __construct($testCase, $redirect = true)
    {
        $this->test = $testCase;
        $url = static::URL;
        if (is_null($url)) {
            $url = $this->redirectUrl;
        }
        if (!is_null($url) && $redirect) {
            $this->test->url($url);
            $this->waitPageToLoad();
            $this->waitForAjax();
        }
    }

    /**
     * @return \PHPUnit_Extensions_Selenium2TestCase
     */
    public function getTest()
    {
        return $this->test;
    }

    /**
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public function __call($name, $arguments)
    {
        if (preg_match('/open(.+)/i', "{$name}", $result) > 0) {
            if (isset($arguments[0])) {
                //get name space from arguments
                $namespace = $arguments[0];
                $namespace .= '\\Tests\Selenium';
                unset($arguments[0]);
                $arguments = array_values($arguments);
            } else {
                //get called class namespace
                $namespace = implode('\\', array_slice(explode('\\', get_class($this->test)), 0, -1));
            }
            $class = $namespace . '\\Pages\\' . $result[1];
            $class = new \ReflectionClass($class);
            return $class->newInstanceArgs(array_merge(array($this->test), $arguments));
        }

        return null;
    }

    /**
     * Wait PAGE load
     */
    public function waitPageToLoad()
    {
        // writes javascript errors to screenshots
        $jsCode = <<<JS
            if (!window.onerror) {
                window.onerror = function (errorMsg, url, lineNumber, column, errorObj) {
                    $('body').append(
                        '<div style="background: rgba(255,255,200,0.7); color: rgb(0,0,255); ' +
                            'position: absolute; top: 15px; left: 15px; right: 15px; ' +
                            'z-index: 999999; padding: 10px; border: 1px solid red;  border-radius: 5px;' +
                            'box-shadow: 0 0 40px rgba(0,0,0,0.5); white-space: pre">' +
                            '<h5>Js error occured</h5>' +
                            errorMsg + '\\n' +
                            'at ' + url + ':' + lineNumber + ':' + column +
                            '<h5>Stack trace:</h5>' +
                            errorObj.stack +
                        '</div>'
                    );

                    // Tell browser to run its own error handler as well
                    return false;
                }
            }
JS;

        $this->test->execute(array(
            'script' =>$jsCode,
            'args' =>
                array()
        ));

        $this->test->waitUntil(
            function (\PHPUnit_Extensions_Selenium2TestCase $testCase) {
                $status = $testCase->execute(
                    array('script' => "return 'complete' == document['readyState']", 'args' => array())
                );
                if ($status) {
                    return true;
                } else {
                    return null;
                }
            },
            intval(MAX_EXECUTION_TIME)
        );

        $this->test->waitUntil(
            function (\PHPUnit_Extensions_Selenium2TestCase $testCase) {<commit_msg>BAP-9555: fix js error message in selenium<commit_after><?php

namespace Oro\Bundle\TestFrameworkBundle\Pages;

use Oro\Bundle\UserBundle\Tests\Selenium\Pages\Login;
use PHPUnit_Framework_Assert;

/**
 * Class AbstractPage
 *
 * @package Oro\Bundle\TestFrameworkBundle\Pages
 *
 */
abstract class AbstractPage
{
    const URL = null;
    protected $redirectUrl = null;

    /** @var \PHPUnit_Extensions_Selenium2TestCase */
    protected $test;

    /**
     * @param $testCase
     * @param bool $redirect
     */
    public function __construct($testCase, $redirect = true)
    {
        $this->test = $testCase;
        $url = static::URL;
        if (is_null($url)) {
            $url = $this->redirectUrl;
        }
        if (!is_null($url) && $redirect) {
            $this->test->url($url);
            $this->waitPageToLoad();
            $this->waitForAjax();
        }
    }

    /**
     * @return \PHPUnit_Extensions_Selenium2TestCase
     */
    public function getTest()
    {
        return $this->test;
    }

    /**
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public function __call($name, $arguments)
    {
        if (preg_match('/open(.+)/i', "{$name}", $result) > 0) {
            if (isset($arguments[0])) {
                //get name space from arguments
                $namespace = $arguments[0];
                $namespace .= '\\Tests\Selenium';
                unset($arguments[0]);
                $arguments = array_values($arguments);
            } else {
                //get called class namespace
                $namespace = implode('\\', array_slice(explode('\\', get_class($this->test)), 0, -1));
            }
            $class = $namespace . '\\Pages\\' . $result[1];
            $class = new \ReflectionClass($class);
            return $class->newInstanceArgs(array_merge(array($this->test), $arguments));
        }

        return null;
    }

    /**
     * Wait PAGE load
     */
    public function waitPageToLoad()
    {
        // writes javascript errors to screenshots
        $jsCode = <<<JS
            if (!window.onerror) {
                window.onerror = function (errorMsg, url, lineNumber, column, errorObj) {
                    var html =
                        '<div style="background: rgba(255,255,200,0.7); color: rgb(0,0,255); ' +
                            'position: absolute; top: 15px; left: 15px; right: 15px; ' +
                            'z-index: 999999; padding: 10px; border: 1px solid red;  border-radius: 5px;' +
                            'box-shadow: 0 0 40px rgba(0,0,0,0.5); white-space: pre">' +
                            '<h5>Js error occured</h5>' + errorMsg + '\\n' +
                            'at ' + url + ':' + lineNumber + ':' + column;
                    if (errorObj && errorObj.stack) {
                        html += '<h5>Stack trace:</h5>' + errorObj.stack;
                    }
                    html += '</div>';
                    $('body').append(html);

                    // Tell browser to run its own error handler as well
                    return false;
                }
            }
JS;

        $this->test->execute(array(
            'script' =>$jsCode,
            'args' =>
                array()
        ));

        $this->test->waitUntil(
            function (\PHPUnit_Extensions_Selenium2TestCase $testCase) {
                $status = $testCase->execute(
                    array('script' => "return 'complete' == document['readyState']", 'args' => array())
                );
                if ($status) {
                    return true;
                } else {
                    return null;
                }
            },
            intval(MAX_EXECUTION_TIME)
        );

        $this->test->waitUntil(
            function (\PHPUnit_Extensions_Selenium2TestCase $testCase) {
{'fraction_non_alphanumeric': 0.1392520920502092, 'fraction_numerical': 0.015167364016736401, 'mean_word_length': 1.4618603154168008, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3747264', 'n_tokens_mistral': 2242, 'n_tokens_neox': 2100, 'n_words': 443}