小编Bil*_*ill的帖子

角度量角器:在浏览器上下文中执行脚本

在我的index.html中,我明确地说:

window.myAppInstance = new MyApp.myAppConstructor();
Run Code Online (Sandbox Code Playgroud)

在我的todo-spec.js中,我有以下设置:

describe('my web page', function() {
  it('should have a "myAppInstance" object on the window', function() {
    browser.get('https://my.web.page.com');

    function myTest() {
      return Object.keys(window.myAppInstance).sort();
    };

    var p = browser.driver.executeScript(myTest);
     p.then(function(ret) {
        console.log("yay");
        console.log(ret);
     }, function() {
        console.log("error");
        console.log(arguments);
     });
  });
});
Run Code Online (Sandbox Code Playgroud)

但是量角器找不到我的应用。而是查找null或未定义:

error
{ '0':
   { [WebDriverError: unknown error: Cannot convert undefined or null to object
 (Session info: chrome=50.0.2661.102)
... and more garbage
Run Code Online (Sandbox Code Playgroud)

但是从Chrome的控制台,我可以运行

window.myAppInstance
Run Code Online (Sandbox Code Playgroud)

很好,它可以正确打印出对象。

如何从量角器测试访问此窗口对象?

编辑1:关于构造函数的更明确的说明。

编辑2:在我的应用中,我正在使用angular的手动引导程序。经过进一步调查,我可以将此行添加到测试中:

<snip>
  browser.get('https://my.web.page.com');
  **browser.pause()**
<snip>
Run Code Online (Sandbox Code Playgroud)

我现在的步骤:1)点击F12调出Chrome的开发人员工具2)在控制台中查看。注意一个错误。我的应用崩溃了。3)手动点击浏览器上的刷新按钮。4)注意应用程序是如何刷新的,并且一切加载都很好。困惑中挠我的头。结论可能是通过某种方式以编程方式启动页面

browser.get('https://my.web.page.com'); …
Run Code Online (Sandbox Code Playgroud)

javascript selenium angularjs selenium-webdriver protractor

5
推荐指数
1
解决办法
3740
查看次数