#coding=utf-8
import unittest
from selenium import webdriver
dr=webdriver.Chrome() #把driver提取出来,用例执行后,窗口不被关闭
email={
"email_w":"test_email",
"email_r":"test_email"
}
pwd={
"pwd_w":"test_pwd",
"pwd_r":"test_pwd"
}
class formLogin(unittest.TestCase):
def setUp(self,driver=dr):#用例执行前的准备工作
self.driver=driver
self.host="http://host"
def tearDown(self): #用例执行后,清理环境
self.driver.refresh() #每个用例执行后,刷新页面
def find_ele(self, methods): #定位函数
method=methods.split(",")[0]
value=methods.split(",")[1]
if method == "name":
locate = self.driver.find_element_by_name(value)
elif method == "id":
print 1
locate = self.driver.find_element_by_id(value)
elif method == "xpath":
locate = self.driver.find_element_by_xpath(value)
elif method == "class": #传入复合类名称时,会报错,挑选合适的类名
locate = self.driver.find_element_by_class_name(value)
else:
raise NameError("get a invalid type of name")
return locate
def type(self,methods,text): #输入内容函数
locate=self.find_ele(methods)
locate.clear()
locate.send_keys(text)
def clickButton(self,methods):#根据定位点击按钮
button=self.find_ele(methods)
button.click()
def do_login(self,name,password):#登录通用功能
self.driver.get(self.host+"/auth/login")
self.type("name,email",name)
self.type('name,password',password)
self.clickButton('class,btn-success')
def test_login_fail_email(self):
self.do_login(email["email_w"],pwd['pwd_r'])
url_current=self.driver.current_url
self.assertEqual("http://host/auth/login",url_current,"failed")
def test_login_fail_pwd(self):
self.do_login(email["email_r"],pwd["pwd_w"])
url_current=self.driver.current_url
self.assertEqual("http://host/auth/login",url_current,"failed")
def test_login_success(self):
self.do_login(email["email_r"],pwd["pwd_r"])
url_current=self.driver.current_url
self.assertEqual("http://host/formcenter",url_current)
if __name__=="__main__":
unittest.main(verbosity=2)#verbosity=2输出用例执行的详细信息
思路:
1.在用例设计时,可以将元素定位方法封装为find_ele
,封装后,只需要按照格式传输定位方式和对应的内容即可
2.将每个用例中重复的使用公用的定位,输入方法封装为函数do_login
,在每条用例中重复使用即可
3.将登录数据从用例函数中提取出来,可以在执行用例时,灵活的变更使用