Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1.0.94 #608

Merged
merged 8 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions doc/drivers/unity3d.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,75 @@ If you are going to control multiple devices in the same test case, please follo
ui2.swipe('up')


Integrating and Using Poco Interface Functions in Unity
-------------------------------------------------------

This document serves as a guide for integrating and using the new ``UnityPoco.sendMessage()`` and ``UnityPoco.invoke()`` functions in your Unity project.
These functions facilitate communication between your Unity game and Poco, allowing for simple calls with single string arguments or calls with custom arguments.

Getting Started
````````````````

Before using the new interfaces, ensure that you have the latest version of the Poco SDK that includes the changelog updates mentioned. This functionality relies on the updates provided in https://github.com/AirtestProject/Poco-SDK/pull/123.

Using the ``sendMessage()`` Function
`````````````````````````````````````

The ``UnityPoco.sendMessage()`` function allows you to send simple messages with a single string argument from Poco to Unity.

Unity-side
~~~~~~~~~~

Implement OnPocoMessageReceived and add it to PocoManager.MessageReceived. This function will be called when a message is received from Poco.

.. code-block:: csharp

PocoManager.MessageReceived += OnPocoMessageReceived;


Poco-side
~~~~~~~~~

To use the ``sendMessage()`` function on the Poco side, you just need to call it and pass the message.

.. code-block:: python

poco = UnityPoco()
poco.sendMessage("Your message here")



Using the ``invoke()`` Function
```````````````````````````````

The ``UnityPoco.invoke()`` function allows for more complex interactions with custom arguments.

Poco-side
~~~~~~~~~

To use the ``invoke()`` function on the Poco side, you'll need to specify the listener and the arguments you want to pass.

.. code-block:: python

poco = UnityPoco()
poco.invoke(listener="say_hello", name="anonymous", year=2024)

Unity-side
~~~~~~~~~~

On the Unity side, set up a method that will be called when ``invoke()`` is used from Poco.

1. Create a class that derives from ``PocoListenerBase``.
2. Add a method that corresponds to the ``invoke()`` call:

.. code-block:: csharp

[PocoMethod("say_hello")]
public void SayHello(string name, int year)
{
Debug.Log($"Hi, {name}! The year {year} is coming soon!");
}

3. Add a reference to the new class in the ``PocoManager`` so that it knows to listen for calls to the ``say_hello`` method.

.. _airtest.core.api.connect_device: https://airtest.readthedocs.io/en/latest/all_module/airtest.core.api.html#airtest.core.api.connect_device
Binary file modified poco/drivers/android/lib/pocoservice-debug.apk
Binary file not shown.
20 changes: 13 additions & 7 deletions poco/drivers/android/uiautomation.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ def stopped(self):

def run(self):
while not self.stopped():
if getattr(self.poco, "_instrument_proc", None) is not None:
stdout, stderr = self.poco._instrument_proc.communicate()
print('[pocoservice.apk] stdout: {}'.format(stdout))
print('[pocoservice.apk] stderr: {}'.format(stderr))
if not self.stopped():
self.poco._start_instrument(self.port_to_ping) # 尝试重启
time.sleep(1)
Expand Down Expand Up @@ -256,9 +252,9 @@ def _start_instrument(self, port_to_ping, force_restart=False):
self.adb_client.shell('am start -n {}/.TestActivity'.format(PocoServicePackage))

instrumentation_cmd = [
'am', 'instrument', '-w', '-e', 'debug', 'false', '-e', 'class',
'{}.InstrumentedTestAsLauncher'.format(PocoServicePackage),
'{}/androidx.test.runner.AndroidJUnitRunner'.format(PocoServicePackage)]
'am', 'instrument', '-w', '-e', 'debug', 'false', '-e', 'class',
'{}.InstrumentedTestAsLauncher'.format(PocoServicePackage),
'{}/androidx.test.runner.AndroidJUnitRunner'.format(PocoServicePackage)]
self._instrument_proc = self.adb_client.start_shell(instrumentation_cmd)

def cleanup_proc(proc):
Expand Down Expand Up @@ -287,7 +283,17 @@ def wrapped():
print('[pocoservice.apk] stderr: {}'.format(stderr))
time.sleep(1)
print("still waiting for uiautomation ready.")
try:
self.adb_client.shell(
['monkey', '-p', {PocoServicePackage}, '-c', 'android.intent.category.LAUNCHER', '1'])
except Exception as e:
pass
self.adb_client.shell('am start -n {}/.TestActivity'.format(PocoServicePackage))
instrumentation_cmd = [
'am', 'instrument', '-w', '-e', 'debug', 'false', '-e', 'class',
'{}.InstrumentedTestAsLauncher'.format(PocoServicePackage),
'{}/androidx.test.runner.AndroidJUnitRunner'.format(PocoServicePackage)]
self._instrument_proc = self.adb_client.start_shell(instrumentation_cmd)
continue
return ready

Expand Down
13 changes: 13 additions & 0 deletions poco/drivers/unity3d/unity3d_poco.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,16 @@ def __init__(self, addr=DEFAULT_ADDR, unity_editor=False, connect_default_device
super(UnityPoco, self).__init__(addr[1], dev, ip=addr[0], **options)
# If some devices fail to initialize, the UI tree cannot be obtained
# self.vr = UnityVRSupport(self.agent.rpc)

def send_message(self, message):
self.agent.rpc.call("SendMessage", message)

def invoke(self, listener, **kwargs):
callback = self.agent.rpc.call("Invoke", listener=listener, data=kwargs)

value, error = callback.wait()

if error is not None:
raise Exception(error)

return value
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def parse_requirements(filename='requirements.txt'):

setup(
name='pocoui',
version='1.0.93',
version='1.0.94',
keywords="poco, automation test, ui automation",
description='Poco cross-engine UI automated test framework.',
long_description='Poco cross-engine UI automated test framework. 2018 present by NetEase Games',
Expand Down