代码之家  ›  专栏  ›  技术社区  ›  S.Huston

为什么“就地”函数不能引用它所在的类?

  •  0
  • S.Huston  · 技术社区  · 6 年前

    我使用的代码等待页面上的面包屑加载。问题是在第一行中,我创建了自己的函数来代替ExpectedCondition,因为ExpectedConditions没有执行包含检查,这就是我在这里需要的。因为我做了自己的就地函数,所以我似乎不在我所在的类的范围之内。所以我不明白为什么这会破坏范围,为什么我不能引用其他方法 getProjectNameBreadCrumbText() 存在于我的类中的。

    我收到错误时发现了问题 Failed: Cannot read property 'getProjectNameBreadCrumbText' of undefined this 没有定义,但我不明白为什么。我也不知道我是否能通过考试

    // My Page Object class
    export class MapperPage {
        ...
        async getProjectNameBreadCrumbText() {
            return await this.breadCrumbs.get(2).getText();
        }
        ...
        async waitAndVerifyProjectNameBreadCrumbs(projectName: string) {
            await browser.wait(async function() {return (await this.getProjectNameBreadCrumbText()).indexOf(projectName) > -1;},
                this.DEFAULT_WAIT_TIME_SECONDS * 1000, 'Name Breadcrumb for this page never loaded.');
            expect(await this.getProjectNameBreadCrumbText()).toContain(projectName);
            expect(await this.getProjectMapperBreadCrumbText()).toEqual(this.MAPPER_BREADCRUMB);
        }
    }
    
    
    // In my test I call the waitAndVerify method like so
    ...
    it('test', async() => {
        const PROJECT_NAME = 'Project Name';
        ... // do other things
        // wait for the page to load by making sure the breadcrumbs have loaded
        await waitAndVerifyProjectNameBreadCrumbs(this.PROJECT_NAME);
    });
    

    如果这是一个重复的问题,我很抱歉,我不知道如何谷歌我所要求的,因为我不知道我写的这个代码的正确术语。

    1 回复  |  直到 6 年前
        1
  •  2
  •   mykhailo.romaniuk    6 年前

    the scope can be changed . 你可以用 arrow function 为了访问正确的范围:

    await browser.wait(
        async () => (await this.getProjectNameBreadCrumbText()).includes(projectName),
        this.DEFAULT_WAIT_TIME_SECONDS * 1000, 
        'Name Breadcrumb for this page never loaded.'
    );
    

    作为其他选择-你可以 bind 手动将函数添加到所需范围:

    this.getProjectNameBreadCrumbText = this.getProjectNameBreadCrumbText.bind(this);
    await browser.wait(
        async function() {return (await this.getProjectNameBreadCrumbText()).indexOf(projectName) > -1;},
        this.DEFAULT_WAIT_TIME_SECONDS * 1000, 
        'Name Breadcrumb for this page never loaded.')