Completed
Push — master ( edb2c6...4336c2 )
by Toni
02:45
created

RawMultilingualContext::assertValueInInput()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 13
nc 6
nop 2
1
<?php
2
/**
3
 * @author Toni Kolev, <[email protected]>
4
 */
5
namespace kolev\MultilingualExtension\Context;
6
use Behat\MinkExtension\Context\RawMinkContext;
7
8
/**
9
 * Class RawMultilingualContext.
10
 *
11
 * @package kolev\MultilingualExtension\Context
12
 */
13
class RawMultilingualContext extends RawMinkContext implements MultilingualContextInterface
14
{
15
16
    /**
17
     * Parameters of MultilingualExtension.
18
     *
19
     * @var array
20
     */
21
    public $multilingual_parameters = [];
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function setMultilingualParameters(array $parameters)
27
    {
28
        if (empty($this->multilingual_parameters)) {
29
            $this->multilingual_parameters = $parameters;
30
        }
31
    }
32
33
    /**
34
     * @param string $name
35
     *   The name of parameter from behat.yml.
36
     *
37
     * @return mixed
38
     */
39
    protected function getMultilingualParameter($name)
40
    {
41
        return isset($this->multilingual_parameters[$name]) ? $this->multilingual_parameters[$name] : false;
42
    }
43
44
    /**
45
     *  RAW definitions of simple functions to  be used in MultilingualContext
46
     */
47
48
    public function iClickOnTheText($text) {
49
50
        $session = $this->getSession();
51
        $element = $session->getPage()->find(
52
            'xpath',
53
            $session->getSelectorsHandler()->selectorToXpath('xpath', '//*[contains(text(),"' . $text . '")]'));
54
55
        if (null === $element) {
56
            throw new \InvalidArgumentException(sprintf('Cannot find text: "%s"', $text));
57
        }
58
59
        $element->click();
60
    }
61
62
    public function assertValueInInput($value, $input) {
63
64
        if (substr($input,0,1) != "#") {
65
            $input = "#" . $input;
66
        }
67
        $session = $this->getSession();
68
        $element = $session->getPage()->find('css', $input);
69
        if(isset($element)) {
70
            $text = $element->getValue();
71
        }
72
        else {
73
            throw new \Exception(sprintf("Element is null"));
74
        }
75
76
        if($text === $value) {
77
            return true;
78
        }
79
        else {
80
            throw new \Exception(sprintf('Value of input : "%s" does not match the text "%s"', $text, $value));
81
        }
82
    }
83
84
    public function iWaitForTextToAppearWithMaxTime($text, $maxExecutionTime) {
85
86
        $isTextFound = false;
87
88
        for ($i = 0; $i < $maxExecutionTime; $i++) {
89
            try {
90
                $this->iShouldSeeInTheSourceOfThePage($text);
91
                $isTextFound = true;
92
                break;
93
            }
94
            catch (\Exception $e) {
95
                sleep(1);
96
            }
97
        }
98
99
        if (!$isTextFound) {
100
            throw new \Exception("'$text' didn't appear on the page for $maxExecutionTime seconds");
101
        }
102
    }
103
104
    public function iShouldSeeInTheSourceOfThePage($text) {
105
106
        $html = $this->getSession()->getDriver()->getContent();
107
        $text = $this->validateTextForSearchInSource($text);
108
        $regex = '/' . $text . '/';
109
110
        preg_match($regex, $html, $results);
111
112
        if ($results == null) {
113
            throw new \Exception('The searched text ' . $text . ' was not found in the source of the page.');
114
        }
115
116
        return true;
117
    }
118
119
    public function validateTextForSearchInSource($text) {
120
121
        $text = preg_replace("/(\\.)/", '\\\\.', $text);
122
        $text = preg_replace("/(\/)/", '\\/', $text);
123
        $text = preg_replace("/(\\?)/", '\\\\\?', $text);
124
125
        return $text;
126
    }
127
128 View Code Duplication
    public function iClickOnTheTextInRegion($text, $region) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
130
        $session = $this->getSession();
131
        $element = $session->getPage()->find('region', $region)->find('xpath', $session->getSelectorsHandler()->selectorToXpath('xpath',
132
            '//*[contains(text(),"' . $text . '")]'));
133
        if (null === $element) {
134
            throw new \InvalidArgumentException(sprintf('Cannot find text: "%s"', $text));
135
        }
136
137
        $element->click();
138
    }
139
140 View Code Duplication
    public function selectOptionWithJavascript($text, $region) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
142
        $session = $this->getSession();
143
        $element = $session->getPage()->find('region', $region)->find('xpath', $session->getSelectorsHandler()->selectorToXpath('xpath',
144
            '//*[contains(text(),"' . $text . '")]'));
145
146
        if (null === $element) {
147
            throw new \InvalidArgumentException(sprintf('Cannot find text: "%s"', $text));
148
        }
149
150
        $element->click();
151
    }
152
153
    public function assertSelectRadioById($label, $id = '') {
154
        $element = $this->getSession()->getPage();
155
        $radiobutton = $id ? $element->findById($id) : $element->find('named', array('radio', $this->getSession()->getSelectorsHandler()->xpathLiteral($label)));
0 ignored issues
show
Deprecated Code introduced by
The method Behat\Mink\Selector\Sele...Handler::xpathLiteral() has been deprecated with message: since Mink 1.7. Use \Behat\Mink\Selector\Xpath\Escaper::escapeLiteral when building Xpath or pass the unescaped value when using the named selector.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
156
        if ($radiobutton === NULL) {
157
            throw new \Exception(sprintf('The radio button with "%s" was not found on the page %s', $id ? $id : $label, $this->getSession()->getCurrentUrl()));
158
        }
159
        $value = $radiobutton->getAttribute('value');
160
        $labelonpage = $radiobutton->getParent()->getText();
161
        if ($label != $labelonpage) {
162
            throw new \Exception(sprintf("Button with id '%s' has label '%s' instead of '%s' on the page %s", $id, $labelonpage, $label, $this->getSession()->getCurrentUrl()));
163
        }
164
        $radiobutton->selectOption($value, FALSE);
165
    }
166
167
}
168