From 5c4a04864fbfe62c82fa457b8e6c38410a163b12 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Thu, 7 Sep 2023 10:23:44 +1200 Subject: [PATCH 1/2] ENH Migrate image selection logic from asset-admin (#248) --- src/Context/FixtureContext.php | 64 ++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/Context/FixtureContext.php b/src/Context/FixtureContext.php index c5dba7c..2222b5e 100644 --- a/src/Context/FixtureContext.php +++ b/src/Context/FixtureContext.php @@ -885,6 +885,70 @@ YAML; return Injector::inst()->get(AssetStore::class); } + /** + * Selects the first match of $select in the given HTML editor (tinymce) + */ + protected function selectInTheHtmlField(string $select, string $field) + { + $inputField = $this->getHtmlField($field); + $inputField->getParent()->find('css', 'iframe')->click(); + $inputFieldId = $inputField->getAttribute('id'); + $js = <<getMainContext()->getSession()->executeScript($js); + } + + /** + * Selects the first image match in the HTML editor (tinymce) + * + * @When /^I select the image "([^"]+)" in the "([^"]+)" HTML field$/ + * @param string $filename + * @param string $field + */ + public function iSelectTheImageInHtmlField($filename, $field) + { + $this->selectInTheHtmlField("img[src*='$filename']", $field); + } + + /** + * Locate an HTML editor field + * + * @param string $locator Raw html field identifier as passed from + * @return NodeElement + */ + protected function getHtmlField($locator) + { + $locator = str_replace('\\"', '"', $locator ?? ''); + $page = $this->getMainContext()->getSession()->getPage(); + $element = $page->find('css', 'textarea.htmleditor[name=\'' . $locator . '\']'); + if ($element) { + return $element; + } + $label = $page->findAll('xpath', sprintf('//label[contains(text(), \'%s\')]', $locator)); + if (!empty($label)) { + Assert::assertCount(1, $label, "Found more than one element containing the phrase \"$locator\""); + $label = array_shift($label); + $fieldId = $label->getAttribute('for'); + $element = $page->find('css', '#' . $fieldId); + } + Assert::assertNotNull($element, sprintf('HTML field "%s" not found', $locator)); + return $element; + } + /** * Converts a natural language class description to an actual class name. * Respects {@link DataObject::$singular_name} variations. From 5ea2f157b0775ad5eb27a293753edaeb61468feb Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Tue, 12 Sep 2023 13:03:49 +1200 Subject: [PATCH 2/2] NEW Assert element attributes --- src/Context/BasicContext.php | 11 +++++++++++ src/Context/FixtureContext.php | 12 +++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Context/BasicContext.php b/src/Context/BasicContext.php index 2ed1dd6..abacd57 100644 --- a/src/Context/BasicContext.php +++ b/src/Context/BasicContext.php @@ -545,6 +545,17 @@ JS; $this->iDismissTheDialog(); } + /** + * @Then /^the "([^"]+)" element "([^"]+)" attribute should be "([^"]*)"$/ + */ + public function theElementAttributeShouldBe($selector, $attribute, $value) + { + $page = $this->getSession()->getPage(); + $element = $page->find('css', $selector); + Assert::assertNotNull($element, sprintf('Element %s not found', $selector)); + Assert::assertEquals($value, $element->getAttribute($attribute)); + } + /** * @Given /^I see the text "([^"]+)" in the alert$/ * @param string $expected diff --git a/src/Context/FixtureContext.php b/src/Context/FixtureContext.php index 2222b5e..34724ae 100644 --- a/src/Context/FixtureContext.php +++ b/src/Context/FixtureContext.php @@ -30,6 +30,7 @@ use SilverStripe\Security\Group; use SilverStripe\Security\Member; use SilverStripe\Security\Permission; use SilverStripe\Versioned\Versioned; +use SilverStripe\Core\Config\Config; /** * Context used to create fixtures in the SilverStripe ORM. @@ -659,7 +660,16 @@ class FixtureContext implements Context // Add the extension to the CLI context /** @var Extensible $targetClass */ - $targetClass = $this->convertTypeToClass($class); + try { + $targetClass = $this->convertTypeToClass($class); + } catch (InvalidArgumentException $e) { + // will end up here if the class is not a subclass of DataObject + if (class_exists($class)) { + $targetClass = $class; + } else { + throw $e; + } + } $targetClass::add_extension($extension); // Write config for this extension too...