如何编写一个函数来检查按钮是否被禁用

问题描述 投票:0回答:4

我想写一个函数,可以用来检查搜索按钮是否被禁用。

  • 如果搜索按钮已停用,则执行后续步骤 - 填写搜索字段并单击“开始搜索”按钮。

  • 如果搜索按钮已激活,则首先单击该按钮以显示搜索字段,然后执行后续步骤 - 填写搜索字段并单击“开始搜索”按钮。

button is disabled

button is enabled

<button id="mainbody:searchPersRecord:searchIcon" 
  name="mainbody:searchPersRecord:searchIcon" type="button" 
  class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only btNormal showHeadArea ui-state-disabled" 
  alt="Open search" title="Open search" onclick="showHeadArea(true); tryFocusComponentInAnyForm('searchHeadField1_Surname'); return false;;window.open('\/matrix\/views\/basis\/personalmanagement\/searchPersRecord.jsf','_self')" 
  role="button" 
  aria-disabled="false" disabled="disabled">
    <span class="ui-button-icon-left ui-icon ui-c ma-icon ma-search"></span>
    <span class="ui-button-text ui-c">ui-button</span>
</button>

<button id="mainbody:searchPersRecordDivisionForm:searchIcon"
  name="mainbody:searchPersRecordDivisionForm:searchIcon" type="button" 
  class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only btNormal showHeadArea" 
  alt="Open search" title="Open search" 
  onclick="showHeadArea(true); tryFocusComponentInAnyForm('number'); return false;;window.open('\/matrix\/views\/basis\/personalmanagement\/searchDivision.jsf','_self')" role="button" aria-disabled="false">
  <span class="ui-button-icon-left ui-icon ui-c ma-icon ma-search"></span>
  <span class="ui-button-text ui-c">ui-button</span>
</button>
javascript function cypress cypress-conditional-testing
4个回答
3
投票
    cy.get('your_button').then(($button) => {
        if(cy.get($button).should('not.be.disabled')){
            //your logical code for button ACTIVE
        }
        else{
            //your logical code for button NOT ACTIVE
        }
    })

2
投票

检查按钮是否禁用的函数可以使用 jQuery 方法

.is(':disabled')

你会像这样在测试中使用它

const isDisabled = ($el) => $el.is(':disabled');

cy.get('button[id="mainbody:searchPersRecord:searchIcon"]')
  .then($el => {
    if (isDisabled($el)) {
      $el.click()
    }
  }

// fill out the form, etc

0
投票
cy.get('your button').then(($val)=>{ 
   if($val.is(':enabled')){
     //enabled
   }else{
     //disabled
   }
})

0
投票

检查是否已禁用

cy.get('your_selector').should("be.disabled");

检查是否未禁用

cy.get('your_selector').should("not.be.disabled");

由于按钮上有 id,因此您可以将选择器写为

cy.get('#mainbody:searchPersRecord:searchIcon')
© www.soinside.com 2019 - 2024. All rights reserved.