是否可以通过编程确定是否使用W3C动作命令? [英] Is it possible to programmatically determine whether W3C action commands are used?

查看:136
本文介绍了是否可以通过编程确定是否使用W3C动作命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

The Selenium Javadoc for Actions.moveToElement indicate that the meanings of the xOffset and yOffset arguments are as follows.

xOffset - Offset from the top-left corner. A negative value means coordinates left from the element.
yOffset - Offset from the top-left corner. A negative value means coordinates above the element.

请考虑以下程序,该程序可在Linux上针对Firefox Quantum运行.

Consider the following program, run on Linux against Firefox Quantum.

public class FirefoxTest {
    public static void main(String[] args) {
        // Set up driver
        WebDriver driver = new FirefoxDriver();
        JavascriptExecutor js = (JavascriptExecutor) driver;

        driver.get("http://www.google.com");
        WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));

        // Perform a move and click action to see where it lands.
        Actions moveAndClick = new Actions(driver).moveToElement(element,0,0).doubleClick();
        moveAndClick.perform();
    }
}

运行以下程序时,双击发生在搜索框的中间中,而不是在左上角(我知道这一点是因为我注入了JS来记录点击).此外,在运行程序的终端中将输出以下消息.

When the following program is run, the double-click occurs in the middle of the search box rather than at the top left corner (I know this because I injected JS to log the location of the click). Moreover, the following message is output in the terminal where the program is run.

org.openqa.selenium.interactions.Actions moveToElement
INFO: When using the W3C Action commands, offsets are from the center of element

是否可以以编程方式确定偏移量是相对于Actions.moveToElement元素的中心还是左上角?

Is it possible to programmatically determine whether the offsets are from the center or the top-left corner of the element for Actions.moveToElement?

推荐答案

首先,调用 GeckoDriver 时,第一组日志确认方言 W3C 如下:

To start with, when invoking GeckoDriver the first set of logs confirms that dialect being W3C as follows:

org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C

您作为Java文档是正确的.html#moveToElement-org.openqa.selenium.WebElement-int-int-"rel =" nofollow noreferrer> moveToElement() 仍然提到:

You are correct as the Java Docs for moveToElement() still mentions that:

public Actions moveToElement(WebElement target, int xOffset, int yOffset)

Description:
Moves the mouse to an offset from the top-left corner of the element. The element is scrolled into view and its location is calculated using getBoundingClientRect.

Parameters:
    target - element to move to.
    xOffset - Offset from the top-left corner. A negative value means coordinates left from the element.
    yOffset - Offset from the top-left corner. A negative value means coordinates above the element.

Returns:
A self reference.

我可以按照以下步骤重现您的问题:

I am able to reproduce your issue as follows:

  • 代码块:

  • Code Block:

new Actions(driver).moveToElement(element,0,0).doubleClick().build().perform();

  • 跟踪日志:

  • Trace Logs:

    Oct 16, 2018 6:06:13 PM org.openqa.selenium.interactions.Actions moveToElement
    INFO: When using the W3C Action commands, offsets are from the center of element
    1539693373141   webdriver::server   DEBUG   -> POST /session/180ab0f0-21a3-4e38-8c92-d208fac77827/actions {"actions":[{"id":"default mouse","type":"pointer","parameters":{"pointerType":"mouse"},"actions":[{"duration":100,"x":0,"y":0,"type":"pointerMove","origin":{"ELEMENT":"774efad2-7ee0-40a3-bcaa-3a4d5fcff47b","element-6066-11e4-a52e-4f735466cecf":"774efad2-7ee0-40a3-bcaa-3a4d5fcff47b"}},{"button":0,"type":"pointerDown"},{"button":0,"type":"pointerUp"},{"button":0,"type":"pointerDown"},{"button":0,"type":"pointerUp"}]}]}
    1539693373166   Marionette  TRACE   0 -> [0,5,"WebDriver:PerformActions",{"actions":[{"actions":[{"duration":100,"origin":{"element-6066-11e4-a52e-4f735466cecf":"774e ... pointerDown"},{"button":0,"type":"pointerUp"}],"id":"default mouse","parameters":{"pointerType":"mouse"},"type":"pointer"}]}]
    

  • @Andreas在讨论中指出偏移是从元素中心而不是元素中心左上角 @FlorentB.明确指出:

    As @Andreas pointed out in the discussion offsets are from the center of element instead of from the top-left corner @FlorentB. clearly pointed that:

    /session/:sessionId/moveto
        POST /session/:sessionId/moveto
        Move the mouse by an offset of the specificed element. If no element is specified, the move is relative to the current mouse cursor. If an element is provided but no offset, the mouse will be moved to the center of the element. If the element is not visible, it will be scrolled into view.
    
        URL Parameters:
        :sessionId - ID of the session to route the command to.
    
        JSON Parameters:
        element - {string} Opaque ID assigned to the element to move to, as described in the WebElement JSON Object. If not specified or is null, the offset is relative to current position of the mouse.
        xoffset - {number} X offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element.
        yoffset - {number} Y offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element.
    

  • 按照指针操作 WebDriver W3C编辑器草案中的部分提到:

  • As per the Pointer Actions section within WebDriver W3C Editor's Draft it is mentioned that:

    代表网络元素的对象

    1. 让元素等于尝试获取自变量来源的已知连接元素的结果.

    1. Let element be equal to the result of trying to get a known connected element with argument origin.

    让x元素和y元素成为计算元素的视图内中心点的结果.

    Let x element and y element be the result of calculating the in-view center point of element.

    让x等于x元素+ x偏移量,并且y等于y元素+ y偏移量.

    Let x equal x element + x offset, and y equal y element + y offset.

  • 元素的视图中心点是矩形的原点位置,该矩形是元素的第一个DOM客户矩形和初始视口之间的交点.给定已知可见的元素,其计算公式为:

    An element’s in-view center point is the origin position of the rectangle that is the intersection between the element’s first DOM client rectangle and the initial viewport. Given an element that is known to be in view, it is calculated as:

    • 让矩形成为通过调用元素上的getClientRects返回的DOMRect序列的第一个元素.
    • 让左为(max(0,min(x坐标,x坐标+宽度尺寸))).
    • 让我们成为(min(innerWidth,max(x坐标,x坐标+宽度尺寸))).
    • 让top成为(max(0,min(y坐标,y坐标+高度尺寸))).
    • 让底部为(min(innerHeight,max(y坐标,y坐标+高度尺寸)))).
    • 让x为(0.5×(左+右)).
    • 让我们成为(0.5×(顶部+底部)).
    • 将x和y成对返回.

    因此可以得出结论,偏移量来自中心,但是 Jave Docs 尚未更新.

    So it is can be concluded that the offsets are from the center but the Jave Docs are yet to be updated.

    这篇关于是否可以通过编程确定是否使用W3C动作命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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