Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

javascript - Accessing Angular inside Protractor Test

Is it possible to access angular within your protractor tests like you do in unit testing?

Use case is that I have a service that transforms text and I want to access that service to transform some data within the actual test script. I know there is the addMockModule method in protractor but I cannot figure out how to use it for this purpose.

Would appreciate any help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a function called evaluate(). Find an element in the dom and then run the expression.

For example. If you want to count the number of todos in the http://angularjs.org/ website (under Add Some Control), do this:

Open the element explorer in protractor

./node_modules/protractor/bin/elementexplorer.js
browser.get('http://angularjs.org/')
element(by.model('todoText')).evaluate('todos.length').
  then(function(count) {
    console.log(count)
  });

It should give you a 2

You can also use executeAsyncScript

browser.executeAsyncScript(function(callback) {
  // Here we use document.body, but your app may live under a different
  // element.
  var service = angular.element(document.body)
      .injector()
      .get('myService');
  service.query({}, function(data) {
    callback(data);
  });
}).then(function (output) {
  console.log(output);
});

See an example: https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...