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

  1. Create a new python project, then check Robotframework and appium.
    (https://img-blog.csdn.net/20161116104156326)

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:

Appium for Robot Keywords:
http://jollychang.github.io/robotframework-appiumlibrary/doc/AppiumLibrary.html
Robot keywords:
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html
http://robotframework.org/robotframework/
http://robotframework.org/robotframework/latest/libraries/BuiltIn.html

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值