如何将图像与机器人框架进行比较

时间:2017-10-06 22:22:41

标签: python robotframework

我是机器人框架的新手并试图比较两个图像。我从我的最后做了一些R& D,发现现有的RobotAppEyes库有助于比较图像。

Link1: http://navinet.github.io/Robot-AppEyes/RobotAppEyes-KeywordDocumentation.html#Compare%20Image

Link2: https://github.com/NaviNet/Robot-AppEyes

我使用相同的库来比较图像,但在比较两个图像时面临下面的问题。

** Settings ***
Library                 Selenium2Library
Library                 RobotAppEyes

*** Test Cases ***
RobotAppEyes 1.0 Test
    Open Browser    http://www.google.com.uk    gc      
    Maximize Browser Window     
    Open Eyes Session   http://www.google.com.uk    RobotAppEyes_Test   NaviNet_RobotAppEyes_Test1  JkaJK50dp1NTEhPufx08SaztsXmfBZas8z0MZVcaqcA110  
    # ${isOpen}=    Eyes Session Is Open                    
    # Log    ${isOpen}
    Compare Image        C:\\Users\\Downloads\\logo.png       C:\\Users\\user\\Downloads\\logo.png     ignore_mismatch=False    includeEyesLog=False     httpDebugLog=False
    Check Eyes Region   .//*[@id='splash']/div[1]       500     120     logo
    Run Keyword If  ${isOpen}==True Close Eyes Session
    Close Eyes Session

执行机器人脚本后的响应:

RobotAppEyes 1.0 Test                                                 | FAIL |
TypeError: _create_match_data_bytes() takes at least 6 arguments (5 given)
------------------------------------------------------------------------------

任何人都可以帮助我在机器人框架的帮助下进行图像比较吗?

2 个答案:

答案 0 :(得分:0)

这是我在评论中讨论后的答案。这不是迪帕克问题的完美答案,我意识到这一点,但他让我发布它。

 from robot.libraries.BuiltIn import BuiltIn
    import pyautogui as pag

class click_by_image(object):
    def __init__(self):
        self.selenium_lib = None

    def click_by_image(self, image_name):
        """
        Click the center of a rectangle on the screen given the
        image_name of the image rectangle to search for
        :param image_name: Path to image
        """
        #   If ExtendedSelenium2Library instance is undefined
        if self.selenium_lib is None:
            # Instantiate ExtendedSelenium2Library
            self.selenium_lib = BuiltIn().get_library_instance('ExtendedSelenium2Library')
        pag.click(pag.locateCenterOnScreen(str(image_name)))

它是来自的完整关键字。小图片很滑(大约20px到大约40px是有问题的地方,虽然我还没准确测量过它),但它适用于大图片,假设确切的像素集存在于屏幕。它在屏幕上的任何位置,而不仅仅是在DOM中,所以这是我用来在标签之间点击并与普通桌面交互的内容。

要使用此关键字,请将图片的本地副本作为绝对路径或相对路径提供给click_by_image(),或者可以正常工作,并按照文档记录。

我提到的具体代码是pag.locateCenterOnScreen(),来自pyautogui。它会返回一组坐标,因此如果您没有获得这些坐标,那么您的图像就不会出现在屏幕上。如果你这样做,那么你知道a)你的形象存在,b)它在哪里。

答案 1 :(得分:0)

能够使用“图像魔术”解决此问题

Compare Images
    [Arguments]      ${Reference_Image_Path}    ${Test_Image_Path}    ${Allowed_Threshold}
    ${TEMP}=         Replace String     ${IMAGE_COMPARATOR_COMMAND}    __REFERENCE__     ${Reference_Image_Path}
    ${COMMAND}=      Replace String     ${TEMP}    __TEST__     ${Test_Image_Path}
    Log              ${Allowed_Threshold}
    Log              Executing: ${COMMAND}
    ${RC}            ${OUTPUT}=     Run And Return Rc And Output    ${COMMAND}
    # ${RC}            ${OUTPUT}=     Run And Return Rc And Output    C:/"Program Files"/ImageMagick-7.0.7-Q16/convert.exe    C:/Users/user/imagecompare/Test/src/reference-screenshots/reference-1.png    C:/Users/user/imagecompare/Test/src/reference-screenshots/reference-11.png -metric RMSE -compare -format "%[distortion]" info:
    Log              Return Code: ${RC}
    Log              Return Output: ${OUTPUT}
    ${RESULT}        Evaluate    ${OUTPUT} < ${Allowed_Threshold}
    Should be True   ${RESULT}

https://blog.codecentric.de/en/2017/09/robot-framework-compare-images-screenshots/