Skip to content

Commit 4736b8f

Browse files
bahmutovjennifer-shehane
authored andcommitted
feat: add select assertions (#349)
1 parent 483aaa9 commit 4736b8f

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

app/commands/actions.html

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ <h4><a href="https://on.cypress.io/rightclick">.rightclick()</a></h4>
297297
</form>
298298
</div>
299299
</div>
300-
300+
301301
<div class="col-xs-12">
302302
<hr>
303303
</div>
@@ -454,17 +454,34 @@ <h4><a href="https://on.cypress.io/uncheck">.uncheck()</a></h4>
454454
<div class="col-xs-7">
455455
<h4><a href="https://on.cypress.io/select">.select()</a></h4>
456456
<p>To select an option in a <code>select</code>, use the <a href="https://on.cypress.io/select"><code>.select()</code></a> command.</p>
457-
<pre><code class="javascript">// Select option(s) with matching text content
457+
<pre><code class="javascript">// at first, no option should be selected
458+
cy.get('.action-select')
459+
.should('have.value', '--Select a fruit--')
460+
461+
// Select option(s) with matching text content
458462
cy.get('.action-select').select('apples')
463+
// confirm the apples were selected
464+
// note that each value starts with "fr-" in our HTML
465+
cy.get('.action-select').should('have.value', 'fr-apples')
459466

460467
cy.get('.action-select-multiple')
461-
.select(['apples', 'oranges', 'bananas'])
468+
.select(['apples', 'oranges', 'bananas'])
469+
// when getting multiple values, invoke "val" method first
470+
.invoke('val')
471+
.should('deep.equal', ['fr-apples', 'fr-oranges', 'fr-bananas'])
462472

463473
// Select option(s) with matching value
464474
cy.get('.action-select').select('fr-bananas')
475+
// can attach an assertion right away to the element
476+
.should('have.value', 'fr-bananas')
465477

466478
cy.get('.action-select-multiple')
467-
.select(['fr-apples', 'fr-oranges', 'fr-bananas'])</code></pre>
479+
.select(['fr-apples', 'fr-oranges', 'fr-bananas'])
480+
.invoke('val')
481+
.should('deep.equal', ['fr-apples', 'fr-oranges', 'fr-bananas'])
482+
// assert the selected values include oranges
483+
cy.get('.action-select-multiple')
484+
.invoke('val').should('include', 'fr-oranges')</code></pre>
468485
</div>
469486
<div class="col-xs-5">
470487
<div class="well">

cypress/integration/examples/actions.spec.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,34 @@ context('Actions', () => {
183183
it('.select() - select an option in a <select> element', () => {
184184
// https://on.cypress.io/select
185185

186+
// at first, no option should be selected
187+
cy.get('.action-select')
188+
.should('have.value', '--Select a fruit--')
189+
186190
// Select option(s) with matching text content
187191
cy.get('.action-select').select('apples')
192+
// confirm the apples were selected
193+
// note that each value starts with "fr-" in our HTML
194+
cy.get('.action-select').should('have.value', 'fr-apples')
188195

189196
cy.get('.action-select-multiple')
190-
.select(['apples', 'oranges', 'bananas'])
197+
.select(['apples', 'oranges', 'bananas'])
198+
// when getting multiple values, invoke "val" method first
199+
.invoke('val')
200+
.should('deep.equal', ['fr-apples', 'fr-oranges', 'fr-bananas'])
191201

192202
// Select option(s) with matching value
193203
cy.get('.action-select').select('fr-bananas')
204+
// can attach an assertion right away to the element
205+
.should('have.value', 'fr-bananas')
194206

195207
cy.get('.action-select-multiple')
196208
.select(['fr-apples', 'fr-oranges', 'fr-bananas'])
209+
.invoke('val')
210+
.should('deep.equal', ['fr-apples', 'fr-oranges', 'fr-bananas'])
211+
// assert the selected values include oranges
212+
cy.get('.action-select-multiple')
213+
.invoke('val').should('include', 'fr-oranges')
197214
})
198215

199216
it('.scrollIntoView() - scroll an element into view', () => {

0 commit comments

Comments
 (0)