github.com/getgauge/gauge@v1.6.9/execution/stepExecutor_test.go (about)

     1  /*----------------------------------------------------------------
     2   *  Copyright (c) ThoughtWorks, Inc.
     3   *  Licensed under the Apache License, Version 2.0
     4   *  See LICENSE in the project root for license information.
     5   *----------------------------------------------------------------*/
     6  
     7  package execution
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/getgauge/gauge/gauge"
    13  
    14  	"github.com/getgauge/gauge-proto/go/gauge_messages"
    15  )
    16  
    17  func TestStepExecutionShouldAddBeforeStepHookMessages(t *testing.T) {
    18  	r := &mockRunner{}
    19  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
    20  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
    21  		if m.MessageType == gauge_messages.Message_StepExecutionStarting {
    22  			return &gauge_messages.ProtoExecutionResult{
    23  				Message:       []string{"Before Step Called"},
    24  				Failed:        false,
    25  				ExecutionTime: 10,
    26  			}
    27  		}
    28  		return &gauge_messages.ProtoExecutionResult{}
    29  	}
    30  	ei := &gauge_messages.ExecutionInfo{
    31  		CurrentStep: &gauge_messages.StepInfo{
    32  			Step: &gauge_messages.ExecuteStepRequest{
    33  				ActualStepText:  "a simple step",
    34  				ParsedStepText:  "a simple step",
    35  				ScenarioFailing: false,
    36  			},
    37  			IsFailed: false,
    38  		},
    39  	}
    40  	se := &stepExecutor{runner: r, pluginHandler: h, currentExecutionInfo: ei, stream: 0}
    41  	step := &gauge.Step{
    42  		Value:     "a simple step",
    43  		LineText:  "a simple step",
    44  		Fragments: []*gauge_messages.Fragment{{FragmentType: gauge_messages.Fragment_Text, Text: "a simple step"}},
    45  	}
    46  	protoStep := gauge.ConvertToProtoItem(step).GetStep()
    47  	protoStep.StepExecutionResult = &gauge_messages.ProtoStepExecutionResult{}
    48  
    49  	stepResult := se.executeStep(step, protoStep)
    50  	beforeStepMsg := stepResult.ProtoStep.PreHookMessages
    51  
    52  	if len(beforeStepMsg) != 1 {
    53  		t.Errorf("Expected 1 message, got : %d", len(beforeStepMsg))
    54  	}
    55  	if beforeStepMsg[0] != "Before Step Called" {
    56  		t.Errorf("Expected `Before Step Called` message, got : %s", beforeStepMsg[0])
    57  	}
    58  }
    59  
    60  func TestStepExecutionShouldAddAfterStepHookMessages(t *testing.T) {
    61  	r := &mockRunner{}
    62  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
    63  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
    64  		if m.MessageType == gauge_messages.Message_StepExecutionEnding {
    65  			return &gauge_messages.ProtoExecutionResult{
    66  				Message:       []string{"After Step Called"},
    67  				Failed:        false,
    68  				ExecutionTime: 10,
    69  			}
    70  		}
    71  		return &gauge_messages.ProtoExecutionResult{}
    72  	}
    73  	ei := &gauge_messages.ExecutionInfo{
    74  		CurrentStep: &gauge_messages.StepInfo{
    75  			Step: &gauge_messages.ExecuteStepRequest{
    76  				ActualStepText:  "a simple step",
    77  				ParsedStepText:  "a simple step",
    78  				ScenarioFailing: false,
    79  			},
    80  			IsFailed: false,
    81  		},
    82  	}
    83  	se := &stepExecutor{runner: r, pluginHandler: h, currentExecutionInfo: ei, stream: 0}
    84  	step := &gauge.Step{
    85  		Value:     "a simple step",
    86  		LineText:  "a simple step",
    87  		Fragments: []*gauge_messages.Fragment{{FragmentType: gauge_messages.Fragment_Text, Text: "a simple step"}},
    88  	}
    89  	protoStep := gauge.ConvertToProtoItem(step).GetStep()
    90  	protoStep.StepExecutionResult = &gauge_messages.ProtoStepExecutionResult{}
    91  
    92  	stepResult := se.executeStep(step, protoStep)
    93  	afterStepMsg := stepResult.ProtoStep.PostHookMessages
    94  
    95  	if len(afterStepMsg) != 1 {
    96  		t.Errorf("Expected 1 message, got : %d", len(afterStepMsg))
    97  	}
    98  	if afterStepMsg[0] != "After Step Called" {
    99  		t.Errorf("Expected `After Step Called` message, got : %s", afterStepMsg[0])
   100  	}
   101  }
   102  
   103  func TestStepExecutionShouldGetScreenshotsBeforeStep(t *testing.T) {
   104  	r := &mockRunner{}
   105  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
   106  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
   107  		if m.MessageType == gauge_messages.Message_StepExecutionStarting {
   108  			return &gauge_messages.ProtoExecutionResult{
   109  				ScreenshotFiles: []string{"screenshot1.png", "screenshot2.png"},
   110  				Failed:          false,
   111  				ExecutionTime:   10,
   112  			}
   113  		}
   114  		return &gauge_messages.ProtoExecutionResult{}
   115  	}
   116  	ei := &gauge_messages.ExecutionInfo{
   117  		CurrentStep: &gauge_messages.StepInfo{
   118  			Step: &gauge_messages.ExecuteStepRequest{
   119  				ActualStepText:  "a simple step",
   120  				ParsedStepText:  "a simple step",
   121  				ScenarioFailing: false,
   122  			},
   123  			IsFailed: false,
   124  		},
   125  	}
   126  	se := &stepExecutor{runner: r, pluginHandler: h, currentExecutionInfo: ei, stream: 0}
   127  	step := &gauge.Step{
   128  		Value:     "a simple step",
   129  		LineText:  "a simple step",
   130  		Fragments: []*gauge_messages.Fragment{{FragmentType: gauge_messages.Fragment_Text, Text: "a simple step"}},
   131  	}
   132  	protoStep := gauge.ConvertToProtoItem(step).GetStep()
   133  	protoStep.StepExecutionResult = &gauge_messages.ProtoStepExecutionResult{}
   134  
   135  	stepResult := se.executeStep(step, protoStep)
   136  	beforeStepScreenShots := stepResult.ProtoStep.PreHookScreenshotFiles
   137  
   138  	expected := []string{"screenshot1.png", "screenshot2.png"}
   139  
   140  	if len(beforeStepScreenShots) != len(expected) {
   141  		t.Errorf("Expected 2 screenshots, got : %d", len(beforeStepScreenShots))
   142  	}
   143  
   144  	for i, e := range expected {
   145  		if string(beforeStepScreenShots[i]) != e {
   146  			t.Errorf("Expected `%s` screenshot, got : %s", e, beforeStepScreenShots[i])
   147  		}
   148  	}
   149  }
   150  
   151  func TestStepExecutionShouldGetScreenshotsAfterStep(t *testing.T) {
   152  	r := &mockRunner{}
   153  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
   154  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
   155  		if m.MessageType == gauge_messages.Message_StepExecutionEnding {
   156  			return &gauge_messages.ProtoExecutionResult{
   157  				ScreenshotFiles: []string{"screenshot1.png", "screenshot2.png"},
   158  				Failed:          false,
   159  				ExecutionTime:   10,
   160  			}
   161  		}
   162  		return &gauge_messages.ProtoExecutionResult{}
   163  	}
   164  	ei := &gauge_messages.ExecutionInfo{
   165  		CurrentStep: &gauge_messages.StepInfo{
   166  			Step: &gauge_messages.ExecuteStepRequest{
   167  				ActualStepText:  "a simple step",
   168  				ParsedStepText:  "a simple step",
   169  				ScenarioFailing: false,
   170  			},
   171  			IsFailed: false,
   172  		},
   173  	}
   174  	se := &stepExecutor{runner: r, pluginHandler: h, currentExecutionInfo: ei, stream: 0}
   175  	step := &gauge.Step{
   176  		Value:     "a simple step",
   177  		LineText:  "a simple step",
   178  		Fragments: []*gauge_messages.Fragment{{FragmentType: gauge_messages.Fragment_Text, Text: "a simple step"}},
   179  	}
   180  	protoStep := gauge.ConvertToProtoItem(step).GetStep()
   181  	protoStep.StepExecutionResult = &gauge_messages.ProtoStepExecutionResult{}
   182  
   183  	stepResult := se.executeStep(step, protoStep)
   184  	afterStepScreenShots := stepResult.ProtoStep.PostHookScreenshotFiles
   185  
   186  	expected := []string{"screenshot1.png", "screenshot2.png"}
   187  
   188  	if len(afterStepScreenShots) != len(expected) {
   189  		t.Errorf("Expected 2 screenshots, got : %d", len(afterStepScreenShots))
   190  	}
   191  
   192  	for i, e := range expected {
   193  		if string(afterStepScreenShots[i]) != e {
   194  			t.Errorf("Expected `%s` screenshot, got : %s", e, afterStepScreenShots[i])
   195  		}
   196  	}
   197  }