Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switchWindow does the job, but throws unexpected error #762

Closed
gavvvr opened this issue Sep 6, 2024 · 5 comments
Closed

switchWindow does the job, but throws unexpected error #762

gavvvr opened this issue Sep 6, 2024 · 5 comments
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed investigation required
Milestone

Comments

@gavvvr
Copy link
Contributor

gavvvr commented Sep 6, 2024

Hello
I noticed this weird behavior several months ago when created a very first WDIO test for Electron-based app.

Now I know how to reproduce it:

  1. Create a new electron app: npm init electron-app@latest wdio-electron-hello-world && cd wdio-electron-hello-world
  2. Init sample WDIO setup for testing Electron app: npm init wdio@latest .
  3. Change index.js to add functionality to open a new window:
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('node:path');

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
    },
  });

  // and load the index.html of the app.
  mainWindow.loadFile(path.join(__dirname, 'index.html'));

  // Open the DevTools.
  mainWindow.webContents.openDevTools();
};

app.whenReady().then(() => {
  createWindow();
  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();
    }
  });
});

ipcMain.on('open-new-window', () => {
  const newWindow = new BrowserWindow({
    width: 400,
    height: 300,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
    },
  });

  newWindow.loadFile('src/extra.html');
});

Add the following to preload.js:

const { contextBridge, ipcRenderer } = require('electron');

// Expose ipcRenderer to the renderer process
contextBridge.exposeInMainWorld('electron', {
  openNewWindow: () => ipcRenderer.send('open-new-window')
});

Add a button to index.html for opening a new window:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello World!</title>
  </head>
  <body>
    <h1>💖 Hello World!</h1>
    <p>Welcome to your Electron application.</p>
    <button id="open-window">Open New Window</button>
    <script>
      document.getElementById('open-window').addEventListener('click', () => {
        window.electron.openNewWindow();
      });
    </script>
  </body>
</html>

Add src/extra.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Extra window</title> <!--  👈🏻 notice the different title  -->
  </head>
</html>
  1. Write a test case which switches the window and observe how window's title changes (expected ✅), while the switchWindow() function throws an error (not expected ❌)
it('should switch windows without throwing an error', async () => {
    console.log('>>>>>>>> Hello', await browser.getTitle(), 'application!')

    await $('button').click()

    const lastWindow = (await browser.getWindowHandles()).at(1)
    try {
        await browser.switchWindow(lastWindow)
    } catch (e) {
        console.error(e)
        // doing nothing... it throws, but it does switch the window 🤔
    }

    console.log('>>>>>>>> Hello', await browser.getTitle(), 'application!')
})

The error thrown is:

Error: No window found with title, url or name matching "60D914353FD09F8852B27C98E42A1460"
    at Browser.switchWindow (/wdio-electron-hello-world/node_modules/webdriverio/build/index.js:4092:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Browser.wrapCommandFn (/wdio-electron-hello-world/node_modules/@wdio/utils/build/index.js:1368:23)
    at async Context.<anonymous> (/wdio-electron-hello-world/test/specs/test.e2e.js:11:11)
    at async Context.executeAsync (/wdio-electron-hello-world/node_modules/@wdio/utils/build/index.js:1488:20)
    at async Context.testFrameworkFnWrapper (/wdio-electron-hello-world/node_modules/@wdio/utils/build/index.js:1559:14)

If switchWindow won't be wrapped with try-catch, the test will fail.

@gavvvr
Copy link
Contributor Author

gavvvr commented Sep 6, 2024

Created an example repository to easily reproduce the issue: https://github.com/gavvvr/wdio-electron-service-762

@goosewobbler goosewobbler added bug Something isn't working help wanted Extra attention is needed investigation required labels Sep 8, 2024
@goosewobbler
Copy link
Member

Might be a WDIO issue with v9:
webdriverio/webdriverio#13442

@gavvvr
Copy link
Contributor Author

gavvvr commented Sep 9, 2024

@goosewobbler I'm not sure about it 🤔.

switchWindow had the same behavior as I described since the very first time I've added wdio e2e testing to my project. Back then it was WDIO v8.35.1.

@goosewobbler
Copy link
Member

goosewobbler commented Sep 9, 2024

@gavvvr good to know. To start with, it would be useful to find out if this switchWindow behaviour is specific to the Electron service or a more general WDIO issue. Whichever it is, some window switching E2Es in the service will be beneficial - raised #771 to cover this.

@gavvvr
Copy link
Contributor Author

gavvvr commented Oct 13, 2024

My bad, in this example I was using switchWindow + handle id, instead of switchToWindow. And, by luck, it worked! (because of the bug I've just submitted to WDIO repository). switchWindow "does the job", because it's an unexpected side-effect in webdriverio.

@gavvvr gavvvr closed this as not planned Won't fix, can't repro, duplicate, stale Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed investigation required
Projects
None yet
Development

No branches or pull requests

2 participants