一、啟動(dòng)測(cè)試機(jī)或者Android模擬器(Genymotion俗稱(chēng)世界上最快的模擬器,可自行百度安裝)
二、啟動(dòng)Appium(Appium環(huán)境安裝可自行百度)
三、安裝應(yīng)用到Genymotion上,如下圖我安裝一個(gè)計(jì)算機(jī)的小應(yīng)用,包名為CalcTest.apk
安裝步驟:(基于Android SDK已經(jīng)配置好了環(huán)境變量,可自行百度)
1、Win + R
2、CMD
3、adb devices ? --檢查操作,列出存在的設(shè)置名稱(chēng)
4、adb ?install ?F:\Appium\CalcTest.apk ? ? --正式安裝App
測(cè)試apk下載地址:https://files.cnblogs.com/files/yyym/CalcTest.apk
如下圖:192.168.229.101:5555就是我剛開(kāi)啟的Genymotion虛擬機(jī)
四、安裝成功之后回到Genymotiong可以看到已經(jīng)安裝成功了
打開(kāi)該應(yīng)用,可以看到實(shí)際是個(gè)簡(jiǎn)單的計(jì)算器
?五、打開(kāi)Eclipse創(chuàng)建Maven項(xiàng)目并使用uiautomatorviewer工具(Android SDK工具包自帶的)進(jìn)行基本元素定位操作,元素定位方式前面我們已經(jīng)詳細(xì)講解過(guò)了。
1、打開(kāi)Android SDK可找到路徑:android-sdks\tools如下(獲取App包名可反編譯:aapt dump badging apk路徑)
2、打開(kāi)uiautomatorviewr.bat
?3、編寫(xiě)基本代碼如下僅供參考:
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 李小衛(wèi) E-mail:yyymlxw@163.com @date 創(chuàng)建時(shí)間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 //自動(dòng)化的模式選擇
// des.setCapability("app", "C:\\software\\CalcTest.apk");//配置待測(cè)試的apk的路徑
// des.setCapability("browserName", "chrome"); //h5
des.setCapability("platformName", "Android");//平臺(tái)名稱(chēng)
des.setCapability("platformVersion", "4.4");//手機(jī)操作系統(tǒng)版本
des.setCapability("udid", "192.168.229.101:5555");//連接的物理設(shè)備的唯一設(shè)備標(biāo)識(shí)
des.setCapability("deviceName", "S4");//使用的手機(jī)類(lèi)型或模擬器類(lèi)型 UDID
des.setCapability("appPackage", "com.sky.jisuanji");//App安裝后的包名,注意與原來(lái)的CalcTest.apk不一樣
des.setCapability("appActivity", ".JisuanjizixieActivity");//app測(cè)試人員常常要獲取activity,進(jìn)行相關(guān)測(cè)試,后續(xù)會(huì)講到
des.setCapability("unicodeKeyboard", "True");//支持中文輸入
des.setCapability("resetKeyboard", "True");//支持中文輸入
des.setCapability("newCommandTimeout", "10");//沒(méi)有新命令時(shí)的超時(shí)時(shí)間設(shè)置
des.setCapability("nosign", "True");//跳過(guò)檢查和對(duì)應(yīng)用進(jìn)行 debug 簽名的步驟
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), des);//虛擬機(jī)默認(rèn)地址
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//設(shè)置超時(shí)等待時(shí)間,默認(rèn)250ms
driver.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編寫(xiě)正式測(cè)試用例并開(kāi)始執(zhí)行測(cè)試了
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 李小衛(wèi) E-mail:yyymlxw@163.com @date 創(chuàng)建時(shí)間2018年2月11日上午10:30:02 */
public class CalcTest {
AndroidDriver driver;
@BeforeTest
public 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.contacts
des.setCapability("appActivity", ".Calculator");//.activities.PeopleActivity
des.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("http://android.widget.Button[@text='5']")).click();
driver.findElement(By.xpath("http://android.widget.Button[@text='+']")).click();
driver.findElement(By.xpath("http://android.widget.Button[@text='8']")).click();
driver.findElement(By.xpath("http://android.widget.Button[@text='=']")).click();
String value = driver.findElement(By.xpath("http://android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text");
Assert.assertEquals(value, "13");
}
@Test(enabled = false)
public void sub() {
driver.findElement(By.xpath("http://android.widget.Button[@text='1']")).click();
driver.findElement(By.xpath("http://android.widget.Button[@text='0']")).click();
driver.findElement(By.xpath("http://android.widget.Button[@text='-']")).click();
driver.findElement(By.xpath("http://android.widget.Button[@text='8']")).click();
driver.findElement(By.xpath("http://android.widget.Button[@text='=']")).click();
String value = driver.findElement(By.xpath("http://android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text");
Assert.assertEquals(value, "2");
}
@Test(enabled = false)
public void mul() {
driver.findElement(By.xpath("http://android.widget.Button[@text='5']")).click();
driver.findElement(By.xpath("http://android.widget.Button[@text='×']")).click();
driver.findElement(By.xpath("http://android.widget.Button[@text='8']")).click();
driver.findElement(By.xpath("http://android.widget.Button[@text='=']")).click();
String value = driver.findElement(By.xpath("http://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("http://android.widget.Button[@text='"+String.valueOf(num)+"']")).click();
}
driver.findElement(By.xpath("http://android.widget.Button[@text='"+calcType+"']")).click();
for(char num:num2.toCharArray()){
driver.findElement(By.xpath("http://android.widget.Button[@text='"+String.valueOf(num)+"']")).click();
}
driver.findElement(By.xpath("http://android.widget.Button[@text='=']")).click();
String value = driver.findElement(By.xpath("http://android.widget.EditText[@class='android.widget.EditText']")).getAttribute("text");
Assert.assertEquals(value, result);
}
}
?下面是配套學(xué)習(xí)資料,對(duì)于做【軟件測(cè)試】的朋友來(lái)說(shuō)應(yīng)該是最全面最完整的備戰(zhàn)倉(cāng)庫(kù),這個(gè)倉(cāng)庫(kù)也陪伴我走過(guò)了最艱難的路程,希望也能幫助到你!
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-731165.html
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-731165.html
到了這里,關(guān)于Java + Selenium + Appium自動(dòng)化測(cè)試的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!