SeleniumHQ / selenium-ide

Open Source record and playback test automation for the web.
https://selenium.dev/selenium-ide/
Apache License 2.0
2.78k stars 754 forks source link

no such window error in selenium-side-runner because waitForNewWindow does not wait long enough #1568

Open mdobrzanski opened 1 year ago

mdobrzanski commented 1 year ago

🐛 Bug Report

It seems there's a problem with getting new window identifier.

The test is fairly simple it:

It works fine in Selenium IDE but fails in selenium-side-runner with no such window error or precisely Operation timed out running command selectWindow:handle=${win701}:!

mike@robocop:~/Downloads$ selenium-side-runner -f "select window" select-window.side 
  console.info

          Project select-window doesn't have any suites matching filter select window,
          attempting to iterate all tests in project

      at src/main.test.ts:162:11
          at Array.forEach (<anonymous>)

info: Running test select window
  console.log
    driver: undefined

      at new WebDriverExecutor (node_modules/@seleniumhq/side-runtime/src/webdriver.ts:155:11)

info: Building driver for chrome
info: Driver has been built for chrome
  console.log
    [ 'CDwindow-6235180ACAF19DEAD557DEEA7FD12122' ]

      at WebDriverExecutor.waitForNewWindow (node_modules/@seleniumhq/side-runtime/src/webdriver.ts:295:11)

  console.warn
    Unexpected error occured during command: selectWindow-handle=${win701}- retrying...

      at CommandNode.handleTransientError (node_modules/@seleniumhq/side-runtime/src/playback-tree/command-node.ts:193:6)
      at CommandNode.retryCommand (node_modules/@seleniumhq/side-runtime/src/playback-tree/command-node.ts:164:6)
      at CommandNode.execute (node_modules/@seleniumhq/side-runtime/src/playback-tree/command-node.ts:100:20)
      at Playback._executeCommand (node_modules/@seleniumhq/side-runtime/src/playback.ts:555:22)
      at Playback._executionLoop (node_modules/@seleniumhq/side-runtime/src/playback.ts:437:18)
      at Playback._executionLoop (node_modules/@seleniumhq/side-runtime/src/playback.ts:491:14)

  console.error
    no such window
      (Session info: chrome=107.0.5304.121)

      at CommandNode.handleTransientError (node_modules/@seleniumhq/side-runtime/src/playback-tree/command-node.ts:196:15)
      at CommandNode.retryCommand (node_modules/@seleniumhq/side-runtime/src/playback-tree/command-node.ts:164:6)
      at CommandNode.execute (node_modules/@seleniumhq/side-runtime/src/playback-tree/command-node.ts:100:20)
      at Playback._executeCommand (node_modules/@seleniumhq/side-runtime/src/playback.ts:555:22)
      at Playback._executionLoop (node_modules/@seleniumhq/side-runtime/src/playback.ts:437:18)
      at Playback._executionLoop (node_modules/@seleniumhq/side-runtime/src/playback.ts:491:14)

  console.error
    Command failure: selectWindow-handle=${win701}-

      at CommandNode.handleTransientError (node_modules/@seleniumhq/side-runtime/src/playback-tree/command-node.ts:201:19)
      at CommandNode.retryCommand (node_modules/@seleniumhq/side-runtime/src/playback-tree/command-node.ts:164:6)
      at CommandNode.execute (node_modules/@seleniumhq/side-runtime/src/playback-tree/command-node.ts:100:20)
      at Playback._executeCommand (node_modules/@seleniumhq/side-runtime/src/playback.ts:555:22)
      at Playback._executionLoop (node_modules/@seleniumhq/side-runtime/src/playback.ts:437:18)
      at Playback._executionLoop (node_modules/@seleniumhq/side-runtime/src/playback.ts:491:14)

info: Finished test select window Failure
warn: Completed with failure
 FAIL  ../.nvm/versions/node/v19.0.0/lib/node_modules/selenium-side-runner/dist/main.test.js (13.755 s)
  Running project select-window
    ✕ Running test select window (12772 ms)

  ● Running project select-window › Running test select window

    Operation timed out running command selectWindow:handle=${win701}:!

      at Timeout._onTimeout (node_modules/@seleniumhq/side-runtime/src/playback-tree/command-node.ts:157:7)

  ● Running project select-window › Running test select window

    thrown: true

      at node_modules/jest-each/build/bind.js:43:11
          at Array.forEach (<anonymous>)
      at dist/main.test.js:145:22
          at Array.forEach (<anonymous>)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        13.823 s, estimated 15 s
Ran all test suites within paths "/home/mike/.nvm/versions/node/v19.0.0/lib/node_modules/selenium-side-runner/dist/main.test.js".

I did some debugging and found that the problem can be mitigated by slowing down waitForNewWindow webdriver.ts#L276

My version of the method:

async waitForNewWindow() {
        // remove me
        // sleep 2s
        await new Promise(r => setTimeout(() => r(), 2000));        

        const currentHandles = await this.driver.getAllWindowHandles();

        // remove me
        console.log('currentHandles:', currentHandles);

        return currentHandles.find((handle) => !this[state].openedWindows.includes(handle));
    }

To me it looks like some racing condition. It happens only when more js is involved on the page. I suspect the waitForNewWindow is executed before the actual window is really opened. The this.driver.getAllWindowHandles() returns only single handle because the window did not open yet.

To Reproduce

Expected behavior

Find opened window so that it can be selected.

Project file reproducing this issue (highly encouraged)

Sorry it's not the simplest test and uses https://beta.slashdb.com but that's the easiest way to expose it without setting up an artificial web server.

{
  "id": "0286a5c4-f858-11ec-ad79-43a00d6adb94",
  "version": "2.0",
  "name": "select-window",
  "url": "https://beta.slashdb.com/",
  "tests": [{
    "id": "61a0b483-9e9d-43a4-88bf-24a7a4c9e848",
    "name": "select window",
    "commands": [{
      "id": "6368dd0e-14c0ce",
      "comment": "",
      "command": "open",
      "target": "/logout",
      "targets": [],
      "value": ""
    }, {
      "id": "6368dd0e-14c109",
      "comment": "",
      "command": "runScript",
      "target": "window.localStorage.clear(); window.sessionStorage.clear();",
      "targets": [],
      "value": ""
    }, {
      "id": "6368dd0e-14c145",
      "comment": "",
      "command": "setWindowSize",
      "target": "1440x900",
      "targets": [],
      "value": ""
    }, {
      "id": "1f602837-44ae-45ab-92e0-ac94c54d7a60",
      "comment": "",
      "command": "open",
      "target": "/querystudio/all-customers?apikey=dcwteq75z5i7e6f29qi23i7xj14s205g",
      "targets": [],
      "value": ""
    }, {
      "id": "41aff869-7341-439e-88f7-c401a23290e6",
      "comment": "",
      "command": "mouseOver",
      "target": "css=.fa-play-circle",
      "targets": [],
      "value": ""
    }, {
      "id": "cea32537-68be-4d88-aba7-1272ad0dcb3e",
      "comment": "",
      "command": "click",
      "target": "css=.btn-html",
      "targets": [
        ["css=.btn-html", "css:finder"],
        ["xpath=//a[contains(text(),'html')]", "xpath:link"],
        ["xpath=//div[@id='sql-container']/execute-button/div/div/div/a", "xpath:idRelative"],
        ["xpath=//execute-button/div/div/div/a", "xpath:position"]
      ],
      "value": "",
      "opensWindow": true,
      "windowHandleName": "win701",
      "windowTimeout": 20000
    }, {
      "id": "1ddf8231-2b0b-426d-902b-720e72310b12",
      "comment": "",
      "command": "storeWindowHandle",
      "target": "root",
      "targets": [],
      "value": ""
    }, {
      "id": "384c626d-72ce-4ec0-a230-163b062e5d95",
      "comment": "",
      "command": "selectWindow",
      "target": "handle=${win701}",
      "targets": [],
      "value": ""
    }, {
      "id": "56801f4b-2de1-4322-8a33-2f1bc7473130",
      "comment": "",
      "command": "pause",
      "target": "1000",
      "targets": [],
      "value": ""
    }, {
      "id": "19cf5058-e0f3-402d-9d0a-10880e81e01d",
      "comment": "",
      "command": "close",
      "target": "",
      "targets": [],
      "value": ""
    }, {
      "id": "99764f09-39d5-489a-9c3f-7feb6248f661",
      "comment": "",
      "command": "selectWindow",
      "target": "handle=${root}",
      "targets": [],
      "value": ""
    }]
  }],
  "suites": [],
  "urls": [],
  "plugins": []
}

Environment

OS: Ubuntu 22.04.1 LTS Selenium IDE Version: 4.0.0-alpha.30 Selenium SIDE Runner Version: 4.0.0-alpha.34 Node version: v12.22.9 Browser: Chrome Browser Version: 107.0.5304.121 (Official Build) (64-bit)

mdobrzanski commented 1 year ago

I've added the project side

smildlzj commented 1 year ago

I've added the project side

https://github.com/SeleniumHQ/selenium-ide/pull/1569

image

May be this PR will help you

mdobrzanski commented 1 year ago

@smildlzj Is it possible to pass the delay to selenium-side-runner?

BTW I see it's a screenshot from selenium-ide. The homepage mentions browser extensions but there's also standalone selenium-ide. Which one is advised to use?

toddtarsi commented 1 year ago

@mdobrzanski - In January, Chrome will change the manifest such that a recording / playback interaction needs to use their dev tools. They don't support dynamic string evals, which is basically the core of v3 (an eval-based Selenium emulation). v4 will be all that remains later in January. I hope that clarifies things.