jquery 如何在不使用类选择器的情况下测试我在Cypress中返回的HTML元素?

5lhxktic  于 2023-06-22  发布在  jQuery
关注(0)|答案(1)|浏览(112)

我试图确定cypress选择的元素是“p-inputmask”还是“input”,如果我可以使用hasClass()就可以了,但我不能。以下是我的当前代码:

Cypress.Commands.add('textInputWithLabelTest', (inputId,isDisabled,expectedLabel) => {
    let mask = false;
    cy.get(`[for="${inputId}"]`).should('contain',expectedLabel);
    cy.get(`#${inputId}`).then( ($el) => {
        cy.log($el.find('.ng-pristine'));
        console.log($el.find('p-inputmask')); //TODO fix this

    })
    if(mask) {
        cy.get(`#${inputId}`).siblings('input').should(isDisabled?'be.disabled':'not.be.disabled');
    } else {
        cy.get(`#${inputId}`).should(isDisabled?'be.disabled':'not.be.disabled');
    }
})

我尝试使用包含('p-inputmask')的JSON.stringify(),但对象是循环的,无法解析。
我尝试了Jquery.toString(),但由于某种原因,jQuery不想在文件中,即使我像这样导入它:const $ = require( "jquery" )( window );
我用.it('p-inputmask')试了一下,没有用。
我尝试了.find('p-inputmask'),由于某种原因,即使$el没有p-inputmask,它也会返回一个值。
我用.has()试了一下,没有用。
我用$el[0]和$el尝试了所有这些方法,都没有效果。
console.log($el[0]);两个不同元素的输出。

一定有一个微不足道的解决方案,但什么呢?有人能帮帮忙吗?

nzk0hqpo

nzk0hqpo1#

尝试使用tagName属性。

Cypress.Commands.add('textInputWithLabelTest', (inputId,isDisabled,expectedLabel) => {
  cy.get(`[for="${inputId}"]`).should('contain',expectedLabel);
  cy.get(`#${inputId}`).then($el => {

    if($el[0].tagName === 'INPUT') {
      cy.get(`#${inputId}`).should(isDisabled ? 'be.disabled' : 'not.be.disabled')
    } else {
      cy.get(`#${inputId}`)
        .siblings('input')
        .should(isDisabled ? 'be.disabled' : 'not.be.disabled')
    }
  })
})

我颠倒了if-else的顺序,因为我知道<input>INPUTtagName,但我不确定其他元素。你可以用我展示的东西来做实验。

相关问题