Appium Basic five: Appium related APIs

Source: Internet
Author: User

1. Get the information class:

1.1 Get the components of the current interface:

Driver.currentactivity (); Gets the activity of the current interface, which can be used to assert whether to jump to the expected activity

1.2 Get the tree structure source code for the current page

Driver.pagesource (); Gets the tree chart of the current page

2.App Install and uninstall related classes API

2.1 Determine if the app is installed

Driver.isappinstalled ("package name"); Determine if the app is installed

Driver.installapp (AppPath); Install the App

Driver.removeapp (Stringbundleid); Uninstall package, Android is the package name, iOS is Bundleid

Driver.closeapp (); Close the app, press and hold the home button to back up the app

Driver.launchapp (); Launch the App

Driver.resetapp (); First close, then launch.

2.2 Launch Other applications:

Driver.startactivity ("Com.android.camera", ". Cameralauncher ");

3. Set the related classes:

3.1 Getting and setting network status

int status = Driver.getnetworkconnection (). value; Get network status

SYSTEM.OUT.PRINTLN (status);

Driver.setnetworkconnection (new networkconnectionsetting (status)); Set network status

Or

Driver.setnetworkconnection (New Networkconnectionsetting (False, True, false));

4. file Operation class:

4.1.push files, pull files

File File = new file ("D:\\test.txt"); Test.txt content is "test"

String content = null;

try {

Content = fileutils.readfiletostring (file);

} catch (IOException e) {

E.printstacktrace ();

}

byte[] data = Base64.encodebase64 (Content.getbytes ());

Driver.pushfile ("Sdcard/test.txt", data);

byte[] Resultdate = Driver.pullfile ("Sdcard/test.txt");

System.out.println (New String (Base64.decodebase64 (resultdate))); Print the result as "test"

5. Get the control class (get multiple elements just change findelement to findelements):

5.1 Find the target element of the current page through the element ID:

Driver.findelementbyid ("id"); Locating elements by ID

5.2 Find the target element of the current page through the element name:

Driver.findelementbyclassname ("classname"); Locating elements by name

5.3 Find the target element of the current page through the element XPath:

Driver.findelementbyxpath ("XPath"); Positioning elements through XPath

5.4 Positioning by the element's name value (that is, text)

Driver.findelementbyname ("name"); Position by text value

5.5 hybrid positioning, combined with XPath and index

Driver.find_element_by_xpath ("//android.widget.textview[contains (@text, ' gallery ')]")// In the page detection all Android.widget.TextView classes contain elements of the text= "Gallery"

6. user operation class:

6.1 Click event:

1) Driver.click ();

2) Driver.tap (position.duration); Analog finger tap, can be set to hold the length of time (MS)

such as: Driver.tap ([(300,500)],10)

6.2 Shake a phone

1) Driver.shake ()

6.3 Input Events

1) Driver.elememt.sendKeys ();

2) Driver.elememt.sendText ();

6.4 Slide screen (flip screen and quick move):

Driver.swipe (Startx,starty, Endx,endy, during (ms));//Slide screen from (Startx,starty) to (Endx,endy) interval during (MS)

Driver.flick (Startx,starty,endx,endy)//press and hold a (startx,starty) point to move quickly to B (Endx,endy) point

6.5 Scaling events (suitable for testing a scene similar to a motion map):

1) driver.pinch (element,percent,steps); Perform a shrink on an element, percent and steps do not write, which means default when not written

such as: Driver.pinch (element,150,30)

2) Driver.zoom (element,percent,steps); Perform magnification on elements, percent and steps are not writable and indicate default when not written

such as: Driver.zoom (element,200,50)

6.6 Long Press events:

1) driver.longpress ();

6.7 KeyEvent Events

Driver.keyevent ("4") #返回键操作

6.8 Opening and closing operations:

Driver.close ()//close the current window

Driver.quit ()//exit script to run and close each associated window connection

6.9 Getting the height and width of the window

Driver.manage (). window (). GetSize (). width;

driver.manage (). window (). GetSize (). Height;

7. waiting for wait related classes;

7.1 Sleep Method:

Thread.Sleep (60000) Force wait 60s//force wait 60s

7.2 Implicitlywait Method

Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); Implicit wait, global wait 30S regardless of whether the element is U no already loaded

7.3 Webdriverwait Method (display wait, need to increase a certain wait time, can be determined by webdriverwait and util)

7.4 Waitactivity method (implicit Wait, accepts three parameters, activity name, timeout out-of-date, and detection interval (S)):

Driver.waitactivity ("Activity", timeout (s), during (s));

8.AppiumDriver Auxiliary classes:

8.1 Screenshot and save local

public static void screenshot (Androiddriver driver,string screenname) {

String dir_name = System.getproperty ("User.dir") + "\ \" + "Exception picture";

if (! ( New File (Dir_name). Isdirectory ()) {

New File (Dir_name). mkdir ();

}

File screen = Driver.getscreenshotas (outputtype.file);

try{

System.out.println ("Exception Picture save path:" +new File (Dir_name));

Fileutils.copyfile (screen, new File (dir_name+ "\ \" +screenname+ ". jpg"));

}catch (Exception e) {

E.printstacktrace ();

}

}

8.2 Open the Notification bar interface:

Driver.opennotifications ();

8.3 Lock Screen:

Driver.lockscreen (2);//Lock screen

Deciding whether to lock the screen

Driver.islocked ();

8.4 Auto Slide List

Driver.scrollto ("text"); Auto Slide list

Or

Driver.scrolltoexact ("text");

Appium Basic five: Appium related APIs

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.