【问题标题】:how to accept real time input from user如何接受用户的实时输入
【发布时间】:2019-02-15 07:31:44
【问题描述】:

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

“ReferenceError:提示未定义”。

我该如何解决这个问题?

还有其他方法可以接受来自测试人员的实时用户输入吗?

使用的命令:

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

【问题讨论】:

  • 请添加minimal reproducible example。谢谢。
  • 这是一个有趣的问题,我尝试了类似await browser.executeScript("return prompt('please enter a value')"); 的方法,但未能按预期工作
  • 此密码会在短时间内更改吗?
  • 是的。每次登录时,我都会收到一个新的密码,我需要输入该密码。登录过程是这样的。 1. 我需要提供我的用户名和密码,然后单击登录按钮 2. 将显示一个新页面,要求我选择将密码发送到哪里(邮件/手机) 3. 将显示一个新页面要求我输入我收到的密码(通过邮件或手机)
  • 我的代码:element(by.name("username")).sendKeys('XXXXX'); element(by.name("password")).sendKeys('XXXXX'); element(by.buttonText('登录')).click(); browser.wait(EC.urlcontains'XXXX',9000); element(by.linkText("发送密码到我的手机")).click(); browser.wait(EC.visibilityOf(element(by.id("otppswd"))),9000).then (function(){ prompt('输入手机密码', '').then(function(passcode ){ element(by.id("otppswd")).sendKeys(passcode); element(by.buttonText('Submit')).click(); }) })

标签: input protractor real-time


【解决方案1】:

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

选项 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 管理器的替代方法 根据您的评论,我可以看到您正在使用承诺管理器,因此我也为该方法提供了答案。但是,我强烈建议您为您的框架迁移 async/await 方法。

    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);
    })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 2015-01-08
    相关资源
    最近更新 更多