-
Notifications
You must be signed in to change notification settings - Fork 1
/
screenshot.php
165 lines (131 loc) · 5.1 KB
/
screenshot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
require_once realpath(__DIR__ . '/../vendor') . '/autoload.php';
use SMB\Screru\Factory\DesiredCapabilities;
use SMB\Screru\Screenshot\Screenshot;
use SMB\UrlStatus;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\WebDriverBrowserType;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverDimension;
use Facebook\WebDriver\WebDriverExpectedCondition;
/**
* screenshot
*
* 1. Transit to designated URL (Google).
* 2. Perform fullscreen capture.
* 3. Perform screen capture.
*
* @param string $browser 'chrome' or 'firefox' or 'internet explorer'
* @param array $size ['w' => xxx, 'h' => xxx]
* @param string overrideUA true : override Useragent
*/
function screenshot($browser, array $size=[], $overrideUA = '')
{
// headless?
$isHeadless = getenv('ENABLED_CHROME_HEADLESS') === 'true'
|| getenv('ENABLED_FIREFOX_HEADLESS') === 'true';
// selenium
$host = getenv('SELENIUM_SERVER_URL');
switch ($browser) {
case WebDriverBrowserType::CHROME :
$cap = new DesiredCapabilities($browser);
if ($overrideUA !== '') {
$cap->setUserAgent($overrideUA);
}
$driver = RemoteWebDriver::create($host, $cap->get());
break;
case WebDriverBrowserType::FIREFOX :
$cap = new DesiredCapabilities($browser);
if ($overrideUA !== '') {
$cap->setUserAgent($overrideUA);
}
$driver = RemoteWebDriver::create($host, $cap->get());
break;
case WebDriverBrowserType::IE :
$cap = new DesiredCapabilities($browser);
$driver = RemoteWebDriver::create($host, $cap->get());
break;
}
// When setting the screen size to MAX.
// $driver->manage()->window()->maximize();
// When the screen size is specified.
$dimension = null;
if (isset($size['w']) && isset($size['h'])) {
$dimension = new WebDriverDimension($size['w'], $size['h']);
$driver->manage()->window()->setSize($dimension);
}
// When there is a specified size in headless mode.
if ($dimension !== null && $isHeadless) {
$cap->setWindowSizeInHeadless($dimension);
}
$url = 'https://www.google.com/webhp?gl=us&hl=en&gws_rd=cr';
// Transit to designated URL (Google).
$driver->get($url);
// Search for elements.
$findElement = $driver->findElement(WebDriverBy::name('q'));
// Enter keywords in search box.
$findElement->sendKeys('Hello');
// Search execution.
$findElement->submit();
// Wait until the contents are visualized(Targeting '#botstuff').
$driver->wait(10, 100)->until(
WebDriverExpectedCondition::visibilityOfElementLocated(WebDriverBy::id('botstuff'))
);
// Capture settings.
$suffix = $isHeadless ? '_headless' : '_not-headless';
$fileName = $overrideUA === '' ? __METHOD__ . "_{$browser}" . $suffix
: __METHOD__ . "_sp_{$browser}" . $suffix;
$ds = DIRECTORY_SEPARATOR;
$captureDirectoryPath = realpath(__DIR__ . $ds . 'capture') . $ds;
// Create a Screenshot.
$screenshot = new Screenshot();
// Full screen capture (extension will be .png).
$screenshot->takeFull($driver, $captureDirectoryPath, $fileName.'_fullscreen');
// Screen capture (extension will be .png).
$screenshot->take($driver, $captureDirectoryPath . $fileName.'_not-fullscreen');
// HttpStatus of url
$status = UrlStatus::get($driver->getCurrentURL());
if ($status->is200() === false) {
throw new Exception('fail');
}
// Close window.
$driver->close();
}
// Size of iPhone 6/7/8.
$size4iPhone = ['w' => 375, 'h' => 667];
// UserAgent of iOS12.
$ua4iOS = 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1';
/**
|------------------------------------------------------------------------------
| Please set the value of the driver you want to enable to true.
|------------------------------------------------------------------------------
*/
// initialize
putenv('ENABLED_CHROME_HEADLESS=');
putenv('ENABLED_FIREFOX_HEADLESS=');
// chrome
if (getenv('ENABLED_CHROME_DRIVER') === 'true') {
// headless
putenv('ENABLED_CHROME_HEADLESS=true');
screenshot(WebDriverBrowserType::CHROME);
screenshot(WebDriverBrowserType::CHROME, $size4iPhone, $ua4iOS);
// not headless
putenv('ENABLED_CHROME_HEADLESS=');
screenshot(WebDriverBrowserType::CHROME);
screenshot(WebDriverBrowserType::CHROME, $size4iPhone, $ua4iOS);
}
// firefox
if (getenv('ENABLED_FIREFOX_DRIVER') === 'true') {
// headless
putenv('ENABLED_FIREFOX_HEADLESS=true');
screenshot(WebDriverBrowserType::FIREFOX);
screenshot(WebDriverBrowserType::FIREFOX, $size4iPhone, $ua4iOS);
// not headless
putenv('ENABLED_FIREFOX_HEADLESS=');
screenshot(WebDriverBrowserType::FIREFOX);
screenshot(WebDriverBrowserType::FIREFOX, $size4iPhone, $ua4iOS);
}
// ie
if (getenv('ENABLED_IE_DRIVER') === 'true') {
screenshot(WebDriverBrowserType::IE);
}