【问题标题】:Can't move async functions to Appium PageObjects无法将异步函数移动到 Appium PageObjects
【发布时间】:2020-05-04 03:54:51
【问题描述】:

当函数这样定义时,一切正常,我的测试成功点击按钮(这里的代码是简化的,不是复制粘贴的,所以如果票证描述中有任何语法错误,那不是票证的答案):

    const wdio = require("webdriverio");
    const options = require('./android.conf.js').config;

    describe('Main',async ()=> {
      it('Should click button', async ()=> {
        const client = await wdio.remote(options);
        const button = await client.findElement('id', 'app.debug:id/theID');
        await client.isElementDisplayed(button.ELEMENT).then(async () => {
          await client.elementClick(button.ELEMENT);
     })
})

但我这样描述 pageObject:

  function PageObjects() {

    this.clickButton = async (client)=> {
            const button = await client.findElement('id', 'app.debug:id/theID');
            await client.isElementDisplayed(button.ELEMENT).then(async () => {
              await client.elementClick(button.ELEMENT);
            });
    }
  };
    module.exports = PageObjects;

我的功能是这样的:

    const wdio = require("webdriverio");
const options = require('./android.conf.js').config;
   const PageObjects = require('./po.js');

   describe('Main', async ()=> {
      const po = new PageObjects();
     it('Should click button', async ()=> {
       const client = await wdio.remote(options);
       po.clickButton(client);
      })
    })

它停止工作,我的测试不能再点击按钮了!我做错了什么?

【问题讨论】:

    标签: javascript appium pageobjects


    【解决方案1】:

    显然,由于这是一个异步流程,po.clickButton()尝试比 wdio 驱动程序更快地执行。为了防止这种情况发生,需要添加await,以便所有测试步骤都按正确的顺序执行:

         describe('Main', async ()=> {
           const po = new PageObjects();
           it('Should click button', async ()=> {
             const client = await wdio.remote(options);
             await po.clickButton(client);
           })
         })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 2021-06-21
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      相关资源
      最近更新 更多