Sonic云真機(jī)直接獲取POCO控件實(shí)戰(zhàn)

前言

Sonic官網(wǎng)
用戶(hù)交流社區(qū)

效果展示

image.png
image.png
image.png
image.png

目前已支持POCO-SDK的所有游戲引擎(Unity3d、Cocos2dx系列、白鷺、UE4),使用前需游戲已接入SDK。v1.4.0-beta更新后就可以使用啦~ (預(yù)計(jì)五一后更新)

什么是POCO

Poco 是一個(gè)基于 UI 控件搜索的跨引擎自動(dòng)化測(cè)試框架。支持主流游戲引擎:Cocos2d-x、Unity3d、安卓原生應(yīng)用
https://poco-chinese.readthedocs.io/en/latest/

實(shí)現(xiàn)過(guò)程

以往獲取poco控件更多是使用Airtest框架的python client或者Airtest IDE去獲取,但是Sonic是Java為后端主要開(kāi)發(fā)語(yǔ)言,僅獲取控件而引入python環(huán)境就顯得浪費(fèi),于是嘗試從SDK層面去尋找答案。SDK github
以Unity3d為例:
PocoManager.cs
我們從這個(gè)文件可以看到SDK暴露的TCPServer是RPC協(xié)議,暴露的方法有:

rpc = new RPCParser();
        rpc.addRpcMethod("isVRSupported", vr_support.isVRSupported);
        rpc.addRpcMethod("hasMovementFinished", vr_support.IsQueueEmpty);
        rpc.addRpcMethod("RotateObject", vr_support.RotateObject);
        rpc.addRpcMethod("ObjectLookAt", vr_support.ObjectLookAt);
        rpc.addRpcMethod("Screenshot", Screenshot);
        rpc.addRpcMethod("GetScreenSize", GetScreenSize);
        rpc.addRpcMethod("Dump", Dump);
        rpc.addRpcMethod("GetDebugProfilingData", GetDebugProfilingData);
        rpc.addRpcMethod("SetText", SetText);
        rpc.addRpcMethod("GetSDKVersion", GetSDKVersion);

很明顯,Dump方法就是我們需要的。
話(huà)不多說(shuō),服務(wù)端開(kāi)搞。

// Call a method in the server
    public string formatRequest(string method, object idAction, List<object> param = null)
    {
        Dictionary<string, object> data = new Dictionary<string, object>();
        data["jsonrpc"] = "2.0";
        data["method"] = method;
        if (param != null)
        {
            data["params"] = JsonConvert.SerializeObject(param, settings);
        }
        // if idAction is null, it is a notification
        if (idAction != null)
        {
            data["id"] = idAction;
        }
        return JsonConvert.SerializeObject(data, settings);
    }

從這里得知,TCPServer是獲取這個(gè)請(qǐng)求體,然后根據(jù)method字段映射到不同方法。

// Send a response from a request the server made to this client
    public string formatResponse(object idAction, object result)
    {
        Dictionary<string, object> rpc = new Dictionary<string, object>();
        rpc["jsonrpc"] = "2.0";
        rpc["id"] = idAction;
        rpc["result"] = result;
        return JsonConvert.SerializeObject(rpc, settings);
    }

返回時(shí)的id便是請(qǐng)求的id一一對(duì)應(yīng),然后result就是對(duì)應(yīng)方法的返回內(nèi)容,我們需要的信息就在這里面提取。

具體通信細(xì)節(jié)

我們從這里得知TcpServer.cs

 public class SimpleProtocolFilter : ProtoFilter
    {
        /* 簡(jiǎn)單協(xié)議過(guò)濾器
        協(xié)議按照 [有效數(shù)據(jù)字節(jié)數(shù)][有效數(shù)據(jù)] 這種協(xié)議包的格式進(jìn)行打包和解包
        [有效數(shù)據(jù)字節(jié)數(shù)]長(zhǎng)度HEADER_SIZE字節(jié)
        [有效數(shù)據(jù)]長(zhǎng)度有效數(shù)據(jù)字節(jié)數(shù)字節(jié)
        本類(lèi)按照這種方式,順序從數(shù)據(jù)流中取出數(shù)據(jù)進(jìn)行拼接,一旦接收完一個(gè)完整的協(xié)議包,就會(huì)將協(xié)議包返回
        [有效數(shù)據(jù)]字段接收到后會(huì)按照utf-8進(jìn)行解碼,因?yàn)樵趥鬏斶^(guò)程中是用utf-8進(jìn)行編碼的
        所有編解碼的操作在該類(lèi)中完成
        */

        private byte[] buf = new byte[0];
        private int HEADER_SIZE = 4;
        private List<string> msgs = new List<string>();

        public void input(byte[] data)
        {
            buf = Combine(buf, data);

            while (buf.Length > HEADER_SIZE)
            {
                int data_size = BitConverter.ToInt32(buf, 0);
                if (buf.Length >= data_size + HEADER_SIZE)
                {
                    byte[] data_body = Slice(buf, HEADER_SIZE, data_size + HEADER_SIZE);
                    string content = System.Text.Encoding.Default.GetString(data_body);
                    msgs.Add(content);
                    buf = Slice(buf, data_size + HEADER_SIZE, buf.Length);
                }
                else
                {
                    break;
                }
            }
        }

      public byte[] pack(String content)
        {
            int len = content.Length;
            byte[] size = BitConverter.GetBytes(len);
            if (!BitConverter.IsLittleEndian)
            {
                //reverse it so we get little endian.
                Array.Reverse(size);
            }
            byte[] body = System.Text.Encoding.Default.GetBytes(content);
            byte[] ret = Combine(size, body);
            return ret;
        }

無(wú)論是發(fā)消息或是收消息,都是將消息體的長(zhǎng)度作為head先發(fā)送一次,再發(fā)送請(qǐng)求體。收消息時(shí)同理。

Sonic通信如下:

                poco = new Socket("localhost", port);
                inputStream = poco.getInputStream();
                outputStream = poco.getOutputStream();
                int len = jsonObject.toJSONString().length();
                ByteBuffer header = ByteBuffer.allocate(4);
                header.put(BytesTool.intToByteArray(len), 0, 4);
                header.flip();
                ByteBuffer body = ByteBuffer.allocate(len);
                body.put(jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8), 0, len);
                body.flip();
                ByteBuffer total = ByteBuffer.allocate(len + 4);
                total.put(header.array());
                total.put(body.array());
                total.flip();
                outputStream.write(total.array());

過(guò)程總結(jié)

那么總結(jié)一下,其實(shí)通信過(guò)程沒(méi)有想象復(fù)雜。

  1. 第一步是拼接我們需要的請(qǐng)求體。
JSONObject jsonObject = new JSONObject();
        jsonObject.put("jsonrpc", "2.0");
        jsonObject.put("params", Arrays.asList(true));
        jsonObject.put("id", UUID.randomUUID().toString());
        jsonObject.put("method", "Dump");
//部分引擎為小寫(xiě)dump
  1. 根據(jù)協(xié)議規(guī)則發(fā)送給Socket
  2. 接收信息
              while (poco.isConnected() && !Thread.interrupted()) {
                    byte[] buffer = new byte[1024];
                    int realLen;
                    realLen = inputStream.read(buffer);
                    if (buffer.length != realLen && realLen >= 0) {
                        buffer = subByteArray(buffer, 0, realLen);
                    }
                    if (realLen >= 0) {
                        s.append(new String(buffer));
                        if (s.toString().getBytes(StandardCharsets.UTF_8).length == headLen) {
                            result.set(s.toString());
                            break;
                        }
                    }
                }
  1. 將結(jié)果發(fā)送給前端解析
  2. 兼容不同引擎協(xié)議,有的引擎是走websocket特殊兼容一下。

結(jié)語(yǔ)

POCO是用戶(hù)呼聲很高的一個(gè)需求,目前功能只是獲取游戲控件,后續(xù)會(huì)利用POCO做更多游戲自動(dòng)化上的工作。也感謝大家一直關(guān)注Sonic。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容