# Misc

Examples of miscellaneous commands in Cypress, for a full reference of commands, go to docs.cypress.io

# .end()

To end the command chain, use the .end() command.

<table class="table table-bordered misc-table">
  <thead>
    <tr>
      <th>Table</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>User: Cheryl</td>
    </tr>
    <tr>
      <td>User: Charles</td>
    </tr>
    <tr>
      <td>User: Darryl</td>
    </tr>
  </tbody>
</table>
Table
User: Cheryl
User: Charles
User: Darryl
// cy.end is useful when you want to end a chain of commands
// and force Cypress to re-query from the root element
cy.get('.misc-table').within(() => {
  // ends the current chain and yields null
  cy.contains('Cheryl').click().end()

  // queries the entire table again
  cy.contains('Charles').click()
})

# cy.exec()

To execute a system command, use the cy.exec() command.

// execute a system command.
// so you can take actions necessary for
// your test outside the scope of Cypress.
// https://on.cypress.io/exec

// we can use Cypress.platform string to
// select appropriate command
// https://on.cypress/io/platform
cy.log(`Platform ${Cypress.platform} architecture ${Cypress.arch}`)

// on CircleCI Windows build machines we have a failure to run bash shell
// https://github.com/cypress-io/cypress/issues/5169
// so skip some of the tests by passing flag "--env circle=true"
const isCircleOnWindows =
  Cypress.platform === 'win32' && Cypress.env('circle')

if (isCircleOnWindows) {
  cy.log('Skipping test on CircleCI')

  return
}

// cy.exec problem on Shippable CI
// https://github.com/cypress-io/cypress/issues/6718
const isShippable =
  Cypress.platform === 'linux' && Cypress.env('shippable')

if (isShippable) {
  cy.log('Skipping test on ShippableCI')

  return
}

cy.exec('echo Jane Lane').its('stdout').should('contain', 'Jane Lane')

if (Cypress.platform === 'win32') {
  cy.exec('print cypress.json').its('stderr').should('be.empty')
} else {
  cy.exec('cat cypress.json').its('stderr').should('be.empty')

  cy.exec('pwd').its('code').should('eq', 0)
}

# cy.focused()

To get the DOM element that has focus, use the cy.focused() command.

<form class="misc-form">
  <div class="form-group">
    <label for="name">Name</label>
    <input
      type="text"
      class="form-control"
      id="name"
      placeholder="Name"
    />
  </div>
  <div class="form-group">
    <label for="description">Description</label>
    <textarea class="form-control" id="description"></textarea>
  </div>
</form>
// https://on.cypress.io/focused
cy.get('.misc-form').find('#name').click()
cy.focused().should('have.id', 'name')

cy.get('.misc-form').find('#description').click()
cy.focused()
  // we can check if the focused element has a specific ID
  .should('have.id', 'description')
  // or if it matches the entire given CSS selector
  .and('match', '.misc-form textarea#description')

# cy.screenshot()

To take a screenshot, use the cy.screenshot() command.

<code>cypress/screenshots/my-image.png</code>
cypress/screenshots/my-image.png
cy.screenshot('my-image')
Cypress.Screenshot.defaults({
  blackout: ['.foo'],
  capture: 'viewport',
  clip: { x: 0, y: 0, width: 200, height: 200 },
  scale: false,
  disableTimersAndAnimations: true,
  screenshotOnRunFailure: true,
  beforeScreenshot() {},
  afterScreenshot() {},
})

# cy.wrap()

To wrap an object or a value, use the cy.wrap() command.

// https://on.cypress.io/wrap
cy.wrap({ foo: 'bar' })
  .should('have.property', 'foo')
  .and('include', 'bar')
cy.wrap(42).should('equal', 42)

Once you have an object wrapped, you can access its properties, invoke its methods, and even pass it via an alias.

cy.wrap({
  name: 'Joe',
  getName() {
    return this.name
  },
})
  .invoke('getName')
  .should('equal', 'Joe')

// the method could be a function
const getMagicNumber = () => 42
cy.wrap({
  getNumber: getMagicNumber,
})
  .invoke('getNumber')
  .should('equal', 42)

// you can wrap the object as alias
cy.wrap({
  name: 'Joe',
  getName() {
    return this.name
  },
  getNumber: getMagicNumber,
}).as('wrappedObject')
// some time later get the alias and use it
cy.get('@wrappedObject').its('name').should('equal', 'Joe')
cy.get('@wrappedObject').invoke('getName').should('equal', 'Joe')
cy.get('@wrappedObject').invoke('getNumber').should('equal', 42)

If cy.wrap receives a Promise as an argument, Cypress automatically waits for the promise to complete.

const p = new Promise((resolve) => {
  resolve('Hello')
}, 100)
cy.wrap(p).should('equal', 'Hello')