Search code examples
javascriptnode.jsselenium-webdriverautomated-testswebdriver

How to consecutively call asynchronous functions in Javascript


everyone, in my school, i have a project that i code javascript to automated test a page i use webdriver to text but i have a error : TypeError: loginPageInstance.inputUsername(...).inputPassword is not a function at Context. (D:\PVD_HOCTAP\Back End\Javascript\Automated_Testing_demo\test\pageObject2\testScript/testLogin.js:18:67) I still don't know how to fix it. I hope everyone will help me. Sorry for my poor English, I hope everyone will forgive meenter image description hereenter image description here

I want my testcase must be setValue username and password before click


Solution

  • Since each of your functions return promises, you will need to await each one before you can call the next function. This make function chaining very cumbersome:

    const loginPageInstance = new loginPage();
    await (
      await (
        await loginPageInstance.inputUsername(loginData.correctCredentials.username)
      ).inputPassword(loginData.correctCredentials.password)
    ).clickOnLoginBtn();
    

    Since the promise resolves to the same instance you started with, i would instead recommend doing it like this:

    await loginPageInstance.inputUsername(loginData.correctCredentials.username)
    await loginPageInstance.inputPassword(loginData.correctCredentials.password)
    await loginPageInstance.clickOnLoginBtn();