Completed
Push — master ( 9e8b20...f27a06 )
by Vitaly
09:55 queued 07:35
created

FeatureContext::toggleStyle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
use Behat\Behat\Context\Context;
3
use Behat\Behat\Context\SnippetAcceptingContext;
4
use Behat\Behat\Hook\Scope\AfterStepScope;
5
use Behat\Mink\Driver\Selenium2Driver;
6
use Behat\MinkExtension\Context\MinkContext;
7
use Behat\Testwork\Tester\Result\TestResult;
8
9
/**
10
 * Defines application features from the specific context.
11
 */
12
class FeatureContext extends MinkContext implements Context, SnippetAcceptingContext
13
{
14
    /**
15
     * Initializes context.
16
     *
17
     * Every scenario gets its own context instance.
18
     * You can also pass arbitrary arguments to the
19
     * context constructor through behat.yml.
20
     */
21
    public function __construct($session = null)
22
    {
23
    }
24
25
    /**
26
     * @Given /^I log out$/
27
     */
28
    public function iLogOut()
29
    {
30
        $this->getSession()->reset();
31
    }
32
33
    /**
34
     * @Given /^I wait for ajax response$/
35
     */
36
    public function iWaitForAjaxResponse()
37
    {
38
        $this->getSession()->wait(1000);
39
    }
40
41
    /**
42
     * @Given /^I wait "([^"]*)" milliseconds for response$/
43
     */
44
    public function iWaitMillisecondsForResponse($delay)
45
    {
46
        $this->getSession()->wait($delay);
47
    }
48
49
    /**
50
     * @Given /^I set browser window size to "([^"]*)" x "([^"]*)"$/
51
     */
52
    public function iSetBrowserWindowSizeToX($width, $height)
53
    {
54
        $this->getSession()->resizeWindow((int)$width, (int)$height, 'current');
55
    }
56
57
    /**
58
     * Click on the element with the provided xpath query
59
     *
60
     * @When I click on the element :arg1
61
     */
62
    public function iClickOnTheElement($selector)
63
    {
64
        $session = $this->getSession();
65
        $element = $session->getPage()->find('css', $selector);
66
67
        // If element with current selector is not found then print error
68
        if (null === $element) {
69
            throw new \InvalidArgumentException(sprintf('Could not evaluate CSS selector: "%s"', $selector));
70
        }
71
72
        // Click on the founded element
73
        $element->click();
74
    }
75
76
    /**
77
     * @AfterStep
78
     */
79
    public function takeScreenShotAfterFailedStep(AfterStepScope $scope)
80
    {
81
        if (TestResult::FAILED === $scope->getTestResult()->getResultCode()) {
82
            $driver = $this->getSession()->getDriver();
83
            if (!($driver instanceof Selenium2Driver)) {
84
                return;
85
            }
86
87
            // Create unique folder for this inspection
88
            $artifactsPath = '~/artifacts/'.date('YmdHis').'/';
89
            if (!file_exists($artifactsPath)) {
90
                mkdir($artifactsPath, 0777, true);
91
            }
92
93
            // Create screenshot
94
            file_put_contents('~/artifacts/'.uniqid().'.png', $this->getSession()->getDriver()->getScreenshot());
95
        }
96
    }
97
98
    public function toggleStyle($selector)
99
    {
100
        $function = <<<JS
101
(function(){
102
  $ = jQuery;
103
  s = $('$selector');
104
105
  s.each(function(){
106
    if($(this).attr('style')) {
107
        $(this).attr('data-old-style', $(this).attr('style'));
108
        $(this).attr('style', '');
109
      }
110
      else {
111
        $(this).attr('style', $(this).attr('data-old-style'));
112
        $(this).attr('data-old-style', '');
113
      }
114
  });
115
})()
116
JS;
117
        $this->getSession()->executeScript($function);
118
    }
119
120
    /**
121
     * @When I fill in :arg1 in the hidden field :arg2 with selector :arg3
122
     */
123
    public function iFillInTheHiddenField($value, $field, $id)
124
    {
125
        $this->toggleStyle($id);
126
        $this->fillField($field, $value);
127
        $this->toggleStyle($id);
128
    }
129
130
    /**
131
     * @Then I hover over :arg1
132
     */
133
    public function iHoverOver($id)
134
    {
135
        $page = $this->getSession()->getPage();
136
        $findName = $page->find("css", $id);
137
        if (!$findName) {
138
            throw new Exception($id . " could not be found");
139
        } else {
140
            $findName->mouseOver();
141
        }
142
    }
143
144
145
}
146
147