/
Navigate.ts
184 lines (172 loc) · 5.88 KB
/
Navigate.ts
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import type { Answerable, AnswersQuestions, UsesAbilities } from '@serenity-js/core';
import { d, Interaction, TestCompromisedError } from '@serenity-js/core';
import { BrowseTheWeb } from '../abilities';
/**
* Instructs an {@apilink Actor|actor} who has the {@apilink Ability|ability} to {@apilink BrowseTheWeb}
* to navigate to a specific destination, as well as back and forth in the browser history,
* or reload the current page.
*
* ## Learn more
*
* - {@apilink BrowseTheWeb}
*
* @group Activities
*/
export class Navigate {
/**
* Instructs an {@apilink Actor|actor} who has the {@apilink Ability|ability} to {@apilink BrowseTheWeb}
* to navigate to a given URL.
*
* The URL can be:
* - absolute, e.g. `https://example.org/search`
* - relative, e.g. `/search`
*
* If the URL is relative, your Web test integration tool will append it to any base URL
* specified in its respective configuration file.
*
*
* #### Navigate to path relative to baseUrl
*
* ```ts
* import { actorCalled } from '@serenity-js/core'
* import { Navigate } from '@serenity-js/web'
*
* await actorCalled('Hannu')
* .attemptsTo(
* Navigate.to('/search'),
* )
* ```
*
* ## Navigate to an absolute URL (overrides baseUrl)
*
* ```ts
* import { actorCalled } from '@serenity-js/core'
* import { Navigate } from '@serenity-js/web'
*
* await actorCalled('Hannu')
* .whoCan(BrowseTheWeb.using(browser))
* .attemptsTo(
* Navigate.to('https://mycompany.org/login'),
* )
* ```
*
* #### Learn more
*
* - {@apilink Page.navigateTo}
* - [WebdriverIO: Configuration Options](https://webdriver.io/docs/options/#baseurl)
* - [Playwright: Browser](https://playwright.dev/docs/api/class-browser#browser-new-context)
* - [Playwright: Test Options](https://playwright.dev/docs/api/class-testoptions#test-options-base-url)
* - [Protractor: Configuration](https://github.com/angular/protractor/blob/master/lib/config.ts)
*
* @param url
* An absolute URL or path an {@apilink Actor}
* should navigate to
*/
static to(url: Answerable<string>): Interaction {
return new NavigateToUrl(url);
}
/**
* Instructs an {@apilink Actor|actor} who has the {@apilink Ability|ability} to {@apilink BrowseTheWeb}
* to navigate back one page in the joint session history of the current top-level browsing context.
*
* #### Navigate back in browsing history
*
* ```ts
* import { actorCalled } from '@serenity-js/core'
* import { Ensure, endsWith } from '@serenity-js/assertions'
* import { Navigate, Page } from '@serenity-js/web'
*
* await actorCalled('Hannu')
* .whoCan(BrowseTheWeb.using(browser))
* .attemptsTo(
* Navigate.to('/first'),
* Navigate.to('/second'),
*
* Navigate.back(),
*
* Ensure.that(Page.current().url().href, endsWith('/first')),
* )
* ```
*/
static back(): Interaction {
return Interaction.where(`#actor navigates back in the browser history`, async actor => {
const page = await BrowseTheWeb.as(actor).currentPage();
await page.navigateBack();
});
}
/**
* Instructs an {@apilink Actor|actor} who has the {@apilink Ability|ability} to {@apilink BrowseTheWeb}
* to navigate forward one page in the joint session history of the current top-level browsing context.
*
* #### Navigate forward in browsing history
*
* ```ts
* import { actorCalled } from '@serenity-js/core'
* import { Ensure, endsWith } from '@serenity-js/assertions'
* import { Navigate, Page } from '@serenity-js/web'
*
* await actorCalled('Hannu')
* .whoCan(BrowseTheWeb.using(browser))
* .attemptsTo(
* Navigate.to('/first'),
* Navigate.to('/second'),
*
* Navigate.back(),
* Navigate.forward(),
*
* Ensure.that(Page.current().url().href, endsWith('/second')),
* )
* ```
*/
static forward(): Interaction {
return Interaction.where(`#actor navigates forward in the browser history`, async actor => {
const page = await BrowseTheWeb.as(actor).currentPage();
await page.navigateForward();
});
}
/**
* Instructs an {@apilink Actor|actor} who has the {@apilink Ability|ability} to {@apilink BrowseTheWeb}
* to reload the current page.
*
* #### Navigate to path relative to baseUrl
*
* ```ts
* import { actorCalled } from '@serenity-js/core'
* import { Ensure, endsWith } from '@serenity-js/assertions'
* import { Navigate, Cookie } from '@serenity-js/web'
* import { browser } from '@wdio/globals'
*
* await actorCalled('Hannu')
* .whoCan(BrowseTheWebWithWebdriverIO.using(browser))
* .attemptsTo(
* Navigate.to('/login'),
* Cookie.called('session_id').delete(),
* Navigate.reloadPage(),
* )
* ```
*/
static reloadPage(): Interaction {
return Interaction.where(`#actor reloads the page`, async actor => {
const page = await BrowseTheWeb.as(actor).currentPage();
await page.reload();
});
}
}
/**
* @package
*/
class NavigateToUrl extends Interaction {
constructor(private readonly url: Answerable<string>) {
super(d`#actor navigates to ${ url }`);
}
/**
* @inheritDoc
*/
async performAs(actor: UsesAbilities & AnswersQuestions): Promise<void> {
const url = await actor.answer(this.url);
const page = await BrowseTheWeb.as(actor).currentPage();
return page.navigateTo(url).catch(error => {
throw new TestCompromisedError(`Couldn't navigate to ${ url }`, error);
});
}
}