Repository navigation
Expand file tree
/
Copy pathWebDebug.php
More file actions
128 lines (110 loc) · 3.6 KB
/
Copy pathWebDebug.php
File metadata and controls
128 lines (110 loc) · 3.6 KB
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
<?php
namespace Codeception\Module;
use Codeception\Util\Mink;
/**
* This is a mini-module with helper actions to debug acceptance tests.
* Use it with Selenium, Selenium2, ZombieJS, or PhpBrowser module.
* Whenever none of this modules are connected the exception is thrown.
*
* ## Status
*
* * Maintainer: **davert**
* * Stability: **stable**
* * Contact: codecept@davert.mail.ua
*
* ## Configuration:
*
* * disable: false (optional) - stop making dumps and screenshots. Useful when you don't need debug anymore but you don't wanna change the code of your tests.
*
* ## Features
*
* * save screenshots of current page
* * save html (xml, json) code of current page
* * more to come...
*
* ## Example configuration
*
* ``` yaml
*
* class_name: WebGuy
* modules:
* enabled:
* - Selenium
* - WebDebug # <-- this module
* - WebHelper
* - Db
* config:
* Selenium:
* url: http://web.tenderway
* browser: firefox
* ```
*
*/
class WebDebug extends \Codeception\Module
{
protected $test = null;
protected $module = null;
protected $config = array('disable' => false);
private $fileOrderCounter = 0;
public function _initialize()
{
foreach ($this->getModules() as $module) {
if ($module instanceof Mink) {
$this->module = $module;
return;
}
}
throw new \Codeception\Exception\ModuleConfig(__CLASS__,"couldn't connect to any of web manipulation modules.");
}
public function _before(\Codeception\TestCase $test) {
$this->test = $test;
$this->fileOrderCounter = 0;
}
/**
* Saves screenshot of browser window and saves to `_logs/debug/`
*
* Optionally you can provide a screenshot name.
*
* @param $name
*/
public function makeAScreenshot($name = null)
{
if ($this->config['disable']) return;
if (!method_exists($this->module,'_saveScreenshot')) {
$this->debugSection('Warning',"Screenshot taking disabled for this backend. Source code will be saved instead");
$this->makeAResponseDump($name);
return;
}
$filename = $this->generateFilename($name);
try {
$this->module->_saveScreenshot($filename.'.png');
} catch (\Exception $e) {
$this->debugSection('Warning', "Screenshot couldn't be saved. HTML dump will be stored instead. ");
$this->debug('Screenshot saving error:'. $e->getMessage());
}
}
/**
* Saves current response content to `_logs/debug/`
* By default a response is treated as HTML, so all stored files will have html extension
*
* Optionally you can provide a dump name.
*
* @param $name
*/
public function makeAResponseDump($name) {
if ($this->config['disable']) return;
$filename = $this->generateFilename($name);
file_put_contents($filename.'.html', $this->module->session->getPage()->getContent());
}
protected function generateFilename($name = null)
{
$debugDir = \Codeception\Configuration::logDir().'debug';
if (!is_dir($debugDir)) mkdir($debugDir, 0777);
//make dir for screens
//define screenshot name
$this->fileOrderCounter++;
$caseName = str_replace('Cept.php', '', $this->test->getFileName());
$screenName = is_null($name) ? $caseName.' - '.$this->fileOrderCounter : $caseName.' - '.$this->fileOrderCounter.' - '.$name;
return $debugDir . DIRECTORY_SEPARATOR . $screenName;
}
}