I would like to create a Behat rule that allows me to find text with wildcards, since I couldn't find anything close to it.
I sometimes have to create some contents in Drupal projects, but the title isn't defined by the user so I can't know before running the test what the title will be when saved. I.E., in my situation, I would like to check if I can see the following text in the page
The content 'N° 1', of type 'ISAI', has been created.
Here, I would like to put N°1
in a wildcard, so I only have to look for
"The content '/^[A-Z]{1}°[0-9]+$/', of type 'ISAI', has been created."
I started to create a function that looks like the following:
And I should see the text "The content 'WILDCARD', of type 'ISAI', has been created" with wildcard matching "/[a-zA-Z °]+$/"
/**
* @Then I should see the text :arg1 with wildcard matching :arg2
*/
public function iShouldSeeTheTextWithWildcardMatching($text, $regex){
$string = str_replace('WILDCARD', $regex, $text);
$result = $this->getSession()->getPage()->find('css', sprintf('div.alert-success:contains("%s")', $string));
echo $result; // is null
}
However, it fails because the selector can't find anything with "/" (since it's a regex).
My question is: is it possible to find some text with a regex, and what should I do to make my method work?
Some of the options would be:
A
Create a selector based on the 2 partial text and exclude what you would have in the wildcard.
//div[@class='alert-success'][contains(text(), 'The content')][contains(text(), 'of type \'ISAI\', has been created.')]
B
Another solution would be to see if you have a match in the text from this element.
Steps:
- define a regex
- wait for success message and get text
- check if you have a match in the saved text using the defined regex
Your string/regex can be created using variables if needed. Use echo to mage sure your selector is valid. Make sure the page is loaded.