Appium

環(huán)境搭建地址:https://www.cnblogs.com/ydnice/p/5787800.html

這個(gè)算是比較齊全的了,不過(guò)有一點(diǎn)一定要注意——node版本、node版本、node版本!!!

我就是因?yàn)閚ode版本問(wèn)題導(dǎo)致環(huán)境搭建失敗,這個(gè)里面沒(méi)有寫,可以去node官網(wǎng)查看版本的對(duì)應(yīng)關(guān)系

在appium自動(dòng)化腳本編寫的過(guò)程中,合理正確的使用api事半功倍,下面介紹使用的語(yǔ)言是Java,appium的版本是1.3.4,java-client的版本是java-client-2.1.0。

第一:AndroidDriver的使用,其已經(jīng)繼承了AppiumDriver。

private AndroidDriver driver;

@Before? ? ? ? public void setUp() throws Exception {? ? ? ? ? ? ? ?

?DesiredCapabilities capabilities = new DesiredCapabilities();? ? ? ? ? ? ? ? capabilities.setCapability("Name", "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/"),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? capabilities);? ? ? ? }? ? ? ? @After? ? ? ?

?public void tearDown() throws Exception {? ? ? ? ? ? ? ??

driver.quit();? ? ??

? }

2.截屏并保存至本地

//截屏并保存至本地? ? ? ? ? ? ? ??

File screen = driver.getScreenshotAs(OutputType.FILE);? ? ? ? ? ? ? ??

File screenFile = new File("d:\\screen.png");? ? ? ? ? ? ? ?

?try {? ? ? ? ? ? ? ? ? ? ? ??

FileUtils.copyFile(screen, screenFile); //commons-io-2.0.1.jar中的}?

catch (IOException e) {? ? ? ? ? ? ? ? ? ? ? ??

e.printStackTrace();? ? ? ? ? ? ? ??

}

3.push文件、pull文件

File file = new File("d:\\test.txt"); //test.txt內(nèi)容為"test"? ? ? ? ? ? ? ? String content = null;? ? ? ? ? ? ? ? try {? ? ? ? ? ? ? ? ? ? ? ? content = FileUtils.readFileToString(file);? ? ? ? ? ? ? ? } catch (IOException e) {? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? byte[]= 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))); //打印結(jié)果為"test"

4.

? ? ? ? //獲取當(dāng)前界面的activity,可用于斷言是否跳轉(zhuǎn)到預(yù)期的activity

? ? ? ? driver.currentActivity();

5.

? ? ? ? //打開(kāi)通知欄界面

? ? ? ? driver.openNotifications();

6.

//獲取網(wǎng)絡(luò)狀態(tài)? ? ? ? int= driver.getNetworkConnection().value;? ? ? ? System.out.println(status);? ? ? ? //設(shè)置網(wǎng)絡(luò)狀態(tài)? ? ? ? driver.setNetworkConnection(new NetworkConnectionSetting(status));? ? ? ? //或者? ? ? ? driver.setNetworkConnection(new NetworkConnectionSetting(false, true, false));

7.

? ? ? ? //啟動(dòng)其他應(yīng)用,跨APP

? ? ? ? driver.startActivity("com.android.camera", ".CameraLauncher");

8.

? ? ? ? //自動(dòng)滑動(dòng)列表

? ? ? ? driver.scrollTo("text");

? ? ? ? //或者

? ? ? ? driver.scrollToExact("text");

9.

? ? ? ? ? ? ? ? //安裝APP

? ? ? ? ? ? ? ? driver.installApp(appPath);


? ? ? ? ? ? ? ? //判斷應(yīng)用是否已安裝

? ? ? ? ? ? ? ? driver.isAppInstalled("package name");

10.

//拖動(dòng)圖標(biāo)至日歷圖標(biāo)位置? ? ? ? new TouchAction(driver).longPress(driver.findElementByName("相機(jī)"))? ? ? ? .moveTo(driver.findElementByName("日歷")).release().perform();

11.

? ? ? ? ? ? ? ? //鎖屏

? ? ? ? ? ? ? ? driver.lockScreen(2);


? ? ? ? ? ? ? ? //判斷是否鎖屏

? ? ? ? ? ? ? ? driver.isLocked();

12.

? ? ? ? ? ? ? ? //發(fā)送按鍵事件

? ? ? ? ? ? ? ? driver.sendKeyEvent(AndroidKeyCode.HOME);

13.

?//通過(guò)uiautomator定位clickable屬性為true的元素并點(diǎn)擊? ? ? ? ? ? ? ? driver.findElementByAndroidUIAutomator("new UiSelector().clickable(true)").click(); ? ? ? ? ? ? ? ? ? ? ? ? ?//相同屬性的元素使用List存放? ? ? ? ? ? ? ??

List elements = driver.findElementsByClassName("class name");? ? ? ? ? ? ? ? elements.get(0).click(); //點(diǎn)擊List中的第一個(gè)元素? ? ? ? ? ? ? ? //,點(diǎn)擊元素位置? ? ? ? ? ? ? ? driver.tap(1, driver.findElementByName("日期和時(shí)間"), 0);



初始化完成以后可以使用driver進(jìn)行各種操作——

1、按壓控件

方法:

press()

開(kāi)始按壓一個(gè)元素或坐標(biāo)點(diǎn)(x,y)。通過(guò)手指按壓手機(jī)屏幕的某個(gè)位置。

press(WebElement el, int x, int y)

press也可以接收屏幕的坐標(biāo)(x,y)。

例:

TouchAction(driver).press(x=0,y=308).release().perform()

除了press()方法之外,本例中還用到了別外兩個(gè)新方法。

release() 結(jié)束的行動(dòng)取消屏幕上的指針。

Perform() 執(zhí)行的操作發(fā)送到服務(wù)器的命令操作。

2、長(zhǎng)按控件

方法:

longPress()

開(kāi)始按壓一個(gè)元素或坐標(biāo)點(diǎn)(x,y)。 相比press()方法,longPress()多了一個(gè)入?yún)?,既然長(zhǎng)按,得有按的時(shí)間吧。duration以毫秒為單位。1000表示按一秒鐘。其用法與press()方法相同。

longPress(WebElement el, int x, int y, Duration duration)

例:

TouchAction action =newTouchAction(driver);

action.longPress(names.get(200),1000).perform().release();

action.longPress(200,200,1000).perform().release();

3、點(diǎn)擊控件

方法:

tap()

對(duì)一個(gè)元素或控件執(zhí)行點(diǎn)擊操作。用法參考press()。

tap(WebElement el, int x, int y)

例:

TouchAction action =newTouchAction(driver);

action.tap(names.get(200)).perform().release();

action.tap(200,200).perform().release();

4、移動(dòng)

方法:

moveTo()

將指針(光標(biāo))從過(guò)去指向指定的元素或點(diǎn)。

movTo(WebElement el, int x, int y)

其用法參考press()方法。

例:

TouchAction action =newTouchAction(driver);

action.moveTo(names.get(200)).perform().release();

action.moveTo(200,200).perform().release();

5、暫停

方法:

wait()

暫停腳本的執(zhí)行,單位為毫秒。

action.wait(1000);



最后附上一部分實(shí)例代碼————————

public static DesiredCapabilities setCapsforMobileBrowser(DesiredCapabilities capabilities) {

? ? ? ? capabilities = new DesiredCapabilities();

? ? ? ? capabilities.setCapability(MobileCapabilityType.APPIUM_VERSION, "1.9.0");//appium版本

? ? ? ? capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "2b2dc1d4ea9e67eb");

? ? ? ? capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.2");

? ? ? ? capabilities.setCapability("platformName", "Android");//平臺(tái)

? ? ? ? capabilities.setCapability("appPackage", "com.ireader.pad");//包名

? ? ? ? capabilities.setCapability("appActivity", "com.ireader.pad.business.splash.WelcomeActivity");

? ? ? ? capabilities.setCapability("unicodeKeyboard", true); //中文輸入

? ? ? ? capabilities.setCapability("resetKeyboard", true); //重置輸入法

? ? ? ? capabilities.setCapability("noSign", true);? //無(wú)需重復(fù)簽名

? ? ? ? capabilities.setCapability("noRest", true); //無(wú)需重啟

? ? ? ? //capabilities.setCapability("fullReset", true); //卸載清除文件

? ? ? ? return capabilities;

? ? }


/*

? * 截圖

? * */

? ? public static String screenShot() {

? ? ? ? String time = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());

? ? ? ? String screenShotPath = dir + File.separator + time + ".png";

? ? ? ? try {

? ? ? ? ? ? File sourceFile = driver.getScreenshotAs(OutputType.FILE);

? ? ? ? ? ? FileUtils.copyFile(sourceFile, new File(screenShotPath));

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? return "Failed to screenshot";

? ? ? ? }

? ? ? ? return screenShotPath.replace("\\", "/");

? ? }


/*

? ? * 上劃

? ? * */

? ? public void swipeToUp(AndroidDriver driver) {

? ? ? ? int width = driver.manage().window().getSize().width;

? ? ? ? int height = driver.manage().window().getSize().height;

? ? ? ? TouchAction action1 = new TouchAction(driver).press(PointOption.point(width / 2, height * 3 / 4)).waitAction(WaitOptions.waitOptions(duration))

? ? ? ? ? ? ? ? .moveTo(PointOption.point(width / 2, height / 4)).release();

? ? ? ? action1.perform();

? ? }

? ? /*

? ? * 左滑

? ? * */

? ? public void swipeToLeft(AndroidDriver driver) {

? ? ? ? int width = driver.manage().window().getSize().width;

? ? ? ? int height = driver.manage().window().getSize().height;

? ? ? ? TouchAction action3 = new TouchAction(driver);

? ? ? ? action3.press(PointOption.point(width * 4 / 5, height / 2)).waitAction(WaitOptions.waitOptions(duration))

? ? ? ? ? ? ? ? .moveTo(PointOption.point(width / 5, height / 2)).release();

? ? ? ? action3.perform();

? ? }


public void login() {

? ? ? ? driver.findElement(By.id("com.ireader.pad:id/next_btn")).click();

? ? ? ? driver.findElement(By.id("com.ireader.pad:id/know_btn")).click();

? ? ? ? try {

? ? ? ? ? ? //強(qiáng)制等待

? ? ? ? ? ? Thread.sleep(1000);

? ? ? ? } catch (InterruptedException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? //元素是否已顯示

? ? ? ? if (driver.findElement(By.id("com.ireader.pad:id/know_btn")).isDisplayed()) {

? ? ? ? ? ? driver.findElement(By.id("com.ireader.pad:id/know_btn")).click();

? ? ? ? }

? ? ? ? //返回

? ? ? ? if (driver.findElement(By.id("android:id/up")).isDisplayed()) {

? ? ? ? ? ? driver.findElement(By.id("android:id/up")).click();

? ? ? ? }

? ? ? ? //手機(jī)號(hào)登陸

? ? ? ? driver.findElement(By.id("com.ireader.pad:id/rl_selectlogin_mobile")).click();

? ? ? ? //賬號(hào)密碼登陸

? ? ? ? //driver.findElement(By.id("com.ireader.pad:id/rl_selectlogin_studentno")).click();

? ? ? ? //輸入

? ? ? ? /*AndroidElement phone = driver.findElement(By.id("com.ireader.pad:id/le_smslogin_mobile"));

? ? ? ? phone.sendKeys("17600868885");

? ? ? ? AndroidElement code = driver.findElement(By.id("com.ireader.pad:id/le_smslogin_code"));

? ? ? ? code.setValue("1234");

? ? ? ? AndroidElement sms = driver.findElement(By.id("com.ireader.pad:id/le_smslogin_sms"));

? ? ? ? sms.replaceValue("123456");*/

? ? ? ? List<AndroidElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");

? ? ? ? textFieldsList.get(0).sendKeys("17600868885");

? ? ? ? textFieldsList.get(1).setValue("1234");

? ? ? ? textFieldsList.get(2).replaceValue("123456");

? ? ? ? waitTwoTime();

? ? ? ? driver.findElement(By.id("com.ireader.pad:id/tv_login_finish")).click();

? ? ? ? waitTwoTime();

? ? ? ? for (int i=0; i<1;i++){

? ? ? ? ? ? countDownTest();? //倒計(jì)時(shí)

? ? ? ? ? ? //calendarTest();? ? //日歷

? ? ? ? ? ? //copyBookTest();? //字帖

? ? ? ? ? ? //englishTest();? ? //翻譯

? ? ? ? ? ? //setTest();? ? ? ? //設(shè)置

? ? ? ? ? ? //noteTest();? ? ? //筆記本

? ? ? ? ? ? //noteAddTest();? ? ? //筆記本重復(fù)添加測(cè)試

? ? ? ? ? ? //noteAddTest2();? ? ? //筆記本重復(fù)添加測(cè)試

? ? ? ? ? ? //teachShopTest();? //教輔商店

? ? ? ? ? ? //testPaper();? ? ? //試卷真題

? ? ? ? ? ? //textBook();? ? ? //課本

? ? ? ? ? ? //coursewareTest(); //課件

? ? ? ? }

? ? }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容