简体   繁体   English

为什么不是在Protractor中使用executeScript创建的对象

[英]Why isn't an object created using executeScript in Protractor

I have this code inside a describe in a protractor script that tests an angular js app. 我在一个测量角度js应用程序的量角器脚本中的describe中有这个代码。

beforeEach(function() {
  browser.get('index.html#/device_list');
  browser.executeScript("chrome.bluetooth = {};");
  browser.executeScript("console.log('test')");
  browser.executeScript("alert('test')");
});

Without this code the tests run and fail as expected. 如果没有此代码,测试将按预期运行和失败。 When I add this code and run the script the object isn't created, the console log isn't printed but an alert is created (and throws an async error as expected). 当我添加此代码并运行脚本时,不会创建对象,不会打印控制台日志,但会创建警报(并按预期引发异步错误)。

I have tried it without the alert and their is still no object or log. 我已经尝试过没有警报但它们仍然没有对象或日志。

Why is this? 为什么是这样? Is it possible to create a simple object which can be used by a test using executeScript in protractor? 是否可以创建一个简单的对象,可以在量角器中使用executeScript进行测试? If not is there another way of doing it that doesn't involve creating a service? 如果没有,那么还有另一种不涉及创建服务的方法吗?

I'm using this code inside the Protractor script to look for the console log. 我在Protractor脚本中使用此代码来查找控制台日志。

browser.manage().logs().get('browser').then(function(browserLog) {
    if(browserLog.length > 0) {
        console.log(browserLog);
    }
});

Logging only shows error-level logs. 日志记录仅显示错误级别日志。

You'd need to do: 你需要这样做:

browser.executeScript("console.error('test')");

executeScript runs in the browser. executeScript在浏览器中运行。 If you are expecting to see the log in your node process it will not be there. 如果您希望在节点进程中看到日志,那么它将不在那里。 It I will in you browser's log. 我会在你浏览器的日志中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM