您的位置:首页 > 编程语言 > Python开发

Selenium-AndroidDriver(python) (一) --- 环境配置及脚本运行

2013-10-29 12:58 1026 查看
androiddriver环境配置
建模拟器
1.下载android SDK并放到非中文路径下:http://developer.android.com/sdk/index.html
2. 解压并放到非中文路径下,如 D:\adt-bundle-windows-x86-20130917
3.  配置环境变量:我的电脑>属性>高级>环境变量>Path>变量值中加入adb.exe路径,如:..\android-sdk\sdk\platform-tools(前后要以分号隔开)
4.双击SDK Manager.exe,初始化的时候有时会报错(未解决),可以忽略,点击Tools>Manage AVDs,点击New按下图参数配置一个模拟器(保险些,有些不能用)
     Nexus S (4.0",480 x 800: hdpi)
     Android 4.3 - API Level 18

安装Androiddirver
1. 下载android server apk:http://code.google.com/p/selenium/downloads/list

2. cmd>adb devices获取序列ID,如下图:



3. 启动Android WebDriver程序:
$./adb -s <ID> shell am start -a android.intert.action.MAIN -n org.openqa.selenium.android.app/.ManiActivit



4. 设置端口号,便于模拟器与主机通信
 adb -s emulator-5554 forward tcp:8080 tcp:8080
 
其它:
1. DK Manager.exe,初始化的时候有时会报错(未解决)
2. 上下键不能用,作以下修改:
C:\Documents and Settings\Administrator\.android\avd\A4.avd\ config.ini文件中” hw.dPad”的值改成”yes”保存后,重新打开SDK
Manager.exe,启动模拟器
3.  Android driver最好用android-server-2.21.0.apk
4.  模拟器设置:
Settings -> Applications -> Development ->勾选"USB debugging", "Stay Awake" and "Allow mock locations"(默认是勾上的)
5.建立模拟器也可以用命令建,个人觉得手动方便些,有兴趣的可以自己找资料研究。
6.手机设备也可以是真机,操作步骤和模拟器一样,只是少了建模拟器的步骤。
 
执行Python脚本

前提:配置好selenium webdriver for python、selenium IDE环境

1.Selenium IDE录制脚本

2.导出脚本: 文件>Export Test Case As..>Python /WebDriver ,任意命名为.py结尾的文件,如 test.py

3.用Python 打开test.py,修改脚本:

 1). 开头加入一下这行脚本:

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

 2). self.driver = webdriver.Firefox()改成:

        self.driver = webdriver.Remote("http://localhost:8080/wd/hub", webdriver.DesiredCapabilities.ANDROID)

 或者self.driver = webdriver.Remote("http://localhost:8080/wd/hub",desired_capabilities={"browserName": "android"})都可以

以下是完整的例子:

# -*- coding: utf-8 -*-

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.support.ui import Select

from selenium.common.exceptions import NoSuchElementException

import unittest, time, re

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities#手动加入

class AndroidBaidu(unittest.TestCase):

    def setUp(self):

        #self.driver = webdriver.Firefox()

        self.driver = webdriver.Remote("http://localhost:8080/wd/hub", webdriver.DesiredCapabilities.ANDROID)#手动加入

        self.driver.implicitly_wait(30)

        self.base_url = "http://m.baidu.com"

        self.verificationErrors = []

        self.accept_next_alert = True

   

    def test_android_baidu(self):

        driver = self.driver

        driver.get(self.base_url + "/")

        driver.find_element_by_id("kw").send_keys("cheese!")

        driver.find_element_by_id("se_bn").click()

   

    def is_element_present(self, how, what):

        try: self.driver.find_element(by=how, value=what)

        except NoSuchElementException, e: return False

        return True

   

    def is_alert_present(self):

        try: self.driver.switch_to_alert()

        except NoAlertPresentException, e: return False

        return True

   

    def close_alert_and_get_its_text(self):

        try:

            alert = self.driver.switch_to_alert()

            alert_text = alert.text

            if self.accept_next_alert:

                alert.accept()

            else:

                alert.dismiss()

            return alert_text

        finally: self.accept_next_alert = True

   

    def tearDown(self):

        #self.driver.quit()

        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":

    unittest.main()

 

资料参考:

http://code.google.com/p/selenium/wiki/AndroidDriver#Supported_Platforms

https://github.com/truebit/AndroidWebDriver4Python

http://stackoverflow.com/questions/16220896/androiddriver-python-bindings-httplib

问题:

1.手机端如何处理弹出窗口?如,登录界面,输入错误用户名,点登录,提示“用户名密码错误”

  执行一下脚本无效:self.assertEqual(u"用户名或密码错误", self.close_alert_and_get_its_text())#在 webdriver python中可以关闭弹出窗

2.在真机中执行脚本,如何将真机屏幕显示在电脑屏幕上?

   91是不行的,开启91,androiddriver服务就断了

大家有好的解决办法,欢迎留言。

 

问题2已解决:

安装91assistant_3.0130.exe 就可以同时打开adb 命令窗口与91 

 

 点击查看更多.........

 

 点击查看更多.........

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: