Angular Protractor: running scripts in a browser context

In my index.html, I directly say:

window.myAppInstance = new MyApp.myAppConstructor();

In my todo-spec.js, I have the following setting:

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);
     });
  });
});

But the protractor does not find my application. Instead, it finds null or undefined:

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

But from the Chrome console I can run

window.myAppInstance

just fine and it prints the object correctly.

How can I access this window object from my protractor test?

Edit 1: more about constructors.

Edit 2: In my application I use angular manual download . After further study, I can add this line to my test:

<snip>
  browser.get('https://my.web.page.com');
  **browser.pause()**
<snip>

: 1) F12, Chrome 2) . . . 3) . 4) , . . , , , -,

browser.get('https://my.web.page.com'); 

URL-, .

, . ?

+4
2

- , :

function waitForKey() {
  return browser.executeScript("return window.myAppXXXXXXXXXXXXX");
}

browser.wait(waitForKey, 5000);
var p = browser.executeScript(myTest);
// ...
+1

myAppXXXXXXXXXXXXX, , myTest. , executeAsyncScript , :

function myTest(callback){
  if (window.myAppXXXXXXXXXXXXX) {
    callback(Object.keys(window.myAppXXXXXXXXXXXXX).sort());
  } else {
    setTimeout(myTest, 30);  // try again in 30ms
  }
}

browser.driver.executeAsyncScript(myTest)
  .then(function(ret) {
    console.log("yay");
    console.log(ret);
  }, function() {
    console.log("error");
    console.log(arguments);
  });
+1

All Articles