1. 程式人生 > >Python+Robot+Appium for Robot Sample

Python+Robot+Appium for Robot Sample

For mobile automation testing, there is an appium can support simulate user actions in the real/virtual mobile device. And robot framework is a automation framework which support create test case in text(robot)/html format, which has a graceful report. Appium has created a lib for robot. So it is easy and quick to create an mobile automation testing with Robot, Appium and Appium for Robot library.
Robot has advantages and disadvantages. Robot is easy to create test case when a general and flexible libraries are created, and it also has a great report. It can also integrated with Jenkins. Its disadvantage is not easy to debug and not suitable for complicated test cases especially need multiple components in testing.
Here is the sample to create a simple mobile automation testing.

Setup Environment

Video reference: https://www.youtube.com/watch?v=8mKcw1waMOU
1. Install python, download python zip and install it by clicking. Set PYTHONPATH. Install setup tools, which is easy for installing python libraries.
https://www.python.org/downloads/release/python-352/
Install easy_install, then install pip.
easy_install pip
2. Install JDK and Eclipse IDE.
3. Install Pydev plugin for Eclipse.
4. Install Appium, download setup and install it.
5. Install robot framework lib and Appium for Robot lib.

http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#installation-instructions
pip install robotframework
pip install robotframework-appiumlibrary
pip install robotframework-selenium2library

Selenium2Library is not (yet) compatible with Python 3.x.

We have a Pull Request (github.com/robotframework/Selenium2Library/pull/564) to have Selenium2Library compatible with all Python versions, but it is not in the official Release, and it was tested with Python 2.6, 2.7 and 3.4.

I you want to install you could try github.com/HelioGuilherme66/robotframework-selenium2library/releases/tag/v1.8.0b1 For example with: pip install -U –pre https://github.com/HelioGuilherme66/robotframework-selenium2library/archive/v1.8.0b1.zip

Create Python Project

Create Custom Library

  1. Create your own appium lib which can extend AppiumLibrary. You can add your external methods in his lib. This is not required, if you don’t need extend keywords of AppiumLibrary, you don’t need create your own library.
    CustomAppiumLib.py

import time
from AppiumLibrary import AppiumLibrary

class CustomAppiumLib(AppiumLibrary):
”’
Custom AppiumLibrary, can add extend functions(keywords).
”’

def **click_element_by_id**(self, locator):
    if  'id=' in locator:
        element_loc = locator       
    else:
        element_loc = 'id=' + locator

    self.click_element(element_loc)

def **get_current_application**(self):
    """Get current application instance.        
    """
    return self._current_application()

def **click_element_object**(self, element):
    """Click element identified by `element(WebElement)`.        
    """
    element.click();

def **wait_until_page_contains_elements**(self, loc_list, timeout=30):
    """Wait until page contains element based on locator list.

    Key attributes for arbitrary elements are `index` and `name`. See
    `introduction` for details about locating elements.
    """
    #import sys, pdb; pdb.Pdb(stdout=sys.__stdout__).set_trace()
    if type(timeout)!= int: timeout = int(timeout)
    maxtime = time.time() + timeout*1000
    while True:
        found_elements, found_loc = self._is_elements_present(loc_list)
        if found_elements: return found_loc
        if time.time() > maxtime:
            raise AssertionError("Can't find elements %s in the page" % loc_list)
        time.sleep(0.2)

def **_is_elements_present**(self, loc_list):
    """Verify whether one of elements in locator list is present in the page.

    Key attributes for arbitrary elements are `index` and `name`. See
    `introduction` for details about locating elements.
    """
    if type(loc_list) != list: loc_list = [loc_list]
    found_element = False 
    find_loc = ""       
    for loc in loc_list:
        loc_str = "id=%s" % loc
        self._bi.log("Check whether %s present" % loc)
        if self._is_element_present(loc_str):
            find_loc = loc
            found_element = True
            break;

    return found_element, find_loc

Create Test Cases

  1. Create your test case, import AppiumLibrary or your own AppiumLibrary if you have created.
  2. Create your test cases with Robot keywords, Appium for Robot keywords and your own keywords.
    Sample of test cases:
    demo.robot - test cases file.

*** Settings ***
#— Import libraries —————————————
Library appiumtest/common/CustomAppiumLib.py
Library DebugLibrary

#— Test case tags —————————————–
Force Tags demo

#— Suite setup and teardown.
Suite Setup Backup and Set Appium Timeout
Suite Teardown Close All Applications

*** Variables ***
#— For appium desired capability ————————–
${REMOTE_URL} http://localhost:4723/wd/hub
${AUTOMATION_NAME} appium

#— Device information ————————————–
${DEVICE_OS} Android

#device information
${DEVICE_NAME} Nexus9
${DEVICE_UDID} HT4A1JT03830
${DEVICE_OS_VERSION} 6.0.1

#— app package and activity information ——————–
${PKG_NAME} com.example.helloworld
${WAIT_ACTIVITY_NAME} com.example.HelloWorldActivity
${ACTIVITY_NAME} com.example.HelloWorldActivity

*** Test Cases ***
Log Device Info
Log ${DEVICE_NAME}
Log ${DEVICE_UDID}
Log ${DEVICE_OS_VERSION}

Go to System Settings
[Tags] test
${setting_pkg}= Set Variable com.android.settings
${setting_activity}= Set Variable com.android.settings.Settings
${android_setting}= Open Application \${REMOTE_URL} platformName=\${DEVICE_OS} platformVersion=\${DEVICE_OS_VERSION}
… deviceName=\${DEVICE_NAME} udid=\${DEVICE_UDID}
… automationName=\${AUTOMATION_NAME}
… appPackage=\${setting_pkg} appActivity=\${setting_activity}

Close Application

Open Application Test
[Tags] open_app
Open HelloWorld Application

*** Keywords ***
Open HelloWorld Application
Open Application ${REMOTE_URL} platformName=${DEVICE_OS}
… platformVersion=${DEVICE_OS_VERSION} deviceName=${DEVICE_NAME} udid=${DEVICE_UDID}
… automationName=${AUTOMATION_NAME} appPackage=${PKG_NAME} appActivity=${ACTIVITY_NAME}
… appWaitActivity=${WAIT_ACTIVITY_NAME}

#——– Common keywords ————————————–
Backup and Set Appium Timeout
[Arguments] ${new_timeout}=20
${backup_timeout}= Set Appium Timeout ${new_timeout}
${act_new_timeout}= Get Appium Timeout
Should Be Equal ${act_new_timeout.split(’ ‘)[0]} ${new_timeout.split(’ ‘)[0]}
[Return] ${backup_timeout}

deviceinfo.py - use to override device information when execution.
”’
Created on Aug 5, 2016

@author: yanpingc
”’
#Samsung Glaxy S4
DEVICE_NAME=’4d005f01f6262147’
DEVICE_UDID=’4d005f01f6262147’
DEVICE_OS_VERSION=’4.4.2’

Execute test cases

  1. Start appium server. - You can write python keyword to start appium server automatically. (node appium.js)
  2. Make sure the project path is added to PYTHONPATH:
    Set PYTHONPATH=%PYTHONPATH%;D:\PythonWS\mms_auto\src;
  3. Execute test case with command:
    pybot -V deviceinfo.py demo.robot

References:

Library Documentation Introduction
BuiltIn View Contains generic often needed keywords. Imported automatically and thus always available.
Collections View Contains keywords for handling lists and dictionaries.
DateTime View Supports creating and verifying date and time values as well as calculations between them.
Dialogs View Supports pausing the test execution and getting input from users.
OperatingSystem View Enables performing various operating system related tasks.
Process View Supports executing processes in the system.
Remote N/A Part of the remote library interface. Does not have any keywords of its own.
Screenshot View Provides keywords to capture and store screenshots of the desktop.
String View Library for manipulating strings and verifying their contents.
Telnet View Supports connecting to Telnet servers and executing commands on the opened connections.
XML View Library for verifying and modifying XML documents.

相關推薦

Python+Robot+Appium for Robot Sample

For mobile automation testing, there is an appium can support simulate user actions in the real/virtual mobile device. And robot fr

artificial intelligence for robot -SLAM-練習23

# ------------ # User Instructions # # In this problem you will implement SLAM in a 2 dimensional # world. Please define a function, slam, which take

Summary for Robot Framework

這片文章主要講Robot framework這個框架,因為最近做培訓,所以也想借此將這段時間的東西記錄沉澱下來. 首先是安裝: 安裝Python: HTTPs://www.python.org/downloads/ 新增環境變數 安裝Pip: https://github.com/pypa/pi

Want a robot vacuum for your workspace? Neato's Botvac D5 is $80 off

New technologies are optimizing modern offices for better collaboration, productivity, energy efficiency, employee health, and, believe it or not, cleanlin

Dr. Ben Goertzel, Creator of Sophia Robot, Hopes for a Benevolent AI Future

Dr. Ben Goertzel with his robot Sophia. Dr. Ben Goertzel is the founder and CEO of SingularityNET, a blockchain-based AI marketplac

robot framework for迴圈幾種方式

1、普通for迴圈 2、巢狀for迴圈 infor 3、for - in range 迴圈 普通for- in range for - in range +start-end-step (step-2未截圖出來) 由10開始遞減到2,幅度為2 4、for - in enum

Robot + Appium 搭建 iOS 自動化測試環境

一、iOS安裝條件  需要建立設定的移動平臺執行APPium測試。如以下的平臺資訊:  Mac OS 10.12.6 Xcode 9 Apple開發工具(IPhone simulator SDK,command line tools)。 二、Appium在OS X中安裝

Isight&Abaqus模擬優化例項:Mechanical Design Optimization for Robot Legs with Abaqus and Isight

【參考教程】 Abaqus Isight - Optimization of a Pump Support https://www.youtube.com/watch?v=xCmVjNXlczM 【YouTube轉載】Isight引數優化(士盟科技出品)(中文講解) https://www.bilibili.

Python 7 列表 for 字典,嵌套

功能 python 結果 guest arm 表示 bsp 第一個 必須 列表:   基本格式:變量名 = [元素1,元素2,元素3]   創建:A = [‘訪客‘,‘admin‘,19] 或 A = list([‘armin‘,‘admin‘,19]), 後者更傾向

python列表及for循環要註意的知識點

python 列表列表可以嵌套不同的數據類型,如下是在列表中嵌套字典a = [{"name":‘zhouziqi‘,‘contact‘:17806762943},{"name":‘zhouyu‘,‘contact‘:13246910612}] for i in a: print(i)我們可以用for循

python-循環(for、while)、判斷、字符串格式化

pre 重復 time body 字符串格式化 一次 post int randint python for循環 import random random_num=random.randint(1,1000) print(random_num); for i in ran

python 基礎 6 for 循環

sequence 打印 color pan div bre 循環 log 基礎 for 循環語句:   for循環可以遍歷任何序列的項目,如一個列表或者一個字符串。 for 循環語法格式:   for i in sequence     執行語句 實例: for i i

python 刪除正在for循環遍歷的list正確做法

body dex 之前 遍歷 mov pri div clas 結果 先放一個python遍歷發生的異常: 1 ls =[1,2,3,4,5,6,7,8,9] 2 for i in ls: 3 print("i",i) 4 print("ls",ls) 5

ShadowGun: Optimizing for Mobile Sample Level

logs blank post target https tps unity blog .com https://blogs.unity3d.com/2012/03/23/shadowgun-optimizing-for-mobile-sample-level/Shadow

Python-if、for、while的基礎用法

分支 代碼執行 pan 結束 nbsp 只需要 for 不想 區別 Python的各種代碼執行,都是從上至下執行,滿足條件就返回,不會執行後面的代碼 一、if    假如把寫程序比做走路,那我們到現在為止,一直走的都是直路,還沒遇到過分叉口,想象現實中,你遇到了分叉口,然後

Python——while和for循環

found BE 進行 AC 閱讀 解包 rate sha 但是 while 循環 一般格式: [python] view plain copy while <test>: <statements1> else <test

python while、for循環、list列表

mar 添加 模塊 位置 報錯 print 點擊 最後一個元素 語句 1、while循環(循環也可叫叠代、遍歷)   while 循環 必須有一個計數器  count=0  while count<10:    print(‘hello‘)    count=coun

python學習:for循環

range loop style pre tro for str 輸出 spa for循環 for i in range(1,4): print("loop:",i)輸出100以內的奇數: for i in range(1,101): if i % 2 == 1

Python基礎之for循環

pre code string for 字典 對象 基礎 val pri for循環:用戶按照順序循環可叠代對象的內容 1. for循環字符串 msg = "string" for i in msg: print(i) 執行結果為: s t r i n g 2. f

Python + Robotframework + Appium 之APP自動化測試小試牛刀(Android)

transform all 分享 直接 round letter 執行 image col Robotframework如何好?這裏先不說了~ Python更不用說了~ Appium前面的文章有介紹~ 今天直接來Python+Robotframework+Appium