Благодаря Джонасу Штауденмейру я смог найти хорошее решение.
Я написал функцию для выбора одного элемента элемента select, указав класс или идентификатор элемента и текст элемента для выбора.
Я также написал функцию для проверки, является ли конкретный элемент
Это может выглядеть немного быстро и грязно и может улучшить его, но я счастлив на данный момент.
выбор функции:
/**
* 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;
}
}
}
подтверждение выбранной функции:
/**
* 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;
}
Несколько слов о последней функции:
Чтобы использовать собственные сумерки, вам нужно создать собственный браузер.Я следовал этому руководству .Самые важные шаги здесь:
- Создайте класс
DuskBrowser
, который расширяет Browser
- Включите в него свою пользовательскую функцию подтверждения.
Переопределить функцию protected
newBrowser
.Его единственная работа - вернуть нового DuskBrowser.
protected function newBrowser($driver)
{
return new DuskBrowser($driver);
}