【问题标题】:Selecting unknown item from drop down in Cypress从赛普拉斯的下拉列表中选择未知项目
【发布时间】:2019-02-28 21:31:15
【问题描述】:

我只是想学习这个程序,但我无法从列表中选择一个选项。问题是,我不知道这个特定项目的价值(我也不在乎,我只需要从字面上选择任何选项)。

我当前 Cypress 脚本的 sn-p:

    it('select thing"', function() {
    cy.get('.item-summary__item-selected').click()
    cy.get('item-selector-new > .line-items > :nth-child(1) > .flex-2').select('Biggest')
    cy.get('item-selector-new > .line-items > :nth-child(2) > .flex-2').select('Big')
    cy.get('item-selector-new > .line-items > :nth-child(3) > .flex-2').select('Normal')
    cy.get('item-selector-new > .line-items > :nth-child(4) > .flex-2').?????              
})

第 4 个子订单项的 HTML 如下所示:

<div class="line-item" ng-if="$ctrl.suggested_item.length <= $ctrl.item.length"> <span class="flex-1">Item</span>
<select av-tab-item="" groupindex="1" tabindex="4" class="flex-2 form-control ng-pristine ng-untouched ng-valid ng-empty" ng-model="$ctrl.selected_item_id" ng-options="item.item_id as item.item_name for item in $ctrl.items | orderBy:'sort_order'" ng-disabled="!$ctrl.items.length" ng-change="$ctrl.handleItemSelected()" data-e2e="SELECTOR_NEW_TEST_ITEM_SELECT">
    <option value="?" selected="selected"></option>
    <option label="label name 1" value="string:186199ee-0ab6-464a-8b28-f93ee10e8bdf">label name 1</option>
    <option label="label name 2" value="string:1d58a921-e393-49f5-8df8-30243d14ec4b">label name 2</option>
    <option label="label name 3" value="string:c24d7478-4a7f-42cc-93e5-c27493f921c0">label name 3</option>
    <option label="label name 4" value="string:aa469cd5-b7fe-454b-b67b-9dd9af70038e">label name 4</option>
</select>
<br>

select() 函数对我没有帮助,因为我不知道要选择的值,因为这些选项经常更改。我已经尝试过 first()、last()、eq(),可能还有其他一些,但我不确定我哪里出错了。任何指导将不胜感激。

【问题讨论】:

    标签: cypress


    【解决方案1】:

    以下是基于索引使用 select() 的方法。例如,您可以获得select4th 值,然后使用产生value.then()

    cy.get('select.myselect option').eq(4).invoke('val').then((val)=>{
      cy.get('select.myselect').select(val)
    })
    
    // .eq(n) yields the nth element
    

    【讨论】:

    • 谢谢!我只是在尝试使用 invoke 和/或在 eq 之后玩弄,我可以说我越来越近了,但正在撞墙。这完美!再次感谢!
    • 这是迄今为止我能找到的最简单、最优雅的解决方案!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-10-13
    • 2021-12-27
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多