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)

相关推荐

  • ADO.NET框架是什么

    技术ADO.NET框架是什么这篇文章主要介绍“ADO.NET框架是什么”,在日常操作中,相信很多人在ADO.NET框架是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”ADO.NET框架

    攻略 2021年12月3日
  • 龙虾怎么洗简单又干净,怎样杀小龙虾,洗小龙虾

    技术龙虾怎么洗简单又干净,怎样杀小龙虾,洗小龙虾小龙虾清洗起来很麻烦,特别是那两个大钳子很具有威慑力,让人都不知道该怎么拿捏清洗龙虾怎么洗简单又干净。 步骤如下:
    1、先盐水养半小时很多龙虾生存的水域都不太干净,而且龙

    生活 2021年10月30日
  • 如何分析java eclips

    技术如何分析java eclips如何分析java eclips,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。把下载到的eclipse-SDK-3.2.1-

    攻略 2021年12月2日
  • debug P命令跟踪程序时遇到Int 21使用P命令的分析

    技术debug P命令跟踪程序时遇到Int 21使用P命令的分析debug P命令跟踪程序时遇到Int 21使用P命令的分析,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能

    攻略 2021年11月11日
  • html css js是什么

    技术html css js是什么本篇内容主要讲解“html css js是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“html css js是什么”吧!最准确的网页设计

    攻略 2021年12月10日
  • Windows如何实现任务计划隐藏显示黑窗口

    技术Windows如何实现任务计划隐藏显示黑窗口小编给大家分享一下Windows如何实现任务计划隐藏显示黑窗口,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!Windows 任务计划隐藏显示黑窗口,主要通

    攻略 2021年11月18日