Mobile phone Automation test: Appium Source analysis of Bootstrap four

Source: Internet
Author: User
Tags appium

Orientation is an action that adjusts the orientation of the screen

Package Io.appium.android.bootstrap.handler;

Import android.os.RemoteException;
Import Com.android.uiautomator.core.UiDevice;
Import io.appium.android.bootstrap.*;
Import org.json.JSONException;

Import java.util.Hashtable;

/**
* This handler are used to get or set the orientation of the device.
*
*/
public class Orientation extends CommandHandler {

/*
* @param command the {@link Androidcommand} used for this handler.
*
* @return {@link Androidcommandresult}
*
* @throws jsonexception
*
* @see Io.appium.android.bootstrap.commandhandler#execute (io.appium.android.
* Bootstrap. Androidcommand)
*/
@Override
Public Androidcommandresult Execute (final androidcommand command)
Throws Jsonexception {

Final hashtable<string, object> params = Command.params ();
if (Params.containskey ("orientation")) {
Set the rotation

Final String orientation = (string) params.get ("orientation");
try {
return handlerotation (orientation);
} catch (Final Exception e) {
Return Geterrorresult ("Unable to rotate screen:" + e.getmessage ());
}
} else {
Get the rotation
return Getrotation ();
}

}

/**
* Returns the current rotation
*
* @return {@link Androidcommandresult}
*/
Private Androidcommandresult getrotation () {
String res = null;
Final Uidevice D = uidevice.getinstance ();
Final Orientationenum currentrotation = Orientationenum.frominteger (d
. Getdisplayrotation ());
Logger.debug ("Current rotation:" + currentrotation);
Switch (currentrotation) {
Case ROTATION_0:
Case ROTATION_180:
res = "PORTRAIT";
Break
Case ROTATION_90:
Case ROTATION_270:
res = "LANDSCAPE";
Break
}

if (res! = null) {
Return Getsuccessresult (RES);
} else {
Return Geterrorresult ("Get orientation did not complete successfully");
}
}

/**
* Set the desired rotation
*
* @param orientation
* The rotation desired (LANDSCAPE or PORTRAIT)
* @return {@link Androidcommandresult}
* @throws RemoteException
* @throws interruptedexception
*/
Private Androidcommandresult handlerotation (final String orientation)
Throws RemoteException, Interruptedexception {
Final Uidevice D = uidevice.getinstance ();
Orientationenum desired;
Orientationenum current = Orientationenum.frominteger (d
. Getdisplayrotation ());

Logger.debug ("Desired Orientation:" + orientation);
Logger.debug ("Current rotation:" + current);

if (Orientation.equalsignorecase ("LANDSCAPE")) {
Switch (current) {
Case ROTATION_0:
D.setorientationright ();
desired = orientationenum.rotation_270;
Break
Case ROTATION_180:
D.setorientationleft ();
desired = orientationenum.rotation_270;
Break
Default
Return Getsuccessresult ("already in landscape mode.");
}
} else {
Switch (current) {
Case ROTATION_90:
Case ROTATION_270:
D.setorientationnatural ();
desired = ORIENTATIONENUM.ROTATION_0;
Break
Default
Return Getsuccessresult ("Already in portrait mode.");
}
}
Current = Orientationenum.frominteger (D.getdisplayrotation ());
If the orientation has not changed,
Busy wait until the TIMEOUT has expired
Final int TIMEOUT = 2000;
Final long then = System.currenttimemillis ();
Long now = then;
while (current! = desired && Now-then < TIMEOUT) {
Thread.Sleep (100);
now = System.currenttimemillis ();
Current = Orientationenum.frominteger (D.getdisplayrotation ());
}
if (current! = desired) {
Return Geterrorresult ("Set the orientation, but apps refused to rotate.");
}
Return Getsuccessresult ("Rotation (" + Orientation + ") successful.");
}
}

In the Execute method, first determine whether the parameter contains orientation, or call getrotation if it contains a call to Handlerotation.
Handlerotation,
In this case, the parameter contains orientation, so let's take a look at what has been done in the method.
Final Uidevice D = uidevice.getinstance ();
Orientationenum desired;
Orientationenum current = Orientationenum.frominteger (d
. Getdisplayrotation ());

First get the direction of the current device, and then initialize a private variable for later use. Where the Orientationenum enumeration class defines 4 directions, the Frominteger method is to get the corresponding enumeration value according to the integer value, which means the values of each value.

public enum Orientationenum {
Rotation_0 (0), rotation_90 (1), rotation_180 (2), rotation_270 (3);

public static Orientationenum Frominteger (final int x) {
Switch (x) {
Case 0:
return rotation_0;
Case 1:
return rotation_90;
Case 2:
return rotation_180;
Case 3:
return rotation_270;
}
return null;
}

The Getrotation method, which is to get a horizontal or vertical screen based on the direction of the current screen, returns the result to the client. Very simple.

The above analysis shows that the client has 2 commands on the screen orientation: get the orientation of the screen, change the orientation of the screen
Note the difference between the orientation of the device and the orientation of the inside view.

Mobile phone Automation test: Appium Source analysis of Bootstrap four

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.