我正在自动化我的应用程序的 UI 测试。在某些情况下,我希望我的测试脚本关闭当前浏览器并通过打开新浏览器运行下一个测试。问题是我无法弄清楚如何在实习生中打开新的浏览器窗口。 remote.get(URL)
没有做我想做的事情。有人可以帮忙吗?
我已经更新了我的问题以包含代码。不过,我的问题非常简单。 如何使用测试内部的实习生打开新的浏览器窗口?。 不过如果你想看代码请评论,我会把它写下来。 谢谢。
// in tests/functional/index.js
define([
'intern!object',
'intern/chai!assert',
'Automation/ConfigFiles/dataurl',
'Automation/pages/login/loginpage',
'intern/dojo/node!fs',
'intern/dojo/node!leadfoot/helpers/pollUntil'
], function (registerSuite, assert, dataurl, LoginPage, fs, pollUntil) {
registerSuite(function () {
var loginPage;
var values;
return {
setup: function () {
var data = fs.readFileSync(loginpage, 'utf8');
json = JSON.parse(data);
values = json.values;
loginPage = new LoginPage(this.remote, json.locator);
return this.remote
.get(require.toUrl(json.locator.URL)).setFindTimeout(60000000000).sleep(5000)
},
beforeEach:function() {
// here i want to open new window
},
'valid loginname lands to password page':function () {
loginPage.submitLoginName(values.unamevalue);
loginPage.isPasswordPageDisplayed().then(function(isPasswordPageDisplayed) {
assert.true(isPasswordPageDisplayed, 'password page is not displayed, Invalid Login name');
})
},
'successful login': function () {
loginPage
.login(values.unamevalue, values.pwdvalue)
loginPage.isLoginSuccess().then(function (loginSuccess) {
assert.isTrue(loginSuccess, 'Login Failed');
});
},
afterEach: function () {
return this.remote.closeCurrentWindow()
}
};
});
});
最佳答案
您可以使用window.open
打开一个新窗口。诀窍是您想在远程浏览器中运行该命令。 Intern(技术上是 Leadfoot)为您提供了两种在 this.remote
上执行此操作的方法:execute
和 executeAsync
。例如,要简单地打开一个新窗口,您可以执行以下操作:
this.remote.execute(function () {
window.open();
})
打开新窗口后,您需要切换到它才能与其交互。脚本可能类似于:
var windowHandles;
this.remote
.getAllWindowHandles()
.then(function (handles) {
windowHandles = handles;
})
.execute(function () { window.open() })
.sleep(500)
.getAllWindowHandles()
.then(function (handles) {
// compare the new handles to windowHandles to figure out which
// is the new window, then switch to it
return this.remote.switchToWindow(newWindowHandle);
})
.get('some_new_url')
// rest of test
关于javascript - 如何在实习生js中打开新的浏览器窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41879398/