admin管理员组文章数量:1313801
I have an issue when using the Cypress type() mand in the way I want.
My objective
I want to be able to select and delete text in a textfield. I want this done via holding the shift key, pressing the right arrow key multiple times and then pressing the delete key.
My attempt
//hold shift and use right arrow
cy.type('{shift}{rightarrow}'.repeat(10));
//press delete
cy.type('{del}');
I have an issue when using the Cypress type() mand in the way I want.
My objective
I want to be able to select and delete text in a textfield. I want this done via holding the shift key, pressing the right arrow key multiple times and then pressing the delete key.
My attempt
//hold shift and use right arrow
cy.type('{shift}{rightarrow}'.repeat(10));
//press delete
cy.type('{del}');
Share
Improve this question
edited Jan 27, 2022 at 19:02
Taran
asked Jan 27, 2022 at 18:54
TaranTaran
5312 gold badges8 silver badges22 bronze badges
10
-
Have you tried
cy.type('{shift}{rightarrow}'.repeat(10))
? – Ruan Mendes Commented Jan 27, 2022 at 18:57 - @JuanMendes yes even with that it seems to still not be selecting the text like i expect. I will change my original attempt as cypress docs explain modifiers are released after each type mand – Taran Commented Jan 27, 2022 at 19:01
-
.repeat(10)
where is this ing from. I could see nothing in the docs? – Alapan Das Commented Jan 27, 2022 at 19:04 - @AlapanDas String.repeat is a native javascript mand I believe, to repeat the string. This example i tested works fine with right arrow alone – Taran Commented Jan 27, 2022 at 19:17
-
If you just use
{shift}{rightarrow}
without the repeat, then does this work once ? – Alapan Das Commented Jan 27, 2022 at 19:36
3 Answers
Reset to default 4As per the available information instead of repeat
, you can use the Cypress._.times
loadash method. This will run the entire type mand 10 times.
Cypress._.times(10, () => {
cy.type('{shift}{rightarrow}')
})
The element needs focus before applying arrow keys,
cy.get('#myinput')
.focus()
.type(Cypress._.repeat('{shift}{rightarrow}', 5))
.type('{del}')
This gets the cursor moving within the the element's text, but unfortunately does not extend the selection, so {del}
only removes one element.
You can use an internal input method instead of {shift}{rightarrow}
, if this suits your test
cy.get('#myinput')
.focus()
.then($el => $el[0].setSelectionRange(0, 5))
.type('{del}')
To clear pletely,
cy.get('#myinput')
.clear() // types {selectall}{del}
For anyone else that es looking, I had success with this method of using {release:false}
with the modifier. Here's some example code for selecting and deleting the two lines above:
cy.get("body").type("{shift}", {release: false});
cy.get("body").type("{upArrow}{upArrow}");
cy.get("body").type("{shift}");
cy.get("body").type("{backspace}");
本文标签: javascriptHow to hold shift and use arrow keys in CypressStack Overflow
版权声明:本文标题:javascript - How to hold shift and use arrow keys in Cypress - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741955631a2406972.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论