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

Allow assertion of available options on a given select element #195

Merged
merged 2 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/Concerns/MakesAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,66 @@ public function assertNotSelected($field, $value)
return $this;
}

/**
* Assert that the given array of values are available to be selected on the given field.
*
* @param string $field
* @param array $values
* @return $this
*/
public function assertOptionsAvailable($field, array $values)
{
PHPUnit::assertCount(
count($values),
$this->resolver->resolveSelectOptions($field, $values),
"Expected options [".implode(',', $values)."] for select [{$field}] to be available, but it wasn't."
);

return $this;
}

/**
* Assert that the given array of values are not available to be selected on the given field.
*
* @param string $field
* @param array $values
* @return $this
*/
public function assertOptionsNotAvailable($field, array $values)
{
PHPUnit::assertCount(
0,
$this->resolver->resolveSelectOptions($field, $values),
"Unexpected available options [".implode(',', $values)."] for select [{$field}]."
);

return $this;
}

/**
* Assert that the given value is available to be selected on the given field.
*
* @param string $field
* @param string $value
* @return $this
*/
public function assertOptionAvailable($field, $value)
{
return $this->assertOptionsAvailable($field, [$value]);
}

/**
* Assert that the given value is not available to be selected on the given field.
*
* @param string $field
* @param string $value
* @return $this
*/
public function assertOptionNotAvailable($field, $value)
{
return $this->assertOptionsNotAvailable($field, [$value]);
}

/**
* Determine if the given value is selected for the given select field.
*
Expand Down
20 changes: 20 additions & 0 deletions src/ElementResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,26 @@ public function resolveForSelection($field)
]);
}

/**
* Resolve all the options with the given value on the select field.
*
* @param string $field
* @param array $values
* @return array
*/
public function resolveSelectOptions($field, array $values)
{
$options = $this->resolveForSelection($field)
->findElements(WebDriverBy::tagName('option'));

if (empty($options))
return [];

return array_filter($options, function($option) use ($values) {
return in_array($option->getAttribute('value'), $values);
});
}

/**
* Resolve the element for a given radio "field" / value.
*
Expand Down