量角器 sendKeys 不会向文本框发送任何内容

Posted

技术标签:

【中文标题】量角器 sendKeys 不会向文本框发送任何内容【英文标题】:Protractor sendKeys doesn't send anything to the textbox 【发布时间】:2016-08-05 07:12:30 【问题描述】:

我有以下代码,它倾向于在单个输入表单中输入验证码。

表单是 Angular 应用程序注册过程的一部分,它将检查验证码是否与写在 mysql 表上的验证码相同。

在验证之前有一个先前的表单,其中包含多个输入(名字、姓氏、电话和密码),并带有一个下一步按钮,用于进行验证过程。

所有网页元素均已成功定位,注册功能正在使用q使用promise按顺序执行所有操作。

代码如下:

.then(function() 
  return signupModalButton.click(); // The next button on the first form
)

.then(function() 
  return common.waitForElementToBeVisible(signupModalCode); // Wait for the code input to be visible
)

.then(function() 
  return getVerificationCodeFromDatabase(phoneUsed); // Read the code from mySQL database
)

.then(function(code) 

  console.log('Got verification code: ' + code); // Logs correctly
  signupModalCode.sendKeys(code); // No keys get sent to the text box event if I change this to:
  //signupModalCode.sendKeys('test');

  browser.wait(function() 
    var ec = protractor.ExpectedConditions;
    return ec.textToBePresentInElement(signupModalCode);
  , config.WAITING_TIMEOUT); // Same output with or without this browser.wait

  signupModalCode.getText().then(function(text) 
    console.log('text inside element: ' + text);
  ) // Nothing gets logged

  console.log('Entered code...'); // Gets logged!
  return signupModalButton.click(); // Doesn't happen
)

.then(function()  // Code doesn't reach here
  console.log('Promise resolved...'); // Doesn't get logged
)

测试运行时,我可以看到文本框,但我看到没有输入任何文本,然后浏览器关闭,控制台中将出现以下错误:

C:\Users\ramtin\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\error.js:108
  var template = new Error(this.message);
                 ^
UnknownError: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16'
System info: host: 'Morbid-PC', ip: '169.254.67.136', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_72'
Driver info: driver.version: EventFiringWebDriver
    at new bot.Error (C:\Users\ramtin\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\error.js:108:18)
    at Object.bot.response.checkResponse (C:\Users\ramtin\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\response.js:109:9)
    at C:\Users\ramtin\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:379:20
    at [object Object].promise.ControlFlow.runInFrame_ (C:/Users/ramtin/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:1857:20)
    at [object Object].goog.defineClass.notify (C:/Users/ramtin/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:2448:25)
    at [object Object].promise.Promise.notify_ (C:/Users/ramtin/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:564:12)
    at Array.forEach (native)
    at [object Object].promise.Promise.notifyAll_ (C:/Users/ramtin/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:553:15)
    at goog.async.run.processWorkQueue (C:\Users\ramtin\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:130:15)
    at runMicrotasksCallback (node.js:337:7)
Error
    at [object Object].ElementArrayFinder.applyAction_ (C:\Users\ramtin\AppData\Roaming\npm\node_modules\protractor\lib\element.js:392:21)
    at [object Object].self.(anonymous function) [as sendKeys] (C:\Users\ramtin\AppData\Roaming\npm\node_modules\protractor\lib\element.js:76:19)
    at [object Object].self.(anonymous function) [as sendKeys] (C:\Users\ramtin\AppData\Roaming\npm\node_modules\protractor\lib\element.js:721:11)
    at C:\Users\ramtin\trooptraining\troop\troop-web\test\protractor\assets\lib-auth.js:224:23
    at [object Object].promise.ControlFlow.runInFrame_ (C:/Users/ramtin/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:1857:20)
    at [object Object].goog.defineClass.notify (C:/Users/ramtin/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:2448:25)
    at [object Object].promise.Promise.notify_ (C:/Users/ramtin/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:564:12)
    at Array.forEach (native)
    at [object Object].promise.Promise.notifyAll_ (C:/Users/ramtin/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:553:15)
    at goog.async.run.processWorkQueue (C:\Users\ramtin\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:130:15)
From: Task: WebDriver.quit()
    at [object Object].webdriver.WebDriver.schedule (C:\Users\ramtin\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:362:15)
    at [object Object].webdriver.WebDriver.quit (C:\Users\ramtin\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:445:21)
    at C:\Users\ramtin\AppData\Roaming\npm\node_modules\protractor\lib\driverProviders\driverProvider.js:59:14
    at [object Object].promise.ControlFlow.runInFrame_ (C:/Users/ramtin/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:1857:20)
    at [object Object].goog.defineClass.notify (C:/Users/ramtin/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:2448:25)
    at [object Object].promise.Promise.notify_ (C:/Users/ramtin/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:564:12)
    at Array.forEach (native)
    at [object Object].promise.Promise.notifyAll_ (C:/Users/ramtin/AppData/Roaming/npm/node_modules/protractor/node_modules/selenium-webdriver/lib/goog/../webdriver/promise.js:553:15)
    at goog.async.run.processWorkQueue (C:\Users\ramtin\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:130:15)
    at runMicrotasksCallback (node.js:337:7)
[launcher] Process exited with error code 1

我无法弄清楚访问这个文本框有什么问题,以及量角器如何跳过与该文本框相关的那些。

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

我认为您必须嵌套回调

.then(function() 
  return signupModalButton.click().then(function() 
    return common.waitForElementToBeVisible(signupModalCode).then(function() 
      return getVerificationCodeFromDatabase(phoneUsed).then(function(code) 
        // etc...
      );
    );
  );
);

或者,根本不明确解决承诺,让Control Flow 处理它。

【讨论】:

问题是我必须等待使用 mysql 的函数的 promise 解决后再继续,否则在与 web 元素交互时将没有来自 mysql 的数据。但是嵌套的解决方案奏效了!我在生成问题的唯一部分上使用了嵌套代码,然后使用非嵌套样式继续其余部分。非常感谢!

以上是关于量角器 sendKeys 不会向文本框发送任何内容的主要内容,如果未能解决你的问题,请参考以下文章

量角器 .sendKeys() 无法使用 ngModel 处理输入

如何使用任何 Windows 自动化向 Outlook 文本框添加一些文本?

selenium Python自动化测试SendKeys发送内容和回车,会清空输入框,怎么改?

selenium 操作文本框(textarea输入)

如何使用量角器或selenium在网页中的Shadow-root下的文本框中输入文本。

量角器:单击按钮后如何等待页面完成?