FCTA_JS - Example code for 2.3.3 fails at runtime please help

Iā€™m trying to run the second example code in VSCode

await $('input[name="email"]').setValue('admin@test.com')
await $('input[name="password"]').setValue('password123')
await $('button').click()

const element = await $('.card-title');

const { expect, browser, $ } = require('@wdio/globals')

describe('My Login application', () => {

   it('should login with valid credentials', async () => {
       await browser.url(`http://localhost:3000/#/login`)

       await $('input[name="email"]').setValue('admin@test.com')
       await $('input[name="password"]').setValue('password123')
       await $('button').click()

       const element = await $('.card-title')
       await expect(element).toHaveText('Projects')
   })

})

I get the localhost running and start the script (which always fails) and i get the following error:

[0-0]  Error:  Unable to load spec files quite likely because they rely on `browser` object that is not fully initialized.
`browser` object has only `capabilities` and some flags like `isMobile`.
Helper files that use other `browser` commands have to be moved to `before` hook.
Spec file(s): file:///Users/maxsadler/Documents/MOT_cert_GIT/MOT_cert/test/specs/Login.e2e.js
Error: /Users/maxsadler/Documents/MOT_cert_GIT/MOT_cert/test/specs/Login.e2e.js:2
await $('input[name="email"]').setValue('admin@test.com')

Please help i followed the instructions but it still fails :frowning:

1 Like

You have this above and also inside the it block ā€¦
This should just be inside the it block I believe.

1 Like

Still getting the same error. gone over the tutorial page several times. cannot get this automated test to pass.

[0-0]  Error:  Unable to load spec files quite likely because they rely on `browser` object that is not fully initialized.
`browser` object has only `capabilities` and some flags like `isMobile`.
Helper files that use other `browser` commands have to be moved to `before` hook.
Spec file(s): file:///Users/maxsadler/Documents/MOT_cert_GIT/MOT_cert/test/specs/Login.e2e.js
Error: /Users/maxsadler/Documents/MOT_cert_GIT/MOT_cert/test/specs/Login.e2e.js:1
const element = await $('.card-title');

The error is not quite the same, as the failing command is now const element = await $('.card-title');. It seems like you only removed the first three lines and this one is now the first command. Try removing this line as well, so your file should be:

const { expect, browser, $ } = require('@wdio/globals')

describe('My Login application', () => {

   it('should login with valid credentials', async () => {
       await browser.url(`http://localhost:3000/#/login`)

       await $('input[name="email"]').setValue('admin@test.com')
       await $('input[name="password"]').setValue('password123')
       await $('button').click()

       const element = await $('.card-title')
       await expect(element).toHaveText('Projects')
   })
})

From what the error is saying, you are trying to use a function that relies on the browser having been initialised. This is done in the line const { expect, browser, $ } = require('@wdio/globals') so that needs to be the first line in the file.

2 Likes

Thanks for this andrew i made the two changes. first added the browser to the config and then changed the code as you recommended. i think i got confused with this practical demo but i really appreciate the support.

1 Like