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

javascript - How to use then() and get the value in Cypress - Stack Overflow

programmeradmin0浏览0评论

I have a span element which value is 2. I would like to check if the value is greater than 0, but after checked online and implemented every method, it did not work...

Here is the console when I log the $span

I understand that Cypress works asynchronously, so I use .then() to get the text of element. How can I get the value of 2 and do the follow if-else?

HTML

<div>
  <span class="badge ml-1 badge-primary">2</span>
</div>
   cy.get(".badge.ml-1.badge-primary").then(($span)=> {
     if($span.text().includes(0)) {
      doFunction1()
    } else {
      cy.get(xxxxx)
    }
 )}

I have a span element which value is 2. I would like to check if the value is greater than 0, but after checked online and implemented every method, it did not work...

Here is the console when I log the $span

I understand that Cypress works asynchronously, so I use .then() to get the text of element. How can I get the value of 2 and do the follow if-else?

HTML

<div>
  <span class="badge ml-1 badge-primary">2</span>
</div>
   cy.get(".badge.ml-1.badge-primary").then(($span)=> {
     if($span.text().includes(0)) {
      doFunction1()
    } else {
      cy.get(xxxxx)
    }
 )}
Share Improve this question asked Sep 14, 2021 at 15:36 OSWOSW 1073 silver badges10 bronze badges 1
  • What do you get when you write this cy.get(".badge.ml-1.badge-primary").invoke('text').then((text) => {cy.log(text)}) – Alapan Das Commented Sep 15, 2021 at 5:21
Add a ment  | 

3 Answers 3

Reset to default 6

Do it by chaining conversions methods to go from from element -> text -> number

cy.get(".badge.ml-1.badge-primary")
  .invoke('text')                    // to text
  .then(text => +text)               // to number
  .then(value => {
    if(value > 0) { 
      doFunction1()
    } else {
      cy.get(xxxxx)
    }
  })

Waiting for value 2

cy.get(".badge.ml-1.badge-primary")
  .invoke('text')                    // to text
  .then(text => +text)               // to number
  .should('eq', 2)                   // wait for value 2
  .then(() => {
    doFunction1()                    // now doFunction
  })

What you asked for is to check if content of a span is GRATER THAN 0. So it will require some additional apprach than using includes. For example you may read a value as a variable and check if it is grater than 0:

 cy.get(".badge.ml-1.badge-primary").then(($span)=> {
     const value = $span.text(); // now you know what the value is
     if(value > 0) { // actual check if > 0. Beware, implicit coersion here! read more below.
      doFunction1()
    } else {
      cy.get(xxxxx)
    }
 )}

The value variable, however, is a string. My code works since JS would convert string to number automatically. Depending on your use-case, you might want to make sure it is a valid number and not some other string, which can't be converted to a number (like "2s"). For example, if the value is "2s", than the value > 0 check would be falsy, which might result in a bug in your code.

 cy.get(".badge.ml-1.badge-primary").then(($span)=> {
     const value = Number.parseInt($span.text()); // now you know what the value is
     if(!Number.isNaN(value) && value > 0) { // value is now of type number, but it may be NaN! (yes, NaN is also a number)
      doFunction1()
    } else {
      cy.get(xxxxx)
    }
 )}

First make sure that the selector that you are using is unique, so that is shouldn't fetch an unintended value. Secondly you can wait with a custom timeout in case your webpage takes some time to populate the value and then apply your if-esle condition. Now instead of 4 seconds(cypress default timeout), the should mand will re-try for 7 seconds to check that the element has the inner text value of 2.

cy.get(".badge.ml-1.badge-primary", { timeout: 7000 }).should("have.text", "2")

cy.get(".badge.ml-1.badge-primary")
  .invoke(text)
  .then((num) => {
    if (+num > 0) {
      doFunction1()
    } else {
      cy.get(xxxxx)
    }
  })

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>