InstructionNavigator::navigateTo()   C
last analyzed

Complexity

Conditions 16
Paths 84

Size

Total Lines 66
Code Lines 49

Duplication

Lines 14
Ratio 21.21 %

Importance

Changes 0
Metric Value
dl 14
loc 66
rs 5.8516
c 0
b 0
f 0
nc 84
cc 16
eloc 49
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Magium\Navigators;
4
5
use Facebook\WebDriver\Exception\ElementNotVisibleException;
6
use Facebook\WebDriver\WebDriverBy;
7
use Magium\AbstractTestCase;
8
use Magium\Actions\WaitForPageLoaded;
9
use Magium\InvalidConfigurationException;
10
use Magium\WebDriver\ExpectedCondition;
11
use Magium\WebDriver\WebDriver;
12
13
class InstructionNavigator implements NavigatorInterface
14
{
15
    const NAVIGATOR = 'InstructionNavigator';
16
17
    const INSTRUCTION_MOUSE_CLICK           = 'mouseClick';
18
    const INSTRUCTION_MOUSE_MOVETO          = 'mouseMoveTo';
19
    const INSTRUCTION_WAIT_FOR_DISPLAYED    = 'waitForDisplayed';
20
    const INSTRUCTION_WAIT_FOR_EXISTS       = 'waitForExists';
21
    const INSTRUCTION_WAIT_FOR_NOT_EXISTS   = 'waitForNotExists';
22
    const INSTRUCTION_WAIT_FOR_HIDDEN       = 'waitForHidden';
23
    const INSTRUCTION_PAUSE                 = 'pause';
24
    const INSTRUCTION_USE_MANUAL_TIMING     = 'manualTiming';
25
26
    protected $webdriver;
27
    protected $testCase;
28
    protected $loaded;
29
    
30
    public function __construct(
31
        AbstractTestCase $testCase,
32
        WebDriver $webdriver,
33
        WaitForPageLoaded $loaded
34
    )
35
    {
36
        $this->testCase             = $testCase;
37
        $this->webdriver            = $webdriver;
38
        $this->loaded               = $loaded;
39
    }
40
41
    /**
42
     * @param array $instructions
43
     * @throws ElementNotVisibleException
44
     * @throws InvalidConfigurationException
45
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
46
     * @throws \Facebook\WebDriver\Exception\TimeOutException
47
     */
48
49
    public function navigateTo(array $instructions)
50
    {
51
        $this->testCase->assertGreaterThan(0, count($instructions), 'Instruction navigator requires at least one instruction');
52
        $useAutomaticTiming = true;
53
        foreach ($instructions as $key => $instruction) {
54
            if (count($instruction) > 0 && $instruction[0] == self::INSTRUCTION_USE_MANUAL_TIMING) {
55
                $useAutomaticTiming = false;
56
            }
57
        }
58
59
        foreach ($instructions as $instruction) {
60
            $this->testCase->assertCount(2, $instruction, 'Navigation instructions need to be a 2 member array.  First item is the instruction type, the second is the XPath');
61
            list($instruction, $xpath) = $instruction;
62
            if ($useAutomaticTiming) {
63
                $this->testCase->sleep('100ms'); // Courtesy sleep of 100ms
64
                if ($instruction == self::INSTRUCTION_MOUSE_MOVETO || $instruction == self::INSTRUCTION_MOUSE_CLICK) {
65
                    $this->webdriver->wait()->until(ExpectedCondition::elementExists($xpath, WebDriver::BY_XPATH));
0 ignored issues
show
Documentation introduced by
\Magium\WebDriver\Expect...er\WebDriver::BY_XPATH) is of type object<Facebook\WebDrive...riverExpectedCondition>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
                    $this->webdriver->wait(5)->until(ExpectedCondition::elementToBeClickable(WebDriverBy::xpath($xpath)));
0 ignored issues
show
Documentation introduced by
\Magium\WebDriver\Expect...riverBy::xpath($xpath)) is of type object<Magium\WebDriver\ExpectedCondition>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
                }
68
            }
69
            
70
            switch ($instruction) {
71
                case self::INSTRUCTION_MOUSE_CLICK:
72
                    $element = $this->webdriver->byXpath($xpath);
73
                    if (!$element->isDisplayed()) {
74
                        throw new ElementNotVisibleException('The element is not visible: '.$xpath);
75
                    }
76
                    $element->click();
77
                    break;
78
                case self::INSTRUCTION_MOUSE_MOVETO:
79
                    $element = $this->webdriver->byXpath($xpath);
80
                    $this->webdriver->getMouse()->mouseMove($element->getCoordinates());
81
                    break;
82
                case self::INSTRUCTION_WAIT_FOR_EXISTS:
83
                    $this->webdriver->wait()->until(ExpectedCondition::elementExists($xpath, WebDriver::BY_XPATH));
0 ignored issues
show
Documentation introduced by
\Magium\WebDriver\Expect...er\WebDriver::BY_XPATH) is of type object<Facebook\WebDrive...riverExpectedCondition>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
                    break;
85
                case self::INSTRUCTION_WAIT_FOR_NOT_EXISTS:
86
                    $this->webdriver->wait()->until(
87
                        ExpectedCondition::not(
0 ignored issues
show
Documentation introduced by
\Magium\WebDriver\Expect...r\WebDriver::BY_XPATH)) is of type object<Magium\WebDriver\ExpectedCondition>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
                            ExpectedCondition::elementExists($xpath, WebDriver::BY_XPATH)
0 ignored issues
show
Documentation introduced by
\Magium\WebDriver\Expect...er\WebDriver::BY_XPATH) is of type object<Facebook\WebDrive...riverExpectedCondition>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
                        )
90
                    );
91
                    break;
92 View Code Duplication
                case self::INSTRUCTION_WAIT_FOR_DISPLAYED:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
93
                    $element = $this->webdriver->byXpath($xpath);
94
                    $this->webdriver->wait()->until(ExpectedCondition::visibilityOf($element));
0 ignored issues
show
Documentation introduced by
\Magium\WebDriver\Expect...:visibilityOf($element) is of type object<Magium\WebDriver\ExpectedCondition>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
                    break;
96
                case self::INSTRUCTION_PAUSE:
97
                    $this->testCase->sleep($xpath);
98
                    break;
99 View Code Duplication
                case self::INSTRUCTION_WAIT_FOR_HIDDEN:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
100
                    $element = $this->webdriver->byXpath($xpath);
101
                    $this->webdriver->wait()->until(
102
                        ExpectedCondition::not(
0 ignored issues
show
Documentation introduced by
\Magium\WebDriver\Expect...visibilityOf($element)) is of type object<Magium\WebDriver\ExpectedCondition>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
                            ExpectedCondition::visibilityOf(
0 ignored issues
show
Documentation introduced by
\Magium\WebDriver\Expect...:visibilityOf($element) is of type object<Magium\WebDriver\ExpectedCondition>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
                                $element
105
                            )
106
                        )
107
                    );
108
                    break;
109
                default:
110
                    throw new InvalidConfigurationException('Unknown login instruction: '.$instruction);
111
            }
112
        }
113
114
    }
115
    
116
}
117