python+selenium+chrome 自动化测试TPshop商城项目实战(三) ——注册封装练习

这个是在python+selenium+chrome 自动化测试TPshop商城项目实战(一)的基础上进行简单的注册封装,先对需求进行简单的分析,然后设计类,

首先定义注册类Register,

然后分析类中的对象属性、方法,

其中

注册按钮、手机注册、手机号码输入框、图像验证码输入框、设置密码输入框、确认密码输入框——属性

定位注册按钮、手机注册按钮、手机输入框等定位元素——行为

点击注册按钮、手机注册按钮,提交按钮——行为

在输入框内输入内容——行为

验证内容是否正确——行为

下表时整理的类模板,但是由于输入内容单独定义比较麻烦,就在定位输入框后之间进行输入内容操作,把输入方法取消。

Register注册类

注册按钮:register_button

手机注册:phone_register

手机号码:phone_register_username

图像验证码:phone_register_placeholder

设置密码:phone_register_password

确认密码:phone_register_password_sure

同意协议书:informed_consent

同意注册按钮:agree_register

行为:

定位元素:

输入内容:input

点击按钮:click_button

勾选同意书:click_informed_consent

点击注册按钮:click_agree_register

验证用户名:check_user

验证图像验证码:Checking_placeholder

验证密码输入格式:Checking_password

验证密码是否一致:Checking_password_sure

# web自动化基本代码
# 1.导入模块
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
"""class SearchTest:
    def setup(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
        self.driver.get("http://demo6.tp-shop.cn/")
tpshop_test = SearchTest()
tpshop_test.setup()
"""



class Register:

    #定义定位注册按钮元素方法
    def __init__(self):
        """初始化驱动设置"""

        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
        self.driver.get("http://demo6.tp-shop.cn/")

    def register_button_position(self):

        #定位注册按钮
        self.phone_register_button = self.driver.find_element_by_class_name("red")
        #调用点击注册按钮方法来实现点击注册按钮
        self.click_button()

    def phone_register_username(self):
        #定位注册页面用户名输入框
        self.register_username = self.driver.find_element_by_id("username")
        self.register_username.send_keys("  ")
        time.sleep(3)

    def phone_register_placeholder(self):
        # 定位验证码输入框
        self.register_placeholder = self.driver.find_element_by_id("verify_code2")
        self.register_placeholder.send_keys("qwer")
        time.sleep(3)

    def phone_register_password(self):
        # 定位密码输入框
        self.register_password = self.driver.find_element_by_id("password")
        self.register_password.send_keys("123456")
        time.sleep(3)

    def phone_register_password_sure(self):
        # 定位重复密码输入框
        self.register_password_sure = self.driver.find_element_by_id("password2")
        self.register_password_sure.send_keys("12333456")
        time.sleep(3)


    def click_informed_consent(self):
        self.Signed_informed_consent = self.driver.find_element_by_class_name("iyes")
        Signed_informed_consent.click()
        print("勾选同意书")

    def click_agree_register(self):
        self.agree_register = self.driver.find_element_by_id("submit")
        self.agree_register.click()

    def Checking_user(self):

        self.register_username_message_check = self.driver.find_element_by_id("err_username")
        self.register_username_message_check = self.register_username_message_check.text
        print("错误信息:%s"%self.register_username_message_check)
        print("检查用户名格式")
        time.sleep(3)

    def Checking_placeholder(self):
        self.register_placeholder_check = self.driver.find_element_by_id("err_verify_code")
        self.placeholder_message_check  =  self.register_placeholder_check.text
        print("错误信息:%s" % self.placeholder_message_check)
        time.sleep(3)


    def Checking_password(self):
        self.register_password_check = self.driver.find_element_by_id("err_password")
        self.register_password_message_check = self.register_password_check.text
        print("错误信息:%s" % self.register_password_message_check)



    def Checking_password_sure(self):
        self.register_password_sure_check = self.driver.find_element_by_id("err_password2")

        self.register_password_sure_message_check = self.register_password_sure_check.text

        print("错误信息%s" % self.register_password_sure_message_check)



    def click_button(self):
        #点击注册按钮
        self.phone_register_button.click()

    def __del__(self):
        """
        关闭webdriver驱动
        :return:
        """
        self.driver.quit()
#创建对象
phone_register = Register()
#调用方法
phone_register.register_button_position()
phone_register.phone_register_username()
phone_register.phone_register_placeholder()
phone_register.phone_register_password()
phone_register.phone_register_password_sure()
phone_register.click_agree_register()
phone_register.Checking_user()
phone_register.Checking_placeholder()
phone_register.Checking_password()
phone_register.Checking_password_sure()





你可能感兴趣的:(python,selenium项目实战,自动化测试,python,chrome,selenium,封装)