├── LICENSE
├── composer.json
├── phpcs.xml
└── src
├── Constraint
└── Page.php
├── Exception
├── ElementNotFound.php
└── MalformedLocatorException.php
├── Lib
└── Interfaces
│ ├── ElementLocator.php
│ ├── MultiSession.php
│ ├── PageSourceSaver.php
│ ├── Remote.php
│ ├── ScreenshotSaver.php
│ ├── SessionSnapshot.php
│ └── Web.php
└── Util
├── Locator.php
└── Uri.php
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2011 Michael Bodnarchuk and contributors
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "codeception/lib-web",
3 | "description": "Library containing files used by module-webdriver and lib-innerbrowser or module-phpbrowser",
4 | "license": "MIT",
5 | "type": "library",
6 | "keywords": [
7 | "codeception"
8 | ],
9 | "authors": [
10 | {
11 | "name": "Gintautas Miselis"
12 | }
13 | ],
14 | "homepage": "https://codeception.com/",
15 | "require": {
16 | "php": "^8.1",
17 | "ext-mbstring": "*",
18 | "guzzlehttp/psr7": "^2.0",
19 | "phpunit/phpunit": "^9.5 | ^10.0 | ^11.0 | ^12",
20 | "symfony/css-selector": ">=4.4.24 <8.0"
21 | },
22 | "require-dev": {
23 | "php-webdriver/webdriver": "^1.12"
24 | },
25 | "conflict": {
26 | "codeception/codeception": "<5.0.0-alpha3"
27 | },
28 | "autoload": {
29 | "classmap": [
30 | "src/"
31 | ]
32 | },
33 | "config": {
34 | "classmap-authoritative": true,
35 | "sort-packages": true
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/phpcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Codeception code standard
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/src/Constraint/Page.php:
--------------------------------------------------------------------------------
1 | string = $this->normalizeText($string);
26 | $this->uri = $uri;
27 | }
28 |
29 | /**
30 | * Evaluates the constraint for parameter $other. Returns true if the
31 | * constraint is met, false otherwise.
32 | *
33 | * @param string $other Value or object to evaluate.
34 | * @return bool
35 | */
36 | protected function matches($other): bool
37 | {
38 | $other = $this->normalizeText($other);
39 | return mb_stripos($other, $this->string, 0, 'UTF-8') !== false;
40 | }
41 |
42 | private function normalizeText(string $text): string
43 | {
44 | $text = strtr($text, "\r\n", " ");
45 | return trim(preg_replace('/\\s{2,}/', ' ', $text));
46 | }
47 |
48 | /**
49 | * Returns a string representation of the constraint.
50 | */
51 | public function toString(): string
52 | {
53 | return sprintf(
54 | 'contains "%s"',
55 | $this->string
56 | );
57 | }
58 |
59 | /**
60 | * @param string $pageContent
61 | */
62 | protected function failureDescription($pageContent): string
63 | {
64 | $message = $this->uriMessage('on page');
65 | $message .= "\n--> ";
66 | $message .= mb_substr($pageContent, 0, 300, 'utf-8');
67 | if (mb_strlen($pageContent, 'utf-8') > 300 && function_exists('codecept_output_dir')) {
68 | $message .= "\n[Content too long to display. See complete response in '"
69 | . codecept_output_dir() . "' directory]";
70 | }
71 |
72 | return $message . "\n--> " . $this->toString();
73 | }
74 |
75 | protected function uriMessage(string $onPage = ''): string
76 | {
77 | if (!$this->uri) {
78 | return '';
79 | }
80 | return "{$onPage} {$this->uri}";
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/Exception/ElementNotFound.php:
--------------------------------------------------------------------------------
1 | getModule('{{MODULE_NAME}}')->_findElements('.items');
19 | * $els = $this->getModule('{{MODULE_NAME}}')->_findElements(['name' => 'username']);
20 | *
21 | * $editLinks = $this->getModule('{{MODULE_NAME}}')->_findElements(['link' => 'Edit']);
22 | * // now you can iterate over $editLinks and check that all them have valid hrefs
23 | * ```
24 | *
25 | * WebDriver module returns `Facebook\WebDriver\Remote\RemoteWebElement` instances
26 | * PhpBrowser and Framework modules return `Symfony\Component\DomCrawler\Crawler` instances
27 | *
28 | * @api
29 | */
30 | public function _findElements(mixed $locator): iterable;
31 | }
32 |
--------------------------------------------------------------------------------
/src/Lib/Interfaces/MultiSession.php:
--------------------------------------------------------------------------------
1 | getModule('{{MODULE_NAME}}')->_savePageSource(codecept_output_dir().'page.html');
14 | * ```
15 | * @api
16 | */
17 | public function _savePageSource(string $filename): void;
18 |
19 | /**
20 | * 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.
21 | *
22 | * ```php
23 | * makeHtmlSnapshot('edit_page');
25 | * // saved to: tests/_output/debug/edit_page.html
26 | * $I->makeHtmlSnapshot();
27 | * // saved to: tests/_output/debug/2017-05-26_14-24-11_4b3403665fea6.html
28 | * ```
29 | */
30 | public function makeHtmlSnapshot(?string $name = null): void;
31 | }
32 |
--------------------------------------------------------------------------------
/src/Lib/Interfaces/Remote.php:
--------------------------------------------------------------------------------
1 | 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 | * 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 | }
38 |
--------------------------------------------------------------------------------
/src/Lib/Interfaces/ScreenshotSaver.php:
--------------------------------------------------------------------------------
1 | getModule('{{MODULE_NAME}}')->_saveScreenshot(codecept_output_dir().'screenshot_1.png');
12 | * ```
13 | * @api
14 | */
15 | public function _saveScreenshot(string $filename);
16 | }
17 |
--------------------------------------------------------------------------------
/src/Lib/Interfaces/SessionSnapshot.php:
--------------------------------------------------------------------------------
1 | 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 | }
56 |
--------------------------------------------------------------------------------
/src/Lib/Interfaces/Web.php:
--------------------------------------------------------------------------------
1 | amOnPage('/');
16 | * // opens /register page
17 | * $I->amOnPage('/register');
18 | * ```
19 | */
20 | public function amOnPage(string $page): void;
21 |
22 | /**
23 | * Checks that the current page contains the given string (case insensitive).
24 | *
25 | * You can specify a specific HTML element (via CSS or XPath) as the second
26 | * parameter to only search within that element.
27 | *
28 | * ```php
29 | * see('Logout'); // I can suppose user is logged in
31 | * $I->see('Sign Up', 'h1'); // I can suppose it's a signup page
32 | * $I->see('Sign Up', '//body/h1'); // with XPath
33 | * $I->see('Sign Up', ['css' => 'body h1']); // with strict CSS locator
34 | * ```
35 | *
36 | * Note that the search is done after stripping all HTML tags from the body,
37 | * so `$I->see('strong')` will return true for strings like:
38 | *
39 | * - `
I am Stronger than thou
`
40 | * - ``
41 | *
42 | * But will *not* be true for strings like:
43 | *
44 | * - `Home`
45 | * - `
Home`
46 | * - ``
47 | *
48 | * For checking the raw source code, use `seeInSource()`.
49 | *
50 | * @param array|string $selector optional
51 | */
52 | public function see(string $text, $selector = null): void;
53 |
54 | /**
55 | * Checks that the current page doesn't contain the text specified (case insensitive).
56 | * Give a locator as the second parameter to match a specific region.
57 | *
58 | * ```php
59 | * dontSee('Login'); // I can suppose user is already logged in
61 | * $I->dontSee('Sign Up','h1'); // I can suppose it's not a signup page
62 | * $I->dontSee('Sign Up','//body/h1'); // with XPath
63 | * $I->dontSee('Sign Up', ['css' => 'body h1']); // with strict CSS locator
64 | * ```
65 | *
66 | * Note that the search is done after stripping all HTML tags from the body,
67 | * so `$I->dontSee('strong')` will fail on strings like:
68 | *
69 | * - `