无法使用NightwatchJS关闭浏览器 [英] Not able to close the browser using NightwatchJS
问题描述
我正在尝试使用Nightwatch打开URL,此后我将无法关闭浏览器.
I am trying to open my url using Nightwatch and I wan't able to close the browser afterwards.
我尝试使用超时,以及browser.end()
或browser.closeWindow()
.他们似乎都不适合我的网址.
I tried using timeouts, as well as browser.end()
, or browser.closeWindow()
. None of them seem to be working for my url.
module.exports = {
'Demo test mywrkouts' : function (browser) {
browser.url('https://www.mywrkouts.com/workouts/search')
browser.timeouts('script', 10000, function(result) {
browser.end();
console.log("Test result"+result);
});
//browser.closeWindow();
}
};
它会打开页面,但不会关闭浏览器.我正在使用chromedriver
的Chrome浏览器.我希望关闭该窗口,但是不起作用.
It opens the page, but doesn't close the browser. I am using Chrome browser with chromedriver
. I am expecting to close the window, but it doesn't work.
任何建议都值得赞赏.
推荐答案
LE :就像我在下面广泛描述的那样, 您无需在测试结束 (通过browser.end()
),因为Nightwatch测试运行程序会在每个功能文件的末尾为您执行此操作.
LE: Like I extensibly described below, you don't need to explicitly close the browser at the end of the test (via browser.end()
) as the Nightwatch test-runner does that for you at the end of each feature-file.
但是,如果需要执行一些 teardown操作,然后显式关闭会话,请使用after
(或afterEach
)钩子进行操作.尝试以下代码段:
But, if you need to do some teardown operations and then explicitly close the session, do it in an after
(or afterEach
) hook. Try the following snippet:
module.exports = {
before(browser) {
browser.maximizeWindow();
},
'My Wrkouts Test': (browser) => {
browser.url('https://www.mywrkouts.com/');
// Check if the website logo is visible:
browser.expect.element('#barbell-homepage-top-image-desktop img.app-bar-desktop-logo').to.be.visible;
// Check the articles heading text:
browser.expect.element('h3.blog-carousel-title.primary-blue-text.center').text.to.contain('Foundational Education Series');
},
after(browser, done) {
browser.end(() => {
console.info('*--*--*--*--*--*--*--*--*--*--*--*--*');
console.info('*-- Clossing session... Good bye! --*');
console.info('*--*--*--*--*--*--*--*--*--*--*--*--*');
done();
});
}
};
无论如何,我觉得您对NightwatchJS/WebdriverIO/Protractor(或其他任何基于Webdriver的测试解决方案)处理browser
会话的方式感到困惑.
Anyways, I feel you are confusing the way NightwatchJS/WebdriverIO/Protractor (or any other Webdriver-based test solution) is handling a browser
session.
首先,您不必担心会关闭活动会话. Nightwatch在每个测试功能文件的末尾为您完成此操作.因此,假设运行三个测试套件(login.js
,register.js
,forgot_password.js
),将会依次生成&关闭三个不同的browser
会话.
First off, you need not worry about closing the active session. Nightwatch does it for you at the end of each test feature-file. Thus, running a suit of let's say three test suites (login.js
, register.js
, forgot_password.js
) will sequentially spawn & close three different browser
sessions.
此外, browser.closeWindow()仅用于关闭window
实例( 考虑到您有多个与同一个browser
会话关联的窗口).除非您已切换到另一个window
实例(在测试运行期间先前打开的 ),否则它将不会关闭主window
.
Also, browser.closeWindow() is only used for closing a window
instance (taking into account that you have multiple windows associated with the same browser
session). It won't close your main window
, unless you have switched to another window
instance (which was previously opened during your test run).
如果在测试过程中使用 browser.end(),那么您基本上会终止活动会话,从而使功能文件中的以下逻辑无效:
If you use browser.end() in the middle of your test, then you basically kill the active session, nullifying the following logic from your feature-file:
INFO Request: DELETE /wd/hub/session/4a4bb4cb1b38409ee466b0fc8af78101
- data:
- headers: {"Content-Length":0,"Authorization":"Basic Z29wcm86YmM3MDk2MGYtZGE0Yy00OGUyLTk5MGMtMzA5MmNmZGJhZTMz"}
INFO Response 200 DELETE /wd/hub/session/4a4bb4cb1b38409ee466b0fc8af78101 (56ms) { sessionId: '4a4bb4cb1b38409ee466b0fc8af78101',
status: 0,
value: null }
LOG → Completed command end (57 ms)
之后的所有内容都将如下所示:
Everything after will look like this:
INFO Response 404 POST /wd/hub/session/null/elements (11ms) { sessionId: 'null',
value:
{ error: 'invalid session id',
message: 'No active session with ID null',
stacktrace: '' },
status: 6 }
!注意:不支持您尝试做的事情,也不是常见的用例,因此在整个过程中都缺乏对此的支持 所有这些测试解决方案.
!Note: There is no support for doing what you are trying to do, nor is it a common use-case, thus the lack of support for it across all of these testing solutions.
他们说一张图片价值1000字,所以让我简单地这样说吧...您要尝试的操作与以下内容同义:
They say a picture is worth 1000 words, so let's me simply put it this way... what you are trying to do is synonymous with the following:
这篇关于无法使用NightwatchJS关闭浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!