Thanks to Jonas Staudenmeir, I was able to build a nice solution.
I wrote a function to select one item of a select element by providing the class or id of the element and the text of the item to select.
I also wrote a function to test if a specific item is selected.
It may look a bit quick and dirty and may be able to improve it but I'm happy for now.
select function:
/**
* Selects an mdBootstrap select
* @param Browser $browser
* @param $method string "id" or "class"
* @param $selector string id or class of the searched select element
* @param $text string text of the select item to select
*/
public function mdbSelectByNativeSelector(Browser $browser, $method, $selector, $text)
{
$text = trim($text);
//Find the select element itself
$selects = $browser->elements(".select-wrapper");
$select = 0;
foreach ($selects as $el)
{
if ($el->findElement(WebDriverBy::tagName("select"))->getAttribute($method) == $selector)
{
$select = $el;
}
}
PHPUnit::assertTrue(is_a($select, RemoteWebElement::class), "Select with {$method} {$selector} not found!");
$select->click();
//Find the content of the select
$select_content = $select->findElement(WebDriverBy::className("dropdown-content"));
//Select the nthElement (li) of the select content.
$liElements = $select_content->findElements(WebDriverBy::tagName("li"));
foreach ($liElements as $el)
{
if ($el->getText() == $text)
{
$el->click();
return;
}
}
}
assert selected function:
/**
* Tests if an mdbSelect is selected
* @param $method string "id" or "name"
* @param $selector string the id or name of the native select element
* @param $text string the content of the selectable element (value not possible because it's flushed away)
* @return DuskBrowser
*/
public function assertMDBSelected($method, $selector, $text)
{
//Find the select element itself
$selects = $this->elements(".select-wrapper");
$select = 0;
$success = false;
foreach ($selects as $el)
{
if ($el->findElement(WebDriverBy::tagName("select"))->getAttribute($method) == $selector)
{
$select = $el;
}
}
PHPUnit::assertTrue(is_a($el, RemoteWebElement::class), "Didn't find expected native select with {$method} {$selector}.");
//Find the content of the select
$select_content = $select->findElement(WebDriverBy::className("dropdown-content"));
//Select the nthElement (li) of the select content.
$liElements = $select_content->findElements(WebDriverBy::tagName("li"));
foreach ($liElements as $el)
{
if (strpos($el->getAttribute("class"), "active") !== false)
{
//We need to ltrim because mdb inserts some problematic whitespaces in the text.
if(ltrim($el->findElement(WebDriverBy::tagName("span"))->getAttribute("innerHTML")) == $text){
$success = true;
break;
}
}
}
PHPUnit::assertTrue($success, "Select is not selected!");
return $this;
}
A few words to the last function:
To use own dusk assertions you need to create an own Browser. I followed this guide.
The most important steps here:
- Create a class
DuskBrowser
which extends Browser
- Include your custom assert function there.
Override the protected
function newBrowser
.
Its only job is to return a new DuskBrowser.
protected function newBrowser($driver)
{
return new DuskBrowser($driver);
}