How to use NewInitContext method of js Package

Best K6 code snippet using js.NewInitContext


Source:activity_test.go Github


Full Screen

1package jsexec2import (3 "testing"4 ""5 ""6 ""7 ""8 logger ""9 ""10)11type initContext struct {12 settings map[string]interface{}13}14func newInitContext(values map[string]interface{}) *initContext {15 if values == nil {16 values = make(map[string]interface{})17 }18 return &initContext{19 settings: values,20 }21}22func (i *initContext) Settings() map[string]interface{} {23 return i.settings24}25func (i *initContext) MapperFactory() mapper.Factory {26 return nil27}28func (i *initContext) Logger() logger.Logger {29 return logger.RootLogger()30}31type activityContext struct {32 input map[string]interface{}33 output map[string]interface{}34}35func newActivityContext(values map[string]interface{}) *activityContext {36 if values == nil {37 values = make(map[string]interface{})38 }39 return &activityContext{40 input: values,41 output: make(map[string]interface{}),42 }43}44func (a *activityContext) ActivityHost() activity.Host {45 return a46}47func (a *activityContext) Name() string {48 return "test"49}50func (a *activityContext) GetInput(name string) interface{} {51 return a.input[name]52}53func (a *activityContext) SetOutput(name string, value interface{}) error {54 a.output[name] = value55 return nil56}57func (a *activityContext) GetInputObject(input data.StructValue) error {58 return input.FromMap(a.input)59}60func (a *activityContext) SetOutputObject(output data.StructValue) error {61 a.output = output.ToMap()62 return nil63}64func (a *activityContext) GetSharedTempData() map[string]interface{} {65 return nil66}67func (a *activityContext) ID() string {68 return "test"69}70func (a *activityContext) IOMetadata() *metadata.IOMetadata {71 return nil72}73func (a *activityContext) Reply(replyData map[string]interface{}, err error) {74}75func (a *activityContext) Return(returnData map[string]interface{}, err error) {76}77func (a *activityContext) Scope() data.Scope {78 return nil79}80func (a *activityContext) Logger() logger.Logger {81 return logger.RootLogger()82}83func TestJS(t *testing.T) {84 activity, err := New(newInitContext(map[string]interface{}{85 "script": "result.sum = parameters.a + parameters.b",86 }))87 assert.Nil(t, err)88 ctx := newActivityContext(map[string]interface{}{89 "parameters": map[string]interface{}{"a": 1.0, "b": 2.0},90 })91 _, err = activity.Eval(ctx)92 assert.Nil(t, err)93 assert.Equal(t, 3.0, ctx.output["result"].(map[string]interface{})["sum"].(float64))94}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?