gitee.com/mirrors/gauge@v1.0.6/execution/stepExecutor_test.go (about) 1 // Copyright 2015 ThoughtWorks, Inc. 2 3 // This file is part of Gauge. 4 5 // Gauge is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 10 // Gauge is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 15 // You should have received a copy of the GNU General Public License 16 // along with Gauge. If not, see <http://www.gnu.org/licenses/>. 17 18 package execution 19 20 import ( 21 "testing" 22 23 "github.com/getgauge/gauge/gauge" 24 25 "github.com/getgauge/gauge/gauge_messages" 26 ) 27 28 func TestStepExecutionShouldAddBeforeStepHookMessages(t *testing.T) { 29 r := &mockRunner{} 30 h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}} 31 r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult { 32 if m.MessageType == gauge_messages.Message_StepExecutionStarting { 33 return &gauge_messages.ProtoExecutionResult{ 34 Message: []string{"Before Step Called"}, 35 Failed: false, 36 ExecutionTime: 10, 37 } 38 } 39 return &gauge_messages.ProtoExecutionResult{} 40 } 41 ei := &gauge_messages.ExecutionInfo{ 42 CurrentStep: &gauge_messages.StepInfo{ 43 Step: &gauge_messages.ExecuteStepRequest{ 44 ActualStepText: "a simple step", 45 ParsedStepText: "a simple step", 46 ScenarioFailing: false, 47 }, 48 IsFailed: false, 49 }, 50 } 51 se := &stepExecutor{runner: r, pluginHandler: h, currentExecutionInfo: ei, stream: 0} 52 step := &gauge.Step{ 53 Value: "a simple step", 54 LineText: "a simple step", 55 Fragments: []*gauge_messages.Fragment{{FragmentType: gauge_messages.Fragment_Text, Text: "a simple step"}}, 56 } 57 protoStep := gauge.ConvertToProtoItem(step).GetStep() 58 protoStep.StepExecutionResult = &gauge_messages.ProtoStepExecutionResult{} 59 60 stepResult := se.executeStep(step, protoStep) 61 beforeStepMsg := stepResult.ProtoStep.PreHookMessages 62 63 if len(beforeStepMsg) != 1 { 64 t.Errorf("Expected 1 message, got : %d", len(beforeStepMsg)) 65 } 66 if beforeStepMsg[0] != "Before Step Called" { 67 t.Errorf("Expected `Before Step Called` message, got : %s", beforeStepMsg[0]) 68 } 69 } 70 71 func TestStepExecutionShouldAddAfterStepHookMessages(t *testing.T) { 72 r := &mockRunner{} 73 h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}} 74 r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult { 75 if m.MessageType == gauge_messages.Message_StepExecutionEnding { 76 return &gauge_messages.ProtoExecutionResult{ 77 Message: []string{"After Step Called"}, 78 Failed: false, 79 ExecutionTime: 10, 80 } 81 } 82 return &gauge_messages.ProtoExecutionResult{} 83 } 84 ei := &gauge_messages.ExecutionInfo{ 85 CurrentStep: &gauge_messages.StepInfo{ 86 Step: &gauge_messages.ExecuteStepRequest{ 87 ActualStepText: "a simple step", 88 ParsedStepText: "a simple step", 89 ScenarioFailing: false, 90 }, 91 IsFailed: false, 92 }, 93 } 94 se := &stepExecutor{runner: r, pluginHandler: h, currentExecutionInfo: ei, stream: 0} 95 step := &gauge.Step{ 96 Value: "a simple step", 97 LineText: "a simple step", 98 Fragments: []*gauge_messages.Fragment{{FragmentType: gauge_messages.Fragment_Text, Text: "a simple step"}}, 99 } 100 protoStep := gauge.ConvertToProtoItem(step).GetStep() 101 protoStep.StepExecutionResult = &gauge_messages.ProtoStepExecutionResult{} 102 103 stepResult := se.executeStep(step, protoStep) 104 afterStepMsg := stepResult.ProtoStep.PostHookMessages 105 106 if len(afterStepMsg) != 1 { 107 t.Errorf("Expected 1 message, got : %d", len(afterStepMsg)) 108 } 109 if afterStepMsg[0] != "After Step Called" { 110 t.Errorf("Expected `After Step Called` message, got : %s", afterStepMsg[0]) 111 } 112 } 113 114 func TestStepExecutionShouldGetScreenshotsBeforeStep(t *testing.T) { 115 r := &mockRunner{} 116 h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}} 117 r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult { 118 if m.MessageType == gauge_messages.Message_StepExecutionStarting { 119 return &gauge_messages.ProtoExecutionResult{ 120 Screenshots: [][]byte{[]byte("screenshot1"), []byte("screenshot2")}, 121 Failed: false, 122 ExecutionTime: 10, 123 } 124 } 125 return &gauge_messages.ProtoExecutionResult{} 126 } 127 ei := &gauge_messages.ExecutionInfo{ 128 CurrentStep: &gauge_messages.StepInfo{ 129 Step: &gauge_messages.ExecuteStepRequest{ 130 ActualStepText: "a simple step", 131 ParsedStepText: "a simple step", 132 ScenarioFailing: false, 133 }, 134 IsFailed: false, 135 }, 136 } 137 se := &stepExecutor{runner: r, pluginHandler: h, currentExecutionInfo: ei, stream: 0} 138 step := &gauge.Step{ 139 Value: "a simple step", 140 LineText: "a simple step", 141 Fragments: []*gauge_messages.Fragment{{FragmentType: gauge_messages.Fragment_Text, Text: "a simple step"}}, 142 } 143 protoStep := gauge.ConvertToProtoItem(step).GetStep() 144 protoStep.StepExecutionResult = &gauge_messages.ProtoStepExecutionResult{} 145 146 stepResult := se.executeStep(step, protoStep) 147 beforeStepScreenShots := stepResult.ProtoStep.PreHookScreenshots 148 149 expected := []string{"screenshot1", "screenshot2"} 150 151 if len(beforeStepScreenShots) != len(expected) { 152 t.Errorf("Expected 2 screenshots, got : %d", len(beforeStepScreenShots)) 153 } 154 155 for i, e := range expected { 156 if string(beforeStepScreenShots[i]) != e { 157 t.Errorf("Expected `%s` screenshot, got : %s", e, beforeStepScreenShots[i]) 158 } 159 } 160 } 161 162 func TestStepExecutionShouldGetScreenshotsAfterStep(t *testing.T) { 163 r := &mockRunner{} 164 h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}} 165 r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult { 166 if m.MessageType == gauge_messages.Message_StepExecutionEnding { 167 return &gauge_messages.ProtoExecutionResult{ 168 Screenshots: [][]byte{[]byte("screenshot1"), []byte("screenshot2")}, 169 Failed: false, 170 ExecutionTime: 10, 171 } 172 } 173 return &gauge_messages.ProtoExecutionResult{} 174 } 175 ei := &gauge_messages.ExecutionInfo{ 176 CurrentStep: &gauge_messages.StepInfo{ 177 Step: &gauge_messages.ExecuteStepRequest{ 178 ActualStepText: "a simple step", 179 ParsedStepText: "a simple step", 180 ScenarioFailing: false, 181 }, 182 IsFailed: false, 183 }, 184 } 185 se := &stepExecutor{runner: r, pluginHandler: h, currentExecutionInfo: ei, stream: 0} 186 step := &gauge.Step{ 187 Value: "a simple step", 188 LineText: "a simple step", 189 Fragments: []*gauge_messages.Fragment{{FragmentType: gauge_messages.Fragment_Text, Text: "a simple step"}}, 190 } 191 protoStep := gauge.ConvertToProtoItem(step).GetStep() 192 protoStep.StepExecutionResult = &gauge_messages.ProtoStepExecutionResult{} 193 194 stepResult := se.executeStep(step, protoStep) 195 afterStepScreenShots := stepResult.ProtoStep.PostHookScreenshots 196 197 expected := []string{"screenshot1", "screenshot2"} 198 199 if len(afterStepScreenShots) != len(expected) { 200 t.Errorf("Expected 2 screenshots, got : %d", len(afterStepScreenShots)) 201 } 202 203 for i, e := range expected { 204 if string(afterStepScreenShots[i]) != e { 205 t.Errorf("Expected `%s` screenshot, got : %s", e, afterStepScreenShots[i]) 206 } 207 } 208 }