appium 学习各种小功能总结--功能有《滑动图片、保存截图、验证元素是否存在、》---新手总结(大牛勿喷,新手互相交流)

1.首页滑动图片点击

 1    /**
 2          * This Method for swipe Left
 3          * 大距离滑动 width/6 除数越大向左滑动距离也越大。
 4          * width:720
 5          *height:1280
 6          * @author Young
 7          * @param driver2
 8          * @param during
 9          */
10         public void swipeToLeft2(AndroidDriver driver2, int during) {
11             int width = driver2.manage().window().getSize().width;
12            // System.out.println("width:"+width);
13             int height = driver2.manage().window().getSize().height;
14             //System.out.println("height:"+height);
15             //driver2.swipe(width * 3 / 4, height / 2, width / 4, height / 2, during);
16             driver2.swipe(width * 8 / 9, height / 2, width / 8, height / 2, during);
17             // wait for page loading
18         }

使用方法:

swipeToLeft2(driver,3000);

2.保存截图

  /**
	     * This Method create for take screenshot
	     * 捕获截图功能
	     * @author Young
	     * @param drivername
	     * @param filename
	     * 调用snapshot((TakesScreenshot) driver, "zhihu_showClose.png");
	     */
	    public static void snapshot(TakesScreenshot drivername, String filename) {
	        // this method will take screen shot ,require two parameters ,one is
	        // driver name, another is file name

	        String currentPath = System.getProperty("user.dir"); // get current work
	                                                                // folder
	        File scrFile = drivername.getScreenshotAs(OutputType.FILE);
	        // Now you can do whatever you need to do with it, for example copy
	        // somewhere
	        try {
	            System.out.println("save snapshot path is:" + currentPath + "/"
	                    + filename);
	            FileUtils.copyFile(scrFile, new File(currentPath + "\\" + filename));
	        } catch (IOException e) {
	            System.out.println("Can't save screenshot");
	            e.printStackTrace();
	        } finally {
	            System.out.println("screen shot finished, it's in " + currentPath
	                    + " folder");
	        }
	    }

 使用方法:

snapshot((TakesScreenshot) driver, "firstjt.png");

 

3.验证元素是否存在

		 //验证登录元素是否存在
  public boolean isElementExsitAndroid(AndroidDriver driver,By elemnt){	
			boolean flag = false;  
	         try {  
	             WebElement element=driver.findElement(elemnt);  
	             flag=null!=element;  
	         } catch (NoSuchElementException e) {  
	             System.out.println("Element:" + elemnt.toString()  
	                     + " is not exsit!"); 
	             flag=false;
	          
	         }  
	         return flag;  
		}	   

 使用方法:

isElementExsitAndroid(driver,By.id("com.zhanglb.yijiebao:id/editRemark"));

 

4.appium初始化:

/**
	 * android配置项目初始化
	 * */
	public AndroidDriver appConfige(AndroidDriver driver){
		 //设置apk的路径
	       File classpathRoot = new File(System.getProperty("user.dir"));
	       File appDir = new File(classpathRoot, "apps");
	       File app = new File(appDir, "debuglemonoa.apk");
	       
	       //设置自动化相关参数
	       DesiredCapabilities capabilities = new DesiredCapabilities();
	       capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); //这句不是必须的	
	       //启动哪种设备,是真机还是模拟器?
	       capabilities.setCapability("deviceName", "Android Emulator"); //android模拟器
	       //使用哪种平台
	       capabilities.setCapability("platformName", "Android");
	       //设置安卓系统版本
	       capabilities.setCapability("platformVersion", "4.4.2");	       
	       //设置apk路径
	       capabilities.setCapability("app", app.getAbsolutePath()); 
	       //设置名称超时时间
	       //capabilities.setCapability("newCommandTimeout",30);
	       //以毫秒为单位,等待 Webview 上下文激活的时间
	       //capabilities.setCapability("autoWebviewTimeout",10);
	       //等待设备在启动应用后准备就绪的超时时间。以秒为单位。
	       capabilities.setCapability("androidDeviceReadyTimeout",10);
	       
	       //移动浏览器名称 Chromium 谷歌浏览器
	      // capabilities.setBrowserName("Chromium");
	       
	       //设置app的主包名和主类名 主要的参数      appActivity注意,原生app的话要在activity前加个"."。
	       capabilities.setCapability("appPackage", "com.zhanglb.yijiebao");
	       capabilities.setCapability("appActivity", "com.hzh.main.WelcomeActivity");
	      // capabilities.setCapability("appPackage", "com.qihoo.util");
	       //capabilities.setCapability("appActivity", ".StartActivity");
	       
	       //初始化
	       //driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 
	       //driver=new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
	       try {
			driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
	       } catch (MalformedURLException e) {
			// TODO Auto-generated catch block
	    	   e.printStackTrace();
	       }
	       return driver;
	}

 

posted @ 2016-06-23 18:12  知乎神者  阅读(1459)  评论(0编辑  收藏  举报