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

expect.element().to.not.be.present throws error when using the new element() apis in v2 #2975

Closed
michal-ra opened this issue Dec 8, 2021 · 8 comments · Fixed by #2977
Closed

Comments

@michal-ra
Copy link

Describe the bug

When testing assertion that element is not present on page, error is thrown when element is not found.

Sample test

sampleTest.js

  'error thrown on missing element': function test() {
    browser.url('http://google.com');
    var nothing = element(by.css('#not-to-be-found'));

    expect(nothing).to.not.be.present;
  },

Run with command
no arguments

Verbose output

debug.log

[Validation Test] Test Suite
───────────────────────────────────────────────────────────────────────────────
⠋ Starting ChromeDriver on port 4444...
 Starting ChromeDriver with server_path=/Users/m/projects/XXX-test/node_modules/chromedriver/lib/chromedriver/chromedriver...
   Request POST /session  
   {
     desiredCapabilities: {
       browserName: 'chrome',
       w3c: false,
       acceptInsecureCerts: true,
       chromeOptions: {
         args: [ 'no-sandbox', 'ignore-certificate-errors', 'disable-infobars' ]
       },
       name: ' Validation Test',
       'goog:chromeOptions': {}
     },
     capabilities: {
       alwaysMatch: {
         browserName: 'chrome',
         acceptInsecureCerts: true,
         'goog:chromeOptions': {}
       }
     }
⠏ Starting ChromeDriver on port 4444...
   Response 200 POST /session (1501ms)
   {
     value: {
       capabilities: {
         acceptInsecureCerts: true,
         browserName: 'chrome',
         browserVersion: '96.0.4664.93',
         chrome: {
           chromedriverVersion: '96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947})',
           userDataDir: '/var/folders/31/4h8wpxl91k3brhb07p1k99qc0000gn/T/.com.google.Chrome.wq3MPd'
         },
         'goog:chromeOptions': { debuggerAddress: 'localhost:51974' },
         networkConnectionEnabled: false,
         pageLoadStrategy: 'normal',
         platformName: 'mac os x',
         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:virtualAuthenticators': true
       },
       sessionId: 'f8849d86357c9954e81dbed964db8b73'
     }
ℹ Connected to ChromeDriver on port 4444 (1545ms).
  Using: chrome (96.0.4664.93) on MAC OS X.

 Received session with ID: f8849d86357c9954e81dbed964db8b73

Running error thrown on missing element:
───────────────────────────────────────────────────────────────────────────────────────────────────
→ Running command: url (' http://google.com', )
   Request POST /session/f8849d86357c9954e81dbed964db8b73/url  
   { url: ' http://google.com' }
   Response 200 POST /session/f8849d86357c9954e81dbed964db8b73/url (1403ms)
   { value: null }
  → Completed command: url (' http://google.com', ) (1404ms)

→ Running command: element ({name, __index, __selector, locateStrategy, pseudoSelector, parent, resolvedElement})
   Request POST /session/f8849d86357c9954e81dbed964db8b73/elements  
   { using: 'css selector', value: '#not-to-be-found' }
   Response 200 POST /session/f8849d86357c9954e81dbed964db8b73/elements (13ms)
   { value: [] }
  ✖ Expected element <#not-to-be-found> to not be present - element was not found - expected "not present" but got: "error while locating the element" (15ms)
    at Object.test [as error thrown on missing element] (/Users/m/projects/XXX-test/scenarios/validation-test.js:239:5) 

  → Completed command: element ({name, __index, __selector, locateStrategy, pseudoSelector, parent, resolvedElement}) (17ms)

FAILED: 1 assertions failed, 1 errors and  1 passed (4.043s)

→ Running command: end ()
  → Completed command: sessionLog ('browser', [Function]) (13ms)
 
 → Running command: session ('delete', [Function])
   Request DELETE /session/f8849d86357c9954e81dbed964db8b73  

   Response 200 DELETE /session/f8849d86357c9954e81dbed964db8b73 (118ms)
   { value: null }
  → Completed command: session ('delete', [Function]) (120ms)
   
  → Completed command: end () (122ms)

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

  TEST FAILURE (7.896s): 
   - 1 error during execution; 
   - 1 assertions failed; 1 passed

 NoSuchElementError: Unable to locate element: #not-to-be-found using css selector
   

  ✖ 1) validation-test
 – error thrown on missing element (4.043s)
   Expected element <#not-to-be-found> to not be present - element was not found - expected "not present" but got: "error while locating the element" (15ms)
       at Object.test [as error thrown on missing element] (/Users/m/projects/XXX-test/scenarios/validation-test.js:239:5)

Configuration

nightwatch.json

no special config

Your Environment

Executable Version
nightwatch --version 2.0.0-beta.1
yarn --version 1.22.4
node --version v14.15.5
Browser driver Version
NAME VERSION
chromedriver 96
OS Version
NAME VERSION
macOS Monterey 12.0.1 (21A559)
@beatfactor beatfactor added the bug label Dec 9, 2021
@beatfactor beatfactor added this to To do in Nightwatch v2 Dec 9, 2021
@beatfactor beatfactor added this to the Nightwatch v2 milestone Dec 9, 2021
@vaibhavsingh97 vaibhavsingh97 self-assigned this Dec 10, 2021
@vaibhavsingh97 vaibhavsingh97 moved this from To do to In progress in Nightwatch v2 Jan 11, 2022
Nightwatch v2 automation moved this from In progress to Done Feb 7, 2022
gravityvi added a commit that referenced this issue Feb 8, 2022
beatfactor pushed a commit that referenced this issue Feb 9, 2022
@vaibhavsingh97 vaibhavsingh97 reopened this Feb 9, 2022
Nightwatch v2 automation moved this from Done to In progress Feb 9, 2022
@beatfactor beatfactor changed the title expect.element().to.not.be.present throws error when element is not found expect.element().to.not.be.present throws error when using the new element() apis in v2 Feb 9, 2022
@beatfactor
Copy link
Member

beatfactor commented Feb 9, 2022

This is actually only a problem when using the new element() apis that were introduced in v2. I think if you just pass the selector directly, it works as expected:

browser.url('http://google.com');
expect.element('#not-to-be-found').to.not.be.present;

@irealsourav
Copy link

irealsourav commented Mar 25, 2022

It is not working with elements described via xpaths as well in v2.
example ::

 const windoXpath='//div[contains(@id,"cdk-overlay")]';
this.expect.element(windoXpath).to.not.be.present;

error message:: 
element was not found - expected "not present" but got: "error while locating the element"

@PawelK2012
Copy link

I see the same error with nightwatch 2.0.10 when I try

browser.expect.element('#elemenetID').to.not.be.present;

Error:

Expected element <#elemenetID> to not be present - element was not found - expected "not present" but got: "error while locating the element"

I can see on screen shot that DOM element with #elemenetID is not visible.

@beckyhu
Copy link

beckyhu commented Apr 5, 2022

I got the same error with 2.0.10 by pass the selector directly:
expect.element('#not-to-be-found').to.not.be.present;
The same code works fine with 1.x.
@beatfactor

@ohanslik-vendavo
Copy link

ohanslik-vendavo commented Apr 6, 2022

I have a problem that might be related.

I am trying to upgrade nightwatch from 0.x to 2.x version right now. Unfortunately, we had some hacks implemented so I am not 100% sure my problem is caused by NW 2 but I think it is.

I am waiting for element not to be present using waitForElementNotPresent('@lockedModelIndicator') on a section in a page object.
From my debugging it seems that:

  1. if the element is not present during the first check that everything works fine
  2. if the first check fails (element is present), the next check actually looks at the parent element (which is always) present and periodically checks it until timeout.

@lukaszw82
Copy link

Experiencing same problem when upgrading from 1.7.8 to 2.1.4:
The codebase contains several hundreds tests and this problem occurs in dozens of them.

this.page = client.page.MyPage();
// command below fails: element was not found - expected "not present" but got: "error while locating the element".
this.page.expect.section('@mySelector').to.not.be.present;

@richardjimenez52
Copy link

has this been addressed/solved yet?

@oriolcastellvi
Copy link

Hello,
any news about this issue ?
Thanks.

Nightwatch v2 automation moved this from In progress to Done Jun 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging a pull request may close this issue.

10 participants