Search code examples
angularjsprotractor

Access $scope object with protractor


I got an object like:

$scope.project = {name: 'whatever', description: 'blabla', another: 'another'};

To debug this, I enter in repl mode and try to see what "project" has. When I define project variable as below, and call it, it returns my object, but when I try to access its keys (project.name), I get undefined. If I do Object.keys(project) I am getting the page object methods like click, getAttribute, etc.

Any ideas on how can I have access to the original object keys?

View side:

<h1 id="foo">{{project.name}}</h1>

Test side:

var project = element(by.id('foo')).evaluate('project');

Solution

  • evaluate uses executeScript behind the scenes. It returns an ElementFinder which resolves to the object you are looking for:

    var project;
    element(by.id('foo')).evaluate('project').then(function(value) {
        project = value;
    });
    

    The documentation says:

    which resolves to the evaluated expression for each underlying element. The result will be resolved as in webdriver.WebDriver.executeScript. In summary - primitives will be resolved as is, functions will be converted to string, and elements will be returned as a WebElement.

    Also, check out Accessing Angular inside Protractor Test

    Edit: syntax error