首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cypress和Typescript。获取标题最高的元素

Cypress和Typescript。获取标题最高的元素
EN

Stack Overflow用户
提问于 2021-04-07 19:04:58
回答 1查看 37关注 0票数 0

我有一些元素的标题是这样的:

模板2021-4-7 9:50:53

模板2021-4-7 10:35:52

模板2021-4-7 10:38:17

模板2021-4-7 9:50:24

模板2021-4-7 11:27:10

模板2021-4-7 11:24:21

我想选择最新的一个,所以我写了这样的方法:

代码语言:javascript
复制
Cypress.Commands.add('addLatestTemplateToSheet', () => {
  let templateTitle: string;
  workbookEditor.leftMenu.templateMenuButton.click();
  workbookEditor.leftMenu.templateItem
    .each(($templateItem, index) => {
      if (index === 0) {
        templateTitle = $templateItem.attr('title');
      }
      else{
        if (templateTitle < $templateItem.attr('title')) {
          templateTitle = $templateItem.attr('title');
        }
      }
    })
    .then(() => {
      //next part of code
    });
});

不幸的是,它不能工作,我从列表中得到第一项。我应该如何纠正我的方法?

EN

回答 1

Stack Overflow用户

发布于 2021-04-07 23:04:58

您可以在.then()之后使用.spread()从集合中选择一个元素,例如

代码语言:javascript
复制
cy.get('li')
  .spread((...listElements) => {
    const sortedListElements = Array.prototype.slice.call(listElements).sort(...);
    return sortedListElements[0];
  })
  .then((listElement) => {
    // do what you need with the item
  });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66984790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档