How to use some APIs in Appium

Source: Internet
Author: User

The language used is Java,appium version is 1.3.4,java-client version is java-client-2.1.0, recommended for multi-reference java-client-2.1.0-javadoc.

1. Using Androiddriver, it has inherited the Appiumdriver

    private AndroidDriver driver; @Before public void setUp() throws Exception {      DesiredCapabilities capabilities = new DesiredCapabilities();      capabilities.setCapability( "deviceName" , "Android Emulator" );      capabilities.setCapability( "platformVersion" , "4.4" );      capabilities.setCapability( "platformName" , "Android" );      capabilities.setCapability( "appPackage" , "com.android.settings" );      capabilities.setCapability( "appActivity" , ".Settings" );      driver = new AndroidDriver( new URL( "http://127.0.0.1:4723/wd/hub" ),              capabilities); } @After public void tearDown() throws Exception {      driver.quit(); }2. Screenshot and save to Local          //截屏并保存至本地 File screen = driver.getScreenshotAs(OutputType.FILE); File screenFile = new File( "d:\\screen.png" ); try {      FileUtils.copyFile(screen, screenFile); //commons-io-2.0.1.jar中的api } catch (IOException e) {      e.printStackTrace(); }3.push files, pull files    File file = new File( "d:\\test.txt" ); //test.txt内容为"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))); //打印结果为"test"4. //获取当前界面的activity,可用于断言是否跳转到预期的activity driver.currentActivity();5. //打开通知栏界面 driver.openNotifications();6. //获取网络状态 int status = driver.getNetworkConnection().value; System.out.println(status); //设置网络状态 driver.setNetworkConnection( new NetworkConnectionSetting(status)); //或者 driver.setNetworkConnection( new NetworkConnectionSetting( false , true , false ));7 //启动其他应用,跨APP driver.startActivity( "com.android.camera" , ".CameraLauncher" ); //自动滑动列表 driver.scrollTo( "text" ); //或者 driver.scrollToExact( "text" );     //安装APP driver.installApp(appPath); //判断应用是否已安装 driver.isAppInstalled( "package name" ); //拖动相机图标至日历图标位置 new TouchAction(driver).longPress(driver.findElementByName( "相机" )) .moveTo(driver.findElementByName( "日历" )).release().perform();     //锁屏 driver.lockScreen( 2 ); //判断是否锁屏 driver.isLocked();   //发送按键事件 driver.sendKeyEvent(AndroidKeyCode.HOME); <span style= "color:#ff0000;" > </span>                  //通过uiautomator定位clickable属性为true的元素并点击          driver.findElementByAndroidUIAutomator( "new UiSelector().clickable(true)" ).click();                   //相同属性的元素使用List存放          List<webelement> elements = driver.findElementsByClassName( "class name" );          elements.get( 0 ).click(); //点击List中的第一个元素          //tap,点击元素位置          driver.tap( 1 , driver.findElementByName( "日期和时间" ), 0 );</webelement>

How to use some APIs in 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.