Skip to content

Commit 9f08ee7

Browse files
committed
more refactoring
1 parent 94f6bcc commit 9f08ee7

File tree

8 files changed

+59
-22
lines changed

8 files changed

+59
-22
lines changed

lib/browser.ts

+26-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import 'chromedriver';
22
import { Builder, ThenableWebDriver, WebElement, By, WebElementPromise } from 'selenium-webdriver';
33
import { NewablePage, Page } from './page';
44

5+
export type WaitCondition = () => Promise<boolean>;
6+
57
export class Browser {
68
private driver: ThenableWebDriver;
79
public constructor(private browserName: string) {
@@ -16,19 +18,36 @@ export class Browser {
1618
return this.driver.findElement(By.css(selector));
1719
}
1820

19-
public async waitUntilIsVisible(locator: () => WebElementPromise): Promise<void> {
21+
public async waitUntilAny(conditions: WaitCondition | WaitCondition[]): Promise<void> {
22+
const all = (!(conditions instanceof Array)) ? [ conditions ] : conditions;
23+
2024
await this.driver.wait(async () => {
21-
try {
22-
return await locator().isDisplayed();
23-
} catch (ex) {
25+
for (const condition of all) {
26+
try {
27+
return await condition();
28+
} catch (ex) {
29+
continue;
30+
}
31+
}
2432
return null;
25-
}
2633
});
2734
}
2835

29-
public async waitForPage<T extends Page>(page: NewablePage<T>): Promise<void> {
36+
public async waitUntilIsVisible(locator: () => WebElementPromise) {
37+
await this.waitUntilAny(this.elementIsVisible(locator));
38+
}
39+
40+
public async waitForPage<T extends Page>(page: NewablePage<T>) {
41+
await this.waitUntilAny(this.pageHasLoaded(page));
42+
}
43+
44+
public elementIsVisible(locator: () => WebElementPromise) {
45+
return async () => await locator().isDisplayed();
46+
}
47+
48+
public pageHasLoaded<T extends Page>(page: NewablePage<T>) {
3049
const thePage = new page(this);
31-
await thePage.waitForLoad();
50+
return thePage.loadCondition();
3251
}
3352

3453
public async close(): Promise<void> {

lib/ensure.ts

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ class Ensurer {
1414
}
1515
}
1616

17+
public async isVisible() {
18+
let displayed = false;
19+
const selector = (this.element as any).selector;
20+
21+
try {
22+
displayed = await this.element.isDisplayed();
23+
} catch (ex) {
24+
displayed = false;
25+
}
26+
27+
if (!displayed) {
28+
throw new Error(`Element ${this.selector} is not visible`);
29+
}
30+
}
31+
1732
public async isNotVisible() {
1833
let displayed = false;
1934
const selector = (this.element as any).selector;

lib/page.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Browser } from './browser';
1+
import { Browser, WaitCondition } from './browser';
22

33
export interface NewablePage<T extends Page> {
44
new(browser: Browser): T;
@@ -13,10 +13,10 @@ export abstract class Page {
1313

1414
public async navigate(): Promise<void> {
1515
await this.browser.navigate(this.url);
16-
await this.waitForLoad();
16+
await this.browser.waitUntilAny(this.loadCondition());
1717
}
1818

19-
public abstract async waitForLoad(): Promise<void>;
19+
public abstract loadCondition(): WaitCondition;
2020

2121
public constructor(protected browser: Browser) {
2222

pages/GoogleSignInPage.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export class GoogleSignInPage extends Page {
1919
@findBy('#passwordNext')
2020
public ConfirmPassword: WebElementPromise;
2121

22+
public loadCondition() {
23+
return this.browser.elementIsVisible(() => this.Email);
24+
}
25+
2226
public async signInAsDarthVader() {
2327
return this.signInAs(config.users.darthvader.email, config.users.darthvader.password);
2428
}
@@ -31,8 +35,4 @@ export class GoogleSignInPage extends Page {
3135
await this.ConfirmPassword.click();
3236
await this.browser.waitForPage(HomePage);
3337
}
34-
35-
public async waitForLoad(): Promise<void> {
36-
await this.browser.waitUntilIsVisible(() => this.Email);
37-
}
3838
}

pages/HomePage.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ export class HomePage extends Page {
2323
@findBy('.fdr-profile-popup .button.google')
2424
public GoogleSignIn: WebElementPromise;
2525

26-
public async waitForLoad(): Promise<void> {
27-
await this.browser.waitUntilIsVisible(() => this.IdeaTitle);
26+
@findBy('.ui.form .ui.negative.message')
27+
public ErrorBox: WebElementPromise;
28+
29+
public loadCondition() {
30+
return this.browser.elementIsVisible(() => this.IdeaTitle);
2831
}
2932

3033
public async submitNewIdea(title: string, description: string): Promise<void> {
@@ -34,7 +37,7 @@ export class HomePage extends Page {
3437
await this.browser.waitForPage(ShowIdeaPage);
3538
}
3639

37-
public async clickAtSignInWithGoogle(): Promise<void> {
40+
public async signInWithGoogle(): Promise<void> {
3841
await this.UserMenu.click();
3942
await this.browser.waitUntilIsVisible(() => this.GoogleSignIn);
4043
await this.GoogleSignIn.click();

pages/ShowIdeaPage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class ShowIdeaPage extends Page {
1414
@findBy('.support-counter .value')
1515
public SupportCounter: WebElementPromise;
1616

17-
public async waitForLoad(): Promise<void> {
18-
await this.browser.waitUntilIsVisible(() => this.Title);
17+
public loadCondition() {
18+
return this.browser.elementIsVisible(() => this.Title);
1919
}
2020
}

specs/spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ specification('Users can submit ideas', () => {
2727

2828
when('user sign in', async () => {
2929
action(async () => {
30-
await pages.home.clickAtSignInWithGoogle();
30+
await pages.home.signInWithGoogle();
3131
await pages.google.signInAsDarthVader();
3232
});
3333

tests/test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('Submit ideas', () => {
99
pages = new AllPages(new Browser('chrome'));
1010
});
1111

12-
it('TestCase #1: Unauthenticated cannot submit ideas', async () => {
12+
it('Test Case #1: Unauthenticated cannot submit ideas', async () => {
1313
// Action
1414
await pages.home.navigate();
1515
await pages.home.IdeaTitle.sendKeys('Add support to TypeScript');
@@ -25,7 +25,7 @@ describe('Submit ideas', () => {
2525
it('Test Case #2: Authenticated can submit ideas', async () => {
2626
// Action
2727
await pages.home.navigate();
28-
await pages.home.clickAtSignInWithGoogle();
28+
await pages.home.signInWithGoogle();
2929
await pages.google.signInAsDarthVader();
3030

3131
// Assert

0 commit comments

Comments
 (0)