DriverSwitcher::switchToIFrameByCssId()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
namespace Magefix\Plugin;
3
4
use SensioLabs\Behat\PageObjectExtension\PageObject\Element;
5
6
/**
7
 * Class DriverSwitcher
8
 * @package Magefix\Plugin
9
 * @author  Carlo Tasca <[email protected]>
10
 */
11
trait DriverSwitcher
12
{
13
    public abstract function getElementObject($element);
14
15
    public abstract function getDriver();
16
17
    /**
18
     * @param string | Element $iFrameElement
19
     * @throws \ReflectionException
20
     */
21
    public function switchToIFrame($iFrameElement)
22
    {
23
        $iFrame = null;
24
25
        if (is_string($iFrameElement)) {
26
            $iFrame = $this->getElementObject($iFrameElement);
27
        }
28
29
        if (is_object($iFrameElement)) {
30
            $iFrame = $iFrameElement;
31
        }
32
33
        $this->switchToIFrameByCssId($iFrame);
34
    }
35
36
    /**
37
     * @param null|string $name
38
     */
39
    public function switchToWindow($name = null)
40
    {
41
        $this->getDriver()->switchToWindow($name);
42
    }
43
44
    /**
45
     * Return the name of the currently active window.
46
     *
47
     * @return string the name of the current window
48
     */
49
    public function getDriverActiveWindowName()
50
    {
51
        return $this->getDriver()->getWindowName();
52
    }
53
54
    /**
55
     * @param $iFrame
56
     * @throws \ReflectionException
57
     */
58
    private function switchToIFrameByCssId($iFrame)
59
    {
60
        if ($iFrame instanceof Element) {
61
            $reflectedElement = new \ReflectionClass($iFrame);
62
            if ($reflectedElement->hasMethod('getCssId') === false) {
63
                throw new \ReflectionException("getCssId method not in Element " . get_class($iFrame));
64
            }
65
            $iFrameId = $iFrame->getCssId();
66
            $this->getDriver()->switchToIFrame($iFrameId);
67
        }
68
    }
69
}
70