一:appium相关环境搭建过程略。

二:连接真机:

  1.手机(andriod 4.2.2)连接电脑,打开USB调试模式。

  2.运行cmd 输入 adb devices -l 查看UDID,如图:

3.再在cmd中输入 appium -a 127.0.0.1 -p4723 -U4d007e9a1b0050d1 (-a表示ip,-p表示端口,-U表示设备的udid 可以通过appium -h查看更多命令)

 4.如果如下图所示 就表示 appium服务启动成功了,注意这个窗口不要关闭 因为这是appium的服务 关了就关了服务,后面过程无法执行,而且这个窗口也是 日志输出的窗口用于排错。

或者如图

,虽是debug,但对后面case的运行不影响。

三:示例代码:

package appdemo.mkws;

import java.net.URL;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class XXXX {
       private WebDriver driver;

        @BeforeMethod
         public void setUp() throws Exception {
         // set up appium
            //File classpathRoot = new File(System.getProperty("user.dir"));
                        //File appDir = new File(classpathRoot, "apps/ContactManager");
                                    //File app = new File(appDir, "contactManager.apk");
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("deviceName","XX");//XX可以是手机的型号
            capabilities.setCapability("browserName","Chrome");
            capabilities.setCapability("platformVersion", "4.2.2");
            capabilities.setCapability("platformName", "Android");
            driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
            driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.MINUTES);// 页面加载的超时时间
            driver.manage().timeouts().implicitlyWait(2, TimeUnit.MINUTES);
        }

        @AfterMethod
        public void tearDown() throws Exception {
        System.out.println("end");
        driver.quit();
        }
        @Test
        public void get(){
            driver.get("url");//输入要去到的url
            driver.findElement(By.id("ctl00_tkShared_Header_txtHeaderSearchBox")).sendKeys("fan");
            driver.findElement(By.xpath("//a[@class='tk_searchbtn btn btn-default']")).click();
            String a=driver.findElement(By.id("ctl00_MainContentArea_ctl00_ctl00_ctl00_ucSearchControl_MyResults_EntriesList_ctrl0_ctl00_hlnkProductName")).getText();
            Assert.assertEquals("Sandalwood Fan", a);
            
        }
        

  }