標籤:設定 alc etc pac app測試 inf 位操作 app image
一、啟動Android模擬器(Genymotion俗稱世界上最快的模擬器,可自行百度安裝)
二、啟動Appium(Appium環境安裝可自行百度)
三、安裝應用到Genymotion上,如我安裝一個電腦的小應用,包名為CalcTest.apk
安裝步驟:(基於Android SDK已經配置好了環境變數,可自行百度)
1、Win + R
2、CMD
3、adb devices --檢查操作,列出存在的設定名稱
4、adb install F:\Appium\CalcTest.apk --正式安裝App
如:192.168.229.101:5555就是我剛開啟的Genymotion虛擬機器
四、安裝成功之後回到Genymotiong可以看到已經安裝成功了
開啟該應用,可以看到實際是個簡單的計算機
五、開啟Eclipse建立Maven項目並使用uiautomatorviewer工具(Android SDK工具包內建的)進行基本元素定位操作
1、開啟Android SDK可找到路徑:android-sdks\tools如下(擷取App包名可反編譯:aapt dump badging apk路徑)
2、開啟uiautomatorviewr.bat
3、編寫基本代碼如下僅供參考:
package appium_demo;import java.net.MalformedURLException;import java.net.URL;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.remote.DesiredCapabilities;import io.appium.java_client.android.AndroidDriver;/** * @author 李小衛 E-mail:[email protected] @date 建立時間2018年2月11日上午10:10:02 */public class calc_demo {public static void main(String[] args) throws MalformedURLException {AndroidDriver driver;DesiredCapabilities des = new DesiredCapabilities(); //des.setCapability("automationName", "Appium");//Selendroid //自動化的模式選擇 //des.setCapability("app", "C:\\software\\CalcTest.apk");//配置待測試的apk的路徑//des.setCapability("browserName", "chrome"); //h5des.setCapability("platformName", "Android");//平台名稱des.setCapability("platformVersion", "4.4");//手機作業系統版本des.setCapability("udid", "192.168.229.101:5555");//串連的物理裝置的唯一裝置標識des.setCapability("deviceName", "S4");//使用的手機類型或模擬器類型 UDIDdes.setCapability("appPackage", "com.sky.jisuanji");//App安裝後的包名,注意與原來的CalcTest.apk不一樣des.setCapability("appActivity", ".JisuanjizixieActivity");//app測試人員常常要擷取activity,進行相關測試,後續會講到des.setCapability("unicodeKeyboard", "True");//支援中文輸入des.setCapability("resetKeyboard", "True");//支援中文輸入des.setCapability("newCommandTimeout", "10");//沒有新命令時的逾時時間設定des.setCapability("nosign", "True");//跳過檢查和對應用進行 debug 簽名的步驟driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), des);//虛擬機器預設地址driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//設定逾時等待時間,預設250msdriver.findElement(By.id("com.android.calculator2:id/digit1")).click();//定位‘1‘driver.findElement(By.id("com.android.calculator2:id/plus")).click();//定位‘+‘driver.findElement(By.id("com.android.calculator2:id/digit6")).click();//定位‘6‘driver.findElement(By.id("com.android.calculator2:id/equal")).click();//定位‘=‘}}
六、使用TestNG編寫正式測試案例並開始執行測試了
package appium_operate;import java.net.MalformedURLException;import java.net.URL;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.remote.DesiredCapabilities;import org.testng.Assert;import org.testng.annotations.BeforeTest;import org.testng.annotations.DataProvider;import org.testng.annotations.Test;import io.appium.java_client.android.AndroidDriver;/** * @author 李小衛 E-mail:[email protected] @date 建立時間2018年2月11日上午10:30:02 */public class CalcTest {AndroidDriver driver;@BeforeTestpublic void setUp() throws MalformedURLException{DesiredCapabilities des = new DesiredCapabilities();//des.setCapability("app", "c:\\");des.setCapability("platformName", "Android");des.setCapability("platformVersion", "4.4");des.setCapability("udid", "192.168.43.101:5555");des.setCapability("deviceName", "s4");des.setCapability("appPackage", "com.android.calculator2");//com.android.contactsdes.setCapability("appActivity", ".Calculator");//.activities.PeopleActivitydes.setCapability("unicodeKeyboard", "True");des.setCapability("resetKeyboard", "True");des.setCapability("newCommandTimeout", "15");des.setCapability("nosign", "True");driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),des);driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);} @Test(enabled = false) public void add() { driver.findElement(By.xpath("//android.widget.Button[@text=‘5‘]")).click(); driver.findElement(By.xpath("//android.widget.Button[@text=‘+‘]")).click(); driver.findElement(By.xpath("//android.widget.Button[@text=‘8‘]")).click(); driver.findElement(By.xpath("//android.widget.Button[@text=‘=‘]")).click(); String value = driver.findElement(By.xpath("//android.widget.EditText[@class=‘android.widget.EditText‘]")).getAttribute("text"); Assert.assertEquals(value, "13"); } @Test(enabled = false) public void sub() { driver.findElement(By.xpath("//android.widget.Button[@text=‘1‘]")).click(); driver.findElement(By.xpath("//android.widget.Button[@text=‘0‘]")).click(); driver.findElement(By.xpath("//android.widget.Button[@text=‘-‘]")).click(); driver.findElement(By.xpath("//android.widget.Button[@text=‘8‘]")).click(); driver.findElement(By.xpath("//android.widget.Button[@text=‘=‘]")).click(); String value = driver.findElement(By.xpath("//android.widget.EditText[@class=‘android.widget.EditText‘]")).getAttribute("text"); Assert.assertEquals(value, "2"); } @Test(enabled = false) public void mul() { driver.findElement(By.xpath("//android.widget.Button[@text=‘5‘]")).click(); driver.findElement(By.xpath("//android.widget.Button[@text=‘ב]")).click(); driver.findElement(By.xpath("//android.widget.Button[@text=‘8‘]")).click(); driver.findElement(By.xpath("//android.widget.Button[@text=‘=‘]")).click(); String value = driver.findElement(By.xpath("//android.widget.EditText[@class=‘android.widget.EditText‘]")).getAttribute("text"); Assert.assertEquals(value, "40"); } @DataProvider(name="testdata") public Object[][] getData(){ return new Object[][]{{"20","80","100","+"},{"90","3","270","×"},{"6","2","3","÷"}}; } @Test(dataProvider = "testdata") public void calcTestcase(String num1,String num2,String result,String calcType){ for(char num:num1.toCharArray()){ driver.findElement(By.xpath("//android.widget.Button[@text=‘"+String.valueOf(num)+"‘]")).click(); } driver.findElement(By.xpath("//android.widget.Button[@text=‘"+calcType+"‘]")).click(); for(char num:num2.toCharArray()){ driver.findElement(By.xpath("//android.widget.Button[@text=‘"+String.valueOf(num)+"‘]")).click(); } driver.findElement(By.xpath("//android.widget.Button[@text=‘=‘]")).click(); String value = driver.findElement(By.xpath("//android.widget.EditText[@class=‘android.widget.EditText‘]")).getAttribute("text"); Assert.assertEquals(value, result); }}
Java + Selenium + Appium手機自動化測試