FormContext   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 0
cbo 4
dl 0
loc 40
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A iFillInFieldWithoutLoosingFocus() 0 20 4
A fixStepArgument() 0 4 1
1
<?php
2
3
namespace Novaway\CommonContexts\Context;
4
5
class FormContext extends BaseContext
6
{
7
    /**
8
     * Fills in form field with specified id|name|label|value without unfocus field
9
     *
10
     * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" without loosing focus$/
11
     * @When /^(?:|I )fill in "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)" without loosing focus$/
12
     */
13
    public function iFillInFieldWithoutLoosingFocus($field, $value)
14
    {
15
        $driver = $this->getSession()->getDriver();
16
        if ('Behat\Mink\Driver\Selenium2Driver' != get_class($driver)) {
17
            $field = $this->fixStepArgument($field);
18
            $value = $this->fixStepArgument($value);
19
20
            $this->getSession()->getPage()->fillField($field, $value);
21
        } else {
22
            if (null === ($locator = $this->getSession()->getPage()->findField($field))) {
23
                throw new \Exception(sprintf('Field "%s" not found.', $field));
24
            }
25
26
            if (!($element = $driver->getWebDriverSession()->element('xpath', $locator->getXpath()))) {
27
                throw new \Exception(sprintf('Field "%s" not found.', $field));
28
            }
29
30
            $element->postValue(['value' => [$value]]);
31
        }
32
    }
33
34
    /**
35
     * Returns fixed step argument (with \\" replaced back to ").
36
     *
37
     * @param string $argument
38
     * @return string
39
     */
40
    private function fixStepArgument($argument)
41
    {
42
        return str_replace('\\"', '"', $argument);
43
    }
44
}
45