Skip to content

Commit 7465fd5

Browse files
tests: refactor base-page
1 parent a83bbb0 commit 7465fd5

9 files changed

+68
-94
lines changed

e2e/pages/animate-child-page.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { BasePage } from "./base-page";
55
export class AnimateChildPage extends BasePage {
66
private _parent: UIElement;
77
private _child: UIElement;
8+
89
constructor(_driver: AppiumDriver) {
910
super(_driver)
1011
}
@@ -15,13 +16,13 @@ export class AnimateChildPage extends BasePage {
1516
}
1617

1718
async waitParentToAppear() {
18-
this._parent = await this.awaitItemToAppear("parent");
19+
this._parent = (await this.awaitItemToAppear("parent")).element;
1920
const startTime = Date.now();
2021
while ((await this._parent.location()).x !== 0 && Date.now() - startTime < 3000) { }
2122
}
2223

2324
async waitChildToAppear() {
24-
this._child = await this.awaitItemToAppear("child");
25+
this._child = (await this.awaitItemToAppear("child")).element;
2526
const startTime = Date.now();
2627
while ((await this._child.location()).y !== (await this._parent.location()).y && Date.now() - startTime < 3000) { }
2728
}
@@ -31,14 +32,9 @@ export class AnimateChildPage extends BasePage {
3132
assert.isTrue((await this._parent.location()).y === (await this._child.location()).y);
3233
}
3334

34-
private async awaitItemToAppear(item: string, wait: number = 3000): Promise<UIElement> {
35-
const startTime = Date.now();
35+
private async awaitItemToAppear(item: string, wait: number = 3000): Promise<{ isVisible: boolean, element: UIElement }> {
3636
const findBtn = async () => { return await this._driver.findElementByXPathIfExists(this._elementHelper.findByTextLocator("*", item, true)); };
37-
let btn = await findBtn();
38-
while (!btn || !(await btn.isDisplayed()) && Date.now() - startTime <= wait) {
39-
btn = await findBtn();
40-
}
4137

42-
return btn;
38+
return await this.waitElementTo(findBtn, true, wait);;
4339
}
4440
}

e2e/pages/animation-builder-page.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { AppiumDriver, UIElement } from "nativescript-dev-appium";
22
import { BasePage } from "./base-page";
33

4-
export class AnimationBuilderPage extends BasePage{
4+
export class AnimationBuilderPage extends BasePage {
55
static tapToDisappear: string = "tapToDisappear";
6-
private _btnTapToDisappear: UIElement;
7-
constructor(driver: AppiumDriver) {
6+
private _btnTapToDisappear: Promise<UIElement>;
7+
8+
constructor(driver: AppiumDriver) {
89
super(driver);
910
}
1011

@@ -14,19 +15,12 @@ export class AnimationBuilderPage extends BasePage{
1415
}
1516

1617
async executeAnimation() {
17-
this._btnTapToDisappear = await this._driver.findElementByAccessibilityId(AnimationBuilderPage.tapToDisappear);
18+
this._btnTapToDisappear = this._driver.findElementByAccessibilityId(AnimationBuilderPage.tapToDisappear);
1819
console.log("Btn tap to disappear should disappear");
19-
await this._btnTapToDisappear.tap();
20+
await (await this._btnTapToDisappear).click();
2021
}
2122

2223
async waitElementToHide(wait: number) {
23-
const start = Date.now();
24-
while ((await this._btnTapToDisappear.isDisplayed()) && Date.now() - start <= wait) {
25-
26-
}
27-
}
28-
29-
async isBtnDisplayed() {
30-
return await this._btnTapToDisappear.isDisplayed();
24+
return this.waitElementTo(() => this._btnTapToDisappear, false, wait);
3125
}
3226
}

e2e/pages/animation-with-options-page.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { AppiumDriver, Point, UIElement } from "nativescript-dev-appium";
22
import { assert } from "chai";
33
import { BasePage } from "./base-page";
44

5-
export class AnimationWithOptionsPage extends BasePage{
6-
constructor(driver: AppiumDriver) {
5+
export class AnimationWithOptionsPage extends BasePage {
6+
private initialPositionOfAnimatedBtn: Point
7+
8+
constructor(driver: AppiumDriver) {
79
super(driver);
810
}
9-
private initialPositionOfAnimatedBtn: Point
11+
1012
async enterExample() {
1113
const exampleBtn = await this._driver.findElementByAccessibilityId("options");
1214
await exampleBtn.click();
@@ -18,27 +20,21 @@ export class AnimationWithOptionsPage extends BasePage{
1820
}
1921

2022
get animatedBtn() {
23+
this._driver.findElementsByAccessibilityId("animatedBtn", 10000);
2124
return this._driver.findElementByAccessibilityIdIfExists("animatedBtn");
2225
}
2326

2427
async toggleAnimation() {
2528
const btnTapToDisappear = await this.btnToggleAnimation;
26-
await btnTapToDisappear.tap();
27-
}
28-
29-
async waitElementTo(wait: number) {
30-
const start = Date.now();
31-
while (await this.isBtnDisplayed() === false && Date.now() - start <= wait) {
32-
}
29+
await btnTapToDisappear.click();
3330
}
3431

35-
async isBtnDisplayed() {
36-
let btn: UIElement = await this.animatedBtn;
37-
const isBtnDisplayed = btn ? await btn.isDisplayed() : false;
38-
return isBtnDisplayed;
32+
async waitElementToHide() {
33+
return this.waitElementTo(() => this.animatedBtn, false, 10000);
3934
}
4035

41-
async assertPositionOfToggleAnimationBtn(){
36+
async assertPositionOfToggleAnimationBtn() {
37+
this.waitElementTo(() => this.btnToggleAnimation, true, 5000);
4238
const point: Point = await (await this.btnToggleAnimation).location();
4339
assert.isTrue(point.y === this.initialPositionOfAnimatedBtn.y);
4440
}

e2e/pages/animations-with-default-options-page.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ export class AnimationsWithDefaultOptionsPage extends BasePage {
1919
async addItem() {
2020
this._btnAddItem = await this._driver.findElementByAccessibilityId("add");
2121

22-
await this._btnAddItem.tap();
22+
await this._btnAddItem.click();
2323
}
2424

2525
async clickOnItem(item: string) {
2626
const btn = await this.getItem(item);
2727

28-
await btn.tap();
28+
await btn.click();
2929
}
3030

3131
private getItem(item) {

e2e/pages/base-page.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
import { AppiumDriver, ElementHelper } from "nativescript-dev-appium";
1+
import { AppiumDriver, ElementHelper, UIElement } from "nativescript-dev-appium";
22

33
export class BasePage {
44
protected _elementHelper: ElementHelper;
55

66
constructor(protected _driver: AppiumDriver) {
77
this._elementHelper = new ElementHelper(this._driver.nsCapabilities);
88
}
9+
10+
async waitElementTo(element: () => Promise<UIElement>, shouldBeVissible: boolean, wait: number) {
11+
const start = Date.now();
12+
let btn = await element();
13+
while ((await this.isBtnDisplayed(btn)) !== shouldBeVissible && Date.now() - start <= wait) {
14+
btn = await element();
15+
}
16+
17+
return { isVisible: await this.isBtnDisplayed(btn), element: btn };
18+
}
19+
20+
async isBtnDisplayed(element: UIElement) {
21+
let btn: UIElement = await element;
22+
const isBtnDisplayed = btn ? await btn.isDisplayed() : false;
23+
return isBtnDisplayed;
24+
}
925
}

e2e/pages/extarnal-animation-page.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { AppiumDriver, UIElement } from "nativescript-dev-appium";
22
import { BasePage } from "./base-page";
33

4-
export class ExternalAnimationPage extends BasePage{
4+
export class ExternalAnimationPage extends BasePage {
55
constructor(driver: AppiumDriver) {
66
super(driver);
7-
}
7+
}
88

99
async enterExample() {
1010
const exampleBtn = await this._driver.findElementByAccessibilityId("external");
@@ -13,22 +13,14 @@ export class ExternalAnimationPage extends BasePage{
1313

1414
async toggleAnimation() {
1515
const btnTapToDisappear = await this._driver.findElementByAccessibilityId("toggleAnimation", 5);
16-
await btnTapToDisappear.tap();
16+
await btnTapToDisappear.click();
1717
}
1818

1919
animatedBtn() {
2020
return this._driver.findElementByAccessibilityIdIfExists("animatedBtn", 5);
2121
}
2222

23-
async waitElementTo(wait: number, shouldBeVisible: boolean) {
24-
const start = Date.now();
25-
while ((await this.isBtnDisplayed() !== shouldBeVisible) && Date.now() - start <= wait) {
26-
}
27-
}
28-
29-
async isBtnDisplayed() {
30-
let btn: UIElement = await this.animatedBtn();
31-
const isBtnDisplayed = btn ? await btn.isDisplayed() : false;
32-
return isBtnDisplayed;
23+
async waitElementToToggleVisibilityTo(shouldBeVisible: boolean) {
24+
return this.waitElementTo(() => this.animatedBtn(), shouldBeVisible, 10000);
3325
}
3426
}

e2e/pages/fade-in-out-page.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,14 @@ export class FadeInOutPage extends BasePage {
1313

1414
async toggleAnimation() {
1515
const btnTapToDisappear = await this._driver.findElementByAccessibilityId("toggleAnimation");
16-
await btnTapToDisappear.tap();
16+
await btnTapToDisappear.click();
1717
}
1818

1919
animatedBtn() {
2020
return this._driver.findElementByAccessibilityIdIfExists("animatedBtn");
2121
}
2222

23-
async waitElementTo(wait: number, shouldBeVisible: boolean) {
24-
const start = Date.now();
25-
while (await this.isBtnDisplayed() === shouldBeVisible && Date.now() - start <= wait) {
26-
}
27-
}
28-
29-
async isBtnDisplayed() {
30-
let btn = await this.animatedBtn();
31-
const isBtnDisplayed = btn ? await btn.isDisplayed() : false;
32-
return isBtnDisplayed;
23+
async waitElementToToggleVisibility(shouldBeVisible: boolean) {
24+
return this.waitElementTo(() => this.animatedBtn(), shouldBeVisible, 10000);
3325
}
3426
}

e2e/pages/selector-page.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,11 @@ export class SelectorPage extends BasePage {
2727
async clickOnItem(item: string) {
2828
const btn = await this._driver.findElementByXPath(this.itemXpath(item));
2929

30-
await btn.tap();
30+
await btn.click();
3131
}
3232

33-
async awaitItemToDissapear(item: string, wait: number = 3000) {
34-
const startTime = Date.now();
35-
let btn = await this._driver.findElementByXPathIfExists(this.itemXpath(item));
36-
while (btn && await btn.isDisplayed() && Date.now() - startTime <= wait) {
37-
btn = await this._driver.findElementByXPathIfExists(this.itemXpath(item));
38-
}
39-
}
40-
41-
async awaitItemToApear(item: string, wait: number = 3000) {
42-
const startTime = Date.now();
43-
let btn = await this._driver.findElementByXPathIfExists(this.itemXpath(item));
44-
while ((!btn || await btn.isDisplayed()) && Date.now() - startTime <= wait) {
45-
btn = await this._driver.findElementByXPathIfExists(this.itemXpath(item));
46-
}
33+
async waitItemToToggleVisibility(item: string, visibility: boolean) {
34+
return this.waitElementTo(() => this._driver.findElementByXPathIfExists(this.itemXpath(item)), visibility, 5000);
4735
}
4836

4937
async getChildren() {
@@ -58,7 +46,7 @@ export class SelectorPage extends BasePage {
5846
assert.isTrue(children.length === expctedElementsCount)
5947
for (let index = 0; index < children.length - 1; index++) {
6048
const element = children[index];
61-
const el = await (<any>element.driver()).elementByXPathIfExists( this._elementHelper.getXPathByTextAtributes("//*", `Item No.${index}`, true));
49+
const el = await (<any>element.driver()).elementByXPathIfExists(this._elementHelper.getXPathByTextAtributes("//*", `Item No.${index}`, true));
6250
console.log(await el.text());
6351
assert.isTrue(el && el !== null);
6452
}

e2e/smoke.e2e-spec.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,31 @@ describe("smoke-tests", () => {
3737
const animationBuilder = new AnimationBuilderPage(driver);
3838
await animationBuilder.enterExample();
3939
await animationBuilder.executeAnimation();
40-
await animationBuilder.waitElementToHide(3000);
41-
assert.isFalse(await animationBuilder.isBtnDisplayed(), "The btn should disappear");
40+
const result = await animationBuilder.waitElementToHide(3000);
41+
assert.isFalse(result.isVisible, "The btn should disappear");
4242
});
4343

4444
it("external animation - visibility", async () => {
4545
const externalAnimationPage = new ExternalAnimationPage(driver);
4646
await externalAnimationPage.enterExample();
4747
await externalAnimationPage.toggleAnimation();
48-
await externalAnimationPage.waitElementTo(10000, false);
49-
assert.isFalse(await externalAnimationPage.isBtnDisplayed(), "The button should disappear!");
48+
let result = await externalAnimationPage.waitElementToToggleVisibilityTo(false);
49+
assert.isFalse(result.isVisible, "The button should disappear!");
5050

5151
await externalAnimationPage.toggleAnimation();
52-
await externalAnimationPage.waitElementTo(10000, true);
53-
assert.isTrue(await externalAnimationPage.isBtnDisplayed(), "The button should appear!");
52+
result = await externalAnimationPage.waitElementToToggleVisibilityTo(true);
53+
assert.isTrue(result.isVisible, "The button should appear!");
5454
});
5555

5656
it("selector", async () => {
5757
const selectorPage = new SelectorPage(driver);
5858
await selectorPage.enterExample();
5959
await selectorPage.addItem();
60-
await selectorPage.awaitItemToApear("Item No.2");
60+
await selectorPage.waitItemToToggleVisibility("Item No.2", true);
6161
await selectorPage.assertElementPossition(4);
6262

6363
await selectorPage.clickOnItem("second");
64-
await selectorPage.awaitItemToDissapear("second");
64+
await selectorPage.waitItemToToggleVisibility("second", false);
6565
await selectorPage.assertElementPossition(3);
6666
});
6767

@@ -76,20 +76,20 @@ describe("smoke-tests", () => {
7676
const fadeInOutPage = new FadeInOutPage(driver);
7777
await fadeInOutPage.enterExample();
7878
await fadeInOutPage.toggleAnimation();
79-
await fadeInOutPage.waitElementTo(3000, false);
80-
assert.isFalse(await fadeInOutPage.isBtnDisplayed(), "The button should disappear!");
79+
let result = await fadeInOutPage.waitElementToToggleVisibility(false);
80+
assert.isFalse(result.isVisible, "The button should disappear!");
8181

8282
await fadeInOutPage.toggleAnimation();
83-
await fadeInOutPage.waitElementTo(3000, false);
84-
assert.isTrue(await fadeInOutPage.isBtnDisplayed(), "The button should appear!");
83+
result = await fadeInOutPage.waitElementToToggleVisibility(true);
84+
assert.isTrue(result.isVisible, "The button should appear!");
8585
});
8686

8787
it("animation with options", async () => {
8888
const animationWithOptionsPage = new AnimationWithOptionsPage(driver);
8989
await animationWithOptionsPage.enterExample();
9090
await animationWithOptionsPage.toggleAnimation();
91-
await animationWithOptionsPage.waitElementTo(3000);
92-
assert.isFalse(await animationWithOptionsPage.isBtnDisplayed(), "The button should disappear!");
91+
const result = await animationWithOptionsPage.waitElementToHide();
92+
assert.isFalse(result.isVisible, "The button should disappear!");
9393

9494
await animationWithOptionsPage.assertPositionOfToggleAnimationBtn();
9595
});

0 commit comments

Comments
 (0)