1, relative coordinates unlocked nine Gongge
Application Scenarios
QQ unlock screen as above, visible nine keys under the same view, to achieve unlocking, with press MoveTo release perform method
The implementation code is as follows:
Webelement Jiugongge = Pi.findbyxpath ("Jiugongge");//Get nine Gongge position element final Touchaction touchaction = new Touchact Ion (driver); The starting x and y coordinates of the element point start = Jiugongge.getlocation (); int StartX = Start.x; int starty = START.Y; System.out.println ("StartX:" +startx); System.out.println ("Starty:" +starty); The width and height of the element Dimension q = jiugongge.getsize (); int width = q.getwidth (); int hight = Q.getheight (); System.out.println ("width:" +width); System.out.println ("hight:" +hight); Calculates the distance of each digit interval int jianjux = WIDTH/3; int Jianjuy = HIGHT/3; System.out.println ("Jianjux:" +jianjux); System.out.println ("Jianjuy:" +jianjuy); Calculates the control end coordinate int endx = width + StartX; int EndY = hight + starty; System.out.println ("EndX:" +endx); System.out.println ("EndY:" +endy); 1 coordinates int oneX = StartX + jianjux/2; int Oney = sTarty + JIANJUY/2; System.out.println ("OneX:" +onex); System.out.println ("Oney:" +oney);
int Twox = OneX + Jianjux;
int Twoy = Oney;
int Threex = Twox + Jianjux;
int threey = Oney;
When the z-type is sliding up and down, the relative coordinates of the x relative coordinates are 0,y jianju, and the relative coordinate value is positive when you swipe down, and the Zuo slide touchaction.press (OneX, Oney) when it is negative. Waitaction moveTo (Jianjux, 0). MoveTo (Jianjux, 0). MoveTo (-jianjux, Jianjuy). MoveTo (-jianjux, Jianjuy). MoveTo ( Jianjux, 0). MoveTo (Jianjux, 0). Waitaction (+). Release (); Touchaction.perform (); Run will throw an exception
3-2-1-4-7-8-9 operation can be done by
Touchaction.press (Threex, Threey). Waitaction (moveTo, 0).-jianjux (moveTo, 0)
. MoveTo (0, Jianjuy). MoveTo (0, Jianjuy). MoveTo (Jianjux, 0). MoveTo (Jianjux, 0). Waitaction (+). Release ();
Touchaction.perform ();
Basic ideas:
1, find the location of the element;
2, find the coordinates of the first point;
3, find the average moving distance;
4, using the Touchaction Press () MoveTo () and other methods to achieve relative movement
Explanation:Press (OneX, Oney) is the coordinate at the time of pressing, and the coordinates of MOVETO () are relative to the pressed coordinates.
When sliding up and down, x relative coordinates are 0,y relative coordinates are jianju, and the relative coordinate values are positive, swipe down to the right, and swipe up left when negative
The official explanation of the MoveTo method is that the movement is relative to the coordinates of the previous point to move
/** * Move current touch to a new position relative to the current position on * the screen. If the current position of this touchaction are (Xold, yold), * Then this method would move the touchaction to (Xold + x , Yold + y). * * @param x change in x coordinate to move through. * @param y change in Y coordinate to move through. * @return This touchaction, for chaining. * /public touchaction moveTo (int x, int y) { actionparameter action = new Actionparameter ("MoveTo"); Action.addparameter ("x", x); Action.addparameter ("Y", y); Parameterbuilder.add (action); return this; }
Note:
This unlock Z-type will have an exception thrown, of course, I do not know how to solve, but probably know why there is this anomaly,
I tried the other shapes, as long as they did not include the oblique movement can be run successfully, there is a diagonal shift will throw an exception
2. Swipe up or down on the control
Application Scenarios:
Left-slip Delete on first chat control
The implementation code is as follows:
/** * Positioning according to the control * swipe left and right inside the control * @param element control Positioning method * @param heading slide direction up down */public voi D Swipecontrol (webelement element, Heading Heading) {//Get the position of the control axis point start = Element.getlocatio N (); int StartX = Start.x; int starty = START.Y; Get control axis difference Dimension q = element.getsize (); int x = Q.getwidth (); int y = q.getheight (); Calculates the control end coordinate int endx = x + StartX; int EndY = y + starty; Computes intermediate point coordinate int centreX = (endx + StartX)/2; int Centrey = (EndY + starty)/2; Switch (heading) {//Slide case RIGHT:driver.swipe to You (endX-10, EndY, CentreX, EndY, 500); Break Slide Case LEFT:driver.swipe to Zuo (Endx-10,endy, CentreX, endY-5, 1000); Break Swipe up case UP:driver.swipe (Endx,endy + 5,centrex,centrey,1000); Break Swipe down case DOWN:driver.swipe (endx,endy-5,centrex,centrey,1000); Break } }
/** * Control the direction of the slide */public enum Heading {Right , left, up, down }
Basic ideas:
1, find the element to slide;
2, get the starting position of the element;
3, using the swipe (startx,starty,endx,endy,time) function to slide, time for sliding, millisecond units
Explanation: The sliding coordinates can be controlled according to their needs.
3, clear the value of the control
Scenario: The Password box gets no value, the clear function directly with appium cannot be cleaned
Pro-Test, QQ Password box value with clear function sometimes cannot clean up
The implementation code is as follows:
/** * One delete the value in the EditText control * @author Lu * @param driver * @param text * /public void cleartext ( String text, int second) { driver.presskeycode (123);//123: Cursor moves to the far right of the input box if (text.length ()!=0) Driver.presskeycode (67);//67 Delete commonutils.sleep (second); }
4. Operation
public static void Snapshot (Androiddriver driver, String filename) {//Commonutils.sleep (2); boolean ret = Viewutils.waitforwebviewinit (Driver, "WEBVIEW_com.eshare.Purse"); if (ret) {driver.context ("Native_app"); } String Currentpath = System.getproperty ("User.dir"); Get current work//folder File Scrfile = ((takesscreenshot) driver). Getscreenshotas (Outputtype.file) ; try {System.out.println ("Save snapshot Path is:" + Currentpath + "/screen/" + filename); Fileutils.copyfile (Scrfile, New File (Currentpath + "\\screen\\" + filename); } catch (IOException e) {System.out.println ("Can ' t Save screenshot"); E.printstacktrace (); } finally {ret = false; if (!ret) {driver.context ("WEBVIEW"); } System.out.println ("screen shot finished, it's in" + Currentpath + "folder"); Commonutils.sleep (2); }
Code as above
For hybrid apps (Native_app,webview hybrid)
The code is visible, I before, I first determine whether the current is a native page or WebView page, and then restore it to WebView mode
This is because this operation will prompt an exception error in WebView mode
So before, first convert it to Native_app mode.
5. Determine if there is an element in the page
public boolean iselementexist (String XPath) { try{ driver.findelement (By.xpath (XPath)); return true; } catch (Org.openqa.selenium.NoSuchElementException ex) { return false; } }
I've worked with the Isdisplay () function before, but this function only determines if the element is displayed, and it uses the premise that the element exists.
For non-existent elements, to determine if they exist, see the code above.
"Turn" Appium common method of finishing