Skip to content

Commit

Permalink
[6.x] Add assertNotPresent() assertion (#879)
Browse files Browse the repository at this point in the history
* add `assertNotPresent()`

* Fix code styling
  • Loading branch information
GingerNinjaNicko authored Feb 23, 2021
1 parent c35a844 commit ea56e24
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Concerns/MakesAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,24 @@ public function assertPresent($selector)
return $this;
}

/**
* Assert that the element matching the given selector is not present.
*
* @param string $selector
* @return $this
*/
public function assertNotPresent($selector)
{
$fullSelector = $this->resolver->format($selector);

PHPUnit::assertTrue(
is_null($this->resolver->find($selector)),
"Element [{$fullSelector}] is present."
);

return $this;
}

/**
* Assert that the element matching the given selector is not visible.
*
Expand Down
31 changes: 31 additions & 0 deletions tests/MakesAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,37 @@ public function test_assert_present()
}
}

public function test_assert_not_present()
{
$driver = m::mock(stdClass::class);
$element = m::mock(stdClass::class);

$resolver = m::mock(stdClass::class);
$resolver->shouldReceive('format')->with('foo')->andReturn('body foo');
$resolver->shouldReceive('find')->with('foo')->andReturn(
null,
null
);
$resolver->shouldReceive('format')->with('bar')->andReturn('body bar');
$resolver->shouldReceive('find')->with('bar')->andReturn(
$element,
null
);

$browser = new Browser($driver, $resolver);

$browser->assertNotPresent('foo');

try {
$browser->assertNotPresent('bar');
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString(
'Element [body bar] is present.',
$e->getMessage()
);
}
}

public function test_assert_missing_and_element_is_displayed()
{
$driver = m::mock(stdClass::class);
Expand Down

0 comments on commit ea56e24

Please sign in to comment.