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

Add isPresent command. #4216

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

dikwickley
Copy link
Contributor

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.

The isPresent command was quite tricky as there were few edge cases we had to solve.
The main one being when the element in not present, we don't want to show the Timeout Error if there is a isPresent command chained (which should ideally return false).

I solved this by checking if the next sibling of a find command is an elementIsPresent command. It still takes 5 seconds to try to resolve the element.

If this approach looks good, I can add some tests for this too.

Sanity for elements that are not present:
Screenshot 2024-06-03 at 1 24 56 AM

  • Before marking your PR for review, please test and verify your changes by making appropriate modifications to any of the Nightwatch example tests (present in examples/tests directory of the project) and running them. ecosia.js and duckDuckGo.js are good examples to work with.
  • Create a new branch from master (e.g. features/my-new-feature or issue/123-my-bugfix);
  • If you're fixing a bug also create an issue if one doesn't exist yet;
  • 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;
  • 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;
  • If your change is based on existing functionality please consider refactoring first. Pull requests that duplicate code will most likely be ignored;
  • Do not include changes that are not related to the issue at hand;
  • 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.

Copy link

github-actions bot commented Jun 2, 2024

Status

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

@pujagani
Copy link
Member

pujagani commented Jun 4, 2024

There are some failing tests. Not sure if it is due to the changes made here. Can you help take a look? Thank you!

@dikwickley
Copy link
Contributor Author

@pujagani yes, fixed those. There was a missing check for outofbounds index.

@AutomatedTester
Copy link
Member

I think the test that is failing is flakey.

I can't see a test for this change can we add one?

@dikwickley
Copy link
Contributor Author

@garg3133 @AutomatedTester ready for review!

Copy link
Member

@garg3133 garg3133 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Posted a few reviews below. Otherwise, looks good!

Comment on lines +293 to +296
if (args[0]?.suppressNotFoundErrors) {
this._suppressNotFoundErrors = true;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be moved to the top of function.

@@ -172,7 +176,7 @@ class ScopedWebElement {

return await this.findElement({parentElement, selector: condition, index, timeout, retryInterval});
} catch (error) {
if (suppressNotFoundErrors) {
if (this.suppressNotFoundErrors ?? suppressNotFoundErrors) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to have two suppressNotFoundErrors, just keep the this.suppressNotFoundErrors.

If suppressNotFoundErrors comes out to be true here, set this._suppressNotFoundErrors to true and then no need to pass the suppressNotFoundErrors property in this.findElementAction.

Comment on lines +593 to +598
} catch (error) {
if (error.name === 'NoSuchElementError') {
return false; // Element does not exist
}
throw error; // Re-throw other errors for further handling
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what cases will this catch block be hit?

Because the webElement in the new Element API is designed to never actually throw an error in normal cases (including when element is not found), I don't think try-catch is required here. For other errors, they would just be thrown directly.

Is there any case that I am missing here in which this catch block will be useful?

const common = require('../../../../common.js');
const Element = common.require('element/index.js');

describe('element().isPresent() command', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are passing even if we set the suppressNotFoundErrors property to false in the isPresent.js file. We must also include at least one test here which fails in such a case to make sure that our unit tests are testing what they are supposed to test, that we do not throw an error in case of isPresent() command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants