如何使用 webdriver.io 模拟 ctrl-click 或 shift-click? [英] How to simulate ctrl-click or shift-click with webdriver.io?

查看:31
本文介绍了如何使用 webdriver.io 模拟 ctrl-click 或 shift-click?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 webdriver.io,我想使用 shiftctrl 等修饰符模拟点击.keys() 方法似乎做了类似的事情,但我不清楚如何再次释放修饰键,当我使用 16(键代码对于 shift) 作为方法的参数 -链接.

With webdriver.io I would like to simulate clicks with a modifier like shift or ctrl. The keys() method seems to do something like that but it's not clear to me how to release a modifier key again and it throws an error when I use 16 (key code for shift) as a parameter for the method - link.

背景:在我测试的网页中,我有一个与文件浏览器中的文件和文件夹相当的元素列表,可以使用 shift 选择多个元素Ctrl.这很好用,现在我想用 webdriver.io 测试它.为此, webdriver.io 例如必须单击一个元素,然后按 shift,然后单击另一个元素,最后松开 shift 按钮.有没有办法做到这一点?

Background: In my webpage that I test I have a list of elements that are comparable to files and folders in a file browser and it is possible to select multiple of those with shift and ctrl. This works well and now I would like to test it with webdriver.io. To do this, webdriver.io e.g. has to click on an element, then press shift, then click on an other element and finally release the shift button. Is there any way to do that?

推荐答案

编辑:如果你想使用ctrl键选择不同的元素:

Edit: If you want to select different elements using ctrl key:

client.elements(<css selector for your list of elements>, function(err, res) {
    client
         .moveTo(res.value[<index of element you want to select>].ELEMENT, 0, 0)
         .keys('Ctrl') #every action after this within the scope of `client.elements` will have the `ctrl` key depressed
         .buttonPress('left')
         .moveTo(res.value[<index of element you want to select>].ELEMENT, 0, 0)
         .buttonPress('left')
         .moveTo(res.value[<index of element you want to select>].ELEMENT, 0, 0)
         .buttonPress('left')
         #repeat `.moveTo` and `.buttonPress` for every element you want to `ctrl` select
         .keys('NULL'); #This command or `.keys('Shift') will release the `shift` key.
});

要使用 shift 键进行选择,请使用以下代码(假设您要选择元素列表中的每个项目——显然您可以更改索引以获取列表的特定子部分元素).它将移动到元素列表中第一个元素的左上角,然后左键单击,然后按 shift 键,然后移动到最后一个元素的左上角,再次左键单击,然后松开 shift 键:

To select using the shift key you use the code below (assuming you want to select every item in your list of elements -- obviously you can change the indexes to get a specific subsection of your list of elements). It will move to the top left of the first element in your list of elements, then left click, then hit the shift key, then move to the top left of the last element, left click again, and then release the shift key:

client.elements(<css selector for your list of elements>, function(err, res) {
    client
         .moveTo(res.value[0].ELEMENT, 0, 0)
         .buttonPress('left')
         .keys('Shift')
         .moveTo(res.value[(res.value.length-1)].ELEMENT, 0, 0)
         .buttonPress('left')
         .keys('NULL'); #This command or `.keys('Shift') will release the `shift` key.
});

这篇关于如何使用 webdriver.io 模拟 ctrl-click 或 shift-click?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆