Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Cypress test if input field value has multiple lines

I want to check with a cypress test if the value is correct in a single-line text fields and multi-line text area.

I here have a single line box where I use the code down below to add an input:

cy.followLabel("When did the incident happen?").type("single \nline");

Same thing for the multi line text area

cy.followLabel("Deenter code herescribe what happened").type("multiple \nlines");

enter image description here

I tried to validate it with .should() but haven't found any chainers that would suit my need.

Is there something like .contains("\n")?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Chai Assertion Library has a singleLine assertion in the chai-string module.

singleLine
assert.singleLine('abcdef');
expect('abcdef').to.be.singleLine();

To use assertions from other libraries, see Cypress recipe Adding Chai Assertions

Simple example

Install

yarn add -D chai-string

//or

npm install -D chai-string

Test

chai.use(require('chai-string'));

it('tests for singleLine', () => {

  cy.get('input')
    .type("single \nline")
    .invoke('val')
    .should('be.singleLine')  // passes

  cy.get('textarea')
    .type("multiple \nlines")
    .invoke('val')
    .should('not.be.singleLine')  // passes
})

enter image description here

about 4 years ago · Juan Pablo Isaza Report

0

You can directly check that the innerText has the new line character \n.

cy.get('selector').then(($ele) => {
  expect($ele).to.contain('\n')
})

In case your inner text doesn't have the newline characters, you assert the style tag containing overflow-wrap: break-word

cy.get('selector').should('have.attr', 'style').and('include', 'overflow-wrap: break-word')
about 4 years ago · Juan Pablo Isaza Report

0

I know there is an assertion "match" for regex.

match(RegExp)
Aliases: matches    
expect('testing').to.match(/^test/)

(from https://docs.cypress.io/guides/references/assertions)

So in your case the regex could be match(/.*\n.*/gm)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!