Reputation: 53
I'm just trying to learn this program and I'm having trouble selecting an option from a list. The problem is, I don't know the value of this particular item (and I don't really care, I just need literally any of the choices selected).
A snippet of my current Cypress script:
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').?????
})
HTML for the 4th child line item looks like this:
<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>
The select() function isn't helping me since I don't know the value to select because these options change frequently. I've tried first(), last(), eq(), and probably a few others and I'm not sure where I'm going wrong. Any guidance would be appreciated.
Upvotes: 3
Views: 3557
Reputation: 18053
Here's how you'd use select()
based on index. For example you can get the 4th
value of the select
, then use a .then()
that yields it's value
:
cy.get('select.myselect option').eq(4).invoke('val').then((val)=>{
cy.get('select.myselect').select(val)
})
// .eq(n) yields the nth element
Upvotes: 5