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

javascript - Is it possible validated blocked fields with cypress? - Stack Overflow

programmeradmin0浏览0评论

I wanna validate a field that doesent allow inputs of values (it is blocked with a determinete status). I have search many asserts, but I did not have luck with find that specific demand.

Here is my field before block:

<select class="form-control form-controle valid" data-val="true" data-val-number="O campo IdCategoria deve ser um número." data-val-required="The IdCategoria field is required." id="IdCategoria" name="IdCategoria" aria-describedby="IdCategoria-error" aria-invalid="false"><option selected="selected" value="0">Selecione uma categoria</option> </select>

Here is my field after block:

<select class="form-control form-controle valid formBloqueaEdicao" data-val="true" data-val-number="O campo IdCategoria deve ser um número." data-val-required="The IdCategoria field is required." id="IdCategoria" name="IdCategoria" readonly="readonly" style="pointer-events: none;"><option selected="selected" value="0">Selecione uma categoria</option> </select>

I have implemented the following code, but I am not sure that is the best and safe solution:

cy.get('#IdCategoria').should('have.class', 'form-control form-controle valid formBloqueaEdicao')

Any others solutions?

I wanna validate a field that doesent allow inputs of values (it is blocked with a determinete status). I have search many asserts, but I did not have luck with find that specific demand.

Here is my field before block:

<select class="form-control form-controle valid" data-val="true" data-val-number="O campo IdCategoria deve ser um número." data-val-required="The IdCategoria field is required." id="IdCategoria" name="IdCategoria" aria-describedby="IdCategoria-error" aria-invalid="false"><option selected="selected" value="0">Selecione uma categoria</option> </select>

Here is my field after block:

<select class="form-control form-controle valid formBloqueaEdicao" data-val="true" data-val-number="O campo IdCategoria deve ser um número." data-val-required="The IdCategoria field is required." id="IdCategoria" name="IdCategoria" readonly="readonly" style="pointer-events: none;"><option selected="selected" value="0">Selecione uma categoria</option> </select>

I have implemented the following code, but I am not sure that is the best and safe solution:

cy.get('#IdCategoria').should('have.class', 'form-control form-controle valid formBloqueaEdicao')

Any others solutions?

Share Improve this question asked May 7, 2021 at 15:59 Lucas SantosLucas Santos 652 silver badges6 bronze badges 3
  • 2 Hi Lucas. If your solution is working you can keep it by adding an explicit ment about what this assertion does, otherwise at first sigh is hard to understand its purpose. Related to the way you check for enable/disable, you can invoke or assert over readonly attribute: cy.get('selector').should('have.attr', 'readonly', 'true') – Alex Izbas Commented May 7, 2021 at 17:32
  • Hi Alex, thanks for the reply! I hadn't noticed this attribute, I will increment that, thank you again! – Lucas Santos Commented May 7, 2021 at 19:04
  • Since It changes the class when disabled, I would just look if it has the specific class cy.get('#IdCategoria').should('have.class', 'formBloqueaEdicao'). I prefer this method because It is simple and is according to your app code. If the code logic is changed in the future, you would need to ajust the test any way, – Rosen Mihaylov Commented May 11, 2021 at 16:38
Add a ment  | 

2 Answers 2

Reset to default 5

Actually, the readonly attribute does not prevent the control from being used.

It is the style="pointer-events: none; that makes the select disabled.

For example, with readonly set

<select 
  class="form-control form-controle valid formBloqueaEdicao" 
  data-val="true" 
  data-val-number="O campo IdCategoria deve ser um número." 
  data-val-required="The IdCategoria field is required." 
  id="IdCategoria" 
  name="IdCategoria" 
  readonly="readonly" 
  
>
  <option selected="selected" value="0">Selecione uma categoria</option> 
  <option value="1">abc</option> 
</select>

cy.get('select')
  .should('have.value', '0')
  .select('abc')              // selects "abc" even when readonly
  .should('have.value', '1')  // value has changed!

but with pointer-events: none

<select 
  class="form-control form-controle valid formBloqueaEdicao" 
  data-val="true" 
  data-val-number="O campo IdCategoria deve ser um número." 
  data-val-required="The IdCategoria field is required." 
  id="IdCategoria" 
  name="IdCategoria" 
  readonly="readonly" 
  style="pointer-events: none;"
>
  <option selected="selected" value="0">Selecione uma categoria</option> 
  <option value="1">abc</option> 
</select>

cy.get('select')
  .select('abc')              // fails to select because of "pointer-events: none"

So your best test is

cy.get('#IdCategoria')
  .should('have.attr', 'style', 'pointer-events: none;')

When your element is disabled I see there is an attribute and value readonly="readonly" that is being added to your element, you can assert the presence of that -

cy.get('#IdCategoria').should('have.attr', 'readonly', 'readonly')
发布评论

评论列表(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; } ?>