使用量角器访问 $scope 对象

Access $scope object with protractor

我得到了一个像这样的对象:

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

为了调试它,我进入 repl 模式并尝试查看“项目”有什么。 当我如下定义项目变量并调用它时,它 returns 我的对象,但是当我尝试访问它的键 (project.name) 时,我得到了未定义。如果我这样做 Object.keys(project) 我将获得页面对象方法,如 click、getAttribute 等

关于如何访问原始对象键的任何想法?

视图:

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

测试方:

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

evaluate 在幕后使用 executeScript。它 returns 一个 ElementFinder 解析为您要查找的对象:

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

documentation 说:

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.

另外,查看 Accessing Angular inside Protractor Test

编辑:语法错误