appium学习【四】:第一个appium脚本

#coding=utf-8
import os
import HTMLTestRunner
import unittest
import time
import sys
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

reload(sys)
sys.setdefaultencoding('utf8')


#Returns abs path relative to this file and not cwd
PATH = lambda p:os.path.abspath(
    os.path.join(os.path.dirname(__file__),p)
)

#类继承unittest.TestCase类,从TestCase类继承是告诉unittest模块的方式,这是一个测试案例。
class BangbanAndroidTests(unittest.TestCase):
    #setUp 用于设置初始化的部分,在测试用例执行前,这个方法中的函数将先被调用。
    def setUp(self):
        desired_caps = {}
        desired_caps['deviceName'] = '127.0.0.1:62001'
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '4.4.2'
        desired_caps['appPackage'] = 'com.cbwlkj.cyzb' #邦办达人App的包名
        desired_caps['appActivity'] = 'com.hsdzkj.husong.ui.activity.IndexActivity' #启动时的Activity
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)

    #tearDown 方法在每个测试方法执行后调用,这个地方做所有清理工作,如退出
    def tearDown(self):
        self.driver.close_app()
        self.driver.quit()

    #放置的就是我们的测试脚本了,这部分我们并不陌生;因为我们执行的脚本就在这里。
    def test_bangban_login(self):
        try:
            WebDriverWait(self.driver,10).until(
            EC.presence_of_element_located((By.ID,'com.cbwlkj.cyzb:id/contact_phone1'))
            )
            self.driver.find_element_by_id('com.cbwlkj.cyzb:id/contact_phone1').send_keys('18602508223')
            time.sleep(5)
        except TimeoutException:
            print u'达人登录页面加载失败'
            funcName = sys._getframe().f_code.co_name
            print funcName
            pngfile = "E:\\appium_code\\png\\" + funcName + timestr + ".png"
            print pngfile
            self.driver.get_screenshot_as_file(pngfile)
        raise



#unitest.main()函数用来测试 类中以test开头的测试用例
if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(
        unittest.defaultTestLoader.loadTestsFromTestCase(BangbanAndroidTests)
    )
    timestr = time.strftime('%Y-%m-%d_%H%M%S', time.localtime(time.time()))
    filename = "E:\\appium_code\\report\\result_" + timestr + ".html"
    fp = open(filename, 'wb')
    runner = HTMLTestRunner.HTMLTestRunner(
        stream=fp,
        title='测试结果',
        description='测试报告'
    )
    runner.run(suite)
    fp.close()  # 测试报告关闭

 

posted @ 2017-06-19 17:02  悠然现南山  阅读(512)  评论(0编辑  收藏  举报