Cypress web自动化4-操作页面元素(Actions行为事件)

前言

ui自动化操作页面上的元素,常用的方法就那么几个,输入文本,点击元素,清空文本,点击按钮。
还有一些特殊的checkbox,radio,滚动条等。

.type()

往输入框输入文本元素

Email addressDisabled Textarea

cy.get('.action-email')
  .type('fake@email.com').should('have.value', 'fake@email.com')

  // .type() with special character sequences
  .type('{leftarrow}{rightarrow}{uparrow}{downarrow}')
  .type('{del}{selectall}{backspace}')

  // .type() with key modifiers
  .type('{alt}{option}') //these are equivalent
  .type('{ctrl}{control}') //these are equivalent
  .type('{meta}{command}{cmd}') //these are equivalent
  .type('{shift}')

  // Delay each keypress by 0.1 sec
  .type('slow.typing@email.com', { delay: 100 })
  .should('have.value', 'slow.typing@email.com')

cy.get('.action-disabled')
  // Ignore error checking prior to type
  // like whether the input is visible or disabled
  .type('disabled error checking', { force: true })
  .should('have.value', 'disabled error checking')
.focus()

要聚焦DOM元素,请使用.focus()命令

Password

cy.get('.action-focus').focus()
  .should('have.class', 'focus')
  .prev().should('have.attr', 'style', 'color: orange;')
.blur()

判断输入框失去焦点时候,按钮的颜色

Full Name

cy.get('.action-blur').type('About to blur').blur()
  .should('have.class', 'error')
  .prev().should('have.attr', 'style', 'color: red;')
.clear()

清空DOM元素

cy.get('.action-clear').type('Clear this text')
  .should('have.value', 'Clear this text')
  .clear()
  .should('have.value', '')
.submit()

提交form表单

Coupon CodeSubmit

cy.get('.action-form')
  .find('[type="text"]').type('HALFOFF')
cy.get('.action-form').submit()
  .next().should('contain', 'Your form has been submitted!')
.click()

点击DOM元素

cy.get('.action-btn').click()

// clicking in the center of the element is the default
cy.get('#action-canvas').click()

cy.get('#action-canvas').click('topLeft')
cy.get('#action-canvas').click('top')
cy.get('#action-canvas').click('topRight')
cy.get('#action-canvas').click('left')
cy.get('#action-canvas').click('right')
cy.get('#action-canvas').click('bottomLeft')
cy.get('#action-canvas').click('bottom')
cy.get('#action-canvas').click('bottomRight')

// .click() accepts a an x and y coordinate
// that controls where the click occurs :)
cy.get('#action-canvas')
  .click(80, 75)
  .click(170, 75)
  .click(80, 165)
  .click(100, 185)
  .click(125, 190)
  .click(150, 185)
  .click(170, 165)

// click multiple elements by passing multiple: true
cy.get('.action-labels>.label').click({ multiple: true })

// Ignore error checking prior to clicking
cy.get('.action-opacity>.btn').click({ force: true })
.dblclick()

双击 DOM 元素

cy.get('.action-div').dblclick().should('not.be.visible')
cy.get('.action-input-hidden').should('be.visible')
.rightclick()

右击 DOM 元素

cy.get('.action-div').rightclick().should('not.be.visible')
                cy.get('.action-input-hidden').should('be.visible')
.check()

检查 checkbox or radio 选项框

// By default, .check() will check all
// matching checkbox or radio elements in succession, one after another
cy.get('.action-checkboxes [type="checkbox"]').not('[disabled]')
  .check().should('be.checked')

cy.get('.action-radios [type="radio"]').not('[disabled]')
  .check().should('be.checked')

// .check() accepts a value argument
cy.get('.action-radios [type="radio"]')
  .check('radio1').should('be.checked')

// .check() accepts an array of values
cy.get('.action-multiple-checkboxes [type="checkbox"]')
  .check(['checkbox1', 'checkbox2']).should('be.checked')

// Ignore error checking prior to checking
cy.get('.action-checkboxes [disabled]')
  .check({ force: true }).should('be.checked')

cy.get('.action-radios [type="radio"]')
  .check('radio3', { force: true }).should('be.checked')
.uncheck()

检查checkbox or radio 未被选中

// By default, .uncheck() will uncheck all matching
// checkbox elements in succession, one after another
cy.get('.action-check [type="checkbox"]')
  .not('[disabled]')
  .uncheck().should('not.be.checked')

// .uncheck() accepts a value argument
cy.get('.action-check [type="checkbox"]')
  .check('checkbox1')
  .uncheck('checkbox1').should('not.be.checked')

// .uncheck() accepts an array of values
cy.get('.action-check [type="checkbox"]')
  .check(['checkbox1', 'checkbox3'])
  .uncheck(['checkbox1', 'checkbox3']).should('not.be.checked')

// Ignore error checking prior to unchecking
cy.get('.action-check [disabled]')
  .uncheck({ force: true }).should('not.be.checked')
.select()

select 选项框

--Select a fruit--applesorangesbananasapplesorangesbananas

// Select option(s) with matching text content
cy.get('.action-select').select('apples')

cy.get('.action-select-multiple')
.select(['apples', 'oranges', 'bananas'])

// Select option(s) with matching value
cy.get('.action-select').select('fr-bananas')

cy.get('.action-select-multiple')
  .select(['fr-apples', 'fr-oranges', 'fr-bananas'])
.scrollIntoView()

内嵌滚动条滑动,三种情况:左右滑动,上下滑动,同时可以左右上下滑动

Horizontal ScrollI'm HereVertical ScrollI'm HereBoth ScrollI'm Here



cy.get('#scroll-horizontal button')
  .should('not.be.visible')

// scroll the button into view, as if the user had scrolled
cy.get('#scroll-horizontal button').scrollIntoView()
  .should('be.visible')

cy.get('#scroll-vertical button')
  .should('not.be.visible')

// Cypress handles the scroll direction needed
cy.get('#scroll-vertical button').scrollIntoView()
  .should('be.visible')

cy.get('#scroll-both button')
  .should('not.be.visible')

// Cypress knows to scroll to the right and down
cy.get('#scroll-both button').scrollIntoView()
  .should('be.visible')
cy.scrollTo()

浏览器自带的滚动条

// if you chain .scrollTo() off of cy, we will
// scroll the entire window
cy.scrollTo('bottom')

cy.get('#scrollable-horizontal').scrollTo('right')

// or you can scroll to a specific coordinate:
// (x axis, y axis) in pixels
cy.get('#scrollable-vertical').scrollTo(250, 250)

// or you can scroll to a specific percentage
// of the (width, height) of the element
cy.get('#scrollable-both').scrollTo('75%', '25%')

// control the easing of the scroll (default is 'swing')
cy.get('#scrollable-vertical').scrollTo('center', { easing: 'linear' })

// control the duration of the scroll (in ms)
cy.get('#scrollable-both').scrollTo('center', { duration: 2000 })
.trigger()

在DOM元素上触发事件

Range Input0

cy.get('.trigger-input-range')
  .invoke('val', 25)
  .trigger('change')
  .get('input[type=range]').siblings('p')
  .should('have.text', '25')


原文地址:https://blog.51cto.com/u_15249893/2851997

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


本文技术难度★★★,如果前编内容顺利执行,请继续。如果Selenium尚无法灵活运用的读者,本文可能难度较大。“理论联系实惠,密切联系领导,表扬和自我表扬”——我就是老司机,曾经写文章教各位怎么打拼职场的老司机。不记得没关系,只需要知道:有这么一位IT老司机,穿上西装,带大家打拼职场!
如果想从头学起Cypress,可以看下面的系列文章哦https://www.cnblogs.com/poloyy/category/1768839.html Cypress简介基于JavaScript的前端测试工具,可以对浏览器中运行的任何内容进行快速、简单、可靠的测试Cypress是自集成的,提供了一套完整的端到端测试,无须借助其他外部
web技术已经进化了,web的测试技术最终还是跟上了脚步,新一代的web自动化技术出现了?Cypress可以对在浏览器中运行的任何东西进行快速、简单和可靠的测试。“Thewebhasevolved.Finally,testinghastoo.Fast,easyandreliabletestingforanythingthatrunsinabrowser.”
不很久不很久以前据说某家公司有两位前端,天天撸bug,为啥嘞?只怪测试MM倾人国,轻语哥哥有bug。✧(๑•̀ㅂ•́)و✧可是最近两位有点犯愁Σ(っ°Д°;)っ。测试MM有几次提了紧急bug,都在旁边鼓励他们改bug了,可是线上bug重现排查比较麻烦,而且改了后还发现没改好,惹得测试MM潸然泪
我刚开始学习JavaScript并且一直在使用赛普拉斯来自动化一些回归测试.我当前编写的测试旨在验证链接的文本和页眉和页脚中的href.我遇到的问题是这些测试需要在各种环境中运行,我似乎无法访问cypress.json中设置的baseUrl属性,以便在我的断言中设置域.在后面的脚本中,它是行cy.ge
cypress在pycharm中编写首先打开pycharm,打开cypress项目 打开此文件  打开后需要在pycharm中设置js语言  设置成功后在intergration文件夹下,创建用例文件夹demo1,创建test.js文件编写代码:  此段代码为进入百度页面,输入Jaredhan,点击搜索后等待1000ms 1、des
前言面试时间经常被问到:你的测试数据放哪?有没有做到测试数据和代码的分类?Cypress使用cypress/fixture目录存放json文件数据,cy.fixture()加载测试数据。官方文档参考https://docs.cypress.io/api/commands/fixture.html#Syntaxfixture使用在cypress/fixture目录写个
示例代码如下:比如现在要点击"loginFrame"中的"J_Quick2Static"按钮cy.get('#loginFrame',{timeout:2000}).then($iframe=>{cy.wrap($iframe.contents().find("#J_Quick2Static"));})
前言Cypress提供了hooks函数,方便我们在组织测试用例的时候,设置用例的前置操作和后置清理。类似于python的unittest里面的setUp和setUpclass功能HooksCypress提供了hooks函数。这些有助于设置要在一组测试之前或每个测试之前运行的条件。它们也有助于在一组测试
前言cypress上默认访问一个跨域的网页会出现异常:CypressdetectedacrossoriginerrorhappenedonpageloadAcrossoriginerrorhappenswhenyourapplicationnavigatestoanewURLwhichdoesnotmatchtheoriginpolicyabove.之前使用selenium的时候,不用关
前言先定位父元素,通过父元素定位子元素.children()通过父元素,定位子元素<olclass="traversal-breadcrumbbreadcrumb"><li><ahref="#">Home</a></li><li><ahref="#">Library</a></l
LoadDatafromTestFixturesinCypressWhencreatingintegrationtestswithCypress,we’lloftenwanttostubnetworkrequeststhatrespondwithlargedatasets.Allofthismockdatacanleadtotestcodethatishardtoread.Inthislesson,we’llseeh
我在应用程序中有一个端点.我们在每个请求中使用params中的不同操作来达到相同的api.网址/application/api样本请求有效负载1:{"action":"CARD_TRANSACTION_HISTORY","data":{"date_from":"2018-12-01","date_to":"2018-12-31","
如果想从头学起Cypress,可以看下面的系列文章哦https://www.cnblogs.com/poloyy/category/1768839.html 前言啥是可操作类型?就是可以和DOM元素交互的命令,比如:点击,双击.....等等等这些命令模拟用户和应用程序交互,Cypress会触发浏览器事件,进而触发应用程序绑定的时间这一
如果想从头学起Cypress,可以看下面的系列文章哦https://www.cnblogs.com/poloyy/category/1768839.html 前言重试(Retry-ability)是Cypress的核心概念之一,有助于我们写出更加健壮的测试 命令和断言Cypress测试中经常被调用的两种类型,仍以前面说到的 testLogin.js 为栗
前言做web自动化的时候,我们希望用例失败了能截图,这是之前用selenium写ui自动化用例的时候经常遇到的问题。cypress可以说是业界良心了,不仅帮我们失败的时候自动截图保存下来,还把每个用例都录制成了mp4的小视频,方便追踪问题的时候回复视频screenshots失败截图当我们用
一:Cypress简介随便着Web应用项目的不断发展,测试也要不断发展,Cypress是为现代网络打造的,基于JavaScript语言的一种前端自动化测试工具,测试也是如此,对浏览器中运行的所有内容进行快速,轻松和可靠的测试。Cypress是自集成的,它提供了一套完整的端到端的测试体验,正如官网上所说:安装C
1、fixture路径下创建.json文件夹,内容如下{"loginName":"huahua","password":"huahua"}  2、加载loginEmgs文件(后缀可省略),使用then读取数据before(()=>{cy.fixtures('loginEmgs').then((data)=>{this.data=data;})}) 3、使用数据cy.
命令行安装Cypress:npminstallcypress--save-devnpminit编辑package.json,添加如下的json语句:"scripts":{"open":"cypressopen"},然后执行命令npmrunopen,即可打开Cypresstestrunner:默认看到的examples文件夹里有很多现成的例子:我们在int
如果想从头学起Cypress,可以看下面的系列文章哦https://www.cnblogs.com/poloyy/category/1768839.html 重试的介绍学习前的三问什么是重试测试为什么重试很重要如何使用重试 为什么要重试使用Cypress进行自动化测试时,仍然会存在一些难以验证的行为,并使得测试变得