nightwatchjs / nightwatch

Integrated end-to-end testing framework written in Node.js and using W3C Webdriver API. Developed at @browserstack
https://nightwatchjs.org
MIT License
11.79k stars 1.31k forks source link

"stale element reference" error sometimes results in unhandled rejections. #3982

Open garg3133 opened 9 months ago

garg3133 commented 9 months ago

Description of the bug/issue

If we send a request on /execute/sync endpoint and it returns the below response:

Response 404 POST /session/820fa3e285be4a9b4419478cb341447c/execute/sync (10ms)
   {
     value: {
       error: 'stale element reference',
       message: 'stale element reference: stale element not found\n' +
         '  (Session info: chrome=120.0.6099.129)',
       stacktrace: ''
     }
  }

Then Nightwatch exits with the below error (while the browser stays open):

Error
   unhandledRejection: stale element reference: stale element not found
  (Session info: chrome=120.0.6099.129)
StaleElementReferenceError: stale element reference: stale element not found
  (Session info: chrome=120.0.6099.129)
    at Object.throwDecodedError (/Users/priyansh/Projects/test_projects/create-nightwatch-demo/yyyy/node_modules/selenium-webdriver/lib/error.js:524:15)
    at parseHttpResponse (/Users/priyansh/Projects/test_projects/create-nightwatch-demo/yyyy/node_modules/selenium-webdriver/lib/http.js:601:13)
    at Executor.execute (/Users/priyansh/Projects/test_projects/create-nightwatch-demo/yyyy/node_modules/selenium-webdriver/lib/http.js:529:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Driver.execute (/Users/priyansh/Projects/test_projects/create-nightwatch-demo/yyyy/node_modules/selenium-webdriver/lib/webdriver.js:745:17)

It should be noted that the error is handled properly if it happens on some /elements endpoint, like below:

# Request/Response
Request POST /session/a3c2b6a1f32d8e288b366f25eec95c54/element/3ACAFAB1F78948D78EE676688D83C048_element_8/elements  
   { using: 'css selector', value: 'option[value =Mission]' }
Response 404 POST /session/a3c2b6a1f32d8e288b366f25eec95c54/element/3ACAFAB1F78948D78EE676688D83C048_element_8/elements (12ms)
   {
     value: {
       error: 'stale element reference',
       message: 'stale element reference: stale element not found\n' +
         '  (Session info: chrome=120.0.6099.129)',
       stacktrace: ''
     }
  }

gives following handled error/failure: image

Steps to reproduce

  1. Go to '...'
  2. Click on '...'
  3. Scroll down to '...'
  4. See error

Sample test

// [Works only sometimes]

const {Select} = require('selenium-webdriver');

describe('Dropdown demo', function() {
  it('Demo dropdown', async function(browser) {
    browser.navigateTo('https://www.radimaging.co.uk/about')

    const selectWebElement = await browser.element('.btn-rad');

    const select = new Select(selectWebElement);

    await select.selectByValue('Mission');
  });

  after(browser => browser.end());
});

Command to run

No response

Verbose Output

[Ecosia.org Demo] Test Suite
────────────────────────────────────────────────────────
⠋ Starting ChromeDriver on port 9515...
 Starting ChromeDriver...
Selenium Manager binary found at /Users/priyansh/Projects/test_projects/create-nightwatch-demo/yyyy/node_modules/selenium-webdriver/bin/macos/selenium-manager
Driver path: /Users/priyansh/.cache/selenium/chromedriver/mac-arm64/120.0.6099.109/chromedriver
Browser path: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
(node:43974) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
⠙ Starting ChromeDriver on port 9515...
   Request POST /session  
   {
     capabilities: {
       firstMatch: [ {} ],
       alwaysMatch: {
         browserName: 'chrome',
         'goog:chromeOptions': {
           binary: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
         }
       }
     }
⠙ Starting ChromeDriver on port 9515...
   Response 200 POST /session (798ms)
   {
     value: {
       capabilities: {
         acceptInsecureCerts: false,
         browserName: 'chrome',
         browserVersion: '120.0.6099.129',
         chrome: {
           chromedriverVersion: '120.0.6099.109 (3419140ab665596f21b385ce136419fde0924272-refs/branch-heads/6099@{#1483})',
           userDataDir: '/var/folders/mp/mcnk1cdj7r348d0zcx279n0h0000gp/T/.org.chromium.Chromium.cZoavD'
         },
         'fedcm:accounts': true,
         'goog:chromeOptions': { debuggerAddress: 'localhost:64279' },
         networkConnectionEnabled: false,
         pageLoadStrategy: 'normal',
         platformName: 'mac',
         proxy: {},
         setWindowRect: true,
         strictFileInteractability: false,
         timeouts: { implicit: 0, pageLoad: 300000, script: 30000 },
         unhandledPromptBehavior: 'dismiss and notify',
         'webauthn:extension:credBlob': true,
         'webauthn:extension:largeBlob': true,
         'webauthn:extension:minPinLength': true,
         'webauthn:extension:prf': true,
         'webauthn:virtualAuthenticators': true
       },
       sessionId: '820fa3e285be4a9b4419478cb341447c'
     }
ℹ Connected to ChromeDriver on port 9515 (901ms).
  Using: chrome (120.0.6099.129) on MAC.

 Received session with ID: 820fa3e285be4a9b4419478cb341447c

 → Running [before]:

 → Running command: navigateTo ('https://www.ecosia.org/')
   Request POST /session/820fa3e285be4a9b4419478cb341447c/url  
   { url: 'https://www.ecosia.org/' }
   Response 200 POST /session/820fa3e285be4a9b4419478cb341447c/url (930ms)
   { value: null }
 → Completed command: navigateTo ('https://www.ecosia.org/') (932ms)
 → Completed [before].

  Running Demo test ecosia.org:
───────────────────────────────────────────────────────────────────────────────────────────────────
 → Running [beforeEach]:
 → Completed [beforeEach].

 → Running command: navigateTo ('https://www.radimaging.co.uk/about')
   Request POST /session/820fa3e285be4a9b4419478cb341447c/url  
   { url: 'https://www.radimaging.co.uk/about' }
   Response 200 POST /session/820fa3e285be4a9b4419478cb341447c/url (2571ms)
   { value: null }
 → Completed command: navigateTo ('https://www.radimaging.co.uk/about') (2571ms)

 → Running command: element.find ()
   Request POST /session/820fa3e285be4a9b4419478cb341447c/elements  
   { using: 'css selector', value: '.btn-rad' }
   Response 200 POST /session/820fa3e285be4a9b4419478cb341447c/elements (5ms)
   {
     value: [
       {
         'element-6066-11e4-a52e-4f735466cecf': 'DFB6DA3ADAFD2A4B9861F15A378FA640_element_8'
       }
     ]
  }
 → Completed command: element.find () (5ms)
   Request POST /session/820fa3e285be4a9b4419478cb341447c/execute/sync  
   {
     script: '/* getAttribute */return (function(){return (function(){var h=this||self;function aa(a){return"string"==typeof a}function ba(a,b){a=a.split(".");var c=h;a[0]in c||"undefined"==typeof c.execScript||c.e... (43206 characters)',
     args: [
       {
         'element-6066-11e4-a52e-4f735466cecf': 'DFB6DA3ADAFD2A4B9861F15A378FA640_element_8',
         ELEMENT: 'DFB6DA3ADAFD2A4B9861F15A378FA640_element_8'
       },
       'tagName'
     ]
  }
   Request POST /session/820fa3e285be4a9b4419478cb341447c/execute/sync  
   {
     script: '/* getAttribute */return (function(){return (function(){var h=this||self;function aa(a){return"string"==typeof a}function ba(a,b){a=a.split(".");var c=h;a[0]in c||"undefined"==typeof c.execScript||c.e... (43206 characters)',
     args: [
       {
         'element-6066-11e4-a52e-4f735466cecf': 'DFB6DA3ADAFD2A4B9861F15A378FA640_element_8',
         ELEMENT: 'DFB6DA3ADAFD2A4B9861F15A378FA640_element_8'
       },
       'multiple'
     ]
  }
   Response 404 POST /session/820fa3e285be4a9b4419478cb341447c/execute/sync (10ms)
   {
     value: {
       error: 'stale element reference',
       message: 'stale element reference: stale element not found\n' +
         '  (Session info: chrome=120.0.6099.129)',
       stacktrace: ''
     }
  }
  Error
   unhandledRejection: stale element reference: stale element not found
  (Session info: chrome=120.0.6099.129)
StaleElementReferenceError: stale element reference: stale element not found
  (Session info: chrome=120.0.6099.129)
    at Object.throwDecodedError (/Users/priyansh/Projects/test_projects/create-nightwatch-demo/yyyy/node_modules/selenium-webdriver/lib/error.js:524:15)
    at parseHttpResponse (/Users/priyansh/Projects/test_projects/create-nightwatch-demo/yyyy/node_modules/selenium-webdriver/lib/http.js:601:13)
    at Executor.execute (/Users/priyansh/Projects/test_projects/create-nightwatch-demo/yyyy/node_modules/selenium-webdriver/lib/http.js:529:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Driver.execute (/Users/priyansh/Projects/test_projects/create-nightwatch-demo/yyyy/node_modules/selenium-webdriver/lib/webdriver.js:745:17)

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

  ️TEST FAILURE (4.738s): 
   - 1 error during execution; 
   - 0 tests failed; 
   - 0/NA tests passed

Nightwatch Configuration

No response

Nightwatch.js Version

latest

Node Version

No response

Browser

No response

Operating System

No response

Additional Information

No response

uditrajput03 commented 6 months ago

I am unable to recreate it, It passes everytime