<commit_before><?php

namespace Oro\Bundle\TestFrameworkBundle\Behat\Driver;

use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Exception\ExpectationException;
use Behat\Mink\Selector\Xpath\Manipulator;
use WebDriver\Element;

class OroSelenium2Driver extends Selenium2Driver
{
    /**
     * @var Manipulator
     */
    private $xpathManipulator;

    public function __construct($browserName, $desiredCapabilities, $wdHost)
    {
        $this->xpathManipulator = new Manipulator();

        parent::__construct($browserName, $desiredCapabilities, $wdHost);
    }

    /**
     * {@inheritdoc}
     */
    public function setValue($xpath, $value)
    {
        $element = $this->getWebDriverSession()->element('xpath', $xpath);
        $elementName = strtolower($element->name());

        if ('input' === $elementName) {

            $classes = explode(' ', $element->attribute('class'));

            if (in_array('select2-offscreen', $classes, true)) {
                $this->setSelect2Result($xpath, $value);

                return;
            } elseif ('text' === $element->attribute('type')) {
                $this->setTextInput($element, $value);

                return;
            }
        } elseif ('textarea' === $elementName) {
            if ('true' === $element->attribute('aria-hidden')) {
                $this->fillAsTinyMce($element, $value);

                return;
            }
        }

        parent::setValue($xpath, $value);
    }

    /**
     * @param Element $element
     * @param string $value
     * @throws ExpectationException
     */
    protected function fillAsTinyMce(Element $element, $value)
    {
        $fieldId = $element->attribute('id');

        $isTinyMce = $this->evaluateScript(
            sprintf('null != tinyMCE.get("%s");', $fieldId)
        );

        if (!$isTinyMce) {
            throw new ExpectationException(
                sprintf('Field was guessed as tinymce, but can\'t find tiny with id "%s" on page', $fieldId),
                $this
            );
        }

        $this->executeScript(
            sprintf('tinyMCE.get("%s").setContent("%s");', $fieldId, $value)
        );
    }

    /**
     * @param Element $element
     * @param string $value
     */
    protected function setTextInput(Element $element, $value)
    {
        $script = <<<JS
var node = {{ELEMENT}};
node.value = '$value';
JS;
        $this->executeJsOnElement($element, $script);
    }

    /**
     * @param string $xpath
     * @param string $value
     * @throws ExpectationException
     * @throws \Exception
     */
    protected function setSelect2Result($xpath, $value)
    {
        $this
            ->findElement($this->xpathManipulator->prepend('/../a/span[contains(@class, "select2-arrow")]', $xpath))
            ->click();
        $this->findElement('//div[contains(@class, "select2-search")]/input')->postValue(['value' => [$value]]);

        $this->wait(3000, "0 == $('ul.select2-results li.select2-searching').length");
        $results = $this->findElementXpaths('//ul[contains(@class, "select2-results")]/li');

        if (1 < count($results)) {
            throw new ExpectationException(sprintf('Too many results for "%s"', $value), $this);
        }

        $firstResult = $this->getWebDriverSession()->element('xpath', array_shift($results));

        if ('select2-no-results' === $firstResult->attribute('class')) {
            throw new ExpectationException(sprintf('Not found result for "%s"', $value), $this);
        }

        $firstResult->click();
    }

    /**
     * Wait PAGE load
     * @param int $time Time should be in milliseconds
     */
    public function waitPageToLoad($time = 15000)
    {<commit_msg>BAP-10843: Fixed naming OroSelenium2Driver methods<commit_after><?php

namespace Oro\Bundle\TestFrameworkBundle\Behat\Driver;

use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Exception\ExpectationException;
use Behat\Mink\Selector\Xpath\Manipulator;
use WebDriver\Element;

class OroSelenium2Driver extends Selenium2Driver
{
    /**
     * @var Manipulator
     */
    private $xpathManipulator;

    public function __construct($browserName, $desiredCapabilities, $wdHost)
    {
        $this->xpathManipulator = new Manipulator();

        parent::__construct($browserName, $desiredCapabilities, $wdHost);
    }

    /**
     * {@inheritdoc}
     */
    public function setValue($xpath, $value)
    {
        $element = $this->findElement($xpath);
        $elementName = strtolower($element->name());

        if ('input' === $elementName) {

            $classes = explode(' ', $element->attribute('class'));

            if (in_array('select2-offscreen', $classes, true)) {
                $this->fillSelect2Entity($xpath, $value);

                return;
            } elseif ('text' === $element->attribute('type')) {
                $this->fillTextInput($element, $value);

                return;
            }
        } elseif ('textarea' === $elementName) {
            if ('true' === $element->attribute('aria-hidden')) {
                $this->fillTinyMce($element, $value);

                return;
            }
        }

        parent::setValue($xpath, $value);
    }

    /**
     * @param Element $element
     * @param string $value
     * @throws ExpectationException
     */
    protected function fillTinyMce(Element $element, $value)
    {
        $fieldId = $element->attribute('id');

        $isTinyMce = $this->evaluateScript(
            sprintf('null != tinyMCE.get("%s");', $fieldId)
        );

        if (!$isTinyMce) {
            throw new ExpectationException(
                sprintf('Field was guessed as tinymce, but can\'t find tiny with id "%s" on page', $fieldId),
                $this
            );
        }

        $this->executeScript(
            sprintf('tinyMCE.get("%s").setContent("%s");', $fieldId, $value)
        );
    }

    /**
     * @param Element $element
     * @param string $value
     */
    protected function fillTextInput(Element $element, $value)
    {
        $script = <<<JS
var node = {{ELEMENT}};
node.value = '$value';
JS;
        $this->executeJsOnElement($element, $script);
    }

    /**
     * @param string $xpath
     * @param string $value
     * @throws ExpectationException
     * @throws \Exception
     */
    protected function fillSelect2Entity($xpath, $value)
    {
        $this
            ->findElement($this->xpathManipulator->prepend('/../a/span[contains(@class, "select2-arrow")]', $xpath))
            ->click();

        foreach ($this->findElementXpaths('//div[contains(@class, "select2-search")]/input') as $input) {
            $element = $this->findElement($input);
            if ($element->displayed()) {
                $element->postValue(['value' => [$value]]);
            }
        };

        $this->wait(3000, "0 == $('ul.select2-results li.select2-searching').length");
        $results = $this->findElementXpaths('//ul[contains(@class, "select2-results")]/li');

        if (1 < count($results)) {
            throw new ExpectationException(sprintf('Too many results for "%s"', $value), $this);
        }

        $firstResult = $this->findElement(array_shift($results));

        if ('select2-no-results' === $firstResult->attribute('class')) {
            throw new ExpectationException(sprintf('Not found result for "%s"', $value), $this);
        }

        $firstResult->click();
    }

    /**
     * Wait PAGE load
     * @param int $time Time should be in milliseconds
     */
    public function waitPageToLoad($time = 15000)
    {
{'fraction_non_alphanumeric': 0.15887480190174327, 'fraction_numerical': 0.006867406233491812, 'mean_word_length': 2.3390652557319225, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6542371', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2195, 'n_words': 426}