Nightwatch.js-如何在新的浏览器选项卡上进行断言

问题描述 投票:0回答:1

我必须测试一种情况。用户输入所需的URL到与按钮链接的文本。当用户单击此按钮时,会将用户定向到键入的URL。

当我对新标签页(Google)进行断言时,输出始终类似于“ localhost:8080”,这是我的上一个标签页(currentPage)。

我想我应该将新标签设置为“ this”,但是我无法弄清楚。如果您能帮助我,我将不胜感激。

linkTest.spec.js

module.exports = {
tags: ['component', 'linkTest'],
before: function(client, done) {
this.currentPage = client.maximizeWindow().page.linkTestPage();
this.currentPage
  .navigate(client.globals.devUrl)
  .waitForElementVisible('body', 60000)
  .customPerform(function() {
    done();
  });
 },

'CASE ID - Case Name'() {
let googleUrl = 'https://www.google.com/';

this.currentPage
  .checkInitialElements()
  .setExternalUrlText(googleUrl)
  .clickLinkLabel();

// Handle with new tab
this.client.windowHandles(function(result) {
  let googleTab = result.value[1]; // New Google tab index
  this.switchWindow(googleTab) // Switch to Google tab
    .assert.urlContains('Google'); // Verify new tab is Google
  });
},

after: function(client, done) {
client.end().customPerform(done);
 },
};

customPerform.js

  exports.command = function(fn, callback) {
  this.perform(fn);
  if (typeof callback === 'function') {
  callback.call(self);
  }
  return this; // allows the command to be chained.
 };
selenium selenium-webdriver nightwatch.js nightwatch
1个回答
1
投票

[不确定您是否已经知道如何执行此操作,但是我能够通过的方式是将新页面的断言放入switchWindow函数的回调函数中。因此,在您的示例中,

// Handle with new tab
this.client.windowHandles(function(result) {
  let googleTab = result.value[1]; // New Google tab index
 // Switch to Google tab
  this.switchWindow(googleTab, function() {
    this.assert.urlContains('Google'); // Verify new tab is Google
  });  
});
© www.soinside.com 2019 - 2024. All rights reserved.