CodeceptJS: Since Codecept 3.6.9, our test that looks for a date time doesn't find it (but it's there)

Hi, we’re seeing this issue and we’ve tried pinning to older versions - 3.6.7 is OK.

What are you trying to achieve?

We have a test that’s been passing up until now and does so with 3.6.7.

I can’t use 3.6.8 because of Error: Cannot find module ‘invisi-data’ (I noticed a hotfix around this in the commits).

It started failing today (it could have failed sooner, we only noticed it today).

What do you get instead?

The following (I’ve noticed the UI here says 3.6.2, but when I do an npm list I am on 3.6.7, 3.6.8, or 3.6.9 depending on the test I am doing - we want to be on the latest and tried downgrading twice and as part of that verified 3.6.7 is ok).

image

This looks to me like it finds our text and then says it hasn’t…

Provide console output if related. Use --verbose mode for more details.

1) User Logs
       It renders a User Log table:
     Text "2024-09-05 04:14:32.000" was not found on page after 1 sec.
      at Playwright.waitForText (node_modules/codeceptjs/lib/helper/Playwright.js:2854:26)
      at async Test.<anonymous> (codecept/user-logs.spec.js:39:3)
  
  

  2) User Logs
       It renders a new User Log table:
     Text "2024-09-06 10:14:50.000" was not found on page after 1 sec.
      at Playwright.waitForText (node_modules/codeceptjs/lib/helper/Playwright.js:2854:26)
      at runNextTicks (node:internal/process/task_queues:60:5)
      at process.processTimers (node:internal/timers:509:9)

Provide test source code if related Sorry - I don’t have a minimal reproduce yet - we basically have a simple logging page and we’re looking for an event time on it in a row

Scenario('It renders a User Log table', async ({ I }) => {
  I.amOnPage('http://localhost:5057/iframe.html?args=&id=demo-logs-page--renders-correctly&viewMode=story')

  I.waitForText('Logs', 10)

  await sleep(10000)

  // Check there is a table
  I.seeElement('.IntegrationDataTable')
  I.seeNumberOfVisibleElements('.UserLogsPage__Table tbody tr[data-parent-row="true"]', 14)
  I.see('14 of 10000')

  // Check column headings
  I.see('Event time (UTC)')
  I.see('Message')
  I.see('Transaction ID')
  I.see('Flow ID')
  I.see('Log level')

  // Check first row
  let firstRow = locate('.UserLogsPage__Table tbody tr[data-parent-row="true"]').first()
  console.log('first', firstRow)

  await within(firstRow, () => {
    I.waitForText('2024-09-05 04:14:32.000') <---------- see here
  })

Details

  • CodeceptJS version: 3.6.9
  • NodeJS Version: we’ve tried with Node.js 22.9.0 and 18.20.4
  • Operating System: I’m on Mac OSX
  • puppeteer || webdriverio || testcafe version (if related)

We are using Playwright 1.49.1

  • Configuration file:

exports.config = {
  tests: './codecept/*.spec.js',
  output: './codecept/screenshots',
  retry: 2,
  helpers: {
    Playwright: {
      url: 'http://localhost:9010',
      show: false,
      browser: 'chromium',
      pressKeyDelay: 2,
      waitForAction: 5000
    }
  },
  bootstrap: null,
  mocha: {},
  name: 'ui-logging',
  plugins: {
    pauseOnFail: {},
    retryFailedStep: {
      enabled: true
    },
    tryTo: {
      enabled: true
    },
    screenshotOnFail: {
      enabled: true
    },
    customLocator: {
      enabled: true,
      attribute: 'data-testid'
    }
  }
}

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 16

Most upvoted comments

so your table is inside a frame.

~Hmm it always has been*~, we’ve been using Codecept for months, if not years, across our project (to great success so thank you).

I wonder if we now need to provide some more information to the locator? But it feels wrong to have to to do that inside of a patch version change (3.6.7 to 3.6.9) indicating that the behaviour is now subtly different and I would consider that a bug - what do you think?

*I’ve just realised I don’t know what a frame is in this context - what I’d like to say is instead is…we haven’t changed our code…

Edit 2: I wonder if we were always relying on unintended behaviour that has since been fixed…

I’d think that if you could provide a sample repo where we could try to reproduce and debug from there. It’ll be great start point.

@a-roberts is it possible to print out the await _contextObject.locator(:has-text(${JSON.stringify(text)}))? just curious if the correct locator is used here.

I have:

  • added code in node_modules/codeceptjs/lib/helper/Playwright.js so we retry five times every 5 second. no good, suggesting not a “timing thing”
  • confirmed we enter this code in lib/helper/playwright.js:
const _contextObject = this.frame ? this.frame : contextObject
      let count = 0
      do {
        waiter = await _contextObject
          .locator(`:has-text(${JSON.stringify(text)})`)
          .first()
          .isVisible()
        if (waiter) break
        await this.wait(1)
        count += 1000
      } while (count <= waitTimeout)

      if (!waiter) throw new Error(`${errorMessage}`)
  • confirmed there’s never a waiter (obvious when you look at the code as that’s the one throwing the error)

If I inspect the value of _contextObject and also this.frame while I’m at it, it is:

_contextObject: {"_type":"Page","_guid":"page@a4a6cdfa1f4af9fa1b39cbbd8766ef17"}
this.frame: null

(not that useful to help our debugging)

This is on Codecept 3.6.9 where the problem is and if I downgrade to 3.6.7 where it works and look at the same values, I don’t gain any useful insights. There aren’t enough things i can inspect in this helper file alone to help me debug what’s going on.

I’m going to try and get the simplest reproduce for you and will push to my github.

Thanks for trying it out. May you give this a try on your local?

async waitForText(text, sec = null, context = null) {
  if (typeof text !== 'string') throw new TypeError('Parameter "text" must be a string');
  const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
  const errorMessage = `Text "${text}" was not found on page after ${waitTimeout / 1000} sec.`;
  const contextObject = await this._getContext();
  const _contextObject = this.frame ? this.frame : contextObject;

  try {
    if (context) {
      const locator = new Locator(context, 'css');
      const locatorQuery = locator.isCustom()
        ? `${locator.type}=${locator.value}`
        : locator.simplify();

      if (!locator.isXPath()) {
        await contextObject
          .locator(`${locatorQuery} >> text=${text}`)
          .first()
          .waitFor({ timeout: waitTimeout, state: 'visible' });
      } else {
        await contextObject.waitForFunction(
          ([locator, text, xpathFn]) => {
            const el = xpathFn(null, locator);
            return el.length && el[0].innerText.includes(text);
          },
          [locator.value, text, $XPath],
          { timeout: waitTimeout }
        );
      }
    } else {
      const startTime = Date.now();
      let waiter;
      do {
        waiter = await _contextObject
          .locator(`:has-text(${JSON.stringify(text)})`)
          .first()
          .isVisible();
        if (waiter) break;
        await this.wait(1);
      } while (Date.now() - startTime <= waitTimeout);

      if (!waiter) throw new Error(errorMessage);
    }
  } catch (e) {
    throw new Error(`${errorMessage}\nDetails: ${e.message}`);
  }
}