Some summary of Appium

Source: Internet
Author: User
Tags appium

original link: http://blog.csdn.net/bear_w/article/details/50345283
    • 1. Create a session frequently with commands:
Desiredcapabilities cap = new desiredcapabilities (); cap. Setcapability ("Browsername", "" "); Web browser name (' Safari ', ' Chrome ', etc.). If the application is tested automatically, the value of this keyword should be empty. Cap. Setcapability ("PlatformName", "Android");//The Phone operating system cap you want to test. Setcapability ("Platformversion", "4.4");//mobile OS version cap.  Setcapability ("Automationname", "selendroid"); The automated test engine you want to use: Appium (default) or Selendroidcap.setcapability ("DeviceName", "Android Emulator"); Use the type of phone or simulator, enter Android emulator or phone model cap.setcapability ("Udid", Udid).  The unique device identity of the connected physical device, Android can not be set cap.setcapability ("Newcommandtimeout", "300"); Set the time-out for receiving the next command, timeout Appium will automatically close session, default 60 second cap. Setcapability ("Unicodekeyboard", "True");//support for Chinese input, Unicode input method is automatically installed. The default value is Falsecap. Setcapability ("Resetkeyboard", "True"); After the Unicode test with the Unicodekeyboard keyword is set, reset the input method to the original status cap.  Setcapability ("' App '", "d:\\androidautomation\\androidautotest\\app\\zhongchou.apk"); When the app is not installed, set the path to the app//mobile app to launch the app directly from the phone, the upper path does not set the CAP.  Setcapability ("Apppackage", "Com.nbbank"); The activity name of the Android app you want to launch | like ' MaInactivity ', '. Settings ' |cap.  Setcapability ("Appactivity", "com.nbbank.ui.ActivityShow");  You want to run the Android app with the package name Cap.setcapability ("Appwaitactivity", "Com.nbbank.ui.ActivityLogo"); You want to wait for the Android activity name to start | like ' splashactivity ' | Uri Serveruri = new Uri ("Http://127.0.0.1:4723/wd/hub");d river = new Androiddriver<iwebelement> (Serveruri, Cap, Timespan.fromseconds (180));

More detailed View official website: https://github.com/appium/appium/blob/master/docs/cn/writing-running-appium/caps.cn.md

    • 2. Driver common methods and precautions

1) Common methods:

Driver. Hidekeyboard ();//Hide keyboard driver. Backgroundapp (60);//60 seconds after the current application is placed in the background to driver. Lockdevice (3); Lock screen//Open an activity in the current app or launch a new app and open a activitydriver. StartActivity ("Com.iwobanas.screenrecorder.pro", "com.iwobanas.screenrecorder.RecorderActivity");d River. Opennotifications ()///Open the dropdown notification bar to use driver on Android only. Isappinstalled ("com.example.android.apis-");//Check if the application has driver installed. Installapp ("path/to/my.apk");//Install the application to the device to go to driver. Removeapp ("Com.example.android.apis");//Remove an app driver from the device. Shakedevice ();//analog device shaking driver. Closeapp ();//Close application driver. Launchapp ();//start session with the Service keyword (desired capabilities). Please note that this must be in effect when setting the Autolaunch=false keyword. This is not used to start the specified app/activitiesdriver. Resetapp ();//apply Reset driver. Getcontexts ();//Lists all available context driver. GetContext ();//Lists the current context driver. SetContext ("name");//Switches the context to the default context driver. Getappstrings ();//Gets the applied string driver. KeyEvent (176);//Send a key event to the device: Keycodedriver. Getcurrentactivity ();//get Current activity. You can only use//driver on Android. Pinch (25, 25);//Pinch the screen (double fingers move inward to narrow the screen)//driver. Zoom (100, 200);//Enlarge the screen (two fingers move outward to enlarge the screen) driver.Pullfile ("Library/addressbook/addressbook.sqlitedb");//Pull the file driver from the device. Pushfile ("/data/local/tmp/file.txt", "Some data for the file"),//Push the file to the device to driver. Findelement (By.name (""));d River. Findelementbyid ("id");d River. Findelementbyname ("text");d River. Findelementbyxpath ("//*[@name = ' 62 ']");

2) Precautions:
Use driver. Sendkeys (String str) before entering content into a text box, it is best to first element. Click (), otherwise, in some cases, the input will not be lost, the text box prompt content will be displayed in front of the input text. The Sendkey method clears the text box before sending the data, generally does not need clear, as in the case of the previous clear still exists, after click Normal

    • 3. Wait for the page load policy:

1) Explicit wait: Call selenium method, need to add Webdriver.support reference
Explicit wait is the wait for a certain condition to occur before the code takes the next action. The worst case scenario is to use Thread.Sleep () to set a confirmation time to wait. But why is that the worst? Because the load time of an element is long and short, you have to grasp the length of time before setting sleep, too short and easy to time out, too long to waste time. Selenium Webdriver provides some ways to help us wait for exactly the time we need to wait.

            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));            element = wait.Until<IWebElement>((d) =>           {               return driver.FindElement(By.Id("userName"));                });

2) Hidden wait: setup time is not easy too long, set to 500 or 1000
Implicit waiting is when you want to find the element, and this element does not appear immediately, tell Webdriver to query the DOM for a certain amount of time. The default value is 0, but after the setting, the time will work for the entire life cycle of the Webdriver object instance.

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1));
    • 4. Drive. Use of keyevent (int): You can use KeyEvent to send keyboard data, such as backspace, enter, etc.
driver.KeyEvent(3); //KEYCODE_HOME 按键Home 3driver.KeyEvent(26);  //KEYCODE_POWER 电源键 26driver.KeyEvent(67);  //KEYCODE_DEL 退格键 67driver.KeyEvent(66);  //KEYCODE_ENTER 回车键driver.KeyEvent(122); //KEYCODE_MOVE_HOME 光标移动到开始driver.KeyEvent(123); //KEYCODE_MOVE_END 光标移动到末尾
    • 5. Coordinate operation

      To avoid the effects of different phone resolutions, you can get the coordinates of an element in the following ways, by avoiding the use of fixed coordinates

double Screen_X = driver.Manage().Window.Size.Width;//获取手机屏幕宽度double Screen_Y = driver.Manage().Window.Size.Height;//获取手机屏幕高度double startX = element.Location.X; //获取元素的起点坐标,即元素最左上角点的横坐标double startY = element.Location.Y; //获取元素的起点坐标,即元素最左上角点的纵坐标double elementWidth = element.Size.Width;  //获取元素的宽度double elementHight = element.Size.Height; //获取元素的宽度

In the encapsulation of "sliding", "touchaction" and other operations can use the above method to obtain coordinates to operate.

Example: slide between two elements in a split

        Iwebelement Elmenta = null;        Iwebelement ELMENTB = null;        int StartX = 0, Starty = 0, EndX = 0, EndY = 0;        int duration=0,time=0; <summary>////from the position of element A to the position of element b///</summary>//<param name= "A" > the name of element a &LT;/PA ram>//<param name= "B" > element B's name </param>//<param name= "sduration" > Sliding duration </param&gt        ; <param name= "Stime" > Number of slides </param> public void Swipeatob (string A, string b,string sduration,string s  Time) {StartX = elmenta.location.x + elmenta.size.width/2; The center axis of element a starty = elmenta.location.y + elmenta.size.height/2;    The center ordinate of element a endx = elmentb.location.x + elmentb.size.width/2;   The center axis of element B EndY = elmentb.location.y + elmentb.size.height/2; The center ordinate of element b Duration = string. IsNullOrEmpty (sduration)? 1500:int. Parse (sduration); When the duration is empty, the default setting is 1500 milliseconds, time = string. IsNUllorempty (stime)? 1500:int. Parse (stime); When the number of slides is empty, the default setting is to slide 1 times for (int i = 0; i < time; i++) {driver.            Swipe (StartX, Starty, EndX, EndY, duration); }        }

Note: element. Loaction and Element.size, each acquisition will be re-access to the phone, in order to save time if you get the same value, it is recommended to store variables.

    • 6. Cancel reinstalling unlock and setting

Unregister the following code:

Appium\node_modules\appium\lib\devices\android\android.js

async.series([    this.initJavaVersion.bind(this),    this.initAdb.bind(this),    this.packageAndLaunchActivityFromManifest.bind(this),    this.initUiautomator.bind(this),    this.prepareDevice.bind(this),    this.checkApiLevel.bind(this),    this.pushStrings.bind(this),    this.processFromManifest.bind(this),    this.uninstallApp.bind(this),    this.installAppForTest.bind(this),    this.forwardPort.bind(this),    this.pushAppium.bind(this),    this.initUnicode.bind(this),    // DO NOT push settings app and unlock app    //this.pushSettingsApp.bind(this),    //this.pushUnlock.bind(this),    function (cb) {this.uiautomator.start(cb);}.bind(this),    this.wakeUp.bind(this),    this.unlock.bind(this),    this.getDataDir.bind(this),    this.setupCompressedLayoutHierarchy.bind(this),    this.startAppUnderTest.bind(this),    this.initAutoWebview.bind(this),    this.setActualCapabilities.bind(this)  ], function (err) {

Some summary of Appium

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.