#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 ' Personal Center '))
#打开个人中心
El_login = self.driver.find_element_by_name (u "Personal Center")
El_login.click ()
#判断当前元素是否存在, if present, the user is logged in, or if it does not exist, perform a login operation
if (webdriverwait (Self.driver). Until (ec.invisibility_of_element_located (by.id, ' Id_username ')):
Pass
ELSE:
sys.exit ()
#打开登录页面
Login_regist ER = self.driver.find_element_by_id (' btn_islogin ')
Login_register.click ()
& nbsp #输入用户名, password, and then login
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):
#重新打开客户端 and then quit the app
Webdriverwait (self.driver,20). Until (Ec.presence_of_element_located ((by.name, U ' Personal Center '))
El_logout = self.driver.find_element_by_name (u "Personal Center")
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)
Reference article:
A) Http://www.tuicool.com/articles/vQ36nyZ
Python Writing Appium test Cases (2)