|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NuvoleWeb\Drupal\DrupalExtension\Context; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Mink\Exception\ExpectationException; |
|
6
|
|
|
use function bovigo\assert\predicate\isOfType; |
|
7
|
|
|
use function bovigo\assert\predicate\hasKey; |
|
8
|
|
|
use function bovigo\assert\assert; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class ResponsiveContext. |
|
12
|
|
|
* |
|
13
|
|
|
* @package NuvoleWeb\Drupal\DrupalExtension\Context |
|
14
|
|
|
*/ |
|
15
|
|
|
class ResponsiveContext extends RawMinkContext { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Default list of device definitions. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $defaultDevices = [ |
|
23
|
|
|
'mobile_portrait' => '360x640', |
|
24
|
|
|
'mobile_landscape' => '640x360', |
|
25
|
|
|
'tablet_portrait' => '768x1024', |
|
26
|
|
|
'tablet_landscape' => '1024x768', |
|
27
|
|
|
'laptop' => '1280x800', |
|
28
|
|
|
'desktop' => '2560x1440', |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Contains list of processed devices. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $devices = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* ResponsiveContext constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $devices |
|
42
|
|
|
* List of devices. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct($devices = []) { |
|
45
|
|
|
assert($devices, isOfType('array')); |
|
46
|
|
|
$this->devices = $devices + $this->defaultDevices; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get device resolution. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* Device name. |
|
54
|
|
|
* |
|
55
|
|
|
* @return \NuvoleWeb\Drupal\DrupalExtension\Component\ResolutionComponent |
|
56
|
|
|
* Resolution object. |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function getDeviceResolution($name) { |
|
59
|
|
|
assert($this->devices, hasKey($name), "Device '{$name}' not found."); |
|
60
|
|
|
$service = $this->getContainer()->get('drupal.behat.component.resolution'); |
|
61
|
|
|
$service->parse($this->devices[$name]); |
|
62
|
|
|
return $service; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Resize browser window according to the specified device. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $device |
|
69
|
|
|
* Device name as specified in behat.yml. |
|
70
|
|
|
* |
|
71
|
|
|
* @Given I view the site on a :device device |
|
72
|
|
|
*/ |
|
73
|
|
|
public function assertDeviceScreenResize($device) { |
|
74
|
|
|
$resolution = $this->getDeviceResolution($device); |
|
75
|
|
|
$this->getSession()->resizeWindow($resolution->getWidth(), $resolution->getHeight(), 'current'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Resize browser window width. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $size |
|
82
|
|
|
* Size in pixel. |
|
83
|
|
|
* |
|
84
|
|
|
* @throws \Behat\Mink\Exception\ExpectationException |
|
85
|
|
|
* |
|
86
|
|
|
* @Then the browser window width should be :size |
|
87
|
|
|
*/ |
|
88
|
|
|
public function assertBrowserWindowWidth($size) { |
|
89
|
|
|
$actual = $this->getSession()->evaluateScript('return window.innerWidth;'); |
|
90
|
|
|
if ($actual != $size) { |
|
91
|
|
|
throw new ExpectationException("Browser window width expected to be {$size} but it is {$actual} instead.", $this->getSession()); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Resize browser window height. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $size |
|
99
|
|
|
* Size in pixel. |
|
100
|
|
|
* |
|
101
|
|
|
* @throws \Behat\Mink\Exception\ExpectationException |
|
102
|
|
|
* |
|
103
|
|
|
* @Then the browser window height should be :size |
|
104
|
|
|
*/ |
|
105
|
|
|
public function assertBrowserWindowHeight($size) { |
|
106
|
|
|
$actual = $this->getSession()->evaluateScript('return window.innerHeight;'); |
|
107
|
|
|
if ($actual != $size) { |
|
108
|
|
|
throw new ExpectationException("Browser window height expected to be {$size} but it is {$actual} instead.", $this->getSession()); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|