6

I'm trying to get a text from a modal on Chrome. Using the console, I can get the inner text as follows:

document.querySelector('.my-form > a').innerText
// returns http://a-url.com

Now, on my test, I can evaluate the element using

const myText = Selector('.my-form > a').innerText;
await t
  .expect(myText).contains('url');

and I can even click on that URL

await t.click(myText);

but I cannot put that inner text to a variable, for instance. I tried using a ClientFunction from this post

const getUrl = ClientFunction(() => document.querySelector('.my-form > a').innerText);

test('My Test', async t => {
const text = await getUrl();
console.log(text);
});

// this results in 
// TypeError: Cannot read property 'innerText' of null

and tried using a plain Selector as this post suggests

const text = Selector('.my-form > a').innerText;
const inner = await text.textContent;
console.log(inner);

// prints: undefined

How to extract a text from an element? I understand that t.selectText is limited in this scenario, right?

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
thenewjames
  • 966
  • 2
  • 14
  • 25
  • 2
    You shouldn't need a client function, Your last example is incorrect, did you try: ```const text = await Selector('.my-form >a').innerText;``` ? Note if there are multiple ```a``` tags in your form you will get all the text – ioseph Mar 22 '19 at 03:12
  • It works! @ioseph Do you want to answer so I can accept your answer? – thenewjames Mar 22 '19 at 13:21

2 Answers2

9

From the documentation you want:

const text = await Selector('.my-form > a').innerText;

ioseph
  • 1,887
  • 20
  • 25
  • Excellent, thank you. I kept wrapping my selector to try and clean up the text I was getting. This worked great. – Daniel Oct 26 '21 at 21:31
1

That will do the trick:

const paragraph      = Selector("p").withText("possible entries");
const extractEntries = await paragraph.textContent;