This repository has been archived by the owner on Sep 29, 2021. It is now read-only.
forked from rnixik/selenium2php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CliController.php
366 lines (342 loc) · 13.9 KB
/
CliController.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
/*
* Copyright 2013 Rnix Valentine
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Selenium2php;
/**
* Handles CLI commands.
*/
class CliController {
/**
*
* @var Converter
*/
protected $_converter;
protected $_htmlPattern = "*.html";
protected $_recursive = false;
protected $_phpFilePrefix = '';
protected $_phpFilePostfix = 'Test';
protected $_destFolder = '';
protected $_sourceBaseDir = '';
protected $_useHashFilePostfix = false;
protected $_tplFile = '';
protected $_browsers = '';
/**
* Build ID specification
* @var string
*/
protected $_projectBuild = null;
/**
*
* @var string Project name
*/
protected $_projectName = null;
public function __construct() {
require_once 'Converter.php';
$this->_converter = new Converter;
}
protected function _printTitle() {
print "Selenium2php converts Selenium HTML tests into PHPUnit test case code.";
print "\n";
print "\n";
}
protected function _printHelp() {
print "Usage: selenium2php [switches] Test.html [Test.php]";
print "\n";
print " selenium2php [switches] <directory>";
print "\n";
print "\n";
print " --dest=<path> Destination folder.\n";
print " --selenium2 Use Selenium2 tests format.\n";
print " --php-prefix=<string> Add prefix to php filenames.\n";
print " --php-postfix=<string> Add postfix to php filenames.\n";
print " --browser=<browsers string> Set browser for tests.\n";
print " --browser-url=<url> Set URL for tests.\n";
print " --remote-host=<host> Set Selenium server address for tests.\n";
print " --remote-port=<port> Set Selenium server port for tests.\n";
print " -r|--recursive Use subdirectories for converting.\n";
print " --class-prefix=<prefix> Set TestCase class prefix.\n";
print " --use-hash-postfix Add hash part to output filename.\n";
print " --files-pattern=<pattern> Glob pattern for input test files (*.html).\n";
print " --output-tpl=<file> Template for result file. See TestExampleTpl.\n";
print " --custom-param1=<value> Assign value to \$customParam1 in template.\n";
print " --custom-param2=<value> Assign value to \$customParam2 in template.\n";
print " --browsers=<value> Comma-separated list of browser names to use (from browsers.ini)\n";
print " --project-build=<value> The build triggering this test (e.g. 42)\n";
print " --project-name=<value> The project name triggering this test (e.g. Specsavers)\n";
print " --screenshotsOnEveryStep=<value> Take screenshots on every `click` or `open` event in the test\n";
print " --browserstackLocal=<value> Execute local testing from BrowserStack\n";
print " --browserstackLocalIdentifier=<value> Specify a local identifier (if any)\n";
print " --video=<value> Record video(1|0)\n";
print " --override-selenium-params=<key,value\$key,value...> Specify a list of stores parameters to always override. E.g var_ORIGIN,http://localhost\n";
}
protected function _applyOptionsAndFlags($options, $flags){
if (is_array($options)){
foreach ($options as $opt){
if (is_string($opt)){
switch ($opt){
case 'recursive':
$this->_recursive = true;
break;
case 'use-hash-postfix':
$this->_useHashFilePostfix = true;
break;
case 'selenium2':
$this->_converter->useSelenium2();
break;
default:
print "Unknown option \"$opt\".\n";
exit(1);
}
} else if (is_array($opt)){
switch ($opt[0]){
case 'php-prefix':
$this->_phpFilePrefix = $opt[1];
break;
case 'php-postfix':
$this->_phpFilePostfix = $opt[1];
break;
case 'browser':
$this->_converter->setBrowser($opt[1]);
break;
case 'browser-url':
$this->_converter->setTestUrl($opt[1]);
break;
case 'remote-host':
$this->_converter->setRemoteHost($opt[1]);
break;
case 'remote-port':
$this->_converter->setRemotePort($opt[1]);
break;
case 'dest':
$this->_destFolder = $opt[1];
break;
case 'class-prefix':
$this->_converter->setTplClassPrefix($opt[1]);
break;
case 'use-hash-postfix':
$this->_useHashFilePostfix = true;
break;
case 'files-pattern':
$this->_htmlPattern = $opt[1];
break;
case 'output-tpl':
$this->_tplFile = $opt[1];
break;
case 'custom-param1':
$this->_converter->setTplCustomParam1($opt[1]);
break;
case 'custom-param2':
$this->_converter->setTplCustomParam2($opt[1]);
break;
case 'browsers':
$this->_browsers = $opt[1];
$this->_converter->browsers = $this->_browsers;
break;
case 'project-build':
$this->_projectBuild = $opt[1];
$this->_converter->_projectBuild = $this->_projectBuild;
break;
case 'project-name':
$this->_projectName = $opt[1];
$this->_converter->_projectName = $this->_projectName;
break;
case 'screenshotsOnEveryStep':
$this->_converter->screenshotsOnEveryStep = $opt[1];
break;
case 'browserstackLocal':
$this->_converter->browserstackLocal = $opt[1];
break;
case 'browserstackLocalIdentifier':
$this->_converter->browserstackLocalIdentifier = $opt[1];
break;
case 'override-selenium-params':
if (isset($opt[1])) {
$this->_converter->overrideSeleniumParams = $opt[1];
}
break;
case 'video':
$this->_converter->video = $opt[1] == 'false' || $opt[1] == '0' ? false : true;
break;
case 'single-test':
if (isset($opt[1])) {
$this->_converter->singleTest = true;
}
break;
default:
print "Unknown option \"{$opt[0]}\".\n";
exit(1);
}
}
}
}
if (is_array($flags)){
foreach ($flags as $flag){
switch($flag){
case 'r':
$this->_recursive = true;
break;
default:
print "Unknown flag \"$flag\".\n";
exit(1);
}
}
}
}
public function run($arguments, $options, $flags) {
$this->_printTitle();
$this->_applyOptionsAndFlags($options, $flags);
if (empty($arguments)) {
$this->_printHelp();
} else if (!empty($arguments)) {
$first = array_shift($arguments);
$second = array_shift($arguments);
if ($first && is_string($first)) {
if (is_file($first)) {
$htmlFileName = $first;
if (is_readable($htmlFileName)) {
if ($second && is_string($second)) {
$phpFileName = $second;
} else {
$phpFileName = '';
}
$this->_sourceBaseDir = rtrim(dirname($htmlFileName), "\\/")."/";
$this->convertFilesInSuite($htmlFileName, $phpFileName);
print "OK.\n";
exit(0);
} else {
print "Cannot open file \"$htmlFileName\".\n";
exit(1);
}
} else if (is_dir($first)) {
$dir = rtrim($first, "\\/")."/";
$this->_sourceBaseDir = $dir;
$res = $this->convertFilesInDirectory($dir);
if ($res){
print "OK.\n";
exit(0);
} else {
exit(1);
}
} else {
print "\"$first\" is not existing file or directory.\n";
exit(1);
}
}
}
}
protected function convertFilesInSuite($suitePath, $phpFileName) {
$suiteContent = file_get_contents($suitePath);
if ($suiteContent) {
if (!$phpFileName) {
$phpFileName = $this->_makeOutputFilename($suitePath, $suiteContent);
}
$dir = dirname(realpath($suitePath));
$result = $this->_converter->convertSuite($suiteContent, $this->_makeTestName($suitePath), $this->_tplFile, $dir);
file_put_contents($phpFileName, $result);
print $phpFileName."\n";
}
}
protected function convertFilesInDirectory($dir){
if ($this->_recursive){
$files = $this->globRecursive($dir . $this->_htmlPattern, GLOB_NOSORT);
} else {
$files = glob($dir . $this->_htmlPattern, GLOB_NOSORT);
}
if (count($files)){
foreach ($files as $htmlFile){
$this->convertFile($htmlFile);
}
return true;
} else {
print "Files \"{$this->_htmlPattern}\" not found in \"$dir\".";
}
return false;
}
protected function _makeOutputFilename($htmlFileName, $htmlContent) {
$fileName = $this->_makeTestName($htmlFileName);
if ($this->_destFolder) {
$filePath = rtrim($this->_destFolder, "\\/") . "/";
if (!realpath($filePath)) {
//path is not absolute
$filePath = $this->_sourceBaseDir . $filePath;
if (!realpath($filePath)) {
print "Directory \"$filePath\" not found.\n";
exit(1);
}
}
} else {
$filePath = dirname($htmlFileName) . "/";
}
if ($this->_useHashFilePostfix) {
$hashPostfix = '_' . substr(md5($htmlContent), 0, 8) . '_';
} else {
$hashPostfix = '';
}
$phpFileName = $filePath . $this->_phpFilePrefix
. preg_replace("/\..+$/", '', $fileName)
. $hashPostfix
. $this->_phpFilePostfix . ".php";
return $phpFileName;
}
/**
* Makes output test name considering path.
*
* If destination folder is not defined
* returns base name of html file without extension.
* Example:
* auth/login/simple.html -> simple
*
* If destination folder is defined
* returns base name of html file prefixed with
* name of folder accordingly to destination folder.
* Example:
* auth/login/simple.html -> Auth_login_simple
*
* @param string $htmlFileName input file name
* @return string output test name
*/
protected function _makeTestName($htmlFileName){
/* get from file if this is empty */
$testName = basename($htmlFileName);
if ($this->_destFolder) {
$absPath = str_replace('\\', '_', $htmlFileName);
$absPath = str_replace('/', '_', $absPath);
$destPath = str_replace('\\', '_', $this->_sourceBaseDir);
$destPath = str_replace('/', '_', $destPath);
$testName= str_replace($destPath, '', $absPath);
}
$testName = preg_replace('/[^A-Za-z0-9]/', '_', ucwords($testName));
return $testName;
}
public function convertFile($htmlFileName, $phpFileName = '') {
$htmlContent = file_get_contents($htmlFileName);
if ($htmlContent) {
if (!$phpFileName) {
$phpFileName = $this->_makeOutputFilename($htmlFileName, $htmlContent);
}
$result = $this->_converter->convert($htmlContent, $this->_makeTestName($htmlFileName), $this->_tplFile);
file_put_contents($phpFileName, $result);
print $phpFileName."\n";
}
}
protected function globRecursive($pattern, $flags) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
$files = array_merge($files, $this->globRecursive($dir . '/' . basename($pattern), $flags));
}
return $files;
}
}