量角器:Ctrl单击 [英] Protractor: Ctrl Click

查看:53
本文介绍了量角器:Ctrl单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ctrl单击页面上的连续元素来选择页面上的多个元素.手动完成此功能后效果很好,但是我在使用量角器进行自动化时遇到了一些麻烦.

I am trying to select multiple elements on my page using ctrl click on successive elements. This functionality works fine when done manually, but I am having some troubles with its automation using protractor.

这是我的ptor函数:

This is my ptor function:

this.selectElements = function (names) {
    for(var i = 0; i < names.length; i++){
        var parentElement = element(by.xpath('//div[@aria-label="select group ' + names[i] + '"]'));
        browser.wait(EC.presenceOf(parentElement), DEFAULT_WAIT_TIMEOUT);
        browser.actions()
            .mouseMove(parentElement).perform();
        browser.sleep(500);
        browser.actions().keyDown(protractor.Key.CONTROL)
            .click()
            .perform();
    }

因此,对于名称中的每个值,它获取DOM中的元素,将鼠标移动到DOM上,休眠,然后按ctrl单击.

So for each value in names it gets the element in the DOM, moves the mouse on it, sleeps then ctrl clicks.

在六个元素上调用此函数的结果如下,选择了以下元素:

The result of calling this function on six elements is as follows, the following elements are selected:

  • 第一个元素
  • 第三名和第二名
  • 第四个
  • 第五与第四
  • 第六名
  • the first element alone
  • the second alone
  • the third with the second
  • the fourth alone
  • the fifth WITH the fourth
  • the sixth alone

换句话说,它最多选择两个元素,然后取消选择所有元素并再次选择最多两个元素.知道那里发生了什么吗?

In other words it selects at most two elements, then deselects all and selects again at most two. Any idea what is going on there?

其他问题:是否可以将这些操作直接发送到元素(而不是使用browser.actions())?似乎只能将键盘键或鼠标操作发送到元素,但不能一次发送到两个元素(类似于sendKeys().click()).

Additional question: is it possible to send these actions directly to the element(instead of using browser.actions())? It seems that only keyboard keys OR mouse actions can be sent to elements, but not both at once (something like sendKeys().click()).

推荐答案

问题是,您正在通过调用 perform()方法来执行每个循环的动作序列.相反,您需要在循环中链接所有动作​​序列,然后最后执行它.请尝试以下示例,

The problem is, you are executing the actions sequence for each loop by calling perform() method. Instead, you need to chain all-action sequence in the loop and then execute it at last. Try the below example,

this.selectElements = function (names) {
    var actionSequence = browser.actions().keyDown(protractor.Key.CONTROL);
    for(var i = 0; i < names.length; i++){
        var parentElement = element(by.xpath('//div[@aria-label="select group ' + names[i] + '"]'));
        actionSequence = actionSequence.mouseMove(parentElement).click();
    }
    actionSequence.perform();
}

这篇关于量角器:Ctrl单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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