-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathWebpage.php
More file actions
118 lines (101 loc) · 2.67 KB
/
Webpage.php
File metadata and controls
118 lines (101 loc) · 2.67 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
<?php
declare(strict_types=1);
namespace Pest\Browser\Api;
use Pest\Browser\Execution;
use Pest\Browser\Playwright\Locator;
use Pest\Browser\Playwright\Page;
use Pest\Browser\Support\GuessLocator;
final readonly class Webpage
{
use Concerns\HasWaitCapabilities,
Concerns\InteractsWithElements,
Concerns\InteractsWithFrames,
Concerns\InteractsWithScreen,
Concerns\InteractsWithTab,
Concerns\InteractsWithToolbar,
Concerns\InteractsWithViewPort,
Concerns\MakesConsoleAssertions,
Concerns\MakesElementAssertions,
Concerns\MakesScreenshotAssertions,
Concerns\MakesUrlAssertions;
/**
* The page instance.
*/
public function __construct(
private Page $page,
private string $initialUrl,
) {
//
}
/**
* Dumps the current page's content and stops the execution.
*/
public function dd(): never
{
dd($this->page->content());
}
/**
* Waits for the page to load and returns the current instance.
*
* This automatically only runs this test + opens the browser in headed mode.
*/
public function debug(): self
{
$this->wait();
return $this;
}
/**
* Gets the page's content.
*/
public function content(): string
{
return $this->page->content();
}
/**
* Gets the page's URL.
*/
public function url(): string
{
return $this->page->url();
}
/**
* Submits the first form found on the page.
*/
public function submit(): self
{
$this->guessLocator('[type="submit"]')->click();
return $this;
}
/**
* Executes a script in the context of the page.
*/
public function script(string $content): mixed
{
return $this->page->evaluate($content);
}
/**
* Gets the page instance.
*/
public function value(string $selector): string
{
return $this->guessLocator($selector)->inputValue();
}
/**
* Saves the current browser context's storage state (cookies and localStorage) to a file.
*
* The file is saved to tests/Browser/StorageState/<name>.json and can be loaded in
* subsequent tests via withStorageState() to avoid repetitive login flows.
*/
public function saveStorageState(?string $name = null): self
{
$this->page->saveStorageState($name);
return $this;
}
/**
* Gets the locator for the given selector.
*/
private function guessLocator(string $selector, ?string $value = null): Locator
{
return (new GuessLocator($this->page))->for($selector, $value);
}
}