-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathAcceptance.php
More file actions
145 lines (118 loc) · 4.58 KB
/
Acceptance.php
File metadata and controls
145 lines (118 loc) · 4.58 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace Codeception\Template;
use Codeception\InitTemplate;
use Codeception\Util\Template;
use Symfony\Component\Yaml\Yaml;
class Acceptance extends InitTemplate
{
protected string $configTemplate = <<<EOF
# suite config
suites:
acceptance:
actor: AcceptanceTester
path: .
modules:
enabled:
- WebDriver:
url: {{url}}
browser: {{browser}}
# add Codeception\Step\Retry trait to AcceptanceTester to enable retries
step_decorators:
- Codeception\Step\ConditionalAssertion
- Codeception\Step\TryTo
- Codeception\Step\Retry
support_namespace: Support
extensions:
enabled: [Codeception\Extension\RunFailed]
params:
- env
gherkin: []
# additional paths
paths:
tests: {{baseDir}}
output: {{baseDir}}/_output
data: {{baseDir}}/Support/Data
support: {{baseDir}}/Support
envs: {{baseDir}}/_envs
settings:
shuffle: false
lint: true
EOF;
protected string $firstTest = <<<EOF
<?php
namespace {{namespace}};
use {{support_namespace}}\AcceptanceTester;
class LoginCest
{
public function _before(AcceptanceTester \$I)
{
\$I->amOnPage('/');
}
public function loginSuccessfully(AcceptanceTester \$I)
{
// write a positive login test
}
public function loginWithInvalidPassword(AcceptanceTester \$I)
{
// write a negative login test
}
}
EOF;
public function setup(): void
{
$this->checkInstalled();
$this->say("Let's prepare Codeception for acceptance testing");
$this->say("Create your tests and run them in real browser");
$this->say('');
$dir = $this->ask("Where tests will be stored?", 'tests');
$browser = $this->ask("Select a browser for testing", ['chrome', 'firefox']);
if ($browser === 'chrome') {
$this->sayInfo("Ensure that you have Selenium Server and ChromeDriver installed before running tests");
}
if ($browser === 'firefox') {
$this->sayInfo("Ensure that you have Selenium Server and GeckoDriver installed before running tests");
}
$url = $this->ask("Start url for tests", "http://localhost");
$this->createDirectoryFor($outputDir = $dir . DIRECTORY_SEPARATOR . '_output');
$this->createDirectoryFor($supportDir = $dir . DIRECTORY_SEPARATOR . 'Support');
$this->createEmptyDirectory($supportDir . DIRECTORY_SEPARATOR . 'Data');
$this->createDirectoryFor($supportDir . DIRECTORY_SEPARATOR . '_generated');
$this->gitIgnore($outputDir);
$this->gitIgnore($supportDir . DIRECTORY_SEPARATOR . '_generated');
$this->sayInfo("Created test directories inside at {$dir}");
if (!class_exists('\\Codeception\\Module\\WebDriver')) {
// composer version
$this->addModulesToComposer(['WebDriver']);
}
$configFile = (new Template($this->configTemplate))
->place('url', $url)
->place('browser', $browser)
->place('baseDir', $dir)
->produce();
$namespace = rtrim($this->namespace, '\\');
$configFile = "namespace: $namespace\nsupport_namespace: {$this->supportNamespace}\n" . $configFile;
$this->createFile('codeception.yml', $configFile);
$this->createActor('AcceptanceTester', $supportDir, Yaml::parse($configFile)['suites']['acceptance']);
$this->sayInfo("Created global config codeception.yml inside the root directory");
$this->createFile(
$dir . DIRECTORY_SEPARATOR . 'LoginCest.php',
(new Template($this->firstTest))
->place('namespace', $this->namespace)
->place('support_namespace', $this->supportNamespace)
->produce()
);
$this->sayInfo("Created a demo test LoginCest.php");
$this->say();
$this->saySuccess("INSTALLATION COMPLETE");
$this->say();
$this->say("<bold>Next steps:</bold>");
$this->say('1. Launch Selenium Server and webserver');
$this->say("2. Edit <bold>{$dir}/LoginCest.php</bold> to test login of your application");
$this->say("3. Run tests using: <comment>codecept run</comment>");
$this->say();
$this->say("HINT: Add '\\Codeception\\Step\\Retry' trait to AcceptanceTester class to enable auto-retries");
$this->say("HINT: See https://codeception.com/docs/03-AcceptanceTests#retry");
$this->say("<bold>Happy testing!</bold>");
}
}