如何接受用户的实时输入

时间:2019-02-15 07:31:45

标签: input protractor real-time

我正在尝试使用量角器自动化我的应用程序测试。登录应用程序时,我需要执行一个步骤,需要输入在手机中收到的密码。我正在尝试使用javascript的“提示”命令来实时接受来自测试仪的密码。但是我遇到了错误

  

“ ReferenceError:提示未定义”。

我该如何解决?

还有其他方法可以使Ш接受来自测试仪的实时用户输入。

常用命令:

prompt('Enter the passcode from your mobile', '')

1 个答案:

答案 0 :(得分:0)

我确实设法通过异步/等待和承诺管理器方法获得了您正在使用的功能的版本。

选项1-从浏览器提示中获取价值(异步/等待)

  let getValueFromUserViaPrompt = async() => {
      await browser.executeScript("window.promptPasscode=prompt('Please enter your passcode','default')");
      //verify that the prompt is displayed
      await browser.wait(EC.alertIsPresent(), 3000, 'alert is not present');

      await browser.wait(async () => {
          try {
              //if alert is still present on page then return false so main browser.wait will check again.
              await browser.wait(EC.alertIsPresent(), 500);
              return false;
          } catch (err) {
              return true;
          }
      }, 30 * 1000, 'Alert has not been closed');

      //return the prompt value
      return await browser.executeScript("return window.promptPasscode");
  }

  console.log(await getValueFromUserViaPrompt());

使用Promise Manager添加了其他选择 根据您的评论,我可以看到您正在使用Promise Manager,因此我也提供了该方法的答案。但是,我强烈建议您为您的框架转移异步/等待方法。

    let getValueFromUserViaPrompt = () => {
        browser.executeScript("window.promptPasscode=prompt('Please enter your passcode','default')");
        //verify that the prompt is displayed
        browser.wait(EC.alertIsPresent(), 3000, 'alert is not present');

        browser.wait(() => {
            //if alert is still present on page then return false so main browser.wait will check again.
            return browser.wait(EC.alertIsPresent(), 500)
                .then(() => false)
                .catch(() => true);
        }, 30 * 1000, 'Alert has not been closed');

        //return the prompt value
        return browser.executeScript("return window.promptPasscode");
    }

    getValueFromUserViaPrompt().then(res => {
        console.log(res);
    })