Robot学习笔记

1、环境安装css

应客户要求,IDE使用Eclipse插件RED,须要使用目前eclipse最新Oxygen版本才能更好兼容RED.html

1.下载并安装Oxygenpython

2.Merketing搜索RED安装插件web

3.下载并安装Python 2.7.13json

4.配置环境变量,//pyhon; //python27/Scripts;windows

5.在线安装robot frameworksession

  cmd> pip install robotframeworkeclipse

6.在线安装selenium2Library测试

  cmd> pip install robotframework-selenium2libraryui

7.使用命令查看在线安装是否正常完成

robot  --version

pip list

 pip ist下能看到robot framework 和 selenium2library为正常安装

8.将IE driver放到path环境变量目录下,能够放到 c:/windows/  或者刚配置的 python 根目录下

9.在eclipse下new robot projecr > new test suite

*** Settings ***
Documentation    description
Library    String
Library    Selenium2Library*** Variables ***
${var}    Hello world!
*** Test Cases ***
Sample Test Case1
    [Documentation]    test case 
    Open Browser    http://www.baidu.com    ie
    Input Text    id=kw    test
    Click Button    id=su

  若是能运行成功表明环境正常

 

 

2、遇到的问题

1. 参数中有中文报错

*** Settings ***
Documentation    description
Library    String
Library    Selenium2Library
*** Variables ***
${var}    Hello world!
*** Test Cases ***
Sample Test Case1
    [Documentation]    test case 
    Open Browser    http://www.baidu.com    ie
    Input Text    id=kw    test
    Click Button    id=su
   ${value}    Get Title   
    Should Be Equal    ${value}    test_百度搜索

  解决方案:

  1.更改eclipse编码  Windows > Preferance > General > Workspace > Text file Encoding >  utf-8

  2.更改robot源码中的编码   

    找到Python安装目录下的\Lib\site-packages\robot\utils\unic.py文件 

      引入json库:import json

    将下面的代码插入,不更改其余部分:

if isinstance(item, (list, dict, tuple)):
            try:
                item = json.dumps(item, ensure_ascii=False, encoding='cp936')
            except UnicodeDecodeError:
                try: item = json.dumps(item, ensure_ascii=False, encoding='cp936')
                except:
                    pass
            except:
                pass

  更改后的代码为:

from pprint import PrettyPrinter

from .platform import IRONPYTHON, JYTHON, PY2
from .robottypes import is_bytes, is_unicode
import json


if PY2:

    def unic(item):
        if isinstance(item, unicode):
            return item
        if isinstance(item, (bytes, bytearray)):
            try:
                return item.decode('ASCII')
            except UnicodeError:
                return u''.join(chr(b) if b < 128 else '\\x%x' % b
                                for b in bytearray(item))
        if isinstance(item, (list, dict, tuple)):
            try:
                item = json.dumps(item, ensure_ascii=False, encoding='cp936')
            except UnicodeDecodeError:
                try: item = json.dumps(item, ensure_ascii=False, encoding='cp936')
                except:
                    pass
            except:
                pass
        try:
            try:
                return unicode(item)
            except UnicodeError:
                return unic(str(item))
        except:
            return _unrepresentable_object(item)

 2.Robot 在定位元素的时候参数不少都写的是locator,可是这个Documentation 中locator 很灵活,能够是str类型的locator,也能够是webElement。而且str的locator是否书写前缀也是可选的。这里建议写前缀,由于在不写前缀的时候默认是按照default方式去查找element,效率会下降。

  同时须要注意的是Get Element Attribute这个关键字的参数为attribute_locator,也就是locator@attr的形式。此处只能使用str类型的locator。若是是使用WebElement@attr进行拼接会把WebElement强制转化为str,并与@attr拼接(selenium.webdriver.remote.webelement.WebElement (session="4dff1f83afb281c77302e0c50d9fce82", element="0.5725955511954164-1")@id)

3. 在实践过程当中发现若是locator使用css会在某些状况下找元素失败,查看了底层代码后发现find element方法中的parse prefix 方法判断逻辑为若是以//开头则认为prefix 为xpath,不然以=做为separator对locator进行分割,等号前为prefix,等号后为locator。

  可是在css的语法中其实也可以包含=,,因此此时若是css类型的locator含有等号且不包含前缀css=那么就会报错为*** is not correct prefix.

  我以为此处应该算是selenium2library库的一个defec

4.  在测试过程当中封装了一个在主页close all popups 的关键字,提取出了主页中的popup 页面的close button的locator为 css=div[style*=display:block] a.close, 当页面有多个popup时循环遍历并点击全部的关闭按钮。

${popCloseXpath_Loc}    xpath=//div[contains(@style,"display:block")]//div[@class="pop_footer"]//a[@class="close"]
    
Clear Home Popup 
    @{popCloses}=    Get WebElements    ${popClose_Loc}
    ${popLen}    Get Length    ${popCloses}
    :FOR    ${index}    IN RANGE    0    ${popLen}            
    \    Run Keyword And Ignore Error  Click Element    ${popClose_Loc}
但在测试中发现新增长的一个popup不可以正常关闭,检查发现是由于这个新的popup的html处于全部popup的第一个,可是实际显示确被其余popup遮挡了。由于时遮挡致使的click失效,首先想到的就是使用js点击。可是发现js只会关闭一个弹窗。
${popCloseXpath_Loc} xpath=//div[contains(@style,"display:block")]//div[@class="pop_footer"]//a[@class="close"]
Clear Home Popup @{popCloses}
= Get WebElements ${popClose_Loc} ${popLen} Get Length ${popCloses} :FOR ${index} IN RANGE 0 ${popLen} \ Run Keyword And Ignore Error Assign Id And Click Element By JS ${popClose_Loc}

#Assign Id And Click Element By J
Assign Id And Click Element By JS
    [Arguments]    ${element}
    Wait Until Page Contains Element    ${element}
    ${element}    Get Sub Locator    ${element}    #去掉locator中prefix部分(xpath=)
    Assign "${element}" Id As boundry    
    Execute Javascript                  $("#${elementId}")[0].click();
    Run Keyword And Ignore Error    Remove "${element}" Id boundry

#Assign Id
Assign "${element}" Id As boundry   
    [Documentation]    Assign Id to element if it not have one    
     ${id}    Get Element Attribute    ${element}@id
     ${id}    Set Variable If    '${id}'!='${EMPTY}'    ${id}
                         ...    '${id}'=='${EMPTY}'       boundary
     Run Keyword If    '${id}'=='boundary'    Assign ID to Element    ${element}    ${id}
     Set Global Variable    ${elementId}    ${id}

#Remove Id
Remove "${element}" Id boundry       
    [Documentation]    Remove Id if it's boundary    
    Return From Keyword If    '${elementId}'=='${EMPTY}'    Log To Console    ID제거(Skip)
    Run Keyword If    '${elementId}'=='boundary'    Assign ID to Element    ${element}    ${EMPTY}
  仔细查看代码发现由于js点击中是经过assign id->js click -> remove id(assign empty id)的方式去实现的。而popups关闭后只是将display属性设置为none ,这个时候咱们但愿的流程是:
assign ID to pop1->click pop1->remove id of pop1->assign id to pop2->click pop2->remove id from pop2
而实际的流程是assign id to pop1->click pop1->remove id from pop2(经过locator查找)->click pop1(经过id查找element)->remove id from pop2
  而后提出采用css样式的locator 取消assign id 的部分解决了问题。
  可是在实际运行时发现,实际上只有第一次js点击生效了,后面的点击报错没有找到元素。查找资料发现js点击事件会点击全部符合当前locator的元素。因此最后去掉循环只是用js点击提取出来的locator就能够了
${popClose_Loc}    css=div[style*="display:block"] div.pop_footer a.close

Clear Home Popup
    [Documentation]    Use Click Element By JS
    ...    can close all popup which match the locator 
    Click Element By JS    ${popClose_Loc}

Click Element By JS
    [Arguments]                         ${locator}
    Wait Until Page Contains Element    ${locator}
    ${locator}    Get Sub Locator    ${locator}
    Execute Javascript    $('${locator}').click();

Get Sub Locator
    [Arguments]    ${locator}
    ${prefix}    ${subLocator}    String.Split String    ${locator}    =    1
    [return]    ${sublocator}
相关文章
相关标签/搜索