66
77use Pest \Browser \Api \ArrayablePendingAwaitablePage ;
88use Pest \Browser \Api \PendingAwaitablePage ;
9+ use Pest \Browser \Enums \BrowserType ;
910use Pest \Browser \Enums \Device ;
1011use Pest \Browser \Playwright \Client ;
1112use Pest \Browser \Playwright \Playwright ;
13+ use TypeError ;
1214
1315/**
1416 * @internal
@@ -34,14 +36,29 @@ public function __markAsBrowserTest(): void
3436 /**
3537 * Browse to the given URL.
3638 *
37- * @template TUrl of array<int, string>|string
39+ * @template TUrl of array<int, string|Page >|string|Page
3840 *
3941 * @param TUrl $url
4042 * @param array<string, mixed> $options
41- * @return (TUrl is array<int, string> ? ArrayablePendingAwaitablePage : PendingAwaitablePage)
43+ * @return (TUrl is array<int, string|Page > ? ArrayablePendingAwaitablePage : PendingAwaitablePage)
4244 */
43- public function visit (array |string $ url , array $ options = []): ArrayablePendingAwaitablePage |PendingAwaitablePage
45+ public function visit (array |string | Page $ url , array $ options = []): ArrayablePendingAwaitablePage |PendingAwaitablePage
4446 {
47+ if ($ url instanceof Page) {
48+ $ options = [
49+ ...$ this ->pageTimezone ($ url ),
50+ ...$ this ->pageLocale ($ url ),
51+ ...$ options ,
52+ ];
53+
54+ return new PendingAwaitablePage (
55+ $ this ->pageBrowserType ($ url ),
56+ $ this ->pageDevice ($ url ),
57+ $ url ,
58+ $ options ,
59+ );
60+ }
61+
4562 if (is_string ($ url )) {
4663 return new PendingAwaitablePage (
4764 Playwright::defaultBrowserType (),
@@ -52,12 +69,123 @@ public function visit(array|string $url, array $options = []): ArrayablePendingA
5269 }
5370
5471 return new ArrayablePendingAwaitablePage (
55- array_map (fn (string $ singleUrl ): PendingAwaitablePage => new PendingAwaitablePage (
56- Playwright::defaultBrowserType (),
57- Device::DESKTOP ,
58- $ singleUrl ,
59- $ options ,
60- ), $ url ),
72+ array_map (fn (string |Page $ singleUrl ): PendingAwaitablePage => $ this ->visit ($ singleUrl , $ options ), $ url ),
73+ );
74+ }
75+
76+ /**
77+ * Get the locale from page.
78+ *
79+ * @return array{locale?: string}
80+ *
81+ * @throws TypeError
82+ */
83+ private function pageLocale (Page $ page ): array
84+ {
85+ if (! method_exists ($ page , 'locale ' )) {
86+ return [];
87+ }
88+
89+ $ locale = $ page ->locale ();
90+
91+ if (is_string ($ locale )) {
92+ return ['locale ' => $ locale ];
93+ }
94+
95+ throw new TypeError (
96+ sprintf (
97+ '%s::%s(): Return value must be of type %s, %s returned ' ,
98+ $ page ::class,
99+ 'locale ' ,
100+ 'string ' ,
101+ gettype ($ locale )
102+ )
103+ );
104+ }
105+
106+ /**
107+ * Get the timezone from page.
108+ *
109+ * @return array{timezoneId?: string}
110+ *
111+ * @throws TypeError
112+ */
113+ private function pageTimezone (Page $ page ): array
114+ {
115+ if (! method_exists ($ page , 'timezone ' )) {
116+ return [];
117+ }
118+
119+ $ timezone = $ page ->timezone ();
120+
121+ if (is_string ($ timezone )) {
122+ return ['timezoneId ' => $ timezone ];
123+ }
124+
125+ throw new TypeError (
126+ sprintf (
127+ '%s::%s(): Return value must be of type %s, %s returned ' ,
128+ $ page ::class,
129+ 'timezone ' ,
130+ 'string ' ,
131+ gettype ($ timezone )
132+ )
133+ );
134+ }
135+
136+ /**
137+ * Get the browser type from page.
138+ *
139+ * @throws TypeError
140+ */
141+ private function pageBrowserType (Page $ page ): BrowserType
142+ {
143+ if (! method_exists ($ page , 'browserType ' )) {
144+ return Playwright::defaultBrowserType ();
145+ }
146+
147+ $ browserType = $ page ->browserType ();
148+
149+ if ($ browserType instanceof BrowserType) {
150+ return $ browserType ;
151+ }
152+
153+ throw new TypeError (
154+ sprintf (
155+ '%s::%s(): Return value must be of type %s, %s returned ' ,
156+ $ page ::class,
157+ 'browserType ' ,
158+ BrowserType::class,
159+ gettype ($ browserType )
160+ )
161+ );
162+ }
163+
164+ /**
165+ * Get the device type from page.
166+ *
167+ * @throws TypeError
168+ */
169+ private function pageDevice (Page $ page ): Device
170+ {
171+ if (! method_exists ($ page , 'device ' )) {
172+ return Device::DESKTOP ;
173+ }
174+
175+ $ device = $ page ->device ();
176+
177+ if ($ device instanceof Device) {
178+ return $ device ;
179+ }
180+
181+ throw new TypeError (
182+ sprintf (
183+ '%s::%s(): Return value must be of type %s, %s returned ' ,
184+ $ page ::class,
185+ 'device ' ,
186+ Device::class,
187+ gettype ($ device )
188+ )
61189 );
62190 }
63191}
0 commit comments