Cypress简易入门教程(下)

简介: Cypress简易入门教程(下)

4.5 鼠标操作


  • 鼠标悬停事件:


cy.get('button').trigger('mouseover')
  • 鼠标按下:


cy.get('button').trigger('mousedown')
  • 鼠标抬起:


cy.get('button').trigger('mouseleave')
cy.get('button').trigger('mouseup')
  • 鼠标长按事件:


cy.get('button').trigger('mousedown')
cy.wait(1000)
cy.get('button').trigger('mouseleave')
  • 鼠标拖拽事件
cy.get('[data-cy=draggable]')
  .trigger('mousedown', { which: 1, pageX: 600, pageY: 100 })
  .trigger('mousemove', { which: 1, pageX: 600, pageY: 600 })
  .trigger('mouseup')


describe('baidu',function(){
  context('测试百度网站',function(){
    it('查询成功',function(){
      cy.visit('https://www.baidu.com')
      cy.get('input[name=wd]').type("软件测试")
      cy.get('#su').click()
      cy.get('title').should('contain','百度搜索')
     }) 
    it('进入高级查询成功',function(){
      cy.get('a[name=tj_settingicon]').trigger('mouseover')
      cy.get('.bdpfmenu').should('exist')
     })  
  })
})


4.6 断言

  • 针对长度的断言


cy.get('li.selected').should('have.length',3)
  • 正对类的断言


cy.get('from').fijd('input').should('not.have.class','disabled')
  • 针对值断言


cy.get('textarea').should('have.value','3testing')
  • 针对文本内容的断言


cy.get('a').parent('span.help').should('contain','click me')
  • 针对元素可见与否的断言


cy.get('button').should('be.visible')
  • 针对元素存在与否的断言


cy.get('#loading').should('not.exist')
  • 针对元素状态的State的断言


cy.get(':radio').should('be.checked')
  • 针对CSS的断言


cy.get('.completed').should('have.css','text-decoration','line-through')


4.7 跨iframe操作

describe('login',function(){
  context('测试啄木鸟软件咨询网',function(){
    it('点击我的介绍成功',function(){
      cy.visit('http://www.3testing.com')
      cy.get('#head',{timeout: 2000})
            .then($iframe => {
                cy.wrap($iframe.contents().find("#introduce"));
            })
            .then($btn => {
                cy.wrap($btn).click()
            });
      //断言
      cy.url().should('include','introduce.html')
      cy.get('title').should('contain','啄木鸟软件测试咨询网-顾翔介绍')
     })  
  })
})


4.8 多窗口操作

Cypress不支持多窗口操作。


5 API测试


5.1 普通API测试

describe('login',function(){
  const username = 'cindy'
  const password = '123456'
  const producturl="http://127.0.0.1:8000/login_action/"
  cy.request({
        method: 'POST',
        url: producturl,
        body: {
          username: username
          password: password
}
      })
  })


5.2 CSRF token API测试

describe('login',function(){
  const username = 'cindy'
  const password = '123456'
  const producturl="http://127.0.0.1:8000/login_action/"
    Cypress.Commands.add('loginByCSRF', (csrfToken) => {
    cy.request({
        method: 'POST',
        url: producturl,
        failOnStatusCode: false, // 不要失败,这样我们才能断言
        form: true, // 我们正在提交一份常规表格
        body: {
          username,
          password,
          csrfmiddlewaretoken: csrfToken // 将此作为主体的一部分插入
        }
      })
  })
  // csrf在返回的html中,我测试的Django产品的CSRF token用这种方法
  it('策略#1:从HTML解析令牌', function(){
    // 如果我们不能改变我们的服务器代码以使解析CSRF令牌变得更容易,
    // 我们可以简单地使用cy.request来获取登录页面,然后解析HTML内容
    // 以找到嵌入在页面中的CSRF令牌
    cy.request(producturl)
      .its('body')
      .then((body) => {
        //我们可以用Cypress.$解析字符串主体,从而使我们能够轻松地查询到它
    cy.log(body)
        const $html = Cypress.$(body)
        const csrf  = $html.find("input[name=csrfmiddlewaretoken]").val()
    cy.loginByCSRF(csrf)
          .then((resp) => {
            expect(resp.status).to.eq(200)
      expect(resp.body).to.contain("Company 2017")
          })
      })
    })
  })
/*
  // 如果csrf在响应头中
  it('策略#2:从响应头解析令牌', function(){
    // 如果我们将csrf令牌嵌入到响应头中,那么我们就可以更容易地提取它,
    // 而不必深究最终的HTML
    cy.request(producturl)
      .its('headers')
      .then((headers) => {
        const csrf = headers['csrftoken']
    cy.log(csrf)
        cy.loginByCSRF(csrf)
          .then((resp) => {
            expect(resp.status).to.eq(200)
            expect(resp.body).to.contain("Company 2017")
          })
      })
    })
   }) */


————————————————————


软件安全测试

https://study.163.com/course/courseMain.htm?courseId=1209779852&share=2&shareId=480000002205486

接口自动化测试

https://study.163.com/course/courseMain.htm?courseId=1209794815&share=2&shareId=480000002205486

DevOps 和Jenkins之DevOps

https://study.163.com/course/courseMain.htm?courseId=1209817844&share=2&shareId=480000002205486

DevOps与Jenkins 2.0之Jenkins

https://study.163.com/course/courseMain.htm?courseId=1209819843&share=2&shareId=480000002205486

Selenium自动化测试

https://study.163.com/course/courseMain.htm?courseId=1209835807&share=2&shareId=480000002205486

性能测试第1季:性能测试基础知识

https://study.163.com/course/courseMain.htm?courseId=1209852815&share=2&shareId=480000002205486

性能测试第2季:LoadRunner12使用

https://study.163.com/course/courseMain.htm?courseId=1209980013&share=2&shareId=480000002205486

性能测试第3季:JMeter工具使用

https://study.163.com/course/courseMain.htm?courseId=1209903814&share=2&shareId=480000002205486

性能测试第4季:监控与调优

https://study.163.com/course/courseMain.htm?courseId=1209959801&share=2&shareId=480000002205486

Django入门

https://study.163.com/course/courseMain.htm?courseId=1210020806&share=2&shareId=480000002205486

啄木鸟顾老师漫谈软件测试

https://study.163.com/course/courseMain.htm?courseId=1209958326&share=2&shareId=480000002205486

相关实践学习
通过性能测试PTS对云服务器ECS进行规格选择与性能压测
本文为您介绍如何利用性能测试PTS对云服务器ECS进行规格选择与性能压测。
目录
相关文章
|
5月前
|
开发框架 小程序 JavaScript
基于mpvue框架的小程序项目搭建入门教程一
基于mpvue框架的小程序项目搭建入门教程一
113 0
|
4月前
|
JavaScript 前端开发
Nodejs 第六章(npx)
Nodejs 第六章(npx)
44 0
|
8月前
|
JSON 自然语言处理 JavaScript
TypeChat 入门指南
TypeChat 是一个革命性的库,它简化了使用 TypeScript 构建自然语言模型界面的过程。 它抹平了自然语言和结构化数据之间的差距,使开发人员更容易将自然语言界面集成到他们的应用程序中。
324 0
|
资源调度 JavaScript 前端开发
Cypress简易入门教程(上)
Cypress简易入门教程(上)
369 0
|
存储 Web App开发 移动开发
Day 19: EmberJS 入门指南
到目前为止,我们这一系列文章涉及了Bower、AngularJS、GruntJS、PhoneGap和MeteorJS 这些JavaScript技术。今天我打算学习一个名为Ember的框架。本文将介绍如何用Ember创建一个单页面的社交化书签应用。本教程将包括两篇:第1篇介绍客户端代码和用HTML 5本地存储持久保存数据,第2篇中我们将使用一个部署在OpenShift上的REST后端。过几天我会写第2篇。
285 0
Day 19: EmberJS 入门指南
|
存储 JavaScript 前端开发
grunt入门笔记
grunt在前端工具中算是很有用的一个工具。 想一想如果没有这个工具,我们需要手动新建一个压缩代码后的文件夹,每次修改原始文件,都要手动压缩一下,再保存到压缩后的文件夹,想想都要疯掉。所以,grunt前端必不可少。
94 0
grunt入门笔记
|
JSON 测试技术 Linux
【HttpRunner v3.x】笔记 ——1. 环境安装
【HttpRunner v3.x】笔记 ——1. 环境安装
【HttpRunner v3.x】笔记 ——1. 环境安装
Cypress系列(0)- 如何学习 Cypress
Cypress系列(0)- 如何学习 Cypress
244 0
Cypress系列(0)- 如何学习 Cypress
|
Web App开发 JSON JavaScript
Cypress系列(2)- Cypress 框架的详细介绍
Cypress系列(2)- Cypress 框架的详细介绍
744 0
Cypress系列(2)- Cypress 框架的详细介绍
|
Web App开发 编解码 前端开发
常用的前端自动化测试工具介绍 —— Karma
常用的前端自动化测试工具介绍 —— Karma
624 0
常用的前端自动化测试工具介绍 —— Karma