function deleteAllRules ( ) { cy . request ( { log : false , method : 'GET' , url : '/api/alerting/rules/_find' , } ) . then ( ( { body } ) => { if ( body . data . length > 0 ) { cy . log ( `Deleting rules` ) ; } body . data . map ( ( { id } ) => { cy . request ( { headers : { 'kbn-xsrf' : 'true' } , log : false , method : 'DELETE' , url : `/api/alerting/rule/${ id }` , } ) ; } ) ; } ) ; } describe ( 'Rules' , ( ) => { describe ( 'Error count' , ( ) => { const ruleName = 'Error count threshold' ; const comboBoxInputSelector = '.euiPopover__panel-isOpen [data-test-subj=comboBoxSearchInput]' ; const confirmModalButtonSelector = '.euiModal button[data-test-subj=confirmModalConfirmButton]' ; describe ( 'when created from APM' , ( ) => { describe ( 'when created from Service Inventory' , ( ) => { before ( ( ) => { cy . loginAsPowerUser ( ) ; deleteAllRules ( ) ; } ) ; after ( ( ) => { deleteAllRules ( ) ; } ) ; it ( 'creates a rule' , ( ) => { cy . visit ( '/app/apm/services' ) ; cy . contains ( 'Alerts and rules' ) . click ( ) ; cy . contains ( 'Error count' ) . click ( ) ; cy . contains ( 'Create threshold rule' ) . click ( ) ; cy . contains ( 'for the last' ) ; cy . contains ( 'button:not(:disabled)' , 'Save' ) . click ( ) ; cy . get ( confirmModalButtonSelector ) . click ( ) ; cy . contains ( `Created rule "${ ruleName }` ) ; } ) ; } ) ; } ) ; describe ( 'when created from Stack management' , ( ) => { before ( ( ) => { cy . loginAsPowerUser ( ) ; deleteAllRules ( ) ; cy . intercept ( 'GET' , '/api/alerting/rules/_find?page=1&per_page=10&default_search_operator=AND&sort_field=name&sort_order=asc' ) . as ( 'list rules API call' ) ; } ) ; after ( ( ) => { deleteAllRules ( ) ; } ) ; it ( 'creates a rule' , ( ) => { cy . visit ( '/app/management/insightsAndAlerting/triggersActions/rules' ) ; cy . wait ( '@list rules API call' , { timeout : 30000 } ) ; cy . contains ( 'button' , 'Create rule' ) . click ( ) ; cy . get ( '[name=name]' ) . type ( ruleName ) ; cy . contains ( '.euiFlyout button' , ruleName ) . click ( ) ; cy . contains ( 'Environment All' ) . click ( ) ; cy . get ( comboBoxInputSelector ) . type ( 'testing{enter}' ) ; cy . contains ( 'button:not(:disabled)' , 'Save' ) . click ( ) ; cy . get ( confirmModalButtonSelector ) . click ( ) ; cy . contains ( `Created rule "${ ruleName }` ) ; } ) ; } ) ; } ) ; } ) ;