<commit_before><?php
/**
 * Provides additional context helpers when using Mink in Behat via Prophet
 *
 * Your feature contexts should extend this.
 *
 * @author <NAME> <<EMAIL>>
 * @date 2015-10-15
 * @company Linus Shops
 */

namespace LinusShops\Prophet\Context;


use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Testwork\Tester\Result\TestResult;

class ProphetContext extends MinkContext
{
    public function waitFor ($lambda, $wait = 60)
    {
        for ($i = 0; $i < $wait; $i++)
        {
            try {
                if ($lambda($this)) {
                    return true;
                }
            } catch (Exception $e) {
                // do nothing
            }

            sleep(1);
        }

        throw new Exception(
            "Step timed out after $wait seconds"
        );
    }

    /**
     * https://blogs.library.ucsf.edu/ckm/2014/05/14/see-your-failures-taking-screenshots-with-phantomjs-running-behat-tests/
     * @AfterStep
     * @param AfterStepScope $event
     */
    public function takeScreenshotAfterFailedStep(AfterStepScope $event)
    {
        $result = $event->getTestResult();
        if (!$result->isPassed()) {
            if ($this->getSession()->getDriver() instanceof Selenium2Driver) {
                $stepText = $event->getStep()->getText();
                $fileTitle = preg_replace("#[^a-zA-Z0-9\._-]#", '', $stepText);
                $fileName = '/tmp' . DIRECTORY_SEPARATOR . $fileTitle . '.png';
                $screenshot = $this->getSession()->getDriver()->getScreenshot();
                file_put_contents($fileName, $screenshot);
                print "Screenshot for '{$stepText}' placed in {$fileName}\n";
            }
        }
    }

    /**
     * Set the size of the window
     *
     * @Given /^the viewport has width "(?P<width>[^"]+)" and height "(?P<height>[^"]+)"$/
     */
    public function setViewportSize($width, $height)
    {
        $this->getSession()->getDriver()->resizeWindow($width, $height);
    }
}
<commit_msg>Add generic wait functions.<commit_after><?php
/**
 * Provides additional context helpers when using Mink in Behat via Prophet
 *
 * Your feature contexts should extend this.
 *
 * @author <NAME> <<EMAIL>>
 * @date 2015-10-15
 * @company Linus Shops
 */

namespace LinusShops\Prophet\Context;


use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\Mink\WebAssert;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Testwork\Tester\Result\TestResult;

class ProphetContext extends MinkContext
{
    public function waitFor ($lambda, $wait = 60)
    {
        for ($i = 0; $i < $wait; $i++)
        {
            try {
                if ($lambda($this)) {
                    return true;
                }
            } catch (Exception $e) {
                // do nothing
            }

            sleep(1);
        }

        throw new Exception(
            "Step timed out after $wait seconds"
        );
    }

    /**
     * https://blogs.library.ucsf.edu/ckm/2014/05/14/see-your-failures-taking-screenshots-with-phantomjs-running-behat-tests/
     * @AfterStep
     * @param AfterStepScope $event
     */
    public function takeScreenshotAfterFailedStep(AfterStepScope $event)
    {
        $result = $event->getTestResult();
        if (!$result->isPassed()) {
            if ($this->getSession()->getDriver() instanceof Selenium2Driver) {
                $stepText = $event->getStep()->getText();
                $fileTitle = preg_replace("#[^a-zA-Z0-9\._-]#", '', $stepText);
                $fileName = '/tmp' . DIRECTORY_SEPARATOR . $fileTitle . '.png';
                $screenshot = $this->getSession()->getDriver()->getScreenshot();
                file_put_contents($fileName, $screenshot);
                print "Screenshot for '{$stepText}' placed in {$fileName}\n";
            }
        }
    }

    /**
     * Set the size of the window
     *
     * @Given /^the viewport has width "(?P<width>[^"]+)" and height "(?P<height>[^"]+)"$/
     */
    public function setViewportSize($width, $height)
    {
        $this->getSession()->getDriver()->resizeWindow($width, $height);
    }

    /**
     * @Given /^I wait "([^"]*)" seconds$/
     */
    public function iWaitSeconds($seconds)
    {
        sleep($seconds);
    }

    /**
     * @Given /^I wait until selector "([^"]*)" exists$/
     */
    public function iWaitUntilISee($selector)
    {
        /** @var $context ProphetContext */
        $this->waitFor(function($context) use ($selector)
        {
            /** @var WebAssert $session */
            $session = $context->assertSession();
            try {
                $session->elementExists('css', $selector, $context->getSession()->getPage());
            } catch (ElementNotFoundException $e) {
                return false;
            }

            return true;
        }, 10);
    }
}

{'fraction_non_alphanumeric': 0.14938418752483115, 'fraction_numerical': 0.009932459276916964, 'mean_word_length': 2.0570734669095323, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 42, 'https://': 2, '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': '2361548', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1480, 'n_words': 309}