0

I would like to assert in Protractor that a link text is composed by the following way: text-1 (where text is a variable, and the number can be composed by any digits).

I tried the following:

browser.wait(
        ExpectedConditions.visibilityOf(
        element(by.xpath(`//a[@class = 'collapsed' and starts-with(text(), '${text}') and ends-with(text(), '-(/d+)')]`))),
 5000)

and

browser.wait(
        ExpectedConditions.visibilityOf(
        element(by.xpath(`//a[@class = 'collapsed' and starts-with(text(), '${text}') and ends-with(text(), '/^-(/d+)$/')]`))),
        5000)

Unfortunately, none of the above xpaths worked.

How can I fix this?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

0

If you change the way to declare the variable and your second predicate you can go with :

//a[@class='collapsed'][starts-with(text(),'" + text_variable + "')][number(replace(.,'^.*-(\d+)$','$1'))*0=0]

where [number(replace(.,'^.*-(\d+)$','$1'))*0=0] test for the presence of a number at the end of a string.

Example. If you have :

<p>
<a class="collapsed">foofighters-200</a>
<a class="collapsed">foofighters</a>
<a class="collapsed">boofighters-200</a>
<a class="collapsed">boofighters-200abc</a>
</p>

The following XPath :

//a[@class='collapsed'][starts-with(text(),'foo')][number(replace(.,'^.*-(\d+)$','$1'))*0=0]

will output 1 element :

<a class="collapsed">foofighters-200</a>

So in Protractor you could have :

var text = "foo";
browser.wait(ExpectedConditions.visibilityOf(element(by.xpath("//a[@class='collapsed'][starts-with(text(),'" + text + "')][number(replace(.,'^.*-(\d+)$','$1'))*0=0]"))), 5000);
...
E.Wiest
  • 5,425
  • 2
  • 7
  • 12
0

You can use regexp for this:

await browser.wait(async () => {
    return new RegExp('^.*(\d+)').test(await $('a.collapsed').getText());
}, 20000, 'Expected link text to contain number at the end');

Tune this regex here if needed:

https://regex101.com/r/9d9yaJ/1

Xotabu4
  • 3,063
  • 17
  • 29