別のウィンドウにフォーカスを変更します。

フォーカスを変更するウィンドウは、サーバーによって割り当てられたウィンドウハンドルで指定する必要があります。ウィンドウハンドルを確認するには、`window.getAllHandles()`コマンドを使用してください。

使用方法

                    .window.switchTo(windowHandle, [callback])
                

module.exports = {
 'switch to another window': function (browser) {
    browser
      .navigateTo('https://nightwatch.dokyumento.jp/__e2e/window/')
      .click('#openWindowBttn')
      .waitUntil(function () {
        // wait until window handle for the new window is available
        return new Promise((resolve) => {
          browser.window.getAllHandles(function (result) {
            resolve(result.value.length === 2);
          });
        });
      })
      .perform(async function () {
        const originalWindow = await browser.window.getHandle();
        const allWindows = await browser.window.getAllHandles();

        // loop through to find the new window handle
        for (const windowHandle of allWindows) {
          if (windowHandle !== originalWindow) {
            await browser.window.switchTo(windowHandle);
            break;
          }
        }

        const currentWindow = await browser.window.getHandle();
        browser.assert.notEqual(currentWindow, originalWindow);
      });
  },

  'switch to another window with ES6 async/await': async function (browser) {
    await browser.navigateTo('https://nightwatch.dokyumento.jp/__e2e/window/');
    await browser.click('#openWindowBttn');

    // wait until window handle for the new window is available
    await browser.waitUntil(async function () {
      const windowHandles = await browser.window.getAllHandles();

      return windowHandles.length === 2;
    });

    const originalWindow = await browser.window.getHandle();
    const allWindows = await browser.window.getAllHandles();

    // loop through available windows to find the new window handle
    for (const windowHandle of allWindows) {
      if (windowHandle !== originalWindow) {
        await browser.window.switchTo(windowHandle);
        break;
      }
    }

    const currentWindow = await browser.window.getHandle();
    await browser.assert.notEqual(currentWindow, originalWindow);
  }
}

パラメータ

名前 説明
windowHandle 文字列

` .window.getAllHandles()`の呼び出しで返された文字列のいずれかである、サーバーによって割り当てられたウィンドウハンドル。

callback
オプション
関数

コマンドが完了したときに呼び出されるオプションのコールバック関数。

参照

W3C WebDriver 仕様