nw.js 测试用例 官方文档 nw.js 测试
上面的 nw.js 测试用例是用 python 编写的。 我在 JavaScript 中编写了相同的内容。我遇到错误,并且很难访问 nw.exe 应用程序 Web 元素。 nw.exe 与浏览器类似,但我发现很难在 selenium webdriver 和 nw.exe 应用程序之间建立连接。
require('chromedriver');
const {Builder, By, Key} = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
let options = new chrome.Options();
options.setChromeBinaryPath('dist/src/nw.exe'); // mentioning the PATH to NW.exe
async function openNwExe(){
let driver = new Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.build();
await driver.get('src/views/index.html'); //to open the front page of the app
try{
//trying find the input box element and sending the key values
await driver.findElement(By.id("input")).sendKeys("San Francisco");
}
finally{
// Close the WebDriver session and exit the NW.js application
await driver.sleep(1000);
await driver.quit();
}
}
openNwExe(); //function call
如果我运行上面的脚本,我会收到以下错误
C:\Users\..\node_modules\selenium-webdriver\lib\error.js:524
let err = new ctor(data.message) ^
SessionNotCreatedError: session not created: Chrome failed to start: exited normally.
(session not created: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:\Users\..\src\src.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
at Object.throwDecodedError (C:\Users\..\node_modules\selenium-webdriver\lib\error.js:524:15)
at parseHttpResponse (C:\Users\..\node_modules\selenium-webdriver\lib\http.js:601:13)
at Executor.execute (C:\Users\..\node_modules\selenium-webdriver\lib\http.js:529:28)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
remoteStacktrace: '\tGetHandleVerifier [0x00007FF6F8F27892+54818]\n' +
'\t(No symbol) [0x00007FF6F8E96AC2]\n' +
'\t(No symbol) [0x00007FF6F8D4DA3B]\n' +
'\t(No symbol) [0x00007FF6F8D7C202]\n' +
'\t(No symbol) [0x00007FF6F8D7802E]\n' +
'\t(No symbol) [0x00007FF6F8DB67FB]\n' +
'\t(No symbol) [0x00007FF6F8DAE883]\n' +
'\t(No symbol) [0x00007FF6F8D83691]\n' +
'\t(No symbol) [0x00007FF6F8D848D4]\n' +
'\tGetHandleVerifier [0x00007FF6F928B992+3610402]\n' +
'\tGetHandleVerifier [0x00007FF6F92E1860+3962352]\n' +
'\tGetHandleVerifier [0x00007FF6F92D9D4F+3930847]\n' +
'\tGetHandleVerifier [0x00007FF6F8FC3646+693206]\n' +
'\t(No symbol) [0x00007FF6F8EA1628]\n' +
'\t(No symbol) [0x00007FF6F8E9D934]\n' +
'\t(No symbol) [0x00007FF6F8E9DA62]\n' +
'\t(No symbol) [0x00007FF6F8E8E113]\n' +
'\tBaseThreadInitThunk [0x00007FF97A62257D+29]\n' +
'\tRtlUserThreadStart [0x00007FF97B94AA78+40]\n'
}
感谢任何帮助!
将
nwapp
参数添加到 chrome.Options
。指定 NW.js 二进制文件的路径时,请确保 chromedriver 二进制文件位于同一目录中。我还添加了相应的 package.json
和 index.html
。
在此示例中,我假设
test.js
、index.html
和 package.json
位于同一目录中:
/path/to/nw-test/index.html
/path/to/nw-test/package.json
/path/to/nw-test/test.py
测试.js
const {Builder, By} = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
options.addArguments([
"nwapp=/path/to/nw/project"
]);
options.setChromeBinaryPath('/path/to/nw.exe');
async function openNwExe(){
let driver = new Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.build();
try{
await driver.findElement(By.id("input")).sendKeys("San Francisco");
}
finally{
await driver.sleep(1000);
await driver.quit();
}
}
openNwExe(); //function call
index.html
<!DOCTYPE html>
<html>
<head>
<title>NW.js Selenium Example</title>
</head>
<body>
<input id="input" value=""/>
</body>
</html>
package.json
{
"name": "nw-selenium-javascript-example",
"main": "index.html"
}
p.s.:以下是如何在 Python 和 javaScript 中使用
ServiceBuilder
API 的示例: