Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

codeceptjs - Calling helper from another helper: `this` is undefined

I've ran into an issue with calling helper from anotter helper. It might be my misunderstanding of concepts but i haven't found any answer over the docs.

On my project i use helpers a lot. Some of them are quite simple and helps me with stuff like select an item from custom-made selection ui element. Others are much bigger - for example i've got a helper that creates the entire Lesson entity (For specific testing scenarios i need few lessons with defferent data). The problem is when i'm calling my helper directly from scenario i can work with Puppeteer inside of it, but when i'm calling helper from another helper - Puppeteer cant be used because this variable return undefined, so i cant call this.helpers.Puppeteer.

Here is simplified code.

Two helpers declared:

exports.config = {
  helpers: {
    Puppeteer: { /* some config */ },
    SelectFromListHelper: {
      require: './selectFromList_helper.js' // <- this one is "simple" helper
    },
    CreateLessonHelper: {
      require: './createLesson_helper.js' // <- this one is "big one" - has to be working with other helerps
    }
  },
  bootstrap: null
}

SelectFromListHelper:

class SelectFromListHelper extends Helper {
  async selectFromList (selector, listItemNumber = 1) {
    // const page = this.helpers['Puppeteer'].page
    // await page.click('#list')

    console.log(this)
  }
}

CreateLessonHelper:

class CreateLessonHelper extends Helper {
  async createLesson () {
    const page = this.helpers['Puppeteer'].page
    // await page.click('#assistants')

    const { selectFromList } = this.helpers.SelectFromListHelper
    await selectFromList('#teachers', 2)
  }
}

Scenario:

Feature('Feature 1')
Scenario('Test 1', async ({ I}) => {
    // calling for SelectFromListHelper.selectFromList
    // it works fine, puppeteer is accessible from that function. (console.log returns helper object)
    await I.selectFromList('#assistants', 1)
    
    // calling for CreateLessonHelper.createLesson
    await I.createLesson()
    // and here the problem starts.
    // Inside of createLesson I do call this.helpers.SelectFromListHelper.selectFromList()
    // and from that call i can't access the Puppeteer. Console.log returns "undefined"
})

If i remove the console.log and uncomment Puppeteer call in selectFromList method i receive this feedback:

--verbose
CodeceptJS v3.0.4
Using test root "C:Worklondon-smp-tests"
Helpers: Puppeteer, SelectFromListHelper, CreateLessonHelper
Plugins: screenshotOnFail

Feature 1 --
    [1]  Starting recording promises
 ? [Session] Starting singleton browser session
  Test 1
    I select from list "#assistants", 1
    I create lesson
    [1] Error | TypeError: Cannot read property 'helpers' of undefined
    [1] Error | TypeError: Cannot read property 'helpers' of undefined
    [1] Starting <teardown> session
    [1] <teardown> Stopping recording promises
 ? <screenshotOnFail> Test failed, try to save a screenshot
 ? Screenshot is saving to C:Worklondon-smp-testsoutputTest_1.failed.png
  × FAILED in 507ms

    [2]  Starting recording promises

-- FAILURES:

  1) Feature 1
       Test 1:
     Cannot read property 'helpers' of undefined
      at selectFromList (selectFromList_helper.js:5:23)
      at CreateLessonHelper.createLesson (createLesson_helper.js:9:11)
      at Step.run (node_modulescodeceptjslibstep.js:66:47)
      at C:Worklondon-smp-tests
ode_modulescodeceptjslibactor.js:117:23

  Scenario Steps:
  - I.createLesson() at Test.<anonymous> (.T1_test.js:5:13)
  - I.selectFromList("#assistants", 1) at Test.<anonymous> (.T1_test.js:4:13)

  Artifacts:
  - screenshot: C:Worklondon-smp-testsoutputTest_1.failed.png


  FAIL  | 0 passed, 1 failed   // 1s

What am i doing / understanding wrong?

question from:https://stackoverflow.com/questions/66061800/calling-helper-from-another-helper-this-is-undefined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...