Search code examples
javascriptseleniumwebdriver-io

JavaScript -WebdriverIO - function does not take the element as argument


I have the following function, which I use to run the accessibility test on a page or a individual component. the function works fine for a page, but when i pass the element as one of the argument to the function it fails . What am i doing wrong here

Below works fine

function runAccessibilityTest(fileName) {
  const fileDelimiter = ",";
  fileName = fileName.split(".")[0] + "_" + getDateTime() + ".csv";
  browser.execute(axeSource);
  const options = { runOnly: { type: "tag", values: ["wcag2aa"] } };
  let results = browser.executeAsync(function (options, done) {
    // run axe on our site
    axe.run(
      {
        include: [["css.elementpath"]],
      },
      function (err, results) {
        if (err) done(err);
        done(results);
      }
    );
  }, options);
}

but this does not work

function runAccessibilityTest(fileName, elem) {
  const fileDelimiter = ",";
  fileName = fileName.split(".")[0] + "_" + getDateTime() + ".csv";
  browser.execute(axeSource);
  const options = { runOnly: { type: "tag", values: ["wcag2aa"] } };
  let results = browser.executeAsync(function (options, done) {
    // run axe on our site
    axe.run(
      {
        include: [[elem]],
      },
      function (err, results) {
        if (err) done(err);
        done(results);
      }
    );
  }, options);
}


Solution

  • I realized that i was not actually passing in the elem value , that's why i was getting the error elem not defined. i have fixed it as below:

    function runAccessibilityTest(fileName, elem) {
        const fileDelimiter = ','
        fileName = fileName.split('.')[0] + '_' + getDateTime() + '.csv'
        browser.execute(axeSource);
        const options = { runOnly: { type: 'tag', values: ['wcag2aa'] } }
        const elemToTest = elem.selector
        let results = browser.executeAsync(function (options, elemToTest, done) {
            // run axe on our site
            axe.run({
                include: [[elemToTest]]
            },function (err, results) {
                if (err) done(err)
                done(results);
            });
        }, options, elemToTest);