#coding=utf-8
import os,sys
import unittest
from appium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)
ISOTIMEFORMAT='%Y-%m-%d %X'
class LoginTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4.4'
desired_caps['deviceName'] = '24c13456'
#desired_caps['app'] = PATH('ContactManager/ContactManager.apk')
desired_caps['appPackage'] = 'com.aaa.bbb'
desired_caps['appActivity'] = '.MainActivity'
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
def tearDown(self):
self.driver.quit()
def test_user_login(self):
WebDriverWait(self.driver,20).until(EC.presence_of_element_located((By.NAME, u'个人中心')))
#打开个人中心
el_login = self.driver.find_element_by_name(u"个人中心")
el_login.click()
#判断当前元素是否存在,若是存在,则用户已登录;若不存在,则执行登录操作
if(WebDriverWait(self.driver, 10).until(EC.invisibility_of_element_located((By.ID,'id_username')))):
pass
else:
sys.exit()
#打开登录页面
login_register = self.driver.find_element_by_id('btn_islogin')
login_register.click()
#输入用户名,密码,然后登录
user_name = self.driver.find_element_by_id('et_usename')
user_name.send_keys('aaa')
user_pass = self.driver.find_element_by_id('et_passwrod')
user_pass.send_keys('123456')
butn_login = self.driver.find_element_by_id('btn_login')
butn_login.click()
def test_user_logout(self):
#重新打开客户端,然后退出app
WebDriverWait(self.driver,20).until(EC.presence_of_element_located((By.NAME, u'个人中心')))
el_logout = self.driver.find_element_by_name(u"个人中心")
el_logout.click()
#打开个人信息页面
WebDriverWait(self.driver,20).until(EC.presence_of_element_located((By.ID, 'id_username')))
login_pic = self.driver.find_element_by_id('btn_islogin')
login_pic.click()
user_logout = self.driver.find_element_by_id('id_logout')
user_logout.click()
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(LoginTests)
unittest.TextTestRunner(verbosity=2).run(suite)
参考文章:
a) http://www.tuicool.com/articles/vQ36nyZ