在Cypress.io中,我可以从一个元素复制内容并将其粘贴到另一个元素例如,转换为表单吗?

问题描述

我已经检查了Cypress.io常见问题解答:https://docs.cypress.io/faq/questions/using-cypress-faq.html#How-do-I-get-an-element%E2%80%99s-text-contents

这些技巧让我自己尝试了,但是失败了。

我想获取一个元素,获取它的内容,将其放入变量中,然后再使用。

我想获得与Selenium element.getText()类似的结果。

describe("Training Test Room Suite",() => {

    it("copy content from an element and past it  into next one",() => {
        let contentText
        cy.visit("http://the-internet.herokuapp.com/login");
        cy.get(".example > h2").should(($h2) => {
            contentText = $h2.text()
        })
        cy.get("#username").type(contentText);
    })

})

CypressError: cy.type()只能接受字符串或数字。您传入了:未定义

在上面的代码中,我试图从H2复制文本,将其放入变量contentText,然后将其粘贴到输入“用户名”中。演示应用程序http://the-internet.herokuapp.com/login

上的所有内容

有人可以帮我吗:)

解决方法

我认为这是一个关闭问题。试试这个:

it("Copy content from an element and past it  into next one",() => {
let contentText
cy.visit("http://the-internet.herokuapp.com/login");
cy.get(".example > h2").should(($h2) => {
  contentText = Cypress.$($h2).text();
    return contentText;
  })
  .then(() => {
    cy.get("#username").type(contentText);
});

});

在此处查看文档:{​​{3}}