Web自动化测试—PO设计模式(一)

前言
很多的测试同学懂得使用selenium进行Web自动化测试,
但是不知道如何去写一个测试框架,或者说是一个容易维护的web自动化项目。
自己写一个最基本的web自动化测试框架需要会什么?
1. 语言基础,学到面向对象
2. 熟练使用selenium API (操作浏览器的方法)
3. 会使用基本的工具库,os,sys等
4. 一个测试框架 例如:unittest
5. page object设计模式 
6. 熟悉html、css
目录结构
--pages
    --__init__.py
    --base_page.py
base_page.py
#coding:utf8
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

#所有页面类的基类,实质上是对selenium进行二次封装
class BasePage(object):
    def __init__(self,driver,path=None):
        self.driver = driver
        # self.driver = webdriver.Chrome()
        self.url = 'https://www.tapd.cn/'
        self.time_out = 10
        self.poll_frequency = 0.3
        self.open_web_page(path)
        self.driver.maximize_window()

    def open_web_page(self,path):
        if path != None:
            url = self.url + path
        else:
            url = None
        if url != None:
            self.driver.get(url)
        else:
            pass

    def wait(self,*locator):
        WebDriverWait(self.driver,self.time_out,self.poll_frequency).until(EC.visibility_of_element_located(locator))

    def wait_text(self,text=None,*locator):
        WebDriverWait(self.driver,self.time_out,self.poll_frequency).until(EC.text_to_be_present_in_element((locator),text))

    def wait_all(self,text=None,*locator):
        if text == None:
            self.wait(*locator)
        else:
            self.wait_text(text,*locator)

    def by_css(self,css,text=None):
        locator = (By.CSS_SELECTOR,css)
        self.wait_all(text,*locator)
        return self.driver.find_element(*locator)




if __name__ == '__main__':
    driver = webdriver.Chrome()
    test_base_page = BasePage(driver,'cloud_logins/login')
    test_base_page.by_css('#username').send_keys('XXXXXX')
    test_base_page.by_css('#password_input').send_keys('XXXXXX')
    test_base_page.by_css('#tcloud_login_button').click()

posted @ 2018-06-24 22:04  深圳-逸遥  阅读(1370)  评论(0编辑  收藏  举报