如何为 ui-codemirror AngularJS 指令编写 Protractor 测试?

How to write Protractor test for ui-codemirror AngularJS directive?

我在 project 中使用 ui-codemirror AngularJS 指令并想编写一个 Protractor 测试 (devopshammer/src/test/webapp/e2e/json-spec.js) 来添加一些输入

通过它的模型(ng-model 属性)找到元素 returns ui-codemirror 元素,但是不可能使用 sendKeys() 方法添加一些输入(既不在它是子元素)。

有人知道怎么做吗?

堆栈跟踪:

Failures:

  1) json formatter shuld say error message when invalid input
   Message:
     UnknownError: unknown error: cannot focus element
  (Session info: chrome=43.0.2357.81)
  (Driver info: chromedriver=2.15.322448 (52179c1b310fec1797c81ea9a20326839860b7d3),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 24 milliseconds
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50'
System info: host: 'HCE-F9HBD12', ip: '3.183.12.101', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=C:\Users2412~1\AppData\Local\Temp\scoped_dir7296_19054}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=43.0.2357.81, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}
Session ID: fffb7bec66f225f772bd998c9bc9c364
   Stacktrace:
     UnknownError: unknown error: cannot focus element
  (Session info: chrome=43.0.2357.81)
  (Driver info: chromedriver=2.15.322448 (52179c1b310fec1797c81ea9a20326839860b7d3),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 24 milliseconds
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50'
System info: host: 'HCE-F9HBD12', ip: '3.183.12.101', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=C:\Users2412~1\AppData\Local\Temp\scoped_dir7296_19054}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=43.0.2357.81, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}
Session ID: fffb7bec66f225f772bd998c9bc9c364
    at new bot.Error (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\error.js:113:18)
    at Object.bot.response.checkResponse (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\atoms\response.js:106:9)
    at C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\webdriver.js:377:20
    at [object Object].promise.ControlFlow.runInFrame_ (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1877:20)
    at [object Object].promise.Callback_.goog.defineClass.notify (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:2464:25)
    at [object Object].promise.Promise.notify_ (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:563:12)
    at Array.forEach (native)
    at Object.goog.array.forEach (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\array\array.js:203:43)
    at [object Object].promise.Promise.notifyAll_ (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:552:16)
    at goog.async.run.processWorkQueue (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:125:21)
Error
    at [object Object].ElementArrayFinder.applyAction_ (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\lib\element.js:408:21)
    at [object Object].self.(anonymous function) [as sendKeys] (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\lib\element.js:76:19)
    at [object Object].self.(anonymous function) [as sendKeys] (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\lib\element.js:736:11)
    at [object Object].<anonymous> (c:\projects\devopshammer-master\src\test\webapp\e2e\json-spec.js:25:40)
    at C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd\index.js:94:14
    at [object Object].promise.ControlFlow.runInFrame_ (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1877:20)
    at [object Object].promise.ControlFlow.runEventLoop_ (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1755:8)
    at [object Object].<anonymous> (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:2056:12)
    at goog.async.run.processWorkQueue (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:125:21)
From: Task: Asynchronous test function: it()
    at [object Object].<anonymous> (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd\index.js:93:33)
    at [object Object].<anonymous> (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\async-callback.js:45:37)
    at [object Object].jasmine.Block.execute (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:1174:17)
    at [object Object].jasmine.Queue.next_ (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2209:31)
    at [object Object]._onTimeout (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:2199:18)
Error
    at [object Object].<anonymous> (c:\projects\devopshammer-master\src\test\webapp\e2e\json-spec.js:22:5)
    at [object Object].jasmine.Env.describe_ (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:913:21)
    at [object Object].jasmine.Env.describe (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:898:15)
    at describe (C:\Users2412346\AppData\Roaming\npm\node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:658:27)
    at Object.<anonymous> (c:\projects\devopshammer-master\src\test\webapp\e2e\json-spec.js:1:63)

模板代码:

<div ng-show="hasInvalidContent()" class="alert alert-danger" role="alert">INVALID {{message}}</div>            

<div>
  <p>
    <ui-codemirror class="text-left" 
                   ui-codemirror-opts="editorOptions"
                   ng-model="jsonInput"></ui-codemirror>
  </p>
  <p>
    <input id="formatJsonButtonId" 
           type="button" 
           class="btn btn-primary" 
           ng-click="jsonInputChange()" 
           value="Format"/>
  </p>
</div>

测试代码:

it('shuld say error message when invalid input', function () {
  var messageError = element(by.css('div.alert-danger'));
  
  element(by.model('jsonInput')).sendKeys('{');
  element(by.id('formatJsonButtonId')).click();
  
  expect(messageError.isDisplayed()).toBeTruthy();
});

Click here for GitHub repository.

你可以解决:

   browser.executeScript("var editor = $('.CodeMirror')[0].CodeMirror;editor.setValue('{');");