zoukankan      html  css  js  c++  java
  • python3.6+selenium_Page Object设计模式

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time : 2019/1/9 11:26
    # @File : test_8_3_2_BasePage.py
    '''
    Project: 基础类BasePage,封装所有页面都公用的方法,
    定义open函数,重定义find_element,switch_frame,send_keys等函数。
    在初始化方法中定义驱动driver,基本url,title
    WebDriverWait提供了显式等待方式。
    '''
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    class BasePage(object):
        """
        BasePage封装所有页面都公用的方法,例如driver, url ,FindElement等
        """
        #初始化driver、url、pagetitle等
        #实例化BasePage类时,最先执行的就是__init__方法,该方法的入参,其实就是BasePage类的入参。
        #__init__方法不能有返回值,只能返回None
        #self只实例本身,相较于类Page而言。
        def __init__(self, selenium_driver, base_url, pagetitle):
            self.driver = selenium_driver
            self.base_url = base_url
            self.pagetitle = pagetitle
    
        #通过title断言进入的页面是否正确。
        #使用title获取当前窗口title,检查输入的title是否在当前title中,返回比较结果(True 或 False)
        def on_page(self, pagetitle):
            return pagetitle in self.driver.title
    
        #打开页面,并校验页面链接是否加载正确
        #以单下划线_开头的方法,在使用import *时,该方法不会被导入,保证该方法为类私有的。
        def _open(self, url, pagetitle):
            #使用get打开访问链接地址
            self.driver.get(url)
            self.driver.maximize_window()
            #使用assert进行校验,打开的窗口title是否与配置的title一致。调用on_page()方法
            assert self.on_page(pagetitle), u"打开开页面失败 %s"%url
    
        #定义open方法,调用_open()进行打开链接
        def open(self):
            self._open(self.base_url, self.pagetitle)
    
        #重写元素定位方法
        def find_element(self,*loc):
    #        return self.driver.find_element(*loc)
            try:
                #确保元素是可见的。
                #注意:以下入参为元组的元素,需要加*。Python存在这种特性,就是将入参放在元组里。
    #            WebDriverWait(self.driver,10).until(lambda driver: driver.find_element(*loc).is_displayed())
                #注意:以下入参本身是元组,不需要加*
                WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc))
                return self.driver.find_element(*loc)
            except:
                print (u"%s 页面中未能找到 %s 元素"%(self, loc))
    
        #重写switch_frame方法
        def switch_frame(self, loc):
            return self.driver.switch_to_frame(loc)
    
        #定义script方法,用于执行js脚本,范围执行结果
        def script(self, src):
            self.driver.execute_script(src)
    
        #重写定义send_keys方法
        def send_keys(self, loc, vaule, clear_first=True, click_first=True):
            try:
                loc = getattr(self,"_%s"% loc)  #getattr相当于实现self.loc
                if click_first:
                    self.find_element(*loc).click()
                if clear_first:
                    self.find_element(*loc).clear()
                    self.find_element(*loc).send_keys(vaule)
            except AttributeError:
                print (u"%s 页面中未能找到 %s 元素"%(self, loc))
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time : 2019/1/9 11:27
    # @File : test_8_3_2_LoginPage.py
    '''
    Project:页面基本操作方法:如open,input_username,input_password,click_submit
    '''
    from selenium.webdriver.common.by import By
    from unittest_test8_5.test_8_3_2_BasePage import BasePage
    
    
    # 继承BasePage类
    class LoginPage(BasePage):
        # 定位器,通过元素属性定位元素对象
        username_loc = (By.NAME, 'email')
        password_loc = (By.NAME, 'password')
        submit_loc = (By.ID, 'dologin')
        span_loc = (By.CSS_SELECTOR, "div.error-tt>p")
        dynpw_loc = (By.ID, "lbDynPw")
        userid_loc = (By.ID, "spnUid")
    
        # 操作
        # 通过继承覆盖(Overriding)方法:如果子类和父类的方法名相同,优先用子类自己的方法。
        # 打开网页
        def open(self):
            # 调用page中的_open打开连接
            self._open(self.base_url, self.pagetitle)
    
        # 输入用户名:调用send_keys对象,输入用户名
        def input_username(self, username):
            #        self.find_element(*self.username_loc).clear()
            self.find_element(*self.username_loc).send_keys(username)
    
        # 输入密码:调用send_keys对象,输入密码
        def input_password(self, password):
            #        self.find_element(*self.password_loc).clear()
            self.find_element(*self.password_loc).send_keys(password)
    
        # 点击登录:调用send_keys对象,点击登录
        def click_submit(self):
            self.find_element(*self.submit_loc).click()
    
        # 用户名或密码不合理是Tip框内容展示
        def show_span(self):
            return self.find_element(*self.span_loc).text
    
        # 切换登录模式为动态密码登录(IE下有效)
        def swich_DynPw(self):
            self.find_element(*self.dynpw_loc).click()
    
        # 登录成功页面中的用户ID查找
        def show_userid(self):
            return self.find_element(*self.userid_loc).text
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time : 2019/1/9 11:28
    # @File : Caselogin126mail.py
    import unittest
    from unittest_test8_5.test_8_3_2_LoginPage import LoginPage
    from selenium import webdriver
    import time
    
    
    class Caselogin126mail(unittest.TestCase):
        """
              登录126邮箱的case
        """
    
        def setUp(self):
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(30)
            self.url = "http://mail.126.com"
            self.username = "hexiuxiu1@126.com"
            self.password = "xiuxiu"
    
        # 用例执行体
        def test_login_mail(self):
            # 声明LoginPage类对象
            login_page = LoginPage(self.driver, self.url, u"网易")
            # 调用打开页面组件
            login_page.open()
            # 切换到登录框Frame
            time.sleep(2)
    
            login_page.switch_frame(0)   # login_page.switch_frame('x-URS-iframe')会报错,原因还没找到
            # 调用用户名输入组件
            login_page.input_username(self.username)
            # 调用密码输入组件
            login_page.input_password(self.password)
            # 调用点击登录按钮组件
            login_page.click_submit()
            time.sleep(2)
    
        def tearDown(self):
            self.driver.quit()
    
    
    if __name__ == "__main__":
        unittest.main()
  • 相关阅读:
    `/usr/java/jdk1.8.0_101': not a valid identifier
    gem sources -a https://ruby.taobao.org/ 提示:Error fetching https://ruby.taobao.org/ SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: ce rtificate verify failed
    ERROR: While executing gem ... (Encoding::UndefinedConversionError) U+7CFB to IBM437 in conversion from UTF-16LE to UTF-8 to IBM437,当你执行gem 命令时,提示如上信息解决方案如下。
    ubuntu15.10安装 jdk
    ubuntu 如何安装tomcat
    Mac pro 上安装 robotframework 时的一个版本问题
    AppBuilder(四)SignatureConversions
    AppBuilder(三)BuildInternal
    AppBuilder(二)UseStageMarker
    AppBuilder(一)Use汇总
  • 原文地址:https://www.cnblogs.com/xiuxiu123456/p/10444153.html
Copyright © 2011-2022 走看看