Use custom Cypress command for reusable assertions

We’re duplicating quite a few commands between the registration and login of our user for assertions. Let’s see how we can take these assertions and create a custom command to make the assertions.

We have the tests:

    it('should register a new user', () => {
cy.createUser().then(user => {
cy.visit('/')
.getByText(/login/i)
.click()
.getByLabelText(/username/i)
.type(user.username)
.getByLabelText(/password/i)
.type(user.password)
.getByText(/submit/i)
.click() // verify the user in localStorage
.url()
.should('eq', `${Cypress.config().baseUrl}/`)
.window()
.its('localStorage.token')
.should('be.a', 'string')
.getByTestId('username-display', {timeout: 500})
.should('have.text', user.username)
})
});

We can create some assertions commands to make it more reusable:

Cypress.Commands.add('assertHome', () => {
cy.url().should('eq', `${Cypress.config().baseUrl}/`)
}) Cypress.Commands.add('assertLoggedInAs', user => {
cy
.window()
.its('localStorage.token')
.should('be.a', 'string')
.getByTestId('username-display', {timeout: 500})
.should('have.text', user.username)
})

Then we can improve the test:

    it('should register a new user', () => {
cy.createUser().then(user => {
cy.visit('/')
.getByText(/login/i)
.click()
.getByLabelText(/username/i)
.type(user.username)
.getByLabelText(/password/i)
.type(user.password)
.getByText(/submit/i)
.click()
.assertHome()
.assertLoggedInAs(user);
})
});

Run tests as an authenticated user with Cypress

For most applications you’re going to need to be logged in as a user to interact with the application. Let’s see how we can register as a new user and login as that user to test using the application as a logged in user.

Sometime you want to check some DOM element is not present, you cna use queryByTestId()

    it('displays the username', () => {
cy.createUser().then(user => {
cy.visit('/')
.getByText(/login/i)
.click()
.getByLabelText(/username/i)
.type(user.username)
.getByLabelText(/password/i)
.type(user.password)
.getByText(/submit/i)
.click()
.assertLoggedInAs(user)
.getByText(/logout/i)
.click()
.queryByTestId('username-display', {timeout: 300})
.should('not.exist')
})
});

Combine custom Cypress commands into a single custom command

Almost every time we need to login, we’ll want to have a newly created user to login as. Let’s go ahead and combine the createNewUser and logincommands to create a single loginAsNewUser which we can use in any test that needs an authenticated user.

// support/commands.js

Cypress.Commands.add('loginAsNewUser', () => {
cy.createUser().then(user => {
cy.login(user)
});
}); Cypress.Commands.add('login', user => {
cy.request({
url: 'http://localhost:3000/login',
method: 'POST',
body: user,
}).then(({body}) => {
window.localStorage.setItem('token', body.user.token);
return body.user;
})
})
// e2e/calcualtor.js

describe('authenticated calculator', () => {
it('displays the username', () => {
cy.loginAsNewUser().then((user) => {
cy.visit('/')
.getByTestId('username-display')
.should('have.text', user.username)
.getByText(/logout/i)
.click()
.queryByTestId('username-display', {timeout: 300})
.should('not.exist')
})
});
})

Install React DevTools with Cypress

Because Cypress runs in a real Chrome browser, we can install extensions, like React DevTools. The tricky bit will be to make our application hook up to the extension properly.

react-dev-tools.js

if (window.Cypress) {
window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = window.parent.__REACT_DEVTOOLS_GLOBAL_HOOK__
}

Import the script before we import the REACT

index.js

import './react-dev-tools'

import './global.css'
import React from 'react'

[Cypress] install, configure, and script Cypress for JavaScript web applications -- part3的更多相关文章

  1. [Cypress] install, configure, and script Cypress for JavaScript web applications -- part1

    Despite the fact that Cypress is an application that runs natively on your machine, you can install ...

  2. [Cypress] install, configure, and script Cypress for JavaScript web applications -- part2

    Use Cypress to test user registration Let’s write a test to fill out our registration form. Because ...

  3. [Cypress] install, configure, and script Cypress for JavaScript web applications -- part4

    Load Data from Test Fixtures in Cypress When creating integration tests with Cypress, we’ll often wa ...

  4. [Cypress] install, configure, and script Cypress for JavaScript web applications -- part5

    Use the Most Robust Selector for Cypress Tests Which selectors your choose for your tests matter, a ...

  5. Cypress系列(3)- Cypress 的初次体验

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 前言 这里的栗子项目时 Cypress ...

  6. Cypress系列(41)- Cypress 的测试报告

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 注意 51 testting 有一篇文章 ...

  7. document.write('<script type=\"text/javascript\"><\/script>')

    document.write('<script type=\"text/javascript\"><\/script>')

  8. <script language = "javascript">, <script type = "text/javascript">和<script language = "application/javascript">(转)

          application/javascript是服务器端处理js文件的mime类型,text/javascript是浏览器处理js的mime类型,后者兼容性更好(虽然application/ ...

  9. 2.1 <script>元素【JavaScript高级程序设计第三版】

    向 HTML 页面中插入 JavaScript 的主要方法,就是使用<script>元素.这个元素由 Netscape 创造并在 Netscape Navigator 2 中首先实现.后来 ...

随机推荐

  1. JavaScript正则表达式-相关的String对象方法

    match()方法 match(regExp); 使用指定的正则表达式来搜索字符串. 如果找到匹配字符串返回一个数组,否则返回null. 返回的数组包含两个属性:index和input. index是 ...

  2. ubuntu 执行apt-get update报错Failed to fetch

    在ubuntu下执行sudo apt-get update时,经常会遇到报错: Err http://security.ubuntu.com precise-security InRelease Er ...

  3. LDAP学习小结【仅原理和基础篇】

    此篇文章花费了好几个晚上,大部分是软件翻译的英文文档,加上自己的理解所写,希望学习者能尊重每个人的努力. 我有句话想送给每个看我文章的人: 慢就是快,快就是慢!!! 另外更希望更多人能从认真从原理学习 ...

  4. 【01】报错:webpack 不是内部或不可执行命令

    [02] webpack 不是内部或不可执行命令 一般来安装完之后是可以直接执行的你可以执行 webpack -v 或者是 webpack --help   这样的就是正确的,我的问题的解决办法是 将 ...

  5. MHA的介绍和测试(一)

    MHA的介绍 MySQL的MHA:MySQL的高级可用性管理器和工具MHA的主要目标是在短(通常为10-30秒)的停机时间内自动化主故障转移和slave升级,不受复制一致性问题的困扰,不需要花费大量的 ...

  6. c++ primer note

    ---恢复内容开始--- 1.decltype 2.auto 3.cbegin 4.cend 5.constexpr 6.(*Parray)[10]=&arr; //Parray 指向一个含有 ...

  7. leetcode 350

    找两个数组的交叉部分,可以用map进行标记 首先对第一个数组进行记录,第二个数组在第一个数组记录基础上进行判断,如果在第一个数组出现过,就记录 class Solution { public: vec ...

  8. #1045 - Access denied for user 'root'@'localhost' (using password: NO)的问题

    问题描述:  修改了root的密码,然后在http://localhost/phpmyadmin下无法登录了  报错:#1045 - Access denied for user 'root'@'lo ...

  9. *AtCoder Grand Contest 002F - Leftmost Ball

    $n \leq 2000,k \leq 2000$,现$n$种球每种有$k$个,在一种排列中,会把每种颜色的球第一个出现的涂成第0种(不同于原来的n种)颜色,问最终会出现多少种不同的序列.膜1e9+7 ...

  10. thinkphp框架做项目的前期配置

    ThinkPHP 目录结构说明 ThinkPHP.php:框架的公共入口文件 App:项目放置目录 Common:包含框架的一些公共文件.系统定义.系统函数和惯例配置等 Lang:系统语言文件目录 L ...