# cypress

***

**1. Verifying Page Title:**

```js
cy.title().should('contain', 'Cypress.io');
```

**2. Checking Element Visibility:**

```js
cy.get('#my-element').should('be.visible');
```

**3. Interacting with Forms:**

```js
cy.get('input[name="username"]').type('admin');
cy.get('input[name="password"]').type('secret');
cy.get('button[type="submit"]').click();
```

**4. Handling Alerts:**

```js
cy.on('window:alert', (alertText) => {
  // Do something with the alert text
});
```

**5. Navigating to a URL:**

```js
cy.visit('https://example.com');
```

**6. Waiting for an Element:**

```js
cy.get('#my-element').should('be.visible').and('have.text', 'Hello World!');
```

**7. Custom Commands:**

```js
Cypress.Commands.add('login', (username, password) => {
  // Login logic
});
cy.login('admin', 'secret');
```

**8. Checking Element State:**

```js
cy.get('#my-element').should('be.checked');
```

**9. Mocking Server Responses:**

```js
cy.server();
cy.route({
  method: 'GET',
  url: '/api/users',
  response: { users: ['John', 'Jane', 'Mark'] },
});
```

**10. Debugging Tests:**

```js
cy.pause();
cy.debug();
```

**11. Using XHR Requests:**

```js
cy.request({
  method: 'GET',
  url: '/api/users',
}).should((response) => {
  expect(response.status).to.eq(200);
});
```

**12. Traversing the DOM:**

```js
cy.get('#my-element').find('.child-element');
```

**13. Using Assertions:**

```js
expect(value).to.equal(expected);
```

**14. Taking Screenshots:**

```js
cy.screenshot();
```

**15. Downloading Files:**

```js
cy.request({
  method: 'GET',
  url: '/file.pdf',
}).then((response) => {
  cy.writeFile('file.pdf', response.body);
});
```

**16. Using Data Tables:**

```js
cy.get('table').should('have.column', 'Name');
```

**17. Working with iframes:**

```js
cy.get('iframe').then(iframe => {
  const body = iframe.contents().find('body');
  // Interact with the iframe
});
```

**18. Checking Cookie Values:**

```js
cy.getCookie('my-cookie').should('contain', 'value');
```

**19. Using Fixtures:**

```js
cy.fixture('data.json').then((data) => {
  // Use the fixture data
});
```

**20. Interacting with Modals:**

```js
cy.get('.modal').should('be.visible').and('have.class', 'open');
```

**21. Checking Local Storage:**

```js
cy.window().its('localStorage.getItem', 'my-item').should('contain', 'value');
```

**22. Custom Assertions:**

```js
Cypress.Commands.add('assertFormat', (element, format) => {
  // Custom assertion logic
});
cy.get('#my-element').should('assertFormat', 'dd-mm-yyyy');
```

**23. Using Network Interception:**

```js
cy.intercept('POST', '/api/users').as('createUser');
cy.get('button[type="submit"]').click();
cy.wait('@createUser').its('response.statusCode').should('eq', 201);
```

**24. Working with Selectors:**

```js
cy.get('#my-element').within(() => {
  // Interact with elements within the selector
});
```

**25. Using Fixtures for Data Parameterization:**

```js
const users = [
  { username: 'admin', password: 'secret' },
  { username: 'user1', password: 'password1' },
];

users.forEach((user) => {
  cy.fixture('data.json').as('data');
  cy.visit('/');
  cy.get('input[name="username"]').type(user.username);
  cy.get('input[name="password"]').type(user.password);
  cy.get('button[type="submit"]').click();
});
```
