Appium implement screenshots and empty EditText

Source: Internet
Author: User
Tags appium testng

Some days ago, the configuration of the Appium test environment, as for the environment how to build, reference: http://www.cnblogs.com/tobecrazy/p/4562199.html

Android Client Login: http://www.cnblogs.com/tobecrazy/p/4579631.html

In the process of using appium, some appium pits are found (which will be described in detail in the rear).

ADB Basic Command Summary (Android Debug Bridge)

ADB is a bridge between PC and device and can be operated by ADB on devices.

    • ADB devices List your devices
    • ADB kill-server kill adb Service (try if the device is connected to a problem)
    • ADB start-server restart ADB service
    • The ADB shell enters the Linux shell of the default device and can execute the linux command directly
    • adb shell Screenrecord/sdcard/runcase.mp4 record video save, default 3min, can also add--time-limit 60 limit Time
    • ADB install jd.apk installs app to device
    • ADB pull/sdcard/runcase.mp4 c://Copy the files from your phone to your computer
    • ADB push c://runcase.mp4/sdcard/to put files on your computer on your phone

These are some basic ADB commands.

Appium Implementation

Since I have the basis of webdriver, it is understood that the easy,appium is actually inherited Webdriver

Selenium is used in the Takesscreenshot interface Getscreenshotas method, specifically implemented as follows

1 /**2 * This Method, create for take screenshot3      * 4      * @author Young5      * @paramdrivername6      * @paramfilename7      */8      Public Static voidSnapshot (takesscreenshot drivername, String filename) {9         //This method would take the screen shot, require, and parameters, one isTen         //driver name, another is file name One  AString Currentpath = System.getproperty ("User.dir");//Get Current work -                                                                 //folder -File Scrfile =Drivername.getscreenshotas (outputtype.file); the         //Now you can do whatever need to do with it, for example copy -         //somewhere -         Try { -System.out.println ("Save snapshot Path is:" + Currentpath + "/" ++filename); - FileUtils +. CopyFile (Scrfile,NewFile (Currentpath + "\ \" +filename)); A}Catch(IOException e) { atSystem.out.println ("Can ' t Save screenshot"); - e.printstacktrace (); -}finally { -System.out.println ("screen shot finished, it's in" +Currentpath -+ "Folder"); -         } in}

Call: Snapshot ((takesscreenshot) driver, "zhihu_showclose.png");

You can call when you catch an exception, you can implement the error, and the test result analysis is effective.

Appium emptying EditText (a pit)

In the use of appium process, found that the SendKeys and clear method is not very good, had to encapsulate their own simulation manually a delete

KeyEvent is used here, please refer to API http://appium.github.io/java-client/for details.

To delete a piece of text, what to do:

1. Get text length

2. Move to Text last

3. Press the Delete button until it is the same length as the text

Move to text last: 123 Delete 67

public static final int BACKSPACE 67
public static final int DEL 67

public static final int KEYCODE_MOVE_END 123

The implementation code is as follows:

/** * This method for the delete text in TextView *  * @author Young * @param text */public void cleartext (String text) {Dr Iver.sendkeyevent (123); for (int i = 0; i < text.length (); i++) {driver.sendkeyevent (67);}}

  

The code for the entire case is pasted.

Initialize driver, execute cmd.exe/c adb shell screenrecord/sdcard/runcase.mp4 start recording test video

Execute Login

Implementation of a modified personal introduction

Package Com.dbyl.core;import Org.apache.commons.io.fileutils;import Org.openqa.selenium.by;import Org.openqa.selenium.outputtype;import Org.openqa.selenium.takesscreenshot;import org.openqa.selenium.WebElement; Import Org.openqa.selenium.remote.capabilitytype;import Org.openqa.selenium.remote.desiredcapabilities;import Org.testng.assert;import Org.testng.annotations.afterclass;import Org.testng.annotations.beforeclass;import Org.testng.annotations.test;import Io.appium.java_client.android.androiddriver;import Java.io.File;import Java.io.ioexception;import Java.net.url;import Java.util.list;import Java.util.concurrent.timeunit;public class Zhihu {private Androiddriver driver;/** * @author Young * @throws ioexception */public void Startrecord () throws Ioexcepti on {Runtime RT = Runtime.getruntime ();//This Code for record the screen of your devicert.exec ("cmd.exe/c adb shell scree Nrecord/sdcard/runcase.mp4 ");} @BeforeClass (Alwaysrun = true) public void SetUp () throws Exception {//Set up aPpiumfile classpathroot = new File (System.getproperty ("User.dir")); File Appdir = new file (Classpathroot, "apps"); File app = new file (appdir, "zhihu.apk");D esiredcapabilities capabilities = new Desiredcapabilities (); Capabilities.setcapability (Capabilitytype.browser_name, ""); Capabilities.setcapability ("PlatformName", "Android" ); Capabilities.setcapability ("DeviceName", "Android Emulator"); Capabilities.setcapability ("Platformversion", "4.4 ");//If no need install don ' t add thiscapabilities.setcapability (" app ", App.getabsolutepath ()); Capabilities.setcapability ("Apppackage", "com.zhihu.android");//Support Chinesecapabilities.setcapability (" Unicodekeyboard "," true "); Capabilities.setcapability (" Resetkeyboard "," true ");/no need Signcapabilities.setcapability ("NoSign", "True"); Capabilities.setcapability ("Appactivity", ". Ui.activity.GuideActivity ");d river = new Androiddriver (New URL (" Http://127.0.0.1:4723/wd/hub "), capabilities); Startrecord ();} @Test (groups = {"Login"}) public void login () {// Find login Buttonwebelement Loginbutton = driver.findelement (By.id ("Com.zhihu.android:id/login")); LoginButton.click ();//wait for 20sdriver.manage (). Timeouts (). implicitlywait (timeunit.seconds);//Find login UserName and password edittextlist<webelement> textfieldslist = Driver.findelementsbyclassname ("Android.widget.EditText"); Textfieldslist.get (0). SendKeys ("[email protected]"); Textfieldslist.get (1). SendKeys ("cookies123"); Driver.manage (). Timeouts (). implicitlywait (timeunit.seconds);//Find OK button Bynamedriver.findelementbyid (" Android:id/button1 "). Click ();d river.manage (). Timeouts (). Implicitlywait (all, timeunit.seconds);//Find keyword Home and Verify it is Displayassert.asserttrue (Driver.findelement (By.name ("Home")). isdisplayed ());} @Test (groups = {"Profilesetting"}, Dependsonmethods = "login") public void profilesetting () {driver.manage (). Timeouts () . implicitlywait (timeunit.seconds);//Find keyword Home and verify it is Displayassert.asserttrue (Driver.findelement ( By.nAme ("Home")). isdisplayed ()); Webelement MyButton = driver.findelement (By.classname ("Android.widget.ImageButton")); Mybutton.click (); Driver.manage (). Timeouts (). Implicitlywait ((), timeunit.seconds);d river.swipe (A.;d river.manage ( ). Timeouts (). implicitlywait (Timeunit.seconds); list<webelement> textviews = Driver.findelementsbyclassname ("Android.widget.TextView"); Textviews.get (0). Click ();d river.manage (). Timeouts (). implicitlywait (timeunit.seconds);d River.findelementbyid (" Com.zhihu.android:id/name "). Click ();d river.manage (). Timeouts (). implicitlywait (Timeunit.seconds); list<webelement> showclose = Driver.findelementsbyid ("Com.zhihu.android:id/showcase_close"); Showclose.isempty ()) {Snapshot ((takesscreenshot) driver, "zhihu_showclose.png"); Showclose.get (0). Click ();} Assert.asserttrue (Driver.findelementsbyclassname ("Android.widget.TextView"). Get (0). GetText (). Contains (" Selenium "));d River.findelementbyid (" Com.zhihu.android:id/menu_people_edit "). Click ();d RiveR.manage (). Timeouts (). implicitlywait (Timeunit.seconds); Webelement intro = Driver.findelementbyid ("Com.zhihu.android:id/introduction"), Intro.click ();d river.manage (). Timeouts (). implicitlywait (Timeunit.seconds); Webelement content = Driver.findelementbyid ("com.zhihu.android:id/content"); String Text = Content.getattribute ("text"); Content.click (); cleartext (text); Content.sendkeys ("Appium Test. Create by Young ");d River.findelementbyid (" Com.zhihu.android:id/menu_question_done "). Click (); Webelement explanation = Driver.findelementbyid ("com.zhihu.android:id/explanation"); Explanation.click (); Driver.manage (). Timeouts (). implicitlywait (timeunit.seconds); content = Driver.findelementbyid (" Com.zhihu.android:id/content "); text = Content.getattribute (" text "); Content.click (); cleartext (text); Content.sendkeys ("Appium Test. Create by Young. This was an appium type Hahahahah ");d River.findelementbyid (" Com.zhihu.android:id/menu_question_done "). Click (); Snapshot ((takesscreenshot) driver, "Zhihu.png");} /** * This method for the delete text in TextView * * @author Young * @param text */public void cleartext (String text) {DRIVER.S Endkeyevent (123); for (int i = 0; i < text.length (); i++) {driver.sendkeyevent (67);}} @AfterClass (Alwaysrun = true) public void TearDown () throws Exception {Driver.quit ();}  /** * This Method create for take screenshot * * @author young * @param drivername * @param filename */public static void Snapshot (takesscreenshot drivername, String filename) {//This method is take-screen shot, require-parameters, one is//driver name, another is file namestring Currentpath = System.getproperty ("User.dir");  Get current work//Folderfile scrfile = Drivername.getscreenshotas (outputtype.file);//Now, can do whatever you need To does with it, for example copy//somewheretry {System.out.println ("Save snapshot Path is:" + Currentpath + "/" + filename ); Fileutils.copyfile (Scrfile, New File (Currentpath + "\ \" + filename);} catch (IOException e) {System.out.println ("Can ' t SaveScreenshot "); E.printstacktrace ();} finally {System.out.println ("screen shot finished, it's in" + currentpath+ "folder");}}}

  

This is two places in operation:

Appium implementation and emptying EditText

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.