when I'm using call backs ,It is throwing call back is not a function in protractor

40 views Asked by At

I'm new to protractor. Here, the main purpose of using callback function is to invoke inputFeild before dropDwn.So, I tried using call back this. before callback I tried using setTimeout ,sleep but they are throwing click intercepted error.

And can anyone suggest me how to invoke multiple function one after other.

inputFeild = (empId: number, callback: any) => {

    let input = element(by.css('input#employeeid'));
    browser.sleep(1000);
    input.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, 'a'));
    browser.sleep(2000);
    input.sendKeys(empId);
    browser.sleep(20000);
    callback();
}

dropDwnField = (propId: any) => {

    let drpDwn = element(by.xpath("//b[text()='Assigned Proposals:']//parent::div//following-sibling::p-dropdown"));

    drpDwn.click();

    setTimeout(() => {

        let search = element(by.xpath("//input[contains(@class,'p-dropdown-filter p-inputtext ')]"));

        browser.sleep(2000);
        search.click();
        search.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, 'a'));
        browser.sleep(2000);
        search.sendKeys(propId);
        browser.sleep(4000);
        element(by.xpath("//span[text()='" + propId + "']")).click();

    }, 5000);
}

spec file

it(' callback function** ', async () => {



epatRateIntlPagePo.inputFeild(5001096, epatRateIntlPagePo.dropDwnField("13227300 - OAQ DALLAS"));

//await epatRateIntlPagePo.empIDTransferPnL(5001096);

await browser.sleep(3000);

Error

TypeError: callback is not a function

1

There are 1 answers

0
cnishina On

I think there might be some things missing like how these functions are imported and exported.

For example, if this is in file foo.ts. Possibly there are some issues with the async / await functions. I have updated some of them to have async await so that your page object functions are async / await. Also I have commented suggestions for the setTimeout section. There are improvements that could be made like waiting to see if you can interact with an element instead of just sleeping:

import {browser, by, element} from 'protractor';

class EpatRateIntlPagePo {
  inputFeild = async (empId: number) => {

    let input = element(by.css('input#employeeid'));
    await browser.sleep(1000);
    await input.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, 'a'));
    await browser.sleep(2000);
    await input.sendKeys(empId);
    await browser.sleep(20000);
    // callback(); what is callback? maybe delete this.
  }

  dropDwnField = async (propId: any) => {

    let drpDwn = element(by.xpath("//b[text()='Assigned Proposals:']//parent::div//following-sibling::p-dropdown"));

    await drpDwn.click();

    // instead of setTimeout, it is preferred to await on the browser to complete. Just remember that everything in browser.wait should be completed.
    await browser.wait(async () => {

        let search = element(by.xpath("//input[contains(@class,'p-dropdown-filter p-inputtext ')]"));

        // instead of this use browser.wait to see if search is in the DOM
        await browser.sleep(2000);
        await search.click();
        await search.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, 'a'));
        await browser.sleep(2000);
        // instead of this use browser.wait to see if search is in the DOM
        await search.sendKeys(propId);
        await browser.sleep(4000);
        await element(by.xpath("//span[text()='" + propId + "']")).click();

    }, 5000); // this time period might need to be greater since 2+2+4=8 seconds of browser sleep > 5 seconds.
  }

In your spec file (import foo.ts assuming these are in the same directory):

import {EpatRateIntlPagePo} from './foo';
import {browser} from 'protractor';

const epatRateIntlPagePo = new EpatRateIntlPagePo();

it(' callback function** ', async () => {
    await epatRateIntlPagePo.inputFeild(5001096);
    await epatRateIntlPagePo.dropDwnField("13227300 - OAQ DALLAS"));

    //await epatRateIntlPagePo.empIDTransferPnL(5001096);

    await browser.sleep(3000);

Hope that helps. Happy testing.