包括外部文件 'signalr.min.js' window 未定义
Including external file 'signalr.min.js' window is not defined
版本
- OpenTest --版本 1.2.2
- Chrome 驱动程序 85.0.4183.87
包括我需要的外部 javascript 文件总是会抛出如下内容:
Caused by: java.lang.RuntimeException: Failed to evaluate JavaScript code at line number 1. The script content was:
$include(["signalr.min.js", "signalr.tests.js"]);
...
Caused by: java.lang.RuntimeException: Failed to run script file signalr.min.js
...
Caused by: javax.script.ScriptException: ReferenceError: "window" is not defined in <eval> at line number 1
我遵循了以下文档:
- https://getopentest.org/reference/javascript-api.html#includeincludesource
- https://getopentest.org/docs/scripting-support.html#external-script-files
但是 none 我尝试的组合似乎有效,我想我遗漏了一些东西:(
我已经为 actor.yml 配置了 chrome 网络驱动程序,如下所示:
# Selenium options
selenium:
# seleniumServerUrl: http://127.0.0.1:9515
desiredCapabilities:
browserName: chrome
chromeOptions:
args: [ --start-maximized ]
chromeDriverExePath: C:/Webdrivers/chromedriver.exe
chromeDriverExeArgs: [--start-maximized, --ignore-certificate-errors, --disable-popup-blocking, --disable-extensions-file-access-check, "--test-name", "--disable-extensions", "--disable-notifications", "--disable-web-security", "--disable-prompt-on-repost", "--browser-test", "--dom-automation", "--enable-caret-browsing", "--expose-internals-for-testing", "--force-login-manager-in-tests" ]
相关测试示例:
description: My Test
actors:
- actor: ACTOR1
segments:
- segment: 1
actions:
- description: include external SignalR script files
script: |
$include(["signalr.min.js", "signalr.tests.js"]);
- description: Initialize SignalR Connection
action: org.getopentest.selenium.ExecuteScript
args:
scriptArgs: $data("config").webChatUrl + $data("config").apiV1SignalRChathub
script: |
var hubUrl = arguments[0];
SignalRConnectionTest(hubUrl);
signalr.min.js 可在以下位置找到:https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/3.1.7/signalr.min.js
所以在经过大量的试验和错误之后,我设法让一些东西工作。我从来没有让 script-includes 在 OpenTest 中工作。我认为这是因为浏览器未打开(尚未)时存在问题,因此 window
将只是未定义。所以基本上你只能包含不使用 DOM...
的脚本
我所做的是创建一个运行某些 javascript 的“本地”html 页面,并通过一些检查从 OpenTest yml 调用该页面。这是我所做的一个例子:
description: My Test
actors:
- actor: ACTOR1
segments:
- segment: 1
actions:
- description: Initialize Browser
action: org.getopentest.selenium.NavigateTo
args:
url: $data("config").someExternalUrlIsAlive
- description: Get Test Page File-Path
script: |
var File = Java.type("java.io.File");
var URLEncoder = Java.type("java.net.URLEncoder");
var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
var testPage = new File("..\test-repo\scripts\testpage.html");
var testPageUrl = testPage.getAbsolutePath();
var argument = $data("config").someProperty;
var argumentUrlEncoded = URLEncoder.encode(argument, StandardCharsets.UTF_8.toString());
$sharedData.testpage = testPageUrl + "?arg=" + argumentUrlEncoded;
$log("The test-page path is " + $sharedData.testpage);
- description: Navigate to Test Page
action: org.getopentest.selenium.NavigateTo
args:
url: $sharedData.testpage
- description: Start Test
action: org.getopentest.selenium.Click
args:
locator: { id: btnTest }
- description: Give the test some time to complete
script: |
var Thread = Java.type("java.lang.Thread");
Thread.sleep(3000); // sleep 3 seconds.
- description: Read Test Page Result
action: org.getopentest.selenium.GetElements
args:
locator: { id: txtResult }
$localData:
resultElements: $output.elements
- description: Validate the Test Page Results
script: |
var txtResult = $localData.resultElements[0];
var testResult = txtResult.getAttribute("value")
if (testResult !== "200") {
$fail(
$format(
"We expected a valid connection result, but we got: {0}",
testResult
)
);
}
版本
- OpenTest --版本 1.2.2
- Chrome 驱动程序 85.0.4183.87
包括我需要的外部 javascript 文件总是会抛出如下内容:
Caused by: java.lang.RuntimeException: Failed to evaluate JavaScript code at line number 1. The script content was:
$include(["signalr.min.js", "signalr.tests.js"]);
...
Caused by: java.lang.RuntimeException: Failed to run script file signalr.min.js
...
Caused by: javax.script.ScriptException: ReferenceError: "window" is not defined in <eval> at line number 1
我遵循了以下文档:
- https://getopentest.org/reference/javascript-api.html#includeincludesource
- https://getopentest.org/docs/scripting-support.html#external-script-files
但是 none 我尝试的组合似乎有效,我想我遗漏了一些东西:(
我已经为 actor.yml 配置了 chrome 网络驱动程序,如下所示:
# Selenium options
selenium:
# seleniumServerUrl: http://127.0.0.1:9515
desiredCapabilities:
browserName: chrome
chromeOptions:
args: [ --start-maximized ]
chromeDriverExePath: C:/Webdrivers/chromedriver.exe
chromeDriverExeArgs: [--start-maximized, --ignore-certificate-errors, --disable-popup-blocking, --disable-extensions-file-access-check, "--test-name", "--disable-extensions", "--disable-notifications", "--disable-web-security", "--disable-prompt-on-repost", "--browser-test", "--dom-automation", "--enable-caret-browsing", "--expose-internals-for-testing", "--force-login-manager-in-tests" ]
相关测试示例:
description: My Test
actors:
- actor: ACTOR1
segments:
- segment: 1
actions:
- description: include external SignalR script files
script: |
$include(["signalr.min.js", "signalr.tests.js"]);
- description: Initialize SignalR Connection
action: org.getopentest.selenium.ExecuteScript
args:
scriptArgs: $data("config").webChatUrl + $data("config").apiV1SignalRChathub
script: |
var hubUrl = arguments[0];
SignalRConnectionTest(hubUrl);
signalr.min.js 可在以下位置找到:https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/3.1.7/signalr.min.js
所以在经过大量的试验和错误之后,我设法让一些东西工作。我从来没有让 script-includes 在 OpenTest 中工作。我认为这是因为浏览器未打开(尚未)时存在问题,因此 window
将只是未定义。所以基本上你只能包含不使用 DOM...
我所做的是创建一个运行某些 javascript 的“本地”html 页面,并通过一些检查从 OpenTest yml 调用该页面。这是我所做的一个例子:
description: My Test
actors:
- actor: ACTOR1
segments:
- segment: 1
actions:
- description: Initialize Browser
action: org.getopentest.selenium.NavigateTo
args:
url: $data("config").someExternalUrlIsAlive
- description: Get Test Page File-Path
script: |
var File = Java.type("java.io.File");
var URLEncoder = Java.type("java.net.URLEncoder");
var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
var testPage = new File("..\test-repo\scripts\testpage.html");
var testPageUrl = testPage.getAbsolutePath();
var argument = $data("config").someProperty;
var argumentUrlEncoded = URLEncoder.encode(argument, StandardCharsets.UTF_8.toString());
$sharedData.testpage = testPageUrl + "?arg=" + argumentUrlEncoded;
$log("The test-page path is " + $sharedData.testpage);
- description: Navigate to Test Page
action: org.getopentest.selenium.NavigateTo
args:
url: $sharedData.testpage
- description: Start Test
action: org.getopentest.selenium.Click
args:
locator: { id: btnTest }
- description: Give the test some time to complete
script: |
var Thread = Java.type("java.lang.Thread");
Thread.sleep(3000); // sleep 3 seconds.
- description: Read Test Page Result
action: org.getopentest.selenium.GetElements
args:
locator: { id: txtResult }
$localData:
resultElements: $output.elements
- description: Validate the Test Page Results
script: |
var txtResult = $localData.resultElements[0];
var testResult = txtResult.getAttribute("value")
if (testResult !== "200") {
$fail(
$format(
"We expected a valid connection result, but we got: {0}",
testResult
)
);
}