返回的值在量角器中未定义

时间:2018-11-23 11:51:56

标签: protractor

当我单击角度页面中的链接时,它将打开一个新选项卡。为此,我需要比较标题和少量文本。 以下是我的代码段。

this.getSupportPageTitle = async function(){
        browser.ignoreSynchronization = true;
        await browser.getAllWindowHandles().then(async function(handles){
            await browser.switchTo().window(handles[1]).then(async function(){
                console.log('focus switched to new Tab');
                var title = await actions.getElementText(element(supportPageDiscription),'check support page title');
                console.log('title : ' +title);
                return title;
            });
        });       
    };

并有一个规格

it('Display Support Page',async function(){
      browser.ignoreSynchronization = true;
      var supportPageTitle = await manageSupportPage.getSupportPageTitle();
      if (await manageSupportPage.getSupportPageTitle()){
        console.log('true');
      }
      else{
        console.log('false');
      }
      console.log('Title from page :' +supportPageTitle);
      await expect(supportPageTitle).toEqual(expected_result.expectedSupportPageDiscription);
    });

在页面中,我可以打印标题,但是当它返回规范时会说未定义。任何帮助,我们将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

因为您错过了该功能的return。然后,您可以使用Sync programming将函数体写为async/await

this.getSupportPageTitle = async function(){
    browser.ignoreSynchronization = true;
    var handles = await browser.getAllWindowHandles();

    await browser.switchTo().window(handles[1]);
    console.log('focus switched to new Tab');

    var title = await actions.getElementText(element(supportPageDiscription),'check support page title');
    console.log('title : ' +title);

    return title;
};