(3)PO模型之登录模块

Page Object (PO模型)

一个页面,看做是一个类,我们通过对象提供的API,找到元素
自动化脚本分为三层

  1. 对象层 用于存放页面元素定位和控件操作
  2. 逻辑层 封装一些常用的功能模块,登录功能
  3. 业务层 真正的测试用例操作部分

封装find_element函数

from selenium.webdriver.support.ui import WebDriverWait
def find_element(driver,find_type,value):
    try:
        element = WebDriverWait(driver,20).until(lambda driver:driver.find_element(by=find_type,value=value))
        return element
    except Exception as e:
        raise e

163mail登录模块示例

对象层Objects

from util.find_element import find_element


class LoginPage():
    def __init__(self,driver):
        self.driver = driver

    def switch_accountlogin(self):
        #切换登录方式
        find_element(self.driver,'id','switchAccountLogin').click()
    def switch_iframe(self):
        #切换iframe
        i_frame = find_element(self.driver, 'tag name', 'iframe')
        self.driver.switch_to.frame(i_frame)
    def input_username(self,username):
        #输入账号
        find_element(self.driver,'name','email').send_keys(username)
    def input_password(self,password):
        #输入密码
        find_element(self.driver,'name','password').send_keys(password)
    def click_login(self):
        find_element(self.driver, 'id', 'dologin').click()



if __name__ == "__main__":
    from selenium import webdriver

    driver = webdriver.Chrome()
    driver.get('https://mail.163.com')
    driver.maximize_window()

    login_page = LoginPage(driver)
    login_page.switch_iframe()
    login_page.input_username('[email protected]')
    login_page.input_password('Ruru970929.')
    login_page.click_login()

逻辑层Account

from objects.LoginPage import LoginPage


class LoginAccount():

    def __init__(self):
        print('------开始登录了--------')
    @staticmethod
    def login(driver,username,password):
        #登录功能
        try:
            login_page = LoginPage(driver)
            login_page.switch_iframe()
            login_page.input_username(username)
            login_page.input_password(password)
            login_page.click_login()
        except Exception as e:
            raise e

if __name__ == "__main__":
    from selenium import webdriver
    driver = webdriver.Chrome()
    driver.get('https://mail.163.com')
    driver.maximize_window()


    LoginAccount.login(driver,'账号','密码')

业务层

import time
from selenium import webdriver
from moudel.LoginAccount import LoginAccount

def test_login():
    #测试登录功能
    try:
        driver = webdriver.Chrome()
        driver.get('http://mail.163.com')
        driver.maximize_window()
        LoginAccount.login(driver,'账号','密码')
    except Exception as e:
        raise e
    finally:
        time.sleep(3)
        driver.quit()

if __name__ == "__main__":
    test_login()

你可能感兴趣的:(Selenium自动化)