多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

Selenium3与Python3实战Web自动化测试框架

更新时间:2022-07-18 09:27:31 作者:多测师 浏览:33

  很多人被这门课的pom模型弄得云里雾里的,根据自己的整理,总结了本课程主要的类和对象的调用顺序:

Selenium3与Python3实战Web自动化测试框架

  1、数据驱动:LocalElement.ini

  [RegisterElement]

  user_email=id>register_email

  user_email_error=id>register_email-error

  user_name=id>register_nickname

  user_name_error=id>register_nickname-error

  password=id>register_password

  password_error=id>register_password-error

  code_image=id>getcode_num

  code_text=id>captcha_code

  code_text_error=id>captcha_code-error

  register_button=id>register-btn

  2、read_ini.py 读取LocalElement.ini数据信息:

  #coding=utf-8import configparserclass ReadIni(object):

  def __init__(self,file_name=None,node=None):

  if file_name == None:

  file_name = "E:/Teacher/Imooc/SeleniumPython/config/LocalElement.ini"

  if node == None:

  self.node = "RegisterElement"

  else:

  self.node = node

  self.cf = self.load_ini(file_name)

  #加载文件

  def load_ini(self,file_name):

  cf = configparser.ConfigParser()

  cf.read(file_name)

  return cf

  #获取value得值

  def get_value(self,key):

  data = self.cf.get(self.node,key)

  return data#if __name__ == '__main__':# read_init = ReadIni()# print(read_init.get_value('user_name'))

  3、find_element.py 【调用read_ini.py,返回元素定位对象】

  #coding=utf-8from util.read_ini import ReadInifrom log.user_log import UserLogclass FindElement(object):

  def __init__(self,driver):

  self.driver = driver

  get_user_log = UserLog()

  self.logger = get_user_log.get_log()

  def get_element(self,key):

  read_ini = ReadIni()

  data = read_ini.get_value(key)

  by = data.split('>')[0]

  value = data.split('>')[1]

  self.logger.info("定位方式:"+by+"--->定位值:"+value)

  try:

  if by == 'id':

  return self.driver.find_element_by_id(value)

  elif by == 'name':

  return self.driver.find_element_by_name(value)

  elif by == 'className':

  return self.driver.find_element_by_class_name(value)

  else:

  return self.driver.find_element_by_xpath(value)

  except:

  return None

  4、register_page.py【调用find_element.py,返回元素定位对象】

  #coding=utf-8from base.find_element import FindElementclass RegisterPage(object):

  def __init__(self,driver):

  self.fd = FindElement(driver)

  #获取邮箱元素

  def get_email_element(self):

  return self.fd.get_element("user_email")

  #获取用户名元素

  def get_username_element(self):

  return self.fd.get_element("user_name")

  #获取密码元素

  def get_password_element(self):

  return self.fd.get_element("password")

  #获取验证码元素

  def get_code_element(self):

  return self.fd.get_element("code_text")

  #获取注册按钮元素

  def get_button_element(self):

  return self.fd.get_element("register_button")

  #获取邮箱错误元素

  def get_email_error_element(self):

  return self.fd.get_element("user_email_error")

  #获取用户名错误元素

  def get_name_error_element(self):

  return self.fd.get_element("user_name_error")

  #获取密码错误元素

  def get_password_error_element(self):

  return self.fd.get_element("password_error")

  #获取验证码错误元素

  def get_code_error_element(self):

  return self.fd.get_element("code_text_errorr")

  5、register_handle.py 【调用register_page.py,通过元素对象输入值或者返回数据】

  #coding=utf-8from page.register_page import RegisterPageimport sysfrom util.get_code import GetCodefrom log.user_log import UserLog

  class RegisterHandle(object):

  def __init__(self,driver):

  self.driver = driver

  self.register_p = RegisterPage(self.driver)

  get_user_log = UserLog()

  self.loger = get_user_log.get_log()

  #输入邮箱

  def send_user_email(self,email):

  self.loger.info("输入的邮箱值是:"+email)

  self.register_p.get_email_element().send_keys(email)

  #输入用户名

  def send_user_name(self,username):

  self.loger.info("输入的用户名是:"+username)

  self.register_p.get_username_element().send_keys(username)

  #输入密码

  def send_user_password(self,password):

  self.loger.info("输入的密码是:"+password)

  self.register_p.get_password_element().send_keys(password)

  #输入验证码

  def send_user_code(self,file_name):

  get_code_text = GetCode(self.driver)

  code = get_code_text.code_online(file_name)

  self.register_p.get_code_element().send_keys(code)

  #获取文字信息

  def get_user_text(self,info,user_info):

  try:

  if info == 'user_email_error':

  text = self.register_p.get_email_error_element().text

  elif info == 'user_name_error':

  text = self.register_p.get_name_error_element().text

  elif info == 'password_error':

  text = self.register_p.get_password_element().text

  else:

  text = self.register_p.get_code_element().text

  except:

  text = None

  return text

  #点击注册按钮

  def click_register_button(self):

  self.register_p.get_button_element().click()

  #获取注册按钮文字

  def get_register_text(self):

  return self.register_p.get_button_element().text

  6、register_business.py 【调用register_handle.py的send_user_XXX和get_user_text方法】

  – 先调用send_user_XXX将case数据传给register_handle处理,再调用get_user_text查看case是否正确

  #coding=utf-8from handle.register_handle import RegisterHandleclass RegisterBusiness:

  def __init__(self,driver):

  self.register_h = RegisterHandle(driver)

  def user_base(self,email,name,password,file_name):

  self.register_h.send_user_email(email)

  self.register_h.send_user_name(name)

  self.register_h.send_user_password(password)

  self.register_h.send_user_code(file_name)

  self.register_h.click_register_button()

  def register_succes(self):

  if self.register_h.get_register_text() == None:

  return True

  else:

  False

  '''

  执行操作

  def login_email_error(self,email,name,password,file_name):

  self.user_base(email,name,password,file_name)

  if self.register_h.get_user_text('email_error',"请输入有效的电子邮件地址") == None:

  #print("邮箱检验不成功")

  return True

  else:

  return False

  '''

  def register_function(self,email,username,password,file_name,assertCode,assertText):

  self.user_base(email,username,password,file_name) #输入邮箱(假设错误的邮箱),user_email_error就会有值

  if self.register_h.get_user_text(assertCode,assertText) == None: # 表示text没值

  #print("邮箱输入正确,检验不成功")

  return True

  else:

  #print("邮箱输入错误,成功检验")

  return False # 表示text有值,邮箱输入错误被校验住了 '''

  #name错误

  def login_name_error(self,email,name,password,file_name):

  self.user_base(email,name,password,file_name)

  if self.register_h.get_user_text('user_name_error',"字符长度必须大于等于4,一个中文字算2个字符") == None:

  #print("用户名检验不成功")

  return True

  else:

  return False

  #密码错误

  def login_password_error(self,email,name,password,file_name):

  self.user_base(email,name,password,file_name)

  if self.register_h.get_user_text('password_error',"最少需要输入 5 个字符") == None:

  #print("密码检验不成功")

  return True

  else:

  return False

  #验证码错误

  def login_code_error(self,email,name,password,file_name):

  self.user_base(email,name,password,file_name)

  if self.register_h.get_user_text('code_text_error',"验证码错误") == None:

  #print("验证码检验不成功")

  return True

  else:

  return False

  '''

  7、first_ddt_case.py 【调用register_business.py的register_function 方法】

  #coding=utf-8import ddtimport unittestimport sys

  sys.path.append('E:\Teacher\Imooc\SeleniumPython')import tracebackfrom business.register_business import RegisterBusinessfrom selenium import webdriverimport HTMLTestRunnerfrom util.excel_util import ExcelUtilimport osfrom log.user_log import UserLogimport time

  ex = ExcelUtil() # 获取操作Excel的对象

  data = ex.get_data() # 根据对象调用获取数据的方法获取数据,存放在data里

  log = UserLog()

  logger = log.get_log()

  file_name = "E:/Teacher/Imooc/SeleniumPython/Image/test001.png"#邮箱、用户名、密码、验证码、错误信息定位元素、错误提示信息

  @ddt.ddtclass FirstDdtCase(unittest.TestCase):

  def setUp(self):

  self.driver = webdriver.Chrome()

  self.driver.get('')

  self.login = RegisterBusiness(self.driver)

  def tearDown(self):

  time.sleep(2)

  #if sys.exc_info()[0]:

  for method_name,error in self._outcome.errors:

  if error:

  case_name = self._testMethodName

  file_path = os.path.join(os.getcwd()+"/report/"+case_name+".png")

  self.driver.save_screenshot(file_path)

  #print("这个是case的后置调键1")

  self.driver.close()

  '''

  @ddt.data(

  ['12','Mushishi01','111111','code','user_email_error','请输入有效的电子邮件地址'],

  ['@qq.com','Mushishi01','111111','code','user_email_error','请输入有效的电子邮件地址'],

  ['12@qq.com','Mushishi01','111111','code','user_email_error','请输入有效的电子邮件地址']

  )

  @ddt.unpack

  '''

  @ddt.data(*data) #data里存放着Excel里的数据

  def test_register_case(self,data):

  #通过list赋值方式,将Excel里数据赋值到各个参数里

  email,username,password,self.file_name,assertCode,assertText = data

  email_error = self.login.register_function(email,username,password,self.file_name,assertCode,assertText)

  self.assertFalse(email_error,"测试失败")

  if __name__ == '__main__':

  file_path = os.path.join(os.getcwd()+"/report/"+"first_case1.html")

  f = open(file_path,'wb')

  suite = unittest.TestLoader().loadTestsFromTestCase(FirstDdtCase)

  runner = HTMLTestRunner.HTMLTestRunner(stream=f,title="This is first report1",description=u"这个是我们第一次测试报告1",verbosity=2)

  runner.run(suite)

  以上内容为大家介绍了Selenium3与Python3实战Web自动化测试框架,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部