使用Protractor专注于angular4弹出窗口

时间:2017-06-02 18:40:08

标签: protractor qa

我已经用Google搜索了几个小时,而且我非常难以让我的UI测试专注于弹出窗口。在这个阶段,我很乐意只需点击" PayBill",等待弹出窗口(立即加载),然后点击取消。

我的代码的最新版本为:

paybillButton.click();
browser.sleep(500);

browser.getAllWindowHandles().then(function (handles) {
  newWindowHandle = handles[1];
  browser.switchTo().window(newWindowHandle).then(function () {
    cancelButton.click();
  });
});

但它在browser.switchTo().window(newWindowHandle).then ...

之间不断失败

错误:

Failed: null value in entry: handle=null
WebDriverError: null value in entry: handle=null
    at Object.checkLegacyResponse (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:505:15)
    at parseHttpResponse (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:509:13)
    at doSend.then.response (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:440:13)
    at process._tickCallback (internal/process/next_tick.js:103:7)
From: Task: WebDriver.switchTo().window(undefined)
    at thenableWebDriverProxy.schedule (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver.js:816:17)
    at TargetLocator.window (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver.js:1755:25)

我还尝试了Failed: null value in entry: name=null error while switching Popup window ...

中的以下代码
function windowCount(count) {
  return function () {
    return browser.getAllWindowHandles().then(function (handles) {
      return handles.length >= count;
    });
  };
};
browser.wait(windowCount(2), 10000);

browser.getAllWindowHandles().then(function (handles) {
  browser.switchTo().window(handles[1]);
  cancelButton.click();
});

但那会返回这个错误:

Failed: Wait timed out after 10002ms
TimeoutError: Wait timed out after 10002ms
    at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2107:17
    at ManagedPromise.invokeCallback_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1366:14)
    at TaskQueue.execute_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2970:14)
    at TaskQueue.executeNext_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2953:27)
    at asyncRun (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2813:27)
    at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:676:7
    at process._tickCallback (internal/process/next_tick.js:103:7)
From: Task: <anonymous wait>
    at scheduleWait (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2094:20)
    at ControlFlow.wait (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2408:12)

1 个答案:

答案 0 :(得分:0)

switchTo方法是selenium-webdriver switchTo方法的包装器。 switchTo方法返回TargetLocator对象,TargetLocator对象没有window方法。正确的方法是使用frame method

browser.getAllWindowHandles().then((handles) => {
  let newWindowHandle = handles[1];
  browser.switchTo().frame(newWindowHandle).then(() => {
    cancelButton.click();
  });
});