The getname&&getattribute&&getdevicesize&&getsize&&getlocation&& of Bootstrap Getdatadir

Source: Internet
Author: User
Tags getmessage gettext json appium
GetName


Package Io.appium.android.bootstrap.handler;
Import com.android.uiautomator.core.UiObjectNotFoundException;
Import io.appium.android.bootstrap.*;

Import org.json.JSONException;
 /** * This handler was used to get the text of elements. * */public class GetName extends CommandHandler {/* * @param command the {@link Androidcommand} used for this Ha
   Ndler. * * @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 Jsonexc
    eption {if (!command.iselementcommand ()) {return Geterrorresult ("Unable to get name without an element.");
      } try {final androidelement el = command.getelement ();
    Return Getsuccessresult (El.getcontentdesc ()); } catch (Final Uiobjectnotfoundexception e) {return new AndroIdcommandresult (Wdstatus.no_such_element, E.getmessage ());
    } catch (Final Exception e) {//Handle nullpointerexception return Geterrorresult ("Unknown error");
 }
  }
}

The Uiobject.getcontentdescription () method is eventually called to get the control when it describes the information


getattribute


Package Io.appium.android.bootstrap.handler;
Import com.android.uiautomator.core.UiObjectNotFoundException;
Import io.appium.android.bootstrap.*;
Import io.appium.android.bootstrap.exceptions.NoAttributeFoundException;

Import org.json.JSONException;

Import java.util.Hashtable;
 /** * This handler are used to get a attribute of an element. * */public class GetAttribute extends CommandHandler {/* * @param command the {@link androidcommand} used for th
   is 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 Jsonexc eption {if (Command.iselementcommand ()) {//only makes sense on an element final hashtable<string, Ob

      ject> params = Command.params (); Try {final androidelement el = Command.geteLement ();
        Final String attr = params.get ("attribute"). toString (); if (attr.equals ("name") | | attr.equals ("text") | | attr.equals ("className")) {return Getsuccessresul
        T (El.getstringattribute (attr));
        } else {return Getsuccessresult (string.valueof (El.getboolattribute (attr)));
            }} catch (Final Noattributefoundexception e) {return new Androidcommandresult (Wdstatus.no_such_element,
      E.getmessage ());
            } catch (Final Uiobjectnotfoundexception e) {return new Androidcommandresult (Wdstatus.no_such_element,
      E.getmessage ());
            } catch (Final Exception e) {//El is null return new Androidcommandresult (Wdstatus.no_such_element,
      E.getmessage ());
    }} else {return Geterrorresult ("Unable to get attribute without an element.");
 }
  }
}

The event is set for getting information about the control, to see what information can be obtained, in fact, the information in the Uiautomatorviewer.


public string Getstringattribute (final string attr)
      throws Uiobjectnotfoundexception, noattributefoundexception {
    String Res;
    if (attr.equals ("name")) {
      res = Getcontentdesc ();
      if (Res.equals ("")) {
        res = GetText ();
      }
    } else if (attr.equals ("text")) {
      res = GetText ();
    } else if (Attr.equals ("ClassName")) {
      res = GetClassName () ;
    } else {
      throw new noattributefoundexception (attr);
    }
    return res;
  }


Text values: Content-desc, Text, ClassName.


Public boolean Getboolattribute (final String attr)
      throws Uiobjectnotfoundexception, noattributefoundexception {
    Boolean res;
    if (Attr.equals ("Enabled")) {
      res = el.isenabled ();
    } else if (Attr.equals ("checkable")) {
      res = El.ischeckable ();
    } else if (attr.equals ("checked")) {
      res = el.ischecked ();
    } else if (Attr.equals ("clickable")) {
      res = EL.ISCL Ickable ();
    } else if (attr.equals ("focusable")) {
      res = el.isfocusable ();
    } else if (Attr.equals ("focused")) {
      res = El . isFocused ();
    } else if (attr.equals ("longclickable")) {
      res = el.islongclickable ();
    } else if (Attr.equals ("scrollable")) { C17/>res = El.isscrollable ();
    } else if (attr.equals ("selected")) {
      res = el.isselected ();
    } else if (Attr.equals ("displayed")) {
      res = El . exists ();
    } else {
      throw new noattributefoundexception (attr);
    }
    return res;
  }


Boolean value: As above and so on.


getdevicesize


Package Io.appium.android.bootstrap.handler;
Import Com.android.uiautomator.core.UiDevice;
Import Io.appium.android.bootstrap.AndroidCommand;
Import Io.appium.android.bootstrap.AndroidCommandResult;
Import Io.appium.android.bootstrap.CommandHandler;
Import org.json.JSONException;

Import Org.json.JSONObject;
 /** * This handler are used to get the size of the screen. * */public class Getdevicesize extends CommandHandler {/* * @param command the {@link androidcommand} used for th
   is handler. * * @return {@link Androidcommandresult} * * @throws jsonexception * * @see Io.appium.android.bootstrap.Com
   Mandhandler#execute (io.appium.android. * Bootstrap. Androidcommand) */@Override public Androidcommandresult Execute (final androidcommand command) {if (!command.i
      Selementcommand ()) {//makes sense on a device final uidevice d = uidevice.getinstance ();
      Final Jsonobject res = new Jsonobject (); try {res.put ("height", D. Getdisplayheight ());
      Res.put ("width", d.getdisplaywidth ());
      } catch (Final Jsonexception e) {geterrorresult ("Error serializing Height/width data into JSON");
    } return Getsuccessresult (res);
    } else {return Geterrorresult ("Unable to get device size in an element.");
 }
  }
}


Gets the length and width of the screen, calling the Uidevice method: Getdisplayheight () and Getdisplaywidth ()


GetSize


Package Io.appium.android.bootstrap.handler;
Import Android.graphics.Rect;
Import com.android.uiautomator.core.UiObjectNotFoundException;
Import io.appium.android.bootstrap.*;
Import org.json.JSONException;

Import Org.json.JSONObject;
 /** * This handler are used to get the size of elements. * */public class GetSize extends CommandHandler {/* * @param command the {@link Androidcommand} used for this Ha
   Ndler. * * @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 Jsonexc eption {if (Command.iselementcommand ()) {//only makes sense on an element final jsonobject res = new J
      Sonobject ();
        Try {final androidelement el = command.getelement ();
        Final rect rect = El.getbounds (); Res. put ("width", rect.width ());
      Res.put ("Height", rect.height ());
            } catch (Final Uiobjectnotfoundexception e) {return new Androidcommandresult (Wdstatus.no_such_element,
      E.getmessage ());
      } catch (Final Exception e) {//Handle nullpointerexception return Geterrorresult ("Unknown error");
    } return Getsuccessresult (res);
    } else {return Geterrorresult ("Unable to get text without an element.");
 }
  }
}

Gets the width and height of the control and calls the UIObject getbounds (). Get a rectangle and then get its width and height.


GetLocation


Package Io.appium.android.bootstrap.handler;
Import Android.graphics.Rect;
Import io.appium.android.bootstrap.*;
Import org.json.JSONException;

Import Org.json.JSONObject;
 /** * This handler was used to get the text of elements. * */public class GetLocation extends CommandHandler {/* * @param command the {@link androidcommand} used for thi
   S 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 Jsonexc eption {if (!command.iselementcommand ()) {return Geterrorresult ("Unable to get location without an element.")
    ;
      } try {final jsonobject res = new Jsonobject ();
      Final androidelement el = command.getelement ();
      Final Rect bounds = El.getbounds (); Res.put ("x", Bounds.left);
      Res.put ("Y", bounds.top);
    Return Getsuccessresult (RES);
    } catch (Final Exception e) {return new Androidcommandresult (Wdstatus.no_such_element, E.getmessage ());
 }
  }
}

Gets the starting point coordinates of the control. The call is also getbounds, and then gets the x, y coordinates of its starting point


Getdatadir


Package Io.appium.android.bootstrap.handler;

Import android.os.Environment;
Import Io.appium.android.bootstrap.AndroidCommand;
Import Io.appium.android.bootstrap.AndroidCommandResult;
Import Io.appium.android.bootstrap.CommandHandler;

/**
 * This handler are used to get the data dir.
 * */Public
class Getdatadir extends CommandHandler {/
   * * @param command the {@link Androidcommand} us Ed 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) {
    return Getsuccessresult (Environment.getdatadirectory ());
  }
}

Gets the root directory of data. The call is Android Api:Environment.getDataDirectory ()

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.