Completed
Pull Request — develop (#27)
by Carlo
03:15
created

DriverSwitcher::switchToWindow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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
        if ($iFrame instanceof Element) {
34
            $reflectedElement = new \ReflectionClass($iFrame);
35
            if ($reflectedElement->hasMethod('getCssId') === false) {
36
                throw new \ReflectionException("getCssId method not in Element " . get_class($iFrame));
37
            }
38
            $iFrameId = $iFrame->getCssId();
39
            $this->getDriver()->switchToIFrame($iFrameId);
40
        }
41
    }
42
43
    /**
44
     * @param null|string $name
45
     */
46
    public function switchToWindow($name = null)
47
    {
48
        $this->getDriver()->switchToWindow($name);
49
    }
50
}
51