TestCafe – How to check if a web element exists or does not exist without failing the test?

I’m trying to write a script that needs to adapt it’s workflow behavior depending on whether a particular browser object found by CSS selector exists or does not exist.

I do not want to use a document.getElementByID method as this is not technically a CSS selector, and our entire enterprise is standardized on CSS selector so anything that walks the DOM other then a CSS selector won’t make it past our code review process anyway.

var thing = await things.thingSelector(thingName);
if (await t.expect(thing.exists).notOk()) {
   await t.click(things.OpenThing(thingName));
} else {
   return false;
return true;

Where thingSelector is:

const thingSelector = name =>
  Selector('p.thing-header-title span')

Where OpenThing is:

const OpenThing = name =>

I need to be able to continue execution if the object is not there and I’m checking that it exists, or if the object is there and I’m checking that it does not exist, and also the cases where the object is not there and it does not exist and the object is not there and I’m checking that it does not exist.

In all cases I still need to proceed with the workflow.

I’ve tried both sides of the logic coin:

if (!await t.expect(thing.exists).ok())


if (await t.expect(thing.exists).notOk())

If one of the above doesn’t fail in one scenario it will fail in the other, and the other one will fail in the scenario that the first one didn’t fail. I need something that will give me the logic, but not ever fail the script execution and still allow me to return either True or False depending on if the object is present or not present.

Read More:   Parse Error: Adjacent JSX elements must be wrapped in an enclosing tag

Thank you in advance for helping me to solve this problem, and also to learn and grow in my Javascript skills!

You can check the async exists property in the if condition in the following way:

if(await things.thingSelector(thingName).exists) {
    // do something 

You can use the following assertions to test existence and non-existence elements:

test('Test existence and non-existence elements', async t => {
    await t

  • How to check if an element exists:

      test('Element with id "element" exists', async t => {
      await t.expect(Selector('#element').exists).ok(); });
  • How to check if an element does NOT exist:

     test('Element with id "element" shouldn\'t exist', async t => {
     await t.expect(Selector('#element').exists).notOk(); });

Check out the official documentation.

this is working for me, you can give it a try

async veriryCreativeIconButtonNotExists(){
await t.expect(this.exportButton.exists).ok()
await t.expect(this.columnPickerIcon.exists).ok()
await t.expect(this.filterColumnIcon.exists).ok()
if (await t.expect(this.columnPickerIcon.exists).ok()){
  await t.expect(this.creavtiveIconButton.exists).ok()
  await t.click(this.creavtiveIconButton)
  await t.expect(this.creativeImage.exists).ok()
  await t.click(this.creativeImage)
} else {
  return false;
return true;

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts