Missing perform for selenium ActionChains

痴心易碎 提交于 2019-12-11 09:28:37

问题


It is a very common and, sometimes, difficult to spot problem when "action chains" are defined but not being actually applied. Example:

# incorrect
ActionChains(driver).move_to_element(some_element).click(some_element)

as opposed to:

# correct
ActionChains(driver).move_to_element(some_element).click(some_element).perform()
                                                                       ^^^^^^^^^

ActionChains would essentially do nothing and perform no action without perform().

Is there a way to catch this type of a problem early with static code analysis?


I've also looked if PyCharm would warn about this, but it reports no suspicious code found which is understandable as without the perform() call it is still a perfectly valid Python.

There is also this missing-perform ESLint rule.


回答1:


perform()

perform() method performs all stored actions.

As per the implementation of ActionChains, perform() is just like another method from the ActionChains Class like move_to_element(), click() etc.

Now, ActionChains Class is used to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions which are useful for doing complex actions like hover over and drag and drop through method chaining.

The documentation clearly mentions, to generate user actions when you call methods for actions on the ActionChains object, the actions are stored in a queue in the ActionChains object. Finally, when you call perform(), the events are fired in the order they are queued up.

So going by this logic, you were pretty correct to point out ...ActionChains would essentially do nothing and perform no action without perform()... and there is no way to catch this type of a problem early with static code analysis.

Even the IDEs, for example Eclipse, PyCharm or even Sublime Text3 a proprietary cross-platform source code editor with a Python application programming interface (API) wouldn't warn about this.

As an example Eclipse wouldn't complain about the missing perfrom():

But Eclipse would complain about Bad Indentation:

These cases are similar to the classic case of the IDEs not complaining when expected_conditions which should be called with a tuple and it is not a function, but actually a class, whose initializer expects just 1 argument beyond the implicit self:

class element_to_be_clickable(object):
    # .....
    def __init__(self, locator):
        # .....

IDE Snapshot:



来源:https://stackoverflow.com/questions/53782016/missing-perform-for-selenium-actionchains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!