需要在量角器测试用例中访问我的角度服务

时间:2021-01-02 01:20:55

I am writing e2e test cases using protractor for my angular application. I have a service in my application called UserService which holds user information. I have an API getUser in this service to get the user data. I need to access UserService in my protractor test cases.

我正在用量角器编写e2e测试用例。我的应用程序中有一个名为UserService的服务,它保存用户信息。我在这个服务中有一个API getUser来获取用户数据。我需要在量角器测试用例中访问UserService。

I looked at some of the articles and found that I could use browser.executeAsyncScript to access my services. But If I use this method , I am gettiing the injection issue.

我看了一些文章,发现我可以使用浏览器。执行异步脚本以访问我的服务。但是如果我使用这种方法,我就会遇到注射问题。

In my application, the module is defined as:

在我的申请中,模块定义为:

var angApp = angular.module('myApp', ['ngCookies','ngResource']); 

and all directives, services and controllers are created using angApp module.

所有的指令、服务和控制器都是使用angApp模块创建的。

Now I need to access my UserService so that I can call getUser API in my protractor testcases.

现在我需要访问我的UserService,以便在pro拖拉机测试用例中调用getUser API。

Could someone help me in accessing userService in my protractor testcases?

有人能帮助我在量角器测试用例中访问userService吗?

1 个解决方案

#1


3  

The problem that you are facing is that protractor tests are run in a separate process from the browser. Any script that runs in the browser is not directly accessible to your protractor tests. There is no way to directly transfer data between the two (the best you can do is interact through the DOM).

您所面临的问题是量角器测试是在与浏览器不同的进程中运行的。在浏览器中运行的任何脚本都不能直接访问量角器测试。无法在两者之间直接传输数据(最好的方法是通过DOM进行交互)。

I'm guessing that your UserService is some kind of CRUD API for manipulating Users on the server. My recommendation would be to make REST requests directly from the protractor tests to the server in order to get the users. It will not be easy to share code between the tests and the app.

我猜您的UserService是某种CRUD API,用于操作服务器上的用户。我的建议是从量角器测试直接向服务器发出REST请求,以便获得用户。在测试和应用程序之间共享代码并不容易。

#1


3  

The problem that you are facing is that protractor tests are run in a separate process from the browser. Any script that runs in the browser is not directly accessible to your protractor tests. There is no way to directly transfer data between the two (the best you can do is interact through the DOM).

您所面临的问题是量角器测试是在与浏览器不同的进程中运行的。在浏览器中运行的任何脚本都不能直接访问量角器测试。无法在两者之间直接传输数据(最好的方法是通过DOM进行交互)。

I'm guessing that your UserService is some kind of CRUD API for manipulating Users on the server. My recommendation would be to make REST requests directly from the protractor tests to the server in order to get the users. It will not be easy to share code between the tests and the app.

我猜您的UserService是某种CRUD API,用于操作服务器上的用户。我的建议是从量角器测试直接向服务器发出REST请求,以便获得用户。在测试和应用程序之间共享代码并不容易。