Fluent interface with protractor and typescript

我的未来我决定 提交于 2020-01-06 06:01:32

问题


I'm preparing project using protractor/typescript/jasmine. In other project we use java with fluent interface. I try to use it in this new project but I have a problem. On the bottom is error I get. Here is structure of it

-Core
--BasePage.ts
-pages
--LoginPage.ts -> extends LEPPage.ts
--HomePage.ts -> extends LEPPage.ts
--SearchPage.ts -> extends LEPPage.ts
-LEPPage.ts (this is the part of page which is always displayed on each page - top menu and footer) -> extends BasePage.ts 
-tests
--e2e
--smoke

Now example code:

BasePage

    export class BaseMeTimePage {
    protected BASE_URL : string = browser.baseUrl;
}

LEPPage

export class LEPPage extends BaseMeTimePage {
    constructor() {
        super();
    }
    searchFor(value): SearchPage {
        new WaitFor().visibilityOf(searchIcon);
        searchInput.sendKeys(value)
        searchInput.sendKeys(protractor.Key.ENTER);
        return new SearchPage();
    }
     navigateToWishlist(): Wishlist {
        wishlist.click()
        return new WishlistPage();
    }

LoginPage

export class LoginPage extends BaseMeTimePage {
public URL: string = this.BASE_URL + '/auth';
constructor() {
    super();
    browser.get(this.URL);
}
     login(email?: string, password?: string): HomePage {
            browser.get(browser.baseUrl);
            email = email || EMAIL;
            password = password || PASSWORD
            email = EMAIL;
            password = PASSWORD;
            usernameInput.sendKeys(email);
            passwordInput.sendKeys(password);
            loginBtn.click();
            new WaitFor().visibilityOf($('.lep-header'));
            return new HomePage();
        }

HomePage

export class HomePage extends LEPPage {
    constructor() {
        super();
    }
}

SearchPage

export class SearchPage extends LEPPage {
    constructor() {
        super();
    }
   clickFirstAddToWishlist() : SearchPage {
        wishListButton.click();
        return this;
    }

Example test:

describe('SearchTest', function () {

    it('add to wishlist', function () {
        new LoginPage()
            .login()
            .searchFor('some item')
            .clickFirstAddToWishlist()
            .navigateToWishlist()
    });

After run test i get error:

E/launcher - Error: TypeError: Object prototype may only be an Object or null: undefined
    at setPrototypeOf (<anonymous>)
    at __extends (/home/kamil/Projects/automatedmt/tmp/LearnerApp/pages/SearchPage.js:7:9)
    at /home/kamil/Projects/automatedmt/tmp/LearnerApp/pages/SearchPage.js:28:5
    at Object.<anonymous> (/home/kamil/Projects/automatedmt/tmp/LearnerApp/pages/SearchPage.js:152:2)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
[10:41:32] E/launcher - Process exited with error code 100

After add element to wishlist (form SearchPage) I cannot navigate to WishlistPage by using method I extend from LEPPage (.navigateToWishlist()) I have no idea how to solve it, I need Your help. Should I pass some driver between classes? I tryed to do this but without results.


回答1:


I found that typescript allows to mark class method with returning parameter this.

Example:

class BasicCalculator {
    public constructor(protected value: number = 0) { }
    public currentValue(): number {
        return this.value;
    }
    public add(operand: number): this {
        this.value += operand;
        return this;
    }
    public multiply(operand: number): this {
        this.value *= operand;
        return this;
    }
    // ... other operations go here ...
}

let v = new BasicCalculator(2)
            .multiply(5)
            .add(1)
            .currentValue();

Then

class ScientificCalculator extends BasicCalculator {
    public constructor(value = 0) {
        super(value);
    }
    public sin() {
        this.value = Math.sin(this.value);
        return this;
    }
    // ... other operations go here ...
}

let v = new ScientificCalculator(2)
        .multiply(5)
        .sin()
        .add(1)
        .currentValue();

See https://www.typescriptlang.org/docs/handbook/advanced-types.html, Polymorphic this types



来源:https://stackoverflow.com/questions/51206739/fluent-interface-with-protractor-and-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!