appium的幾點總結

  • 1. 建立session時常用命令:
DesiredCapabilities cap = new DesiredCapabilities();
cap.SetCapability("browserName", ""); // web 瀏覽器名稱('Safari' ,'Chrome'等)。如果對應用進行自動化測試,這個關鍵字的值應爲空。
cap.SetCapability("platformName", "Android");//你要測試的手機操作系統
cap.SetCapability("platformVersion", "4.4");//手機操作系統版本
cap.SetCapability("automationName", "selendroid");  //你想使用的自動化測試引擎:Appium (默認) 或 Selendroid
cap.SetCapability("deviceName", " Android Emulator"); //使用的手機類型或模擬器類型,真機時輸入Android Emulator或者手機型號
cap.SetCapability("udid", udid); //連接的物理設備的唯一設備標識,Android可以不設置

cap.SetCapability("newCommandTimeout", "300");  //設置收到下一條命令的超時時間,超時appium會自動關閉session ,默認60秒
cap.SetCapability("unicodeKeyboard", "True");//支持中文輸入,會自動安裝Unicode 輸入法。默認值爲 false
cap.SetCapability("resetKeyboard", "True"); //在設定了 unicodeKeyboard 關鍵字的 Unicode 測試結束後,重置輸入法到原有狀態

cap.SetCapability("'app'", "D:\\AndroidAutomation\\AndroidAutoTest\\app\\zhongchou.apk");  //未安裝應用時,設置app的路徑

//手機已安裝app,直接從手機啓動app,上面路徑不設置
cap.SetCapability("appPackage", "com.nbbank");  //你要啓動的Android 應用對應的Activity名稱|比如`MainActivity`, `.Settings`|
cap.SetCapability("appActivity", "com.nbbank.ui.ActivityShow");  //你想運行的Android應用的包名
cap.SetCapability("appWaitActivity", "com.nbbank.ui.ActivityLogo");  //你想要等待啓動的Android Activity名稱|比如`SplashActivity`|

Uri serverUri = new Uri("http://127.0.0.1:4723/wd/hub");
driver = new AndroidDriver<IWebElement>(serverUri, cap, TimeSpan.FromSeconds(180));

更多詳細查看官網:https://github.com/appium/appium/blob/master/docs/cn/writing-running-appium/caps.cn.md

  • 2. driver常用方法及注意事項

1) 常用方法:

driver.HideKeyboard();//隱藏鍵盤
driver.BackgroundApp(60);//60秒後把當前應用放到後臺去
driver.LockDevice(3); //鎖定屏幕

//在當前應用中打開一個 activity 或者啓動一個新應用並打開一個 activity
driver.StartActivity("com.iwobanas.screenrecorder.pro", "com.iwobanas.screenrecorder.RecorderActivity");
driver.OpenNotifications();//打開下拉通知欄 只能在 Android 上使用
driver.IsAppInstalled("com.example.android.apis-");//檢查應用是否已經安裝
driver.InstallApp("path/to/my.apk");//安裝應用到設備中去
driver.RemoveApp("com.example.android.apis");//從設備中刪除一個應用
driver.ShakeDevice();//模擬設備搖晃
driver.CloseApp();//關閉應用
driver.LaunchApp();//根據服務關鍵字 (desired capabilities) 啓動會話 (session) 。請注意這必須在設定 autoLaunch=false 關鍵字時才能生效。這不是用於啓動指定的 app/activities
driver.ResetApp();//應用重置
driver.GetContexts();//列出所有的可用上下文
driver.GetContext();//列出當前上下文
driver.SetContext("name");//將上下文切換到默認上下文
driver.GetAppStrings();//獲取應用的字符串
driver.KeyEvent(176);//給設備發送一個按鍵事件:keycode
driver.GetCurrentActivity();//獲取當前 activity。只能在 Android 上使用
//driver.Pinch(25, 25);//捏屏幕 (雙指往內移動來縮小屏幕)
//driver.Zoom(100, 200);//放大屏幕 (雙指往外移動來放大屏幕)
driver.PullFile("Library/AddressBook/AddressBook.sqlitedb");//從設備中拉出文件
driver.PushFile("/data/local/tmp/file.txt", "some data for the file");//推送文件到設備中去

driver.FindElement(By.Name(""));
driver.FindElementById("id");
driver.FindElementByName("text");
driver.FindElementByXPath("//*[@name='62']");

2) 注意事項:
使用driver.Sendkeys(string str)向文本框輸入內容前,最好先element.Click( )一下,否則某些情況下,輸入的內容會請不掉,文本框提示的內容也會在 輸入的文本前顯示出來。sendkey方法在發送數據之前會清空一下文本框,一般不需要Clear,如前面的情況Clear後仍是存在的,click後正常

  • 3. 等待頁面加載策略:

1) 顯性等待:調用selenium的方法, 需要添加WebDriver.Support引用
顯性等待是指在代碼進行下一步操作之前等待某一個條件的發生。最不好的情況是使用Thread.sleep()去設置一段確認的時間去等待。但爲什麼說最不好呢?因爲一個元素的加載時間有長有短,你在設置sleep的時間之前要自己把握長短,太短容易超時,太長浪費時間。selenium webdriver提供了一些方法幫助我們等待正好需要等待的時間

            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
            element = wait.Until<IWebElement>((d) =>
           {
               return driver.FindElement(By.Id("userName"));     
           });

2) 隱性等待:設置時間不易過長,設置爲500或1000即可
隱性等待是指當要查找元素,而這個元素沒有馬上出現時,告訴WebDriver查詢Dom一定時間。默認值是0,但是設置之後,這個時間將在WebDriver對象實例整個生命週期都起作用。

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1));
  • 4. drive.KeyEvent(int )的使用: 可使用KeyEvent發送鍵盤數據,比如退格,Enter鍵等
driver.KeyEvent(3); //KEYCODE_HOME 按鍵Home 3
driver.KeyEvent(26);  //KEYCODE_POWER 電源鍵 26
driver.KeyEvent(67);  //KEYCODE_DEL 退格鍵 67
driver.KeyEvent(66);  //KEYCODE_ENTER 回車鍵
driver.KeyEvent(122); //KEYCODE_MOVE_HOME 光標移動到開始
driver.KeyEvent(123); //KEYCODE_MOVE_END 光標移動到末尾
  • 5. 座標操作

    爲防止不同手機分辨率不同帶來的影響,要避免使用固定的座標,可以用以下方式獲取元素的座標

double Screen_X = driver.Manage().Window.Size.Width;//獲取手機屏幕寬度
double Screen_Y = driver.Manage().Window.Size.Height;//獲取手機屏幕高度
double startX = element.Location.X; //獲取元素的起點座標,即元素最左上角點的橫座標
double startY = element.Location.Y; //獲取元素的起點座標,即元素最左上角點的縱座標
double elementWidth = element.Size.Width;  //獲取元素的寬度
double elementHight = element.Size.Height; //獲取元素的寬度

在封裝“滑動”、“ TouchAction”等操作時可以用以上方法來獲取座標進行操作。

示例:分裝兩個元素之間的滑動

        IWebElement elmentA = null;
        IWebElement elmentB = null;
        int startX = 0, startY = 0, endX = 0, endY = 0;
        int duration=0,time=0;
        /// <summary>
        /// 從元素A的位置滑動到元素B的位置
        /// </summary>
        /// <param name="A">元素A的名稱</param>
        /// <param name="B">元素B的名稱</param>
        /// <param name="sDuration">滑動持續時間</param>
        /// <param name="sTime">滑動次數</param>
        public void SwipeAToB(string A, string B,string sDuration,string sTime)
        {
            startX = elmentA.Location.X + elmentA.Size.Width / 2;  //元素A的中心橫座標
            startY = elmentA.Location.Y + elmentA.Size.Height / 2; //元素A的中心縱座標
            endX = elmentB.Location.X + elmentB.Size.Width / 2;    //元素B的中心橫座標
            endY = elmentB.Location.Y + elmentB.Size.Height / 2;   //元素B的中心縱座標

            duration = string.IsNullOrEmpty(sDuration) ? 1500 : int.Parse(sDuration); //持續時間爲空時,默認設置爲1500毫秒
            time = string.IsNullOrEmpty(sTime) ? 1500 : int.Parse(sTime); //滑動次數爲空時,默認設置爲滑動1次

            for (int i = 0; i < time; i++)
            {
                driver.Swipe(startX, startY, endX, endY, duration);
            }
        }

注意:element.Loaction和element.Size,每次獲取時都會重新去手機裏獲取,爲節省時間如果有獲取相同值的,建議儲存成變量。

  • 6. 取消重新安裝unlock和setting

註銷如下代碼:

Appium\node_modules\appium\lib\devices\android\android.js

async.series([
    this.initJavaVersion.bind(this),
    this.initAdb.bind(this),
    this.packageAndLaunchActivityFromManifest.bind(this),
    this.initUiautomator.bind(this),
    this.prepareDevice.bind(this),
    this.checkApiLevel.bind(this),
    this.pushStrings.bind(this),
    this.processFromManifest.bind(this),
    this.uninstallApp.bind(this),
    this.installAppForTest.bind(this),
    this.forwardPort.bind(this),
    this.pushAppium.bind(this),
    this.initUnicode.bind(this),

    // DO NOT push settings app and unlock app
    //this.pushSettingsApp.bind(this),
    //this.pushUnlock.bind(this),

    function (cb) {this.uiautomator.start(cb);}.bind(this),
    this.wakeUp.bind(this),
    this.unlock.bind(this),
    this.getDataDir.bind(this),
    this.setupCompressedLayoutHierarchy.bind(this),
    this.startAppUnderTest.bind(this),
    this.initAutoWebview.bind(this),
    this.setActualCapabilities.bind(this)
  ], function (err) {
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章