Search code examples
javascripttestingautomated-testse2e-testingtestcafe

TestCafé: How to use typeText without selector


I have two questions about the typeText function.

  1. According to the docs, I need two arguments: the selector and text to type. However, I'm testing an app that automatically sets focus on an input element and I can't rely on its ID, class or whatever the Selector would need. I just want to type, like in Cypress, without selecting something first. Is this possible?
  2. The typeText method also requires the second parameter (the text). But sometimes, I don't want to put any text into a field (for example testing a login component). Right now, I need to create conditions for these types of tests, because TestCafé throws an error on empty strings. Is there a better way?
static async login({ email = '', password = '' }) {
  await t
    .typeText('#email', email)
    .typeText('#password', password)
    .click('#submit');
}

Solution

  • Maybe pressKey would work for you? It requires that the input is separated by spaces but doesn't need a selector. Documentation for pressKey

    Example:

    await t
        .pressKey('u s e r @ t e s t . c o m enter')
        .pressKey('p a s s w o r d enter')
    

    I'm making the assumption that enter or maybe tab would move to the next field, but I think that could also work for your second point