Skip to content

Commit 2d89201

Browse files
committed
Copy files from codeception/codeception
1 parent e0b76ac commit 2d89201

File tree

15 files changed

+1855
-1
lines changed

15 files changed

+1855
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
],
1414
"homepage": "https://codeception.com/",
1515
"require": {
16-
"php": "^8.0"
16+
"php": "^8.0",
17+
"phpunit/phpunit": "^9.5 | ^10.0"
1718
},
1819
"conflict": {
1920
"codeception/codeception": "<5.0.0-alpha3"

src/Constraint/Page.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Constraint;
6+
7+
use Codeception\Lib\Console\Message;
8+
use PHPUnit\Framework\Constraint\Constraint;
9+
10+
use function codecept_output_dir;
11+
use function mb_stripos;
12+
use function mb_strlen;
13+
use function mb_substr;
14+
use function preg_replace;
15+
use function sprintf;
16+
use function strtr;
17+
use function trim;
18+
19+
class Page extends Constraint
20+
{
21+
/**
22+
* @var string
23+
*/
24+
protected $uri;
25+
/**
26+
* @var string
27+
*/
28+
protected $string;
29+
30+
public function __construct(string $string, string $uri = '')
31+
{
32+
$this->string = $this->normalizeText($string);
33+
$this->uri = $uri;
34+
}
35+
36+
/**
37+
* Evaluates the constraint for parameter $other. Returns true if the
38+
* constraint is met, false otherwise.
39+
*
40+
* @param string $other Value or object to evaluate.
41+
* @return bool
42+
*/
43+
protected function matches($other): bool
44+
{
45+
$other = $this->normalizeText($other);
46+
return mb_stripos($other, $this->string, 0, 'UTF-8') !== false;
47+
}
48+
49+
private function normalizeText(string $text): string
50+
{
51+
$text = strtr($text, "\r\n", " ");
52+
return trim(preg_replace('/\\s{2,}/', ' ', $text));
53+
}
54+
55+
/**
56+
* Returns a string representation of the constraint.
57+
*/
58+
public function toString(): string
59+
{
60+
return sprintf(
61+
'contains "%s"',
62+
$this->string
63+
);
64+
}
65+
66+
/**
67+
* @param string $pageContent
68+
*/
69+
protected function failureDescription($pageContent): string
70+
{
71+
$message = $this->uriMessage('on page');
72+
$message->append("\n--> ");
73+
$message->append(mb_substr($pageContent, 0, 300, 'utf-8'));
74+
if (mb_strlen($pageContent, 'utf-8') > 300) {
75+
$debugMessage = new Message(
76+
"[Content too long to display. See complete response in '" . codecept_output_dir() . "' directory]"
77+
);
78+
$message->append("\n")->append($debugMessage);
79+
}
80+
$message->append("\n--> ");
81+
return $message->getMessage() . $this->toString();
82+
}
83+
84+
protected function uriMessage(string $onPage = ''): Message
85+
{
86+
if (!$this->uri) {
87+
return new Message('');
88+
}
89+
$message = new Message($this->uri);
90+
$message->prepend(" {$onPage} ");
91+
return $message;
92+
}
93+
}

src/Exception/ElementNotFound.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Exception;
6+
7+
use Codeception\Util\Locator;
8+
use PHPUnit\Framework\AssertionFailedError;
9+
10+
use function is_string;
11+
12+
class ElementNotFound extends AssertionFailedError
13+
{
14+
public function __construct($selector, string $message = '')
15+
{
16+
if (!is_string($selector) || !str_contains($selector, "'")) {
17+
$selector = Locator::humanReadableString($selector);
18+
}
19+
parent::__construct("{$message} element with {$selector} was not found.");
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Exception;
6+
7+
use function ucfirst;
8+
9+
class MalformedLocatorException extends TestRuntimeException
10+
{
11+
public function __construct(string $locator, string $type = 'CSS or XPath')
12+
{
13+
parent::__construct(ucfirst($type) . " locator is malformed: {$locator}");
14+
}
15+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Codeception\Lib\Interfaces;
4+
5+
use Symfony\Component\DomCrawler\Crawler;
6+
7+
interface ElementLocator
8+
{
9+
/**
10+
* Locates element using available Codeception locator types:
11+
*
12+
* * XPath
13+
* * CSS
14+
* * Strict Locator
15+
*
16+
* Use it in Helpers or GroupObject or Extension classes:
17+
*
18+
* ```php
19+
* <?php
20+
* $els = $this->getModule('{{MODULE_NAME}}')->_findElements('.items');
21+
* $els = $this->getModule('{{MODULE_NAME}}')->_findElements(['name' => 'username']);
22+
*
23+
* $editLinks = $this->getModule('{{MODULE_NAME}}')->_findElements(['link' => 'Edit']);
24+
* // now you can iterate over $editLinks and check that all them have valid hrefs
25+
* ```
26+
*
27+
* WebDriver module returns `Facebook\WebDriver\Remote\RemoteWebElement` instances
28+
* PhpBrowser and Framework modules return `Symfony\Component\DomCrawler\Crawler` instances
29+
*
30+
* @return Crawler|array of interactive elements
31+
* @api
32+
*/
33+
public function _findElements(mixed $locator): iterable;
34+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Codeception\Lib\Interfaces;
4+
5+
interface MultiSession
6+
{
7+
public function _initializeSession(): void;
8+
9+
public function _loadSession($session): void;
10+
11+
public function _backupSession();
12+
13+
public function _closeSession($session = null): void;
14+
15+
public function _getName(): string;
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Codeception\Lib\Interfaces;
4+
5+
interface PageSourceSaver
6+
{
7+
/**
8+
* Saves page source of to a file
9+
*
10+
* ```php
11+
* $this->getModule('{{MODULE_NAME}}')->_savePageSource(codecept_output_dir().'page.html');
12+
* ```
13+
* @api
14+
*/
15+
public function _savePageSource(string $filename): void;
16+
17+
/**
18+
* Use this method within an [interactive pause](https://codeception.com/docs/02-GettingStarted#Interactive-Pause) to save the HTML source code of the current page.
19+
*
20+
* ```php
21+
* <?php
22+
* $I->makeHtmlSnapshot('edit_page');
23+
* // saved to: tests/_output/debug/edit_page.html
24+
* $I->makeHtmlSnapshot();
25+
* // saved to: tests/_output/debug/2017-05-26_14-24-11_4b3403665fea6.html
26+
* ```
27+
*/
28+
public function makeHtmlSnapshot(string $name = null): void;
29+
}

src/Lib/Interfaces/Remote.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Codeception\Lib\Interfaces;
4+
5+
interface Remote
6+
{
7+
/**
8+
* Changes the subdomain for the 'url' configuration parameter.
9+
* Does not open a page; use `amOnPage` for that.
10+
*
11+
* ``` php
12+
* <?php
13+
* // If config is: 'https://mysite.com'
14+
* // or config is: 'https://www.mysite.com'
15+
* // or config is: 'https://company.mysite.com'
16+
*
17+
* $I->amOnSubdomain('user');
18+
* $I->amOnPage('/');
19+
* // moves to https://user.mysite.com/
20+
* ```
21+
*
22+
*/
23+
public function amOnSubdomain(string $subdomain): void;
24+
25+
/**
26+
* Open web page at the given absolute URL and sets its hostname as the base host.
27+
*
28+
* ``` php
29+
* <?php
30+
* $I->amOnUrl('https://codeception.com');
31+
* $I->amOnPage('/quickstart'); // moves to https://codeception.com/quickstart
32+
* ```
33+
*/
34+
public function amOnUrl(string $url): void;
35+
36+
public function _getUrl();
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Codeception\Lib\Interfaces;
4+
5+
interface ScreenshotSaver
6+
{
7+
/**
8+
* Saves screenshot of current page to a file
9+
*
10+
* ```php
11+
* $this->getModule('{{MODULE_NAME}}')->_saveScreenshot(codecept_output_dir().'screenshot_1.png');
12+
* ```
13+
* @api
14+
*/
15+
public function _saveScreenshot(string $filename);
16+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Codeception\Lib\Interfaces;
4+
5+
interface SessionSnapshot
6+
{
7+
/**
8+
* Saves current cookies into named snapshot in order to restore them in other tests
9+
* This is useful to save session state between tests.
10+
* For example, if user needs log in to site for each test this scenario can be executed once
11+
* while other tests can just restore saved cookies.
12+
*
13+
* ``` php
14+
* <?php
15+
* // inside AcceptanceTester class:
16+
*
17+
* public function login()
18+
* {
19+
* // if snapshot exists - skipping login
20+
* if ($I->loadSessionSnapshot('login')) return;
21+
*
22+
* // logging in
23+
* $I->amOnPage('/login');
24+
* $I->fillField('name', 'jon');
25+
* $I->fillField('password', '123345');
26+
* $I->click('Login');
27+
*
28+
* // saving snapshot
29+
* $I->saveSessionSnapshot('login');
30+
* }
31+
* ```
32+
*
33+
* @return mixed
34+
*/
35+
public function saveSessionSnapshot(string $name);
36+
37+
/**
38+
* Loads cookies from a saved snapshot.
39+
* Allows to reuse same session across tests without additional login.
40+
*
41+
* See [saveSessionSnapshot](#saveSessionSnapshot)
42+
*
43+
* @return mixed
44+
*/
45+
public function loadSessionSnapshot(string $name);
46+
47+
/**
48+
* Deletes session snapshot.
49+
*
50+
* See [saveSessionSnapshot](#saveSessionSnapshot)
51+
*
52+
* @return mixed
53+
*/
54+
public function deleteSessionSnapshot(string $name);
55+
}

0 commit comments

Comments
 (0)