nodejs selenium-webdriver检查新打开的选项卡的当前URL

时间:2018-04-03 11:02:11

标签: node.js selenium-webdriver

我正在使用mocha和Node.js selenium-webdriver测试Firefox网络扩展。

该扩展程序会在https://example.com/处向该页面添加一个按钮,点击该按钮会打开2个新标签https://example.com/a/https://example.com/b/

我想让扩展程序修改这些页面的内容,但首先我要先检查其网址。

以下是我的测试的相关部分和三次(在众多中)不成功的尝试:

// test.js
const expect = require('chai').expect;
const webdriver = require('selenium-webdriver');

const By = webdriver.By;
const until = webdriver.until;
const Button = webdriver.Button;
const Key = webdriver.Key;

describe('my extension', function(){

  let driver;

  this.timeout(15000);

  before(async function(){
    // setting up driver with extension
  });

  after(function(){
    driver.quit();
  });

  describe('open tab button', function(){
    it('opens 2 tabs when button is clicked', async function(){
      // getting https://example.com/
      // clicking button
      await driver.getAllWindowHandles().then(function(windowHandles) {
        expect(windowHandles.length).to.equal(3); // PASS

        let tab1 = windowHandles[0];
        let tab2 = windowHandles[1];
        let tab3 = windowHandles[2];

        // CODE FOR UNSUCCESSFULL ATTEMPTS (SEE BELOW)
      });
    });
  });
});

尝试#1

driver.switchTo().window(tab1).then(() => {
  driver.getCurrentUrl().then(url => {console.log('current url: "' + url + '"');});
}); 

driver.switchTo().window(tab2).then(() => {
  driver.getCurrentUrl().then(url => {console.log('current url: "' + url + '"');});
}); 

driver.switchTo().window(tab3).then(() => {
  driver.getCurrentUrl().then(url => {console.log('current url: "' + url + '"');});
}); 

我得到了

current url: "https://example.com/"
current url: "https://example.com/"
current url: "https://example.com/"

尝试#2

driver.switchTo().window(tab1).then(() => {console.log('switched to tab1')});
driver.sleep(250);
driver.getCurrentUrl().then(url => {console.log('current url: "' + url + '"');});

driver.switchTo().window(tab2).then(() => {console.log('switched to tab2')});
driver.sleep(250);
driver.getCurrentUrl().then(url => {console.log('current url: "' + url + '"');});

driver.switchTo().window(tab3).then(() => {console.log('switched to tab3')});
driver.sleep(250);
driver.getCurrentUrl().then(url => {console.log('current url: "' + url + '"');});

我得到了

current url: "https://example.com/"

1次传球(7s)

current url: "https://example.com/"
current url: "https://example.com/"

switched to tab1
switched to tab2
switched to tab3

尝试#3

driver.switchTo().window(tab1).then(() => {
  console.log('switched to tab1');
  driver.getCurrentUrl().then(url => {console.log('current url: "' + url + '"');});
});

driver.switchTo().window(tab2).then(() => {
  console.log('switched to tab2');
  driver.getCurrentUrl().then(url => {console.log('current url: "' + url + '"');});
});

driver.switchTo().window(tab3).then(() => {
  console.log('switched to tab3');
  driver.getCurrentUrl().then(url => {console.log('current url: "' + url + '"');});
});

我得到了

switched to tab1
switched to tab2
switched to tab3
(node:26698) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:46707
(node:26698) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:26698) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:46707
(node:26698) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:46707

我在尝试了许多其他事情后陷入困境,尽可能多地使用Google搜索。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

唯一的解决方案"到目前为止,我发现在写作时将selenium-webdriver降级到之前的稳定版本3.6.0

npm install --save-dev selenium-webdriver@3.6.0

然后以下代码起作用:

driver.switchTo().window(tab1).then(() => {
  driver.getCurrentUrl().then(url => {console.log('current url: "' + url + '"');});
}); 

driver.switchTo().window(tab2).then(() => {
  driver.getCurrentUrl().then(url => {console.log('current url: "' + url + '"');});
}); 

driver.switchTo().window(tab3).then(() => {
  driver.getCurrentUrl().then(url => {console.log('current url: "' + url + '"');});
});