/
select.ts
56 lines (45 loc) · 2.02 KB
/
select.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
import { step } from '@serenity-js/core/lib/recording';
import { Interaction, UsesAbilities } from '@serenity-js/core/lib/screenplay';
import { by, ElementFinder, protractor } from 'protractor';
import { BrowseTheWeb } from '../abilities/browse_the_web';
import { Target } from '../ui/target';
export class Select {
static theValue(value: string) {
return { from: (target: Target): Interaction => new SelectOption(value, target) };
}
static values(...values: string[]) {
return { from: (target: Target): Interaction => new SelectOptions(values, target) };
}
}
class SelectOption implements Interaction {
@step('{0} selects "#value" from #target')
performAs(actor: UsesAbilities): PromiseLike<void> {
return BrowseTheWeb.as(actor)
.locate(this.target)
.element(by.cssContainingText('option', this.value))
.click();
}
constructor(private value: string, private target: Target) {
}
toString = () => `#actor selects "${this.value}" from ${this.target}`;
}
class SelectOptions implements Interaction {
@step('{0} selects "#values" from #target')
performAs(actor: UsesAbilities): PromiseLike<void> {
const hasRequiredText = (option: ElementFinder) => option.getText().then(value => !!~this.values.indexOf(value)),
isAlreadySelected = (option: ElementFinder) => option.isSelected(),
ensureOnlyOneApplies = (list: boolean[]) => list.filter(_ => _ === true).length === 1,
select = (option: ElementFinder) => option.click();
const optionsToClick = (option: ElementFinder) => protractor.promise.all([
hasRequiredText(option),
isAlreadySelected(option),
])
.then(ensureOnlyOneApplies);
return BrowseTheWeb.as(actor).locate(this.target)
.all(by.css('option'))
.filter(optionsToClick)
.each(select);
}
constructor(private values: string[], private target: Target) {
}
}