如何使用 Cypress 获取元素类型 =“email”?

Ste*_*ple 3 cypress

我目前正在尝试学习 Cypress 进行自动化测试。

我想找到并点击这个元素:

<input type="email" class="form-control text-ellipsis-global GKMU5SYDM3B" id="gwt-uid-1419" data-empty="true">
Run Code Online (Sandbox Code Playgroud)

我不能使用 id 或“GKMU5SYDM3B”,因为它们都是由 GWT 随机生成的。

我希望我的命令类似于这样:

cy.get('input').type('email').click().type('fake@email.com')
  .should('have.value', 'fake@email.com')
Run Code Online (Sandbox Code Playgroud)

我尝试了这个并收到以下错误消息:

CypressError: cy.type() can only be called on a single element. Your subject contained 4 elements.
Run Code Online (Sandbox Code Playgroud)

Mac*_*urt 6

这应该有效。

 cy.get("[type='email']")

 cy.get("[type='email']").type('myemail@domain.com')
Run Code Online (Sandbox Code Playgroud)

我总是做的,首先在浏览器控制台中在 JQUERY 中尝试它。您不必单击它即可输入。

如果您周围有一些父元素,例如 div,您可能还想包含它。

<div id='parent'>
    <input type="email" class="form-control text-ellipsis-global GKMU5SYDM3B" id="gwt-uid-1419" data-empty="true">
</div>
Run Code Online (Sandbox Code Playgroud)

所以你会去

cy.get('#parent').find("[type='email']").type('myemail@domain.com')
Run Code Online (Sandbox Code Playgroud)