最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cypress split string for test - Stack Overflow

programmeradmin2浏览0评论

i need to parse a string in cypress. i am trying to grab a piece of string with the methods provided by javascript. but it doesn't work gives me an error:

type i have this string:

text2 = "change status id 423 in Cult"
text1 = 423

var id = text2.split (' ')
expect (text1).to.eq(id[3])

how can i do such a thing in cypress. I should take part of a string and pare it but with the split method it gives me an error

below the error returned:

i need to parse a string in cypress. i am trying to grab a piece of string with the methods provided by javascript. but it doesn't work gives me an error:

type i have this string:

text2 = "change status id 423 in Cult"
text1 = 423

var id = text2.split (' ')
expect (text1).to.eq(id[3])

how can i do such a thing in cypress. I should take part of a string and pare it but with the split method it gives me an error

below the error returned:

Share Improve this question edited Nov 12, 2020 at 9:50 bigeyes asked Nov 12, 2020 at 8:55 bigeyesbigeyes 1191 gold badge2 silver badges12 bronze badges 8
  • Pls add the error so we can debug the situation – Rosen Mihaylov Commented Nov 12, 2020 at 9:00
  • Edit: First - it seems you arenet selecting any text Second: It seems like it fails because it is a JQuery element. Try using cy.wrap(text2).split(' ')> – Rosen Mihaylov Commented Nov 12, 2020 at 9:26
  • @bigeyes What do you get when you use cy.log(text2) – Alapan Das Commented Nov 12, 2020 at 9:35
  • I updated the post image @AlapanDas with cy.log(text2) is highlighted in purple – bigeyes Commented Nov 12, 2020 at 9:41
  • if i put cy.wrap(text2).split(' ') get the following error cy.wrap(...).split is not a function @RosenMihaylov – bigeyes Commented Nov 12, 2020 at 9:42
 |  Show 3 more ments

1 Answer 1

Reset to default 2

You have to use then() instead of should() -

cy.get('p.text-lg').invoke('text').then((text) => {
    var splitText = text.split(' ')[5]
    expect(splitText).to.equal(291)
})
发布评论

评论列表(0)

  1. 暂无评论