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

[6.x] Add assertNotPresent() assertion #879

Merged
merged 2 commits into from
Feb 23, 2021
Merged

[6.x] Add assertNotPresent() assertion #879

merged 2 commits into from
Feb 23, 2021

Conversation

GingerNinjaNicko
Copy link
Contributor

I recently ran into a use case where I needed to ensure an array of inputs would definitely submit on the page, and another array of inputs definitely were not.

assertPresent() was exactly what I needed, but I could not find an inverse of it.
assertMissing() checked that the input was visible, but some of the inputs are hidden and replaced via JavaScript, so this doesn't work.

Therefore, I added an assertNotPresent() assertion for this specific use-case.
I did think of naming it assertAbsent() but think the distinction between this and assertMissing() could become confusing...

This is the relevant part of the test I wanted to write:

collect($this->data['support_staff_only_fields'])->each(
    fn($field) => $browser->assertPresent("[name='{$field}']")
);
collect($this->data['franchisee_only_fields'])->each(
    fn($field) => $browser->assertNotPresent("[name='{$field}']")
);

@driesvints driesvints changed the title add assertNotPresent() assertion [6.x] Add assertNotPresent() assertion Feb 23, 2021
@taylorotwell
Copy link
Member

So under what circumstances is something "not present"? When it is in the source but not visible? Some other scenario?

@GingerNinjaNicko
Copy link
Contributor Author

GingerNinjaNicko commented Feb 23, 2021

Hi Taylor,

No, something is "not present" when it cannot be found in the source and therefore won't be visible.

Currently, there are the following assertions which descriptions from the docs:

  • assertVisible(): Assert that the element matching the given selector is visible
  • assertPresent(): Assert that the element matching the given selector is present

To ensure something is not visible I can use:

  • assertMissing(): Assert that the element matching the given selector is not visible

But there is no method I can see that allows me to ensure an element is not present

In my case, I am checking the presence of inputs, some of which may be hidden & replaced by javascript for a WYSIWYG editor.
I need to ensure that some of these inputs do not appear in the source, so they don't post along with the form.

@taylorotwell
Copy link
Member

assertMissing checks that something is either not visible OR missing entirely from the source. Will that not be sufficient for your use case?

@GingerNinjaNicko
Copy link
Contributor Author

That was the behaviour I was expecting, but unfortunately, that's not the case.
It only checks that the element is not displayed.

This is the assertMissing function code:

public function assertMissing($selector)
{
    $fullSelector = $this->resolver->format($selector);

    try {
        $missing = ! $this->resolver->findOrFail($selector)->isDisplayed();
    } catch (NoSuchElementException $e) {
        $missing = true;
    }

    PHPUnit::assertTrue(
        $missing,
        "Saw unexpected element [{$fullSelector}]."
    );

    return $this;
}

@GingerNinjaNicko
Copy link
Contributor Author

Rather than create a breaking change by modifying assertMissing I think an extra function would make more sense.

@taylorotwell
Copy link
Member

@GingerNinjaNicko but in the very code you posted, it looks like it catches the exception for a totally missing element and also sets that to $missing = true?

@GingerNinjaNicko
Copy link
Contributor Author

Ah yes apologies @taylorotwell, I think you were right with your first comment but I got a little confused...

The difference is what causes the assertion to fail.

  • assertMissing: passes when the element is not visible, even if it exists within the source.
  • assertNotPresent: fails when the element is not present in the source and so also not visible.

Hopefully I'm making more sense now.

@SjorsO
Copy link
Contributor

SjorsO commented Feb 23, 2021

Maybe assertSourceMissingSelector is a better name for this new method? There is already an assertSourceMissing assertion, but that accepts a string instead of a selector.

@taylorotwell
Copy link
Member

@SjorsO yeah that might be a good idea... @GingerNinjaNicko could you rename the method to that and put it by the other assertSourceMissing method?

@taylorotwell
Copy link
Member

Eh assertNotPresent is fine I guess.

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.

3 participants