javascript - 如何从 div 类 Cypress 中获取文本

标签 javascript cypress end-to-end

好的,我想从 div 和类中获取文本。 html 看起来像这样:

<div class="inventory_item_name">Backpack</div>
我的代码是这样的:
 const article = cy.get('.inventory_item_price').then((theElement) => {
      theElement.text();
   });
问题:当我做 cy.log(article)我得到 Object{5}

最佳答案

就像评论中提到的亚历克斯一样,您不能从 cy 返回值命令。但是您可以在 .then 中这样做阻止就像:

 it("should have text of 'Backpack'", () => {

  // I have changed the selector since the class name in your HTML is ".inventory_item_name" not ".inventory_item_price"
  cy.get(".inventory_item_name").then(($el) => {
    const text = $el.text(); // Now you have the text "Backpack"

    // Do the assertion here
    expect(text).to.eq("Backpack");
  });
});
您可以在此 documentation 中了解更多关于 Cypress 为什么不返回值的信息。

关于javascript - 如何从 div 类 Cypress 中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62932856/

相关文章:

testing - 需要为 Cypress 测试的图像创建代理

selenium-webdriver - 在 headless (headless)服务器上进行端到端测试

javascript - 从 ul li jquery 中获取选定的项目

electron - 使用 Chrome DevTools 协议(protocol)让 Cypress 运行 Headless Electron

javascript - 检查元素是否有 href 如果它不应该为空 Cypress.io

javascript - Cypress ,如果其他/切换案例不起作用

unit-testing - 在两者都适用的情况下,端到端测试是否比单元测试更好?

javascript - jQuery/CSS : Sync height between LI (lists) and TD (table) elements in a Custom TreeTable

javascript - 在 d3.js 中如何根据屏幕位置调整工具提示(向上和向下)?

javascript - Highcharts 手动设置 legendItemClick 事件