nightwatch icon indicating copy to clipboard operation
nightwatch copied to clipboard

Suppressing error for further chained `.find()` commands

Open dikwickley opened this issue 5 months ago • 8 comments

Fixes: #4079

Solves the third point from this comment (https://github.com/nightwatchjs/nightwatch/issues/3991#issuecomment-1969444042)

Further chained .find() commands should not throw the Timeout or any other error again. Ex. in browser.element('.invalid-selector').find('selector').getText();, there should only be one error due to browser.element() and .find() should not throw an error again (suppress the error for it).

Thanks in advance for your contribution. Please follow the below steps in submitting a pull request, as it will help us with reviewing it quicker.

  • [x] Create a new branch from master (e.g. features/my-new-feature or issue/123-my-bugfix);
  • [x] If you're fixing a bug also create an issue if one doesn't exist yet;
  • [x] If it's a new feature explain why do you think it's necessary. Please check with the maintainers beforehand to make sure it is something that we will accept. Usually we only accept new features if we feel that they will benefit the entire community;
  • [x] Please avoid sending PRs which contain drastic or low level changes. If you are certain that the changes are needed, please discuss them beforehand and indicate what the impact will be;
  • [x] If your change is based on existing functionality please consider refactoring first. Pull requests that duplicate code will most likely be ignored;
  • [x] Do not include changes that are not related to the issue at hand;
  • [x] Follow the same coding style with regards to spaces, semicolons, variable naming etc.;
  • [ ] Always add unit tests - PRs without tests are most of the times ignored.

dikwickley avatar Mar 12 '24 11:03 dikwickley

Status

  • ❌ No modified files found in the types directory. Please make sure to include types for any changes you have made. Thank you!.

github-actions[bot] avatar Mar 12 '24 11:03 github-actions[bot]

@garg3133 is this a good solution to suppress the error for chained find commands

dikwickley avatar Mar 12 '24 11:03 dikwickley

Can you add a test to show that this works as anticipated

AutomatedTester avatar Mar 21 '24 11:03 AutomatedTester

Hey, Have you tried the solution on some test cases ? I ran this over a test case which is browser.element.find('.invalid_selector').getProperty('innerHTML');

In my case it still didn't suppress the further errors.

[Testing Chain Commands] Test Suite ────────────────────────────────────────────────────────────────────── ℹ Connected to GeckoDriver on port 4444 (1150ms). Using: firefox (124.0) on MAC.

Running sample test for debug: ─────────────────────────────────────────────────────────────────────────────────────────────────── Error Timed out while waiting for element "By(css selector, .invalid_selector)" to be present for 5000 milliseconds.

Error location:
/Users/arjunchikara/Desktop/nightwatch/lib/api/web-element/scoped-element.js:336
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
 334 | function createNarrowedError({error, condition, timeout}) {
 335 |   return error.name === 'TimeoutError'
 336 |     ? new Error(`Timed out while waiting for element "${condition}" to be present for ${timeout} milliseconds.`) 
 337 |     : error;
 338 | }
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

Error   Error while running .getElementProperty() protocol action: The element with the reference null is not known in the current browsing context

FAILED: 1 errors (5.089s)

chikara1608 avatar Mar 22 '24 08:03 chikara1608

hey @chikara1608 The example you posted is not a further find command, there is only one single find. see this https://github.com/nightwatchjs/nightwatch/issues/3991#issuecomment-1969444042

try it for such an example

browser.element('.invalid_selector').find('.some_other_selector')

dikwickley avatar Mar 22 '24 08:03 dikwickley

Can you add a test to show that this works as anticipated

hey @AutomatedTester how can I add such test case that checks for the errors thrown. can you point to some test that checks for the error thrown (I tried looking in web-element tests but couldn't find)

dikwickley avatar Mar 22 '24 08:03 dikwickley

ohh my bad! So if I got this right, the solution would basically suppress the error thrown by find in case there was an error in the command before it right ? But it would still throw error in the command after the find command (for now at-least) right ? For ex : browser.element('.invalid-selector').find('selector').getText();
This test case throws error at .element as well as .getText but suppresses the find error.

chikara1608 avatar Mar 22 '24 09:03 chikara1608

ohh my bad! So if I got this right, the solution would basically suppress the error thrown by find in case there was an error in the command before it right ? But it would still throw error in the command after the find command (for now at-least) right ? For ex : browser.element('.invalid-selector').find('selector').getText(); This test case throws error at .element as well as .getText but suppresses the find error.

Yes correct

dikwickley avatar Mar 22 '24 11:03 dikwickley