PythonRemoteServer 使用,原文翻译)

技术PythonRemoteServer 使用,原文翻译) PythonRemoteServer 使用(原文翻译)PythonRemoteServerhttps://github.com/robotfr

PythonRemoteServer使用(原文翻译)

PythonRemoteServer

https://github.com/robotframework/PythonRemoteServer

允许在不同于机器人框架本身运行的进程或机器上托管测试库。

安装

点安装robotremoteserver ||python安装程序。巴拉圭安装

配置

远程服务器被实现为一个类RobotRemoteServer,它在初始化时接受以下配置参数:

argumentdefaultexpandionlibrary

测试要承载的库实例或模块。强制参数。

宿主

'127.0.0.1'

倾听的地址。使用"0.0.0.0"来侦听所有可用的接口。

港口

8270

港口听。使用0自动选择自由端口。可以作为整数或字符串给出。默认端口8270由IANAfor注册,用于远程服务器使用。

端口文件

没有人

文件来写入使用的端口。无(默认)表示不写入此类文件。

允许_停止

"已弃用"

自1.1版以来已弃用。请改用使用允许_远程_ .

服务

真实的

如果是,自动启动服务器并等待它停止。如果是,可以使用调查方法启动服务器。1.1版新增。

允许远程停止

真实的

允许/不允许使用停止远程服务器关键字和停止远程服务器XML-RPC方法远程停止服务器。1.1版新增。

启动服务器

简单地通过创建服务器实例并将测试库实例或模块传递给它来启动

从robotremoteserver导入机器人远程服务器

从mylibrary导入我的图书馆

RobotRemoteServer(MyLibrary())

默认情况下,服务器侦听地址127.0.0.1 和端口8270

从robotremoteserver导入机器人远程服务器

来自示例库导入前夫;前妻;前男友;前女友

ampleLibrary
RobotRemoteServer(ExampleLibrary(), host='10.0.0.42', port=0,
port_file='/tmp/remote-port.txt')

  从版本 1.1 开始,服务器可以在不启动的情况下使用参数来初始化serve=False。然后可以通过serve显式调用其方法来启动服务器

from robotremoteserver import RobotRemoteServer
from examplelibrary import ExampleLibrary
server = RobotRemoteServer(ExampleLibrary(), host='10.0.0.42', port=0,
                           port_file='/tmp/remote-port.txt', serve=False)
server.serve()

在后台启动服务器

单独初始化和启动服务器的主要好处是可以更容易地在后台线程中启动服务器。在线程中启动的服务器的工作方式与在主线程中运行的服务器完全一样,只是不支持优雅地使用或 信号停止服务器Ctrl-C。因此,如果需要,用户必须单独注册信号处理程序。

import signal
import threading
from examplelibrary import ExampleLibrary   #  实例
from robotremoteserver import RobotRemoteServer
server = RobotRemoteServer(ExampleLibrary(), port=0, serve=False)
signal.signal(signal.SIGINT, lambda signum, frame: server.stop())
server_thread = threading.Thread(target=server.serve)
server_thread.start()
while server_thread.is_alive():
    server_thread.join(0.1)

获取远程服务器开启端口

  如果服务器使用默认端口8270或在配置服务器时明确给出其他端口,您显然知道连接服务器时使用哪个端口。使用 port 时0,服务器会自动选择一个空闲端口,但有多种方法可以找到实际端

  • 使用的地址和端口打印到启动服务器的控制台中。
  • 如果使用port_file参数,服务器将端口写入指定文件,其他工具可以轻松读取它。从远程服务器版本 1.1 开始,当服务器停止时,服务器会自动删除端口文件。
  • 从 1.1 版本开始,服务器具有activate无需启动即可调用以激活服务器的方法。此方法返回服务器绑定的端口,并通过下面讨论的属性将其设置为可用。
  • 启动或活动的服务器实例具有server_address包含地址和端口作为元组的属性。从版本 1.1 开始,还有一个server_port属性只包含作为整数的端口。

停止远程服务器

  方法

  • 点击Ctrl-C运行服务器的控制台。如果服务器在后台线程上启动,则不自动支持。
  • 发送的过程SIGINTSIGTERMSIGHUP信号。如果服务器在后台线程上启动,则不适用于 Windows 并且不受支持。
  • 使用Stop Remote Server关键字。可以allow_remote_stop=False在初始化服务器时使用禁用。
  • stop_remote_server在 XML-RPC 接口中使用函数。可以使用allow_remote_stop=False初始化参数禁用。
  • 运行python -m robotremoteserver stop [uri]它采用上述stop_remote_server内部XML-RPC功能。可以使用allow_remote_stop=False初始化参数禁用。
  • 使用模块stop_remote_server提供的功能robotremoteserver与测试服务器运行时类似。在stop_remote_server内部使用XML-RPC 功能,可以通过allow_remote_stop=False初始化参数禁用。
  • 调用stop正在运行的服务器实例的方法。在后台运行服务器时主要有用。

测试远程服务器是否运行

$ python -m 机器人远程服务器测试
运行在 http://127.0.0.1:8270 的远程服务器。
$ python -m 机器人远程服务器测试 http://10.0.0.42:57347
没有在 http://10.0.0.42:57347 运行的远程服务器。

从版本 1.1 开始,该robotremoteserver模块包含test_remote_server可以以编程方式使用的功能

robotremoteserver模块还可用于通过stop在命令行上使用参数或以stop_remote_server编程方式使用该函数来停止远程服务器。测试和停止也适用于其他 Robot Framework 远程服务器实现

from robotremoteserver import test_remote_server
if test_remote_server('http://localhost:8270'):
    print('Remote server running!')

列出远程服务器关键字和查看文档

使用内置的Libdoc工具,您可以列出服务器上可用的关键字

$ python -m robot.libdoc Remote::http://127.0.0.1:8270 list
Count Items In Directory
Stop Remote Server
Strings Should Be Equal

也可以使用参数在命令行上显示文档show。可以通过提供输出文件的名称来创建 HTML 文档:

$ python -m robot.libdoc Remote::http://127.0.0.1:8270 MyLibrary.html
/path/to/MyLibrary.html

Example

#!/usr/bin/env python
from __future__ import print_function
import os
import sys
from robotremoteserver import RobotRemoteServer
try:
    basestring
except NameError:   # Python 3
    basestring = str
class ExampleLibrary(object):
    """Example library to be used with Robot Framework's remote server.
    This documentation is visible in docs generated by `Libdoc`.
    """
    def count_items_in_directory(self, path):
        """Returns the number of items in the directory specified by `path`."""
        items = [i for i in os.listdir(path) if not i.startswith('.')]
        return len(items)
    def strings_should_be_equal(self, str1, str2):
        print("Comparing '%s' to '%s'." % (str1, str2))
        if not (isinstance(str1, basestring) and isinstance(str2, basestring)):
            raise AssertionError("Given strings are not strings.")
        if str1 != str2:
            raise AssertionError("Given strings are not equal.")
if __name__ == '__main__':
    RobotRemoteServer(ExampleLibrary(), *sys.argv[1:])

启动

D:\Document\py\workspace\PythonRemoteServer-master\examplepython examplelibrary.py
Robot Framework remote server at 127.0.0.1:8270 started.
C:\Users\Administratorpython -m robot.libdoc Remote::http://127.0.0.1:8270 MyLi
brary.html
C:\Users\Administrator\MyLibrary.html
C:\Users\Administratorpython -m robot.libdoc Remote::http://127.0.0.1:8270 list
Count Items In Directory
Stop Remote Server
Strings Should Be Equal

test.robot

*** Settings ***
Library       Remote    http://${ADDRESS}:${PORT}
*** Variables ***
${ADDRESS}    127.0.0.1
${PORT}       8270
*** Test Cases ***
Count Items in Directory
    ${items1} =    Count Items In Directory    ${CURDIR}
    ${items2} =    Count Items In Directory    ${TEMPDIR}
    Log    ${items1} items in '${CURDIR}' and ${items2} items in '${TEMPDIR}'
Failing Example
    Strings Should Be Equal    Hello    Hello
    Strings Should Be Equal    not      equal

测试结果

Starting test: Tests.Count Items in Directory
20211110 11:00:54.938 :  INFO : ${items1} = 3
20211110 11:00:54.952 :  INFO : ${items2} = 4580
20211110 11:00:54.953 :  INFO : 3 items in 'D:\Document\py\workspace\RtfDistributedSys\workspace\Mdoule\RobotServer' and 4580 items in 'C:\Users\ADMINI~1\AppData\Local\Temp'
Ending test:   Tests.Count Items in Directory
Starting test: Tests.Failing Example
20211110 11:00:54.958 :  INFO : Comparing 'Hello' to 'Hello'.
20211110 11:00:54.961 :  INFO : Comparing 'equal' to 'equal'.
Ending test:   Tests.Failing Example

  

  • 使用的地址和端口打印到启动服务器的控制台中。
  • 如果使用port_file参数,服务器将端口写入指定文件,其他工具可以轻松读取它。从远程服务器版本 1.1 开始,当服务器停止时,服务器会自动删除端口文件。
  • 从 1.1 版本开始,服务器具有activate无需启动即可调用以激活服务器的方法。此方法返回服务器绑定的端口,并通过下面讨论的属性将其设置为可用。
  • 启动或活动的服务器实例具有server_address包含地址和端口作为元组的属性。从版本 1.1 开始,还有一个server_port属性只包含作为整数的端口。

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/79560.html

(0)

相关推荐

  • 美容医院排行,中国排名前十的美容院有哪些

    技术美容医院排行,中国排名前十的美容院有哪些御尚坊颜连锁美容院、梵玛西美容品牌、北京御蜂坊·蜂SPA美容、兰黛之谜、娇悦诗、百莲凯、克丽缇娜、雅致轩、蔓菲国际、美莱。美容行业是最有前景的八大行业之一美容医院排行,目前国内

    生活 2021年10月31日
  • 考公务员学什么专业好,考公务员,读什么专业好

    技术考公务员学什么专业好,考公务员,读什么专业好第一名考公务员学什么专业好:经济学
    一般情况下,公务员考试的所有岗位占比中,经济类岗位占比是最多的,考生不仅选择面广,而且可以在各个单位,各个岗位间抉择,可以说在考公务员的

    生活 2021年10月21日
  • 如何使用node开发一款图集打包工具

    技术如何使用node开发一款图集打包工具这篇文章主要为大家展示了“如何使用node开发一款图集打包工具”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何使用node开发一款图

    攻略 2021年11月30日
  • Python包装不上怎么解决

    技术Python包装不上怎么解决本篇内容介绍了“Python包装不上怎么解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成

    攻略 2021年11月29日
  • JAVA的经典面试问题有哪些

    技术JAVA的经典面试问题有哪些本篇内容介绍了“JAVA的经典面试问题有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成

    攻略 2021年11月30日
  • 离子方程式中不能拆的物质,pbso4离子方程式能拆吗

    技术离子方程式中不能拆的物质,pbso4离子方程式能拆吗把易溶于水易电离的物质化学式改成离子哪些化学式要写成离子即能拆?哪些化学式不能写成离子即不能拆?要改写成离子的物质离子方程式中不能拆的物质:易溶于水的强电解质,包括

    生活 2021年10月28日