iOS 测试 RobotFramework 中导入 IOSLibrary 失败的解决方案

青青 · 2015年07月23日 · 最后由 嘻嘻 回复于 2015年08月11日 · 1740 次阅读

今天,我同时在 windown 和 Mac 两台机器上的 RobotFramework 中导入 IOSLibrary,Windowns 机器上一次性成功(本来也是很简单的事情),但是 Mac 上却一直失败。
最后还是解决掉了,现在分享一下解决过程~~

在 Mac 上使用 pip install --upgrade robotframework-ioslibrary 命令安装 IOSLibrary
安装成功之后,将 IOSLibrary 导入到 RIDE 里面
导入之后,Library 列表中 IOSLibrary 名字会变成黑色,使用库中的关键字时,字体会变成蓝色,好像已经导入成功。

但是。。在 run 的时候却出现了错误

根据错误提示,找打到 IOSLibrary 的安装目录,查看int.py 文件,line 8 显示如下:

from robot.variables import GLOBAL_VARIABLES

似乎是 GLOBAL_VARIABLES 找不到了

于是,继续查看。。。

打开 pyhon 安装目录下的 robot 文件夹,打开找到 variables 文件夹,打开查看int.py 文件,内容如下:

#  Copyright 2008-2015 Nokia Solutions and Networks
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

"""Implements storing and resolving variables.

This package is mainly for internal usage.
"""
from .assigner import VariableAssigner
from .isvar import contains_var, is_var, is_scalar_var, is_list_var, is_dict_var
from .notfound import variable_not_found
from .scopes import VariableScopes
from .splitter import VariableSplitter, VariableIterator
from .tablesetter import VariableTableValue, DictVariableTableValue
from .variables import Variables

然后又找了整个 variables 目录,果然没有 GLOBAL_VARIABLES 的蛛丝马迹。
还好还好,我不是在 windown 上成功了么,然后我对比了两个文件,自己手动修改了int.py 文件,内容如下:

#  Copyright 2008-2015 Nokia Solutions and Networks
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

"""Implements storing and resolving variables.

This package is mainly for internal usage.
"""

import os
import tempfile

from robot import utils
from robot.output import LOGGER


from .assigner import VariableAssigner
from .isvar import contains_var, is_var, is_scalar_var, is_list_var, is_dict_var
from .notfound import variable_not_found
from .scopes import VariableScopes
from .splitter import VariableSplitter, VariableIterator
from .tablesetter import VariableTableValue, DictVariableTableValue
from .variables import Variables

GLOBAL_VARIABLES = Variables()

def init_global_variables(settings):
    GLOBAL_VARIABLES.clear()
    _set_cli_vars(settings)
    for name, value in [ ('${TEMPDIR}', utils.abspath(tempfile.gettempdir())),
                         ('${EXECDIR}', utils.abspath('.')),
                         ('${/}', os.sep),
                         ('${:}', os.pathsep),
                         ('${\\n}', os.linesep),
                         ('${SPACE}', ' '),
                         ('${EMPTY}', ''),
                         ('@{EMPTY}', ()),
                         ('${True}', True),
                         ('${False}', False),
                         ('${None}', None),
                         ('${null}', None),
                         ('${OUTPUT_DIR}', settings['OutputDir']),
                         ('${OUTPUT_FILE}', settings['Output'] or 'NONE'),
                         ('${REPORT_FILE}', settings['Report'] or 'NONE'),
                         ('${LOG_FILE}', settings['Log'] or 'NONE'),
                         ('${LOG_LEVEL}', settings['LogLevel']),
                         ('${DEBUG_FILE}', settings['DebugFile'] or 'NONE'),
                         ('${PREV_TEST_NAME}', ''),
                         ('${PREV_TEST_STATUS}', ''),
                         ('${PREV_TEST_MESSAGE}', '') ]:
        GLOBAL_VARIABLES[name] = value


def _set_cli_vars(settings):
    for path, args in settings['VariableFiles']:
        try:
            GLOBAL_VARIABLES.set_from_file(path, args)
        except:
            msg, details = utils.get_error_details()
            LOGGER.error(msg)
            LOGGER.info(details)
    for varstr in settings['Variables']:
        try:
            name, value = varstr.split(':', 1)
        except ValueError:
            name, value = varstr, ''
        GLOBAL_VARIABLES['${%s}' % name] = value

然后再 run 的时候果然就 OK 了!!

共收到 8 条回复 时间 点赞

额。。具体问题是出在哪儿?

#1 楼 @monkey 感觉像是 Mac 上安装的 RobotFramework 与 IOSlibrary 不够兼容吧。。。我是这么解决的,原因还真是不清楚呢
(⊙﹏⊙) b

#2 楼 @qzhang 待会儿详细看看脚本

从贴出来的代码上看,是 variables__init__.py 文件缺失了一部分内容,而这一部分内容刚好是 iOSLibrary 依赖的(需要 import),所以 iOSLibrary 无法初始化。

PS:我刚刚用 pip 安装了一次 robotframework,里面的 __init__.py 内容是有 GLOBAL_VARIABLES 部分的。我的版本是 robotframework-2.8.7,你可以看看你的 robotframework 安装是否有问题或者是不是版本太低。

#4 楼 @chenhengjie123 看了下,“RIDE 1.3 running on Python 2.7.6. ” 好像确实不是很新呢!

#5 楼 @qzhang 我说的是 RobotFramework 版本,和 RIDE 版本不同的。

#6 楼 @chenhengjie123 。。。。二了,2.8.4

我也遇到了这个问题,
C:\Python27\Lib\site-packages\robot\variables_init.py 中没有 GLOBAL_VARIABLES 函数,C:\Python27\Lib\site-packages\robotide\lib\robot\variables__init_.py 中才有这个函数,修改前面的文件,增加了 GLOBAL_VARIABLES 函数就可以了,所以我觉得可能是 1、环境变量设置错了 2、引用路径设置错了

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册