Page 1 of 1

Safe disconnect Ranorex from device

Posted: Mon Sep 21, 2015 6:42 am
by HardyG
Hi,

I'm doing mobile testing using several virtual Android devices on VmWare Workstation. I'm doing so using user code implementation.
Because of an issue VmWare has I need to power cycle each VM after 20 times suspending it bacause otherwise it will fail after a while - but this is another issue...
My first implementations just power cycled the VMs and after that reusing the (old) still existing Ranorex connection to the device which in general still works (for sure it is reestablished by Ranorex after power cycle). After some hours (and hundrets of cycles) I'm faced with an enormous delay/hang of the ranorex connection to ALL devices I'm connected to.
After I found this issue I decided to better explicitly disconnect the device from Ranorex before power cycling but found NO METHOD TO DISCONNECT. Than I decided to power down the VM and after that to reconnect and synchronize to the still powered off device. Than I'm waiting for the channel to close (what without a dedicated method more or less means to wait for the connection to timeout). After that I restart the VM establishing a new Ranorex connection to the device.
This sounds a bit strange but in general it's working. The backdraw is that it's very time consuming, not only for the one VM to disonnect and reconnect but also all other VMs seem to hang for the time doing so what leads to unpredictable timeouts also there which are hard to handle...

My Question: Could you please advise how I can perform a fast and safe device disconnect from Ranorex in user code? Maybe I'm just blind not finding the right method...

Thanks a lot and Kind regards,
Hardy

Re: Safe disconnect Ranorex from device

Posted: Tue Sep 22, 2015 1:48 pm
by Support Team
Hi HardyG,

Unfortunately I’m not exactly sure how your Test Suite is built, but I’d suggest adding the device under test by code.
Please find a short example below.
using Ranorex.Core.Remoting;
var device = RemoteServiceLocator.Service.AddDevice(  
    "DeviceName",   
    RemotePlatform.Android,   
    RemoteConnectionType.WLAN,   
    "IPAddress");  
device.WaitFor(ChannelState.DeviceConnected);   
device.WaitFor(ChannelState.AppConnected);
More information about this code snippet can be found on the following link

Please note:
If the device is added this way, it will be automatically removed as soon as the test has finished.

Please let me know if this workaround helps you overcoming your issue.

Regards,
Markus (S)

Re: Safe disconnect Ranorex from device

Posted: Wed Sep 23, 2015 6:48 am
by HardyG
Hi Markus and thanks for your reply!

Adding the device is not the my issue - I'm already doing so by user code!
Posting my code would more hide the issue rather than showing it ;-)

I'm trying to show the issue posting some pseudo code.
I do have only ONE test case which is only starting the system - all the rest is done in user code.

[pseudo code]
int StartCounter = 0;
while (true) {
StartCounter++;
start(VM);
start(RanorexConnection); // this is also already adding the device (if needed) as you supposed
start(AdbConnection);
start(AppUnderTest);
do(AllTests);
close(AppUnderTest);
if (StartCounter >= 20) {
PowerOff(VM);
StartCounter = 0;
else {
suspend(VM);
}
}

Counting the VM restarts (from suspend) is needed because of an issue VmWare (VIX) is causing after round about 25 starts from suspend. So I have to power cycle to avoid this!

In general this works and it is! When starting it first it takes a while until Ranorex is connected to the device (what is understandable). All following starts are much faster making me think that the Ranorex connection to the device is surviving / reused somehow. After a while (and planty of starts) this is causing a significant delay with all Ranorex device actions. My guess is that there is something left from the earlier starts not cleaned up within Ranorex what after a while causing this delays. I can only avoid this by DISCONNECTING the Ranorex device connection. The Problem is that I did not find any method DISCONNECTING the device explicitly. So what I'm doing now is to close the VM forcing Ranorex to reconnect (knowing that it'll fail by timeout) before I restart the VM connecting to Ranorex again. This works but it takes a significant time waiting for the timeout and it does not seem to be a CLEAN way to DISCONNECT from Ranorex device:

[pseudo code]
int StartCounter = 0;
while (true) {
StartCounter++;
start(VM);
start(RanorexConnection); // this is also already adding the device as you supposed
start(AdbConnection);
start(AppUnderTest);
do(AllTests);
close(AppUnderTest);
if (StartCounter >= 20) {
PowerOff(VM);
StartCounter = 0;
else {
suspend(VM);
}
start(RanorexConnection); // VM is not running! This forces Ranorex to clean up the connection ending up in a timeout
}

My Question is: Is there a clean way to DISPOSE / DISCONNECT Ranorex from the device rather than shutting down the device, forcing Ranorex to connect ending up with a timeout.

I hope this is explaining my issue a bit better than before.

Thanks a lot for you brilliant support!
Kind regards, Hardy

Re: Safe disconnect Ranorex from device

Posted: Thu Sep 24, 2015 12:24 pm
by Support Team
Hi Hardy,

Thank you for your detailed description. I'm afraid that there is currently no possibility to achieve your plan out of the box, but I'd like to discuss your intention directly with you.

May I ask you to get in touch with me by email [[email protected]] ?

Thank you.

Regards,
Markus (S)

Re: Safe disconnect Ranorex from device

Posted: Thu Oct 01, 2015 6:06 pm
by HardyG
Hi Markus,

thanks a lot for the offer but I think I already found my fault...

I was using

Ranorex.Core.Remoting.IRemoteEndpoint device = RemoteServiceLocator.Service.GetByDisplayName(DeviceName)

to get the device if it was already connected within the Ranorex UI. I did so to speed up the connection time making it unneccessary to find the dynamic IP of the device before connecting. If I'm than using

device.DisconnectAndDisable();

I get in trouble because after that the device is also disabled in the Ranorex Device UI permanently without any chance to find that out and to re-enable it by user code, so this state is persisting independend if I delete my instance in the code or not! After this one needs to manually re-enable the device in the Ranorex Device UI (please correct me if I'm wrong!).

Solution: I’m now always creating my own devices by

device = RemoteServiceLocator.Service.AddDevice(DeviceName, RemotePlatform.Android, RemoteConnectionType.WLAN, DeviceIp);

This is creating a new device (even though the device is already connected via Ranorex Device UI).
So after

RemoteServiceLocator.Service.Refresh();
ICollection<IRemoteEndpoint> allEp = RemoteServiceLocator.Service.AllKnownEndpoints;


you will find 2 devices (even though they are the same) but with different names (my new added one got automatically the name extension “_2” than). One has to take care for this if you are using the device.Name for reference!!!

This can than easily and without any side effect be disconnected by

device.DisconnectAndDisable();
RemoteServiceLocator.Service.Refresh();


and reconnected again by the above mentioned

.AddDevice()

Sorry for bothering you with this, Markus.
Even though I guess a hint in the description of the method regarding to avoid the usage of DisconnectAndDisable() directly on devices that are managed in the Ranorex Device UI would have been saving me some time ;-)

Anyway, thanks for your great support!
Kind regards,
Hardy

Re: Safe disconnect Ranorex from device

Posted: Thu Oct 01, 2015 7:30 pm
by HardyG
P.S.:
One may ask: So why isn't there simply a device.Disconnect() method? (like I did)

The reason is, that the devices you add by Ranorex Devices UI and the devices you add by user code are more or less the same an may be found side by side in the same list (as mentioned above).

If you are trying to just Disconnect() a device added by the Ranorex UI it may automatically getting reconnected by the UI after a while. So even though you have been disconnecting it you could never be sure about the connection state a long as you do not avoid the reconnection. This is done by "disabling" the devices in the same step during disconnecting them.

A simple Disconnect() method would only work on devices NOT added by the Ranorex Devices UI!

The remaining question is: Why isn't there a possibility to still find this "disabled" devices, a possibility to find out IF they are "disabled" and a method to re-enable them by user code???

Kind regards,
Hardy

Re: Safe disconnect Ranorex from device

Posted: Tue Oct 06, 2015 1:16 pm
by Support Team
Hi Hardy,

Thank you for your feedback. :D

I’m glad that you were able to overcome this problem.
HardyG wrote:device.DisconnectAndDisable();

I get in trouble because after that the device is also disabled in the Ranorex Device UI permanently without any chance to find that out and to re-enable it by user code, so this state is persisting independend if I delete my instance in the code or not! After this one needs to manually re-enable the device in the Ranorex Device UI (please correct me if I'm wrong!).
That's correct, as soon as you disable the device via
device.DisconnectAndDisable();
you need to re-enable it manually. Unfortunately, there is currently no "Enable" method available.
HardyG wrote:A simple Disconnect() method would only work on devices NOT added by the Ranorex Devices UI!
Generally, feature requests are handled by email since we want to avoid misunderstandings and we want to handle each request with care. Therefore, I’d like you to send a short email to [email protected] if you want to raise a feature request for a "enable" and "disconnect" method.
Thank you for your understanding.

Regards,
Markus (S)