From dfcafd60cb48f6bae90b55593682c83519a378b4 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Fri, 20 Jan 2023 14:28:38 +1300 Subject: [PATCH 1/2] FIX Correctly set options in tag field --- src/Context/BasicContext.php | 54 ++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/src/Context/BasicContext.php b/src/Context/BasicContext.php index 1ca9843..eaa78be 100644 --- a/src/Context/BasicContext.php +++ b/src/Context/BasicContext.php @@ -21,6 +21,7 @@ use Facebook\WebDriver\WebDriverAlert; use Facebook\WebDriver\WebDriverExpectedCondition; use Facebook\WebDriver\WebDriverKeys; use PHPUnit\Framework\Assert; +use PHPUnit\Framework\ExpectationFailedException; use SilverStripe\Assets\File; use SilverStripe\Assets\Filesystem; use SilverStripe\BehatExtension\Utility\StepHelper; @@ -1350,13 +1351,56 @@ JS; * * @Then /^I add "([^"]+)" to the "([^"]+)" tag field$/ * @param string $value - * @param string $locator + * @param string $selector */ - public function iAddToTheTagField($value, $locator) + public function iAddToTheTagField($value, $selector) { - $tagFieldInput = $this->getElement($locator); - $tagFieldInput->setValue($value); - $tagFieldInput->getParent()->getParent()->getParent()->getParent()->find('css', '.Select-menu-outer')->click(); + $page = $this->getSession()->getPage(); + /** @var NodeElement $parentElement */ + $parentElement = null; + $dropdown = null; + $this->retryThrowable(function () use (&$parentElement, &$page, $selector) { + $parentElement = $page->find('css', $selector); + Assert::assertNotNull($parentElement, sprintf('"%s" element not found', $selector)); + $page = $this->getSession()->getPage(); + }); + + $this->retryThrowable(function () use (&$dropdown, $parentElement, $selector) { + $dropdown = $parentElement->find('css', '.ss-tag-field__dropdown-indicator'); + Assert::assertNotNull($dropdown, sprintf('Unable to find the dropdown in "%s"', $selector)); + $dropdown->click(); + }); + + $inputField = null; + try { + // Try setting to a value already in the dropdown + $this->retryThrowable(function () use ($value, $parentElement, $selector) { + $element = $parentElement->find('xpath', sprintf('//*[count(*)=0 and .="%s"]', $value)); + Assert::assertNotNull($element, sprintf('"%s" not found in "%s"', $value, $selector)); + $element->click(); + }); + } catch (ExpectationFailedException $e) { + // Try creating a new value + $this->retryThrowable(function () use (&$inputField, $value, $parentElement, $selector) { + /** @var NodeElement $parentElement */ + $inputField = $parentElement->find('css', '.ss-tag-field__input'); + Assert::assertNotNull($inputField, sprintf('Could not create "%s" in "%s"', $value, $selector)); + // We need to type the value in - react won't accept us just setting the value via js + $inputField->focus(); + /** @var FacebookWebDriver $driver */ + $driver = $this->getSession()->getDriver(); + $keyboard = $driver->getWebDriver()->getKeyboard(); + $keyboard->sendKeys($value); + }); + + // Try selecting the 'Create "$value"' option + $this->retryThrowable(function () use ($value, $parentElement, $selector) { + $createOption = 'Create "' . $value . '"'; + $element = $parentElement->find('xpath', sprintf('//*[count(*)=0 and .=\'%s\']', $createOption)); + Assert::assertNotNull($element, sprintf('"%s" not found in "%s"', $createOption, $selector)); + $element->click(); + }); + } } /** From 73126eab5cdf0b062d4491ed97f74b7353dcfcd7 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Fri, 27 Jan 2023 15:54:42 +1300 Subject: [PATCH 2/2] FIX Emails are arrays Work missed during #232 --- src/Context/EmailContext.php | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Context/EmailContext.php b/src/Context/EmailContext.php index 78494b6..bef2257 100644 --- a/src/Context/EmailContext.php +++ b/src/Context/EmailContext.php @@ -125,10 +125,10 @@ class EmailContext implements Context $email = $this->lastMatchedEmail; $emailContent = null; - if ($email->Content) { - $emailContent = $email->Content; + if ($email['Content']) { + $emailContent = $email['Content']; } else { - $emailContent = $email->PlainContent; + $emailContent = $email['PlainContent']; } if (trim($negate ?? '')) { @@ -154,8 +154,7 @@ class EmailContext implements Context } $email = $this->lastMatchedEmail; - $emailContent = ($email->Content) ? ($email->Content) : ($email->PlainContent); - $emailPlainText = strip_tags($emailContent ?? ''); + $emailPlainText = $email['PlainContent'] ? $email['PlainContent'] : strip_tags($email['Content']); $emailPlainText = preg_replace("/\h+/", " ", $emailPlainText ?? ''); Assert::assertStringContainsString($content, $emailPlainText); @@ -219,7 +218,7 @@ class EmailContext implements Context } $match = $this->lastMatchedEmail; - $crawler = new Crawler($match->Content); + $crawler = new Crawler($match['Content']); $linkEl = $crawler->selectLink($linkSelector); Assert::assertNotNull($linkEl); $link = $linkEl->attr('href'); @@ -254,10 +253,10 @@ class EmailContext implements Context $email = $this->lastMatchedEmail; $emailContent = null; - if ($email->Content) { - $emailContent = $email->Content; + if ($email['Content']) { + $emailContent = $email['Content']; } else { - $emailContent = $email->PlainContent; + $emailContent = $email['PlainContent']; } // Convert html content to plain text $emailContent = strip_tags($emailContent ?? ''); @@ -309,9 +308,9 @@ class EmailContext implements Context $match = $this->lastMatchedEmail; if (trim($negate ?? '')) { - Assert::assertStringNotContainsString($from, $match->From); + Assert::assertStringNotContainsString($from, $match['From']); } else { - Assert::assertStringContainsString($from, $match->From); + Assert::assertStringContainsString($from, $match['From']); } } @@ -328,9 +327,9 @@ class EmailContext implements Context $match = $this->lastMatchedEmail; if (trim($negate ?? '')) { - Assert::assertStringNotContainsString($to, $match->To); + Assert::assertStringNotContainsString($to, $match['To']); } else { - Assert::assertStringContainsString($to, $match->To); + Assert::assertStringContainsString($to, $match['To']); } } @@ -348,7 +347,7 @@ class EmailContext implements Context } $email = $this->lastMatchedEmail; - $html = $email->Content; + $html = $email['Content']; $dom = new \DOMDocument(); $dom->loadHTML($html);