@@ -142,6 +142,21 @@ async function deleteStage(
142142 await browser . clickVisible ( Selectors . StageDelete ) ;
143143}
144144
145+ function getStageContainers ( browser : CompassBrowser ) {
146+ return browser . $$ ( Selectors . StageCard ) ;
147+ }
148+
149+ async function addStage ( browser : CompassBrowser , expectedStages : number ) {
150+ expect ( await getStageContainers ( browser ) ) . to . have . lengthOf (
151+ expectedStages - 1
152+ ) ;
153+
154+ await browser . clickVisible ( Selectors . AddStageButton ) ;
155+ await browser . $ ( Selectors . stageEditor ( expectedStages - 1 ) ) . waitForDisplayed ( ) ;
156+
157+ expect ( await getStageContainers ( browser ) ) . to . have . lengthOf ( expectedStages ) ;
158+ }
159+
145160describe ( 'Collection aggregations tab' , function ( ) {
146161 let compass : Compass ;
147162 let browser : CompassBrowser ;
@@ -165,11 +180,10 @@ describe('Collection aggregations tab', function () {
165180 // Pipeline flow while at it.
166181 await browser . clickVisible ( Selectors . CreateNewPipelineButton ) ;
167182
168- await browser . clickVisible ( Selectors . AddStageButton ) ;
169- await browser . $ ( Selectors . stageEditor ( 0 ) ) . waitForDisplayed ( ) ;
170- // sanity check to make sure there's only one stage
171- const stageContainers = await browser . $$ ( Selectors . StageCard ) ;
172- expect ( stageContainers ) . to . have . lengthOf ( 1 ) ;
183+ // This is kinda superfluous for the nested beforeEach hooks below where we
184+ // immediately navigate away anyway, but most tests expect there to already
185+ // be one stage.
186+ await addStage ( browser , 1 ) ;
173187 } ) ;
174188
175189 after ( async function ( ) {
@@ -358,14 +372,14 @@ describe('Collection aggregations tab', function () {
358372 await browser . clickVisible ( Selectors . AggregationSettingsApplyButton ) ;
359373
360374 // add a $project
361- await browser . clickVisible ( Selectors . AddStageButton ) ;
375+ await addStage ( browser , 2 ) ;
362376 await browser . selectStageOperator ( 1 , '$project' ) ;
363377
364378 // delete it
365379 await deleteStage ( browser , 1 ) ;
366380
367381 // add a $project
368- await browser . clickVisible ( Selectors . AddStageButton ) ;
382+ await addStage ( browser , 2 ) ;
369383 await browser . selectStageOperator ( 1 , '$project' ) ;
370384
371385 // check that it has no comment
@@ -603,7 +617,7 @@ describe('Collection aggregations tab', function () {
603617
604618 await waitForAnyText ( browser , await browser . $ ( Selectors . stageContent ( 0 ) ) ) ;
605619
606- await browser . clickVisible ( Selectors . AddStageButton ) ;
620+ await addStage ( browser , 2 ) ;
607621
608622 await browser . focusStageOperator ( 1 ) ;
609623 await browser . selectStageOperator ( 1 , '$match' ) ;
@@ -667,7 +681,7 @@ describe('Collection aggregations tab', function () {
667681
668682 await waitForAnyText ( browser , await browser . $ ( Selectors . stageContent ( 0 ) ) ) ;
669683
670- await browser . clickVisible ( Selectors . AddStageButton ) ;
684+ await addStage ( browser , 2 ) ;
671685
672686 await browser . focusStageOperator ( 1 ) ;
673687 await browser . selectStageOperator ( 1 , '$match' ) ;
@@ -1033,8 +1047,6 @@ describe('Collection aggregations tab', function () {
10331047 } ) ;
10341048
10351049 it ( 'shows confirmation modal when create new pipeline is clicked and aggregation is modified' , async function ( ) {
1036- await browser . clickVisible ( Selectors . AddStageButton ) ;
1037- await browser . $ ( Selectors . stageEditor ( 0 ) ) . waitForDisplayed ( ) ;
10381050 await browser . selectStageOperator ( 0 , '$match' ) ;
10391051
10401052 await browser . clickVisible ( Selectors . CreateNewPipelineButton ) ;
@@ -1271,8 +1283,7 @@ describe('Collection aggregations tab', function () {
12711283 } ) ;
12721284
12731285 it ( 'opens an aggregation with confirmation when its modified' , async function ( ) {
1274- await browser . clickVisible ( Selectors . AddStageButton ) ;
1275- await browser . $ ( Selectors . stageEditor ( 0 ) ) . waitForDisplayed ( ) ;
1286+ await addStage ( browser , 1 ) ;
12761287 await browser . selectStageOperator ( 0 , '$match' ) ;
12771288
12781289 await browser . waitForAnimations (
@@ -1621,13 +1632,12 @@ describe('Collection aggregations tab', function () {
16211632 'nestedDocs' ,
16221633 'Aggregations'
16231634 ) ;
1635+
1636+ await addStage ( browser , 1 ) ;
16241637 } ) ;
16251638
16261639 context ( 'when on stage builder mode' , function ( ) {
16271640 it ( 'should expand/collapse all the docs for a stage when "Expand documents" / "Collapse documents" menu option is clicked' , async function ( ) {
1628- await browser . clickVisible ( Selectors . AddStageButton ) ;
1629- await browser . $ ( Selectors . stageEditor ( 0 ) ) . waitForDisplayed ( ) ;
1630-
16311641 await browser . selectStageOperator ( 0 , '$match' ) ;
16321642 await browser . setCodemirrorEditorValue (
16331643 Selectors . stageEditor ( 0 ) ,
@@ -1663,9 +1673,6 @@ describe('Collection aggregations tab', function () {
16631673 } ) ;
16641674
16651675 it ( 'should retain the docs expanded / collapsed state even after switching tabs' , async function ( ) {
1666- await browser . clickVisible ( Selectors . AddStageButton ) ;
1667- await browser . $ ( Selectors . stageEditor ( 0 ) ) . waitForDisplayed ( ) ;
1668-
16691676 await browser . selectStageOperator ( 0 , '$match' ) ;
16701677 await browser . setCodemirrorEditorValue (
16711678 Selectors . stageEditor ( 0 ) ,
@@ -1744,16 +1751,14 @@ describe('Collection aggregations tab', function () {
17441751
17451752 context ( 'when in focus mode' , function ( ) {
17461753 beforeEach ( async function ( ) {
1747- await browser . clickVisible ( Selectors . AddStageButton ) ;
17481754 await browser . $ ( Selectors . stageEditor ( 0 ) ) . waitForDisplayed ( ) ;
17491755 await browser . selectStageOperator ( 0 , '$match' ) ;
17501756 await browser . setCodemirrorEditorValue (
17511757 Selectors . stageEditor ( 0 ) ,
17521758 '{ "names.firstName": "1-firstName" }'
17531759 ) ;
17541760
1755- await browser . clickVisible ( Selectors . AddStageButton ) ;
1756- await browser . $ ( Selectors . stageEditor ( 1 ) ) . waitForDisplayed ( ) ;
1761+ await addStage ( browser , 2 ) ;
17571762 await browser . selectStageOperator ( 1 , '$limit' ) ;
17581763 await browser . setCodemirrorEditorValue ( Selectors . stageEditor ( 1 ) , '1' ) ;
17591764
@@ -1820,8 +1825,7 @@ describe('Collection aggregations tab', function () {
18201825
18211826 context ( 'when on pipeline results' , function ( ) {
18221827 beforeEach ( async function ( ) {
1823- await browser . clickVisible ( Selectors . AddStageButton ) ;
1824- await browser . $ ( Selectors . stageEditor ( 0 ) ) . waitForDisplayed ( ) ;
1828+ expect ( await getStageContainers ( browser ) ) . to . have . lengthOf ( 1 ) ;
18251829
18261830 await browser . selectStageOperator ( 0 , '$match' ) ;
18271831 await browser . setCodemirrorEditorValue (
0 commit comments