前言

测试服务化

一、测试需求分析

二、测试设计

1、技术预研:

2、设计方案:

方案 描述
架构方案 采用 C/S 模式,客户端参照 Remote.py 实现,服务端参照 robotremoteserver 实现
调用方案 通过客户端带特定的参数请求服务端来调用指定的测试库
云测试库管理方案 通过读取配置文件,用标签和测试库一一对应,并生成可执行实例化的云服务器程序

三、测试实现和落地

[
    {"api":"appium","lib":"AppiumLibrary"},
    {"api":"selenium","lib":"Selenium2Library"},
    {"api":"request","lib":"RequestsLibrary"},
    {"api":"macaca","lib":"MacacaLibrary"},
    {"api":"easy","lib":"EasyLibrary"}
]
#coding=utf-8
import sys
import xmlrpclib
from AppiumLibrary import AppiumLibrary
from Selenium2Library import Selenium2Library
from RequestsLibrary import RequestsLibrary
from MacacaLibrary import MacacaLibrary
from EasyLibrary import EasyLibrary



class CloudLibrary():
    def  __init__(self):
        self.appium=AppiumLibrary()
        self.selenium=Selenium2Library()
        self.request=RequestsLibrary()
        self.macaca=MacacaLibrary()
        self.easy=EasyLibrary()



    def get_library(self,library):    

        if library=="appium":
            return self.appium


        elif library=="selenium":
            return self.selenium


        elif library=="request":
            return self.request


        elif library=="macaca":
            return self.macaca


        elif library=="easy":
            return self.easy


        else:
            return self 


if __name__ == '__main__':
    from robotcloudserver import RobotCloudServer
    RobotCloudServer(CloudLibrary(),"192.168.1.1",8270,None)


*** Settings ***
Library           Cloud    192.168.43.228:8270/selenium    WITH NAME    Selenium2Library

*** Test Cases ***
testweb
    Open Browser    http://www.baidu.com    chrome    \    remote_url=http://192.168.43.4:4444/wd/hub
    sleep    0.5


import xmlrpclib
import socket
from SimpleXMLRPCServer import SimpleXMLRPCServer

cloud_dict={}


def get_ip():
      iplist=[]
      iplist = socket.gethostbyname_ex(socket.gethostname())
      ip=iplist[2][0]
      return ip


def set_register(cloud_name,cloud_addr,cloud_port):
        clo_address="{cloud_addr}:{cloud_port}".format(cloud_addr=cloud_addr,cloud_port=cloud_port)
        try:
            print cloud_dict[cloud_name]
            tmpaddr=cloud_dict[cloud_name]
            print "the cloudname:{cloud_name},ip:{addr} is exist".format(cloud_name=cloud_name,addr=tmpaddr)
            return False
        except:
            cloud_dict[cloud_name]=clo_address
            print cloud_dict[cloud_name]
            print "the cloudname:{cloud_name} register ok,ip:{addr} ".format(cloud_name=cloud_name,addr=clo_address)
            return True

def get_cloud_address(cloud_name,taga=None):
        try:
            print "aaa  "+cloud_dict[cloud_name]
            ipaddr=cloud_dict[cloud_name]
            print "It point to {cloud_name}:{ipaddr}\nImport the lib:{taga} ".format(cloud_name=cloud_name,ipaddr=ipaddr,taga=taga)
            return "http://"+ipaddr
        except:     
            ipaddr="http://"+str(get_ip())
            print "The cloud is not exist,It point to local center cloud:{ipaddr}".format(ipaddr=get_ip())    
            return "error"

ipaddress=get_ip()
server = SimpleXMLRPCServer((ipaddress, 8280))
print "The Cloud Proxy Server Listening on {ip}:{port} ...".format(ip=ipaddress,port=8280)
server.register_function(get_cloud_address,"get_cloud_address")
server.register_function(set_register,"set_register")

server.serve_forever()

这样就代表注册成功了,一般云名字都是唯一的,如果有其他测试库云再用如 cloud1 来注册的话,这样是代理服务器会返回云名字已存在,那换个名字注册就好了

def _get_keyword_by_tag(self,name,tag):
    tmpliby=self._library.get_library(tag)
    if type(self._runliby)==type(tmpliby):
        pass
    else:
        self._runliby=tmpliby       
    if name == 'stop_remote_server':
        return self.stop_remote_server
    kw = getattr(self._runliby, name, None)
    if not self._is_function_or_method(kw):
        return None
    return kw   

简单总结

参考资料


↙↙↙阅读原文可查看相关链接,并与作者交流