Cypress学习笔记5——官方示例

  引言

  Cypress项目中官方写了不少demo,咱们能够拿来作测试案例的。地址:https://github.com/cypress-io/cypress-example-recipeshtml

  安装

  一、首先你要安装git,具体如何安装请百度,安装后,如图:node

  

 

 

   二、其次,输入命令,回车:git

git clone https://github.com/cypress-io/cypress-example-recipes.git

  

 

 

   三、安装完后,进入目录查看:github

 

 

   会多一个文件cypress-example-recipesweb

  四、安装依赖:npm

npm install

  若是npm不行,请用下面命令:json

npm install cnpm -g --registry=https://registry.npm.taobao.org

  再次安装:浏览器

cnpm install

  安装完成后,目录结构:性能

  

 

 

  启动

  启动测试应用时,能够进入不一样子项目文件夹来启动不一样的应用;学习

  若是咱们要测试表单类型的登陆,能够打开如下被测应用

cd examples\logging-in__html-web-forms>

  

cnpm start

  

  使用浏览器访问:http://localhost:7077/ ,如图:

  

 

  验证登陆是否可用

  网页是打开了,那么帐号密码在哪里呢?

  咱们打开文件server.js,如图:

  

 

   使用notepad打开:

  

 

   知道帐号和密码,返回浏览器中登陆:

  

 

   证实登陆成功!

  编写测试脚本

  再来回顾一下测试结构:

- cypress // cypress目录
---- fixtures 测试数据配置文件,可使用fixture方法读取
---- integration 测试脚本文件
---- plugin 插件文件
---- support 支持文件
- cypress.json // cypress全局配置文件
- package.json //这个要本身建立

  而咱们脚本是新建在此目录下:cypress安装路径\node_modules\.bin\cypress\integration ,建立一个 js 文件

/*@author: Leo

@software: pycharm

@file:  demo_login.js

@time: 2020/7/6 0006 23:10

@Desc:
*/


/* __author__ = 'Leo' */

describe('登录官方案例', function() {
    beforeEach(() => {
          cy.visit('http://localhost:7077/login')
        })

    it("登录案例", function()
    {
        // 输入用户名
        cy.get('[name="username"]').type('jane.lane')
              .should('have.value', 'jane.lane')
        // 输入密码
        cy.get('[name="password"]').type('password123')
              .should('have.value', 'password123')
        // 提交表单
        cy.get('[type="submit"]').click()
        // 判断页面跳转到 /dashboard
        cy.url().should('include', '/dashboard')
        // Welcome to the Dashboard, jane.lane!
        cy.get('body').should('contain', 'Welcome to the Dashboard, jane.lane!')

    })
    })

  运行测试

  咱们换个命令运行:

yarn cypress:open

  

  启动界面:

 

  运行结果:

 

   速度很快,0.2秒完成。

  总结

  有兴趣能够持续关注。另外喜欢测试开发、性能测试的伙伴能够加入学习交流QQ群,一块儿学习成长。

相关文章
相关标签/搜索