Robot学习笔记

一、环境安装

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

1.下载并安装Oxygen

2.Merketing搜索RED安装插件

3.下载并安装Python 2.7.13

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

5.在线安装robot framework

  cmd> pip install robotframework

6.在线安装selenium2Library

  cmd> pip install robotframework-selenium2library

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

  如果能运行成功代表环境正常

 

 

二、遇到的问题

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}

 

 

 

转载于:https://www.cnblogs.com/tina-cherish/p/7416674.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值