Python自动化教程

1.自动化目录结构

Python自动化教程_第1张图片

2.代码

AntoDriver.py

''''

@Project: xxx
@Description: webdriver API二次封装封装类
@Time:2019/11/11 18:51
@Author:MING

'''

from selenium import webdriver
from selenium.webdriver.support.select import Select

# 创建类
class AutoDriver():

    # 构造初始化方法
    def __init__(self):
        # 浏览器对象实例化
        self.driver = webdriver.Firefox()
        # 绑定URL
        self.base_url ='http://www.baidu.com/'

    #打开URL
    def open_url(self,url):
        self.driver.get(self.base_url+url)

    #全屏
    def max_window(self):
        self.driver.maximize_window()

    #关闭浏览器 close quit
    def close_page(self):
        self.driver.close()

    #八大定位
    def find_e_element(self,by,selector):
        if by=='id' or by =='i':
            return self.driver.find_element_by_id(selector)
        elif by=='name' or by=='n':
            return self.driver.find_element_by_name(selector)
        elif by=='class_name' or by=='c':
            return self.driver.find_element_by_class_name(selector)
        elif by=='tag_name' or by=='t':
            return self.driver.find_element_by_tag_name(selector)
        elif by=='link_text' or by=='l':
            return self.driver.find_element_by_link_text(selector)
        elif by=='partial_link_text' or by=='plt':
            return self.driver.find_element_by_partial_link_text(selector)
        elif by=='xpath' or by=='x':
            return self.driver.find_element_by_xpath(selector)
        elif by=='css_selector' or by=='cs':
            return self.driver.find_element_by_css_selector(selector)

    #警告框
    def switch_alert(self,value):
        if value=='accept' or value=='a':
            return self.driver.switch_to.alert.accept()
        elif value=='dismiss' or value=='d':
            return self.driver.switch_to.alert.dismiss()

    #下拉 by:属性  selector:属性值  val:下拉属性  value:下拉值
    def select_value(self,by,selector,val,value):
        if val=='value' or val=='v':
            return Select(self.find_e_element(by,selector)).select_by_value(value)
        elif val=='index' or val=='i':
            return Select(self.find_e_element(by,selector)).select_by_index(value)
        elif val=='visible_text' or val=='vt':
            return Select(self.find_e_element(by,selector)).select_by_visible_text(value)

BasePage.py

''''

@Project: test_selenium01
@Description: 公共基础类
@Time:2019/11/12 18:05
@Author:MING

'''

from auto_driver.AutoDriver import AutoDriver


class BasePage():
    def __init__(self,adr:AutoDriver):
        self.dr=adr

    # 登录
    def test_login(self,username,password):
        # 定位用户名输入框并传值
        self.dr.find_e_element('n', 'username').send_keys(username)
        self.dr.find_e_element('n','password').send_keys(password)
        # 勾选保存信息
        self.dr.find_e_element('i','remember').click()
        # 点击登录按钮
        self.dr.find_e_element('n','submit').click()

    # 点击登录按钮
    def  login_but(self):
        # 定位登录按钮并点击
        self.dr.find_e_element('x', '/html/body/div[1]/div[2]/ul/li[1]/font/a[1]/img').click()

    # 用户中心
    def user_cen(self):
        # 定位用户中心并点击
        self.dr.find_e_element('x','/html/body/div[1]/div[2]/ul/li[1]/font/font/a[1]').click()

    # 注册点击按钮
    def logon_btn(self):
        self.dr.find_e_element('x','/html/body/div[1]/div[2]/ul/li[1]/font/a[2]/img').click()

        # 注册用户
    def logon(self,username,email,password,conform_pwd,msn,qq,tel,tels,iphon,mimatis,pwd_answer):
        # 用户名
        self.dr.find_e_element('i','username').send_keys(username)
        # email
        self.dr.find_e_element('i','email').send_keys(email)
        # 密码
        self.dr.find_e_element('i','password1').send_keys(password)
        # 确认密码
        self.dr.find_e_element('i','conform_password').send_keys(conform_pwd)
        # MSN
        self.dr.find_e_element('n','extend_field1').send_keys(msn)
        # QQ
        self.dr.find_e_element('n','extend_field2').send_keys(qq)
        # 办公电话
        self.dr.find_e_element('n','extend_field3').send_keys(tel)
        # 家庭电话
        self.dr.find_e_element('n','extend_field4').send_keys(tels)
        # 手机
        self.dr.find_e_element('n','extend_field5').send_keys(iphon)
        # 密码提示
        self.dr.select_value('n','sel_question','v',mimatis)
        # 密码问题答案
        self.dr.find_e_element('n','passwd_answer').send_keys(pwd_answer)
        # 注册
        self.dr.find_e_element('c','us_Submit_reg').click()

SuPageUserCen.py

''''

@Project: test_selenium01
@Description: 子页面类
@Time:2019/11/12 18:06
@Author:MING

'''
from auto_driver.AutoDriver import AutoDriver
from base_page.BasePage import BasePage


class SubPageUserCen(BasePage):
    def __init__(self,adrr:AutoDriver):
        super().__init__(adrr)

    # 安全退出按钮
    def safe_quit(self):
         # 定位安全退出按钮并点击
         self.dr.find_e_element('x','/html/body/div[7]/div[1]/div/div/div/div/a[14]/img').click()

test.py

'''
 
 @Class : test  #当前Class名称;
 @Authr : Administrator  #当前用户名;
 @Date : 2020-03-04 15:21  #当前系统日期;
 
 '''
import csv
import time
import unittest
from selenium import webdriver

#******************************************第一种、原生编写***************************************************************#
# 浏览器实例化
from auto_driver.AutoDriver import AutoDriver
from base_page.SuPageUserCen import SubPageUserCen

driver = webdriver.Firefox()
# 打开浏览器
driver.get('https://www.hao123.com/')
# 全屏
driver.maximize_window()
#4.定位百度元素,输入值
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[1]/div[1]/form/div[2]/input').send_keys('苹果')
#5.点击按钮
time.sleep(2)
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[1]/div[1]/form/div[3]/input').click()
#6.关闭浏览器
driver.close()

#******************************************第二种、框架编写***************************************************************#
class TestCase(unittest.TestCase):
    # 前置
    def setUp(self):
        # 浏览器实例化
        self.driver = webdriver.Firefox()
        # 打开浏览器
        self.driver.get('https://www.hao123.com/')
        # 全屏
        self.driver.maximize_window()
    # 测试方法
    def test_case(self):
        #4.定位百度元素,输入值
        self.driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[1]/div[1]/form/div[2]/input').send_keys('苹果')
        #5.点击按钮
        time.sleep(2)
        self.driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[1]/div[1]/form/div[3]/input').click()
    # 收尾
    def tearDown(self):
        # 关闭
        self.driver.close()

#******************************************第三种、框架+PO编写***************************************************************#
    # 前置
    def setUp(self):
        self.d = AutoDriver()
        self.s = SubPageUserCen(self.d)
        # 全屏
        self.d.max_window()
    # 收尾
    def tearDown(self):
        self.d.close_page()
    # 测试方法
    def test_login(self):
        # 打开URL
        self.d.open_url('/')
        #CSV读取用户数据
        csv_r_file = open(r'D:\IDEpycharm\test_selenium01\test_data\login_data.csv', mode='r', encoding='utf-8')
        rd = csv.reader(csv_r_file)
        for i in rd:
            dict_data = {
                'username':i[0],
                'password':i[1]
            }
            # 定位登录按钮并点击
            self.s.login_but()
            print("用户名: "+dict_data['username'] + " 密码: "+dict_data['password'])
            #用户登录
            self.s.test_login(dict_data['username'],dict_data['password'])
            time.sleep(2)
            # 断言
            # 期望值
            expect_text = dict_data['username']
            # 实际值
            try:
                self.actual_text = self.d.find_e_element('x','/html/body/div[1]/div[2]/ul/li[1]/font/font/font').text
            except:
                print('登录失败,后面代码块不执行,请查看')
            # 断言判断
            self.assertEqual(expect_text,self.actual_text,'使用正确账号hjm/123456,登录失败!')
            # 定位用户中心并点击
            self.s.user_cen()
            time.sleep(0.5)
            # 定位安全退出按钮并点击
            self.s.safe_quit()
            time.sleep(3)
        # 文件关闭
        csv_r_file.close()

login_data.csv

admin,123456

 

你可能感兴趣的:(软件测试)