运维开发网

OP模型下的Python+selenium自动化

运维开发网 https://www.qedev.com 2020-07-05 07:35 出处:网络 作者:运维开发网整理
读取配置文件 1 import configparser 2 3 class ReadIni(): 4 5def __init__(self,file_name=None,node=None):

读取配置文件

 1 import configparser
 2 
 3 class ReadIni():
 4 
 5     def __init__(self,file_name=None,node=None):
 6         if file_name == None:
 7             file_name =‘C:\\Users\\mi\\Desktop\\Seleniumpython_pycharm\\imooc_seleniumyttt\\config\\LocalElement.ini‘
 8         if node == None:
 9             self.node = "RegisterElement"
10         else:
11             self.node = node
12         self.cf = self.load_ini(file_name)
13 
14     def load_ini(self,file_name):
15         cf = configparser.ConfigParser()
16         cf.read(file_name)
17         return cf
18 
19     def get_value(self,key):
20         data = self.cf.get(self.node, key)
21         return data
22 # 获取value的值
23 if __name__ == ‘__main__‘:
24     read_init = ReadIni()
25     print(read_init.get_value(‘user_name‘))
26 
27 
28 # cf.read(r"C:\Users\mi\Desktop\Seleniumpython_pycharm\config\LocalElement.ini")
29 # cf.get(‘RegisterElement‘,‘user_email‘)

 

封装查找元素方法

 1 from imooc_selenium.util.read_ini import ReadIni
 2 
 3 
 4 class FindElement():
 5 
 6     def __init__(self,driver):
 7         self.driver = driver
 8 
 9     def get_element(self,key):
10         read_int = ReadIni()
11         data = read_int.get_value(key)
12         by = data.split(‘>‘)[0]
13         value = data.split(‘>‘)[1]
14         try:
15             if by == ‘id‘:
16                 return self.driver.find_element_by_id(value)
17             elif by == ‘name‘:
18                 return self.driver.find_element_by_name(value)
19             elif by == ‘className‘:
20                 return self.driver.find_element_by_class_name(value)
21             else:
22                 return self.driver.find_element_by_xpath(value)
23         except:
24             return None

 

 

封装页面基本元素

 1 from PO.base.find_element import FindElement
 2 
 3 
 4 class RegisterPage():
 5 
 6     def __init__(self,driver):
 7         self.fd = FindElement(driver)
 8 
 9     # 获取邮箱元素
10     def get_email_element(self):
11         return self.fd.get_element("user_email")
12 
13     def get_username_element(self):
14         return self.fd.get_element("user_name")
15 
16     def get_password_element(self):
17         return self.fd.get_element("password")
18 
19     def get_code_element(self):
20         return self.fd.get_element("code_text")
21 
22     def get_button_element(self):
23         return self.fd.get_element("register_button")
24 
25     def get_email_error_element(self):
26         return self.fd.get_element("user_email_error")
27 
28     def get_name_error_element(self):
29         return self.fd.get_element("user_name_error")
30 
31     def get_password_error_element(self):
32         return self.fd.get_element("password_error")
33 
34     def get_code_error_element(self):
35         return self.fd.get_element("code_text_error")

 

 

封装对一个页面元素的单个操作

 1 from PO.page.register_page import RegisterPage
 2 
 3 
 4 class RegisterHandle():
 5 
 6     def __init__(self,driver):
 7         self.register_p = RegisterPage(driver)
 8 
 9     # 输入邮箱
10     def send_user_email(self,email):
11         self.register_p.get_email_element().send_keys(email)
12     # 输入用户名
13     def send_user_name(self,username):
14         self.register_p.get_username_element().send_keys(username)
15 
16     # 输入密码
17     def send_user_password(self,password):
18         self.register_p.get_password_element().send_keys(password)
19 
20     # 输入验证码
21     def send_user_code(self,code):
22         self.register_p.get_code_element().send_keys(code)
23 
24     # 获取文字信息
25     def get_user_text(self,info,user_info):
26         if info == ‘email_error‘:
27             text = self.register_p.get_email_error_element().get_attribute(‘value‘)
28         elif info == ‘name_error‘:
29             text = self.register_p.get_name_error_element().get_attribute(‘value‘)
30         elif info == ‘password_error‘:
31             text = self.register_p.get_password_element().get_attribute(‘value‘)
32         else:
33             text = self.register_p.get_code_error_element().get_attribute(‘value‘)
34         return text
35 
36     # 点击注册按钮
37     def click_register_button(self):
38         self.register_p.get_button_element().click()
39 
40     # 获取注册按钮文字
41     def get_register_text(self):
42         return self.register_p.get_button_element().text

 

 

封装对一个页面元素的多个操作

 1 from PO.handle.register_handle import RegisterHandle
 2 
 3 
 4 
 5 class RegisterBusiness():
 6 
 7 
 8     def __init__(self,driver):
 9         self.register_h  = RegisterHandle(driver)
10     # 执行操作
11 
12     def user_base(self,email,name,password,code):
13         self.register_h.send_user_email(email)
14         self.register_h.send_user_name(name)
15         self.register_h.send_user_password(password)
16         self.register_h.send_user_code(code)
17         self.register_h.click_register_button()
18 
19     def register_success(self):
20         if self.register_h.get_register_text() == None:
21             return True
22         else:
23             return False
24 
25 
26     def login_email_error(self,email,name,password,code):
27         self.user_base(email,name,password,code)
28         if self.register_h.get_user_text(‘email_error‘,"请输入有效的电子邮件地址")==None:
29             print("邮箱检验不成功")
30             return True
31         else:
32             return False
33 
34     def login_name_error(self,email,name,password,code):
35         self.user_base(email,name,password,code)
36         if self.register_h.get_user_text(‘user_name_error‘,"字符长度必须大于等于4,一个中文字算2个字符")==None:
37             print("用户名检验不成功")
38             return True
39         else:
40             return False
41 
42     # 密码错误
43     def login_password_error(self,email,name,password,code):
44         self.user_base(email,name,password,code)
45         if self.register_h.get_user_text(‘password_error‘,"最少需要输入 5 个字符")==None:
46             print("密码检验不成功")
47             return True
48         else:
49             return False
50 
51     # 验证码错误
52     def login_code_error(self, email, name, password, code):
53         self.user_base(email, name, password, code)
54         if self.register_h.get_user_text(‘code_text_error‘, "验证码错误") == None:
55             print("验证码检验不成功")
56             return True
57         else:
58             return False

 

 

封装自动化测试用例

 1 from PO.business.register_business import RegisterBusiness
 2 from selenium import webdriver
 3 
 4 class FirstCase():
 5     def __init__(self):
 6         driver = webdriver.Chrome()
 7         driver.get(‘http://www.5itest.cn/register‘)
 8         self.login = RegisterBusiness(driver)
 9 
10     def test_login_email_error(self):
11         email_error = self.login.login_email_error(‘34‘,‘user111‘,‘1111111‘,‘test1‘)
12         if email_error == True:
13             print("注册成功了,此条case执行失败")
14 
15     def test_login_username_error(self):
16         name_error = self.login.login_email_error(‘34‘,‘user111‘,‘1111111‘,‘test1‘)
17         if name_error == True:
18             print("注册成功了,此条case执行失败")
19 
20     def test_login_code_error(self):
21         code_error = self.login.login_email_error(‘34‘, ‘user111‘, ‘1111111‘, ‘test1‘)
22         if code_error == True:
23             print("注册成功了,此条case执行失败")
24 
25     def test_login_password_error(self):
26         password_error = self.login.login_email_error(‘34‘, ‘user111‘, ‘1111111‘, ‘test1‘)
27         if password_error == True:
28             print("注册成功了,此条case执行失败")
29 
30     def test_login_success(self):
31         success= self.login.user_base(‘34‘, ‘user111‘, ‘1111111‘, ‘test1‘)
32         if self.login.register_success() == True:
33             print("注册成功")
34 
35 def main():
36     fisrt = FirstCase()
37     fisrt.test_login_code_error()
38     fisrt.test_login_email_error()
39     fisrt.test_login_password_error()
40     fisrt.test_login_username_error()
41     # fisrt.test_login_success()
42 
43 if __name__ == ‘__main__‘:
44     main()

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号