如何使用异步方法(Jasmine-Gradurator-Typescript)返回指示元素是否可单击的布尔值?

如何使用异步方法(Jasmine-Gradurator-Typescript)返回指示元素是否可单击的布尔值?,typescript,asynchronous,promise,jasmine,protractor,Typescript,Asynchronous,Promise,Jasmine,Protractor,我想确认一个可见的webelement是不可点击的。因为我需要将其验证为jasmine期望(可以生成测试报告),所以我需要一个布尔值;这就是我所拥有的: public static async isElementClickable(element: ElementFinder): boolean{ try { await browser.wait(ExpectedConditions.elementToBeClickable(element), 50); ret

我想确认一个可见的webelement是不可点击的。因为我需要将其验证为jasmine期望(可以生成测试报告),所以我需要一个布尔值;这就是我所拥有的:

public static async isElementClickable(element: ElementFinder): boolean{
   try {
       await browser.wait(ExpectedConditions.elementToBeClickable(element), 50);
       return true;
   }catch (e) {
       return false;
   }
}
我会这样称呼这个方法:

expect(isElementClickable(myPage.buttonThatIsNotClickable)).toBe(false);
上面的方法不起作用,因为它是异步的,因此只能返回一个承诺


我必须如何更改它,或者需要添加什么才能得到布尔值?可以这样做吗?

尝试在it回调中使用
async

it('should return false', async () => {
  const result = await isElementClickable(myPage.buttonThatIsNotClickable);
  expect(result).toBe(false);
});

// Alternative way
it('should return false', (done) => {
  isElementClickable(myPage.buttonThatIsNotClickable).then(result => {
    expect(result).toBe(false);
    done();
  });
});
但函数应该返回一个承诺,而不是布尔值。我很惊讶TypeScript允许这样做。应该是这样的:

public static async isElementClickable(element: ElementFinder): Promise<boolean>{
   try {
       await browser.wait(ExpectedConditions.elementToBeClickable(element), 50);
       return Promise.resolve(true);
   }catch (e) {
       return Promise.resolve(false);
   }
}
公共静态异步isElementClickable(元素:ElementFinder):承诺{ 试一试{ wait browser.wait(ExpectedConditions.elementtobelickable(element),50); 返回承诺。解决(真实); }捕获(e){ 返回承诺。解决(错误); } }
尝试在it回调中使用
async

it('should return false', async () => {
  const result = await isElementClickable(myPage.buttonThatIsNotClickable);
  expect(result).toBe(false);
});

// Alternative way
it('should return false', (done) => {
  isElementClickable(myPage.buttonThatIsNotClickable).then(result => {
    expect(result).toBe(false);
    done();
  });
});
但函数应该返回一个承诺,而不是布尔值。我很惊讶TypeScript允许这样做。应该是这样的:

public static async isElementClickable(element: ElementFinder): Promise<boolean>{
   try {
       await browser.wait(ExpectedConditions.elementToBeClickable(element), 50);
       return Promise.resolve(true);
   }catch (e) {
       return Promise.resolve(false);
   }
}
公共静态异步isElementClickable(元素:ElementFinder):承诺{ 试一试{ wait browser.wait(ExpectedConditions.elementtobelickable(element),50); 返回承诺。解决(真实); }捕获(e){ 返回承诺。解决(错误); } }
使用了第二个代码段-它按预期工作。我还不能对你的答案投赞成票,但当我达到15%时会回来。在
async(done)
的顶部示例中,你不需要
done
done()因为测试已经是异步的。使用了第二个代码段-它按预期工作。我还不能对你的答案投赞成票,但当我达到15%时会回来。在
async(done)
的顶部示例中,你不需要
done
done()因为测试已经是异步的。