Poptest is the only training institute for developing Test and development engineers in China, aiming at the ability of the trainees to be competent in automated testing, performance testing and testing tools development. Poptest Test Development Engineer Employment Training please consult qq:908821478) Mobile automation testing is the technical requirements of future Test engineers, we need to lay a good foundation.
Swipe code:
Package Io.appium.android.bootstrap.handler;
Import Com.android.uiautomator.core.UiDevice;
Import com.android.uiautomator.core.UiObjectNotFoundException;
Import io.appium.android.bootstrap.*;
Import io.appium.android.bootstrap.exceptions.InvalidCoordinatesException;
Import Io.appium.android.bootstrap.utils.Point;
Import org.json.JSONException;
Import java.util.Hashtable;
/**
* This handler are used to swipe.
*
*/
public class Swipe 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 ();
Final point start = new Point (Params.get ("StartX"), Params.get ("Starty"));
Final point end = new Point (Params.get ("EndX"), Params.get ("EndY"));
Final Integer steps = (integer) params.get ("Steps");
Final Uidevice device = Uidevice.getinstance ();
Point absstartpos = new Point ();
Point absendpos = new Point ();
if (Command.iselementcommand ()) {
try {
Final androidelement el = command.getelement ();
Absstartpos = El.getabsoluteposition (start);
Absendpos = El.getabsoluteposition (end, false);
} catch (Final Uiobjectnotfoundexception e) {
Return Geterrorresult (E.getmessage ());
} catch (Final Invalidcoordinatesexception e) {
Return Geterrorresult (E.getmessage ());
} catch (Final Exception e) {//Handle NullPointerException
Return Geterrorresult ("Unknown error");
}
} else {
try {
Absstartpos = Getdeviceabspos (start);
Absendpos = Getdeviceabspos (end);
} catch (Final Invalidcoordinatesexception e) {
Return Geterrorresult (E.getmessage ());
}
}
Logger.debug ("Swiping from" + absstartpos.tostring () + "to"
+ absendpos.tostring () + "with steps:" + steps.tostring ());
Final Boolean RV = Device.swipe (Absstartpos.x.intvalue (),
Absstartpos.y.intvalue (), Absendpos.x.intvalue (),
Absendpos.y.intvalue (), steps);
if (!RV) {
Return Geterrorresult ("The swipe did not complete successfully");
}
Return Getsuccessresult (RV);
}
}
First analyze the source code:
Final hashtable<string, object> params = Command.params ();
Final point start = new Point (Params.get ("StartX"), Params.get ("Starty"));
Final point end = new Point (Params.get ("EndX"), Params.get ("EndY"));
Final Integer steps = (integer) params.get ("Steps");
Final Uidevice device = Uidevice.getinstance ();
Point absstartpos = new Point ();
Point absendpos = new Point ();
First get the parameters from the command, and then parse out 3 variables: Start point start, endpoint end, step steps. Then get the device object and define 2 private point objects for later use.
It is then conditionally processed to handle the control or handle the coordinates.
Control:
Final androidelement el = command.getelement ();
Absstartpos = El.getabsoluteposition (start);
Absendpos = El.getabsoluteposition (end, false);
First get the control object, and then pass the different parameters through GetAbsolutePosition to get the start point and end point of the click on the control.
Public Point GetAbsolutePosition (final point,
Final Boolean boundschecking) throws Uiobjectnotfoundexception,
invalidcoordinatesexception {
Final rect rect = El.getbounds ();
Final point pos = new Point ();
Logger.debug ("Element bounds:" + rect.toshortstring ());
if (Point.x = = 0) {
Pos.x = Rect.width () * 0.5 + rect.left;
} else if (point.x <= 1) {
Pos.x = Rect.width () * point.x + rect.left;
} else {
Pos.x = Rect.left + point.x;
}
if (boundschecking) {
if (pos.x > Rect.right | | pos.x < rect.left) {
throw new Invalidcoordinatesexception ("X Coordinate ("
+ pos.x.tostring () + "is outside of element rect:"
+ rect.toshortstring ());
}
}
if (Point.y = = 0) {
Pos.y = Rect.height () * 0.5 + rect.top;
} else if (Point.y <= 1) {
Pos.y = Rect.height () * point.y + rect.top;
} else {
Pos.y = Rect.left + point.y;
}
if (boundschecking) {
if (Pos.y > Rect.bottom | | pos.y < rect.top) {
throw new Invalidcoordinatesexception ("Y Coordinate ("
+ pos.y.tostring () + "is outside of element rect:"
+ rect.toshortstring ());
}
}
return POS;
}
The above code first parses the x-coordinate and then parses the y-coordinate. The x and Y coordinates are judged and processed the same
First determine if the X coordinate is 0, and if 0, the X coordinate of the initial point is the horizontal axis of the control's center point. If the coordinates of x are less than 1, the coordinates are relative coordinates, and the values are evaluated by the percentages, and then the product operation with the width is given a specific value. If the above 2 cases do not meet, that is the specific coordinate value, then the direct element of the x-coordinate value plus the border of the control left coordinate value.
Finally, based on the Boolean value passed in to determine whether to do an out-of-bounds validation. If the boundary is exceeded, the exception is run. The y-coordinate is obtained in a similar way. Finally, we get the coordinate value and return it to the Execute method.
Coordinate
Absstartpos = Getdeviceabspos (start);
Absendpos = Getdeviceabspos (end);
Initializes a previously declared private point object by calling the Getdeviceabspos () method to get the coordinate value.
Protected static point Getdeviceabspos (final point point)
Throws Invalidcoordinatesexception {
Final Uidevice D = uidevice.getinstance ();
Final point retpos = new Point; Copy inputed Point
Final Double width = (double) d.getdisplaywidth ();
if (Point.x < 1) {
retpos.x = width * point.x;
}
if (retpos.x > Width | | retpos.x < 0) {
throw new Invalidcoordinatesexception ("X Coordinate ("
+ retpos.x.tostring () + "is outside for screen width:"
+ width.tostring ());
}
Final Double height = (double) d.getdisplayheight ();
if (Point.y < 1) {
Retpos.y = height * POINT.Y;
}
if (Retpos.y > Height | | retpos.y < 0) {
throw new Invalidcoordinatesexception ("Y Coordinate ("
+ retpos.y.tostring () + "is outside of the screen height:"
+ height.tostring ());
}
return retpos;
}
Similar to the above method, it is also to determine whether the transferred coordinate value is less than 1, if less than 1, as a percentage of the ball coordinate value. If an exception is thrown beyond the range of the screen, the last coordinate value is returned back to the Execute method.
Final Boolean RV = Device.swipe (Absstartpos.x.intvalue (),
Absstartpos.y.intvalue (), Absendpos.x.intvalue (),
Absendpos.y.intvalue (), steps);
Finally, call the Uidevice.swipe method to execute the command to determine whether the execution was successful.
Summarize
The Execute swipe command has 2 command format
A. Controls
B. coordinates
The coordinate is divided into the relative coordinate percentage and the absolute coordinates two kinds of methods.
Mobile phone Automation test: Appium Source analysis of Bootstrap five