Skip to content

Commit

Permalink
#934 - throw an error if assertValue() / assertValueIsNot() are u…
Browse files Browse the repository at this point in the history
…sed with an element that does not support the `value` attribute
  • Loading branch information
u01jmg3 committed Oct 12, 2021
1 parent 696a641 commit 97cb71d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
42 changes: 40 additions & 2 deletions src/Concerns/MakesAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,15 @@ public function assertValue($selector, $value)
{
$fullSelector = $this->resolver->format($selector);

$actual = $this->resolver->findOrFail($selector)->getAttribute('value');
$element = $this->resolver->findOrFail($selector);
$tagName = $element->getTagName();

PHPUnit::assertTrue(
$tagName === 'textarea' || $this->supportsTheValueAttribute($tagName),
__FUNCTION__." cannot be used with the element [{$fullSelector}]."
);

$actual = $element->getAttribute('value');

PHPUnit::assertEquals(
$value,
Expand All @@ -671,7 +679,15 @@ public function assertValueIsNot($selector, $value)
{
$fullSelector = $this->resolver->format($selector);

$actual = $this->resolver->findOrFail($selector)->getAttribute('value');
$element = $this->resolver->findOrFail($selector);
$tagName = $element->getTagName();

PHPUnit::assertTrue(
$tagName === 'textarea' || $this->supportsTheValueAttribute($tagName),
__FUNCTION__." cannot be used with the element [{$fullSelector}]."
);

$actual = $element->getAttribute('value');

PHPUnit::assertNotEquals(
$value,
Expand All @@ -682,6 +698,28 @@ public function assertValueIsNot($selector, $value)
return $this;
}

/**
* Determine if the given element supports the 'value' attribute.
*
* @param string $tagName
* @return bool
*/
public function supportsTheValueAttribute($tagName)
{
return in_array(
$tagName,
[
'button',
'input',
'li',
'meter',
'option',
'param',
'progress',
]
);
}

/**
* Assert that the element matching the given selector has the given value in the provided attribute.
*
Expand Down
52 changes: 50 additions & 2 deletions tests/MakesAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,12 @@ public function test_assert_select_missing_option_and_option_empty()
$browser->assertSelectMissingOption('select[name="users"]', 1);
}

public function test_assert_value()
public function test_assert_value_using_supported_element()
{
$driver = m::mock(stdClass::class);

$element = m::mock(RemoteWebElement::class);
$element->shouldReceive('getTagName')->andReturn('textarea');
$element->shouldReceive('getAttribute')->andReturn('bar');

$resolver = m::mock(stdClass::class);
Expand All @@ -482,11 +483,12 @@ public function test_assert_value()
}
}

public function test_assert_value_is_not()
public function test_assert_value_is_not_using_supported_element()
{
$driver = m::mock(stdClass::class);

$element = m::mock(RemoteWebElement::class);
$element->shouldReceive('getTagName')->andReturn('meter');
$element->shouldReceive('getAttribute')->andReturn('bar');

$resolver = m::mock(stdClass::class);
Expand All @@ -507,6 +509,52 @@ public function test_assert_value_is_not()
}
}

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

$element = m::mock(RemoteWebElement::class);
$element->shouldReceive('getTagName')->andReturn('p');

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

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

try {
$browser->assertValue('foo', 'bar');
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString(
'assertValue cannot be used with the element [body foo].',
$e->getMessage()
);
}
}

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

$element = m::mock(RemoteWebElement::class);
$element->shouldReceive('getTagName')->andReturn('div');

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

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

try {
$browser->assertValueIsNot('foo', 'foo');
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString(
'assertValueIsNot cannot be used with the element [body foo].',
$e->getMessage()
);
}
}

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

0 comments on commit 97cb71d

Please sign in to comment.