首页 > 其他分享 >第14课、POM - Page Object

第14课、POM - Page Object

时间:2023-02-08 01:22:52浏览次数:38  
标签:14 text self Object driver locator POM login def

 

 

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC

class Base():

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

    def find(self,locator):
        ele = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(*locator))
        return ele

    def send(self,locator,text):
        ele =self.find(locator)
        ele.send_keys(text)

    def click(self,locator):
        self.find(locator).click()


    def clear(self,locator):
        self.find(locator).clear()

    def get_text(self, locator):
        '''获取元素文本值'''
        try:
            ele =self.find(locator).text
        except:
            ele =""
        return ele


    def is_element_exist(self,locator):
        '''判断元素是否存在,结果返回布尔值 ture 或 false'''
        try:
            self.find(locator)
            print("找到元素了!!!!!")
            return True
        except:
            print("找不到元素")
            return False

    def text_in_element(self,locator,_text):
        '''判断元素text属性'''
        try:
            r = WebDriverWait(self.driver,30,1).until(EC.text_to_be_present_in_element(locator,_text))
            return r
        except:
            return False

    def value_in_element(self,locator,text):
        '''判断元素value属性'''
        try:
            r = WebDriverWait(self.driver,30,1).until(EC.text_to_be_present_in_element(locator,_text))
            return r
        except:
            return False

    def move_to_element(self,locator):
        '''鼠标悬停'''
        element = self.find(locator)
        ActionChains(self.driver).move_to_element(element).perform()

    def select_by_index(self,locator,index=0):
        '''select下拉框,index索引'''
        element =self.find(locator)
        Select(element).select_by_index(index)

    def select_by_value(self,locator,value):
        '''select下拉框,value'''
        element =self.find(locator)
        Select(element).select_by_index(value)

    def select_by_text(self,locator,_text):
        '''select下拉框,文本'''
        element =self.find(locator)
        Select(element).select_by_visible_text(_text)

    def is_alert(self,timeout=3):
        '''判断是否有alert,没有返回false,有返回alert对象'''
        try:
            alert = WebDriverWait(self.driver,timeout,1).until(EC.alert_is_present())
            return alert
        except:
            return False

    def js_focus_element(self,locator):
        '''聚焦元素'''
        target =self.find(locator)
        self.driver.execute_scrit("arguments[0].scrollIntoView();",target)

    def js_focus_top(self,locator):
        '''滑动到顶部'''
        js ="window.scrollTo(0,0)"
        self.driver.execute_scrit(js)


    def js_focus_end(self,locator):
        '''滑动到底部'''
        js ="window.scrollTo(0,document.body.scrollHeight)"
        self.driver.execute_scrit(js)



if __name__ == '__main__':
    '''自测函数'''
    from selenium import webdriver
    driver = webdriver.Firefox()
    driver.get("http://192.168.43.57/phpwind/admin.php#initiator")
    loc1 = ("name","admin_name")
    loc2 = ("name", "admin_pwd")
    loc3 = ("name", "submit")
    login_xpath = ("xpath",'html/body/table/tbody[1]/tr[1]/td/div/div/p/a[2]')

    b = Base(driver)  #实例
    b.send(loc1,"admin")
    b.send(loc2, "123456")
    b.click(loc3)
    t = b.get_text(login_xpath)
    print(t)
    assert t == "[注销]"
     

 ------------------------------------------------

page object model:顾名思义页面对象模式

          - 把每个页面(page)当成一个对象(object)

          - 封装页面上会用到的操作方法,比如:输入账号,输入密码,点登陆

           (一些行为事件,后面用例会用到的)

          - 写用例的时候,操作哪个页面,就去调用哪个页面的方法

登录页面操作封装:login_page.py

from t7.base import Base

login_url1 = ("http://192.168.43.57/phpwind/admin.php#initiator")
login_url2 = ("http://192.168.153.1/phpwind/admin.php#initiator")

class Login(Base):

    loc1 = ("name","admin_name")
    loc2 = ("name", "admin_pwd")
    loc3 = ("name", "submit")
    login_xpath = ("xpath",'html/body/table/tbody[1]/tr[1]/td/div/div/p/a[2]')

    def login(self, user="admin", psw="123456"):
        '''登录页面'''
        self.send(self.loc1,user)
        self.send(self.loc2,psw)
        self.click(self.loc3)

    def is_login_sucess(self,text):
        '''判断是否登录成功'''
        body =("xpath","//body")
        body_text =self.find(body).text
        return text in body_text


if __name__ == '__main__':
    from selenium import webdriver
    driver = webdriver.Firefox()
    driver.get(login_url1)

    l =Login(driver)
    l.login()
    text =("后台首页")
    result =l.is_login_sucess(text)
    print("结果:%s"%result)

------------------

测试用例模块:

import unittest
from selenium import webdriver

from t7.pages.login_page import Login,login_url1
import ddt

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get(login_url1)
        self.a =Login(self.driver)

    def tearDown(self):
        self.driver.quit()

    def test_login_01(self):
        '''测试数据正常-登录成功'''
        self.a.login("admin","123456")
        result =self.a.is_login_sucess("后台首页")
        print("登录的实际结果:%s"%result)
        self.assertTrue(result)


if __name__ == '__main__':
    unittest.main()

 

标签:14,text,self,Object,driver,locator,POM,login,def
From: https://www.cnblogs.com/pingzi66-ww/p/17100293.html

相关文章

  • 002_springboot项目中 pom.xml 配置的作用
    parent:用以定义一系列的常用坐标版本;定义一系列的常用坐标组合;比如在pom.xml文件中引入一个javax.servlet,<version>那里是可以不写的,也就是不写版本,而决定采用哪个版本......
  • CF14D题解
    CF14DTwoPaths题解题目链接传送门题意简述给定一棵树,找出两条不经过相同点的最长路径,使得他们的长度乘积最大。题目分析首先,如果在一棵树上,两条路径没有共同的点,那......
  • C++ Day14 借助智能指针实现文本查询查询
    一、设计思路数据结构:1、读取文件时,要记住文件的每一行,并且要将每一行分解为独立的单词vector<string>vec;istringstream2、输出时提供每个单词与其关联的行号,且......
  • Educational Codeforces Round 142 E. Divisors and Table
    链接:https://codeforces.com/problemset/problem/1792/E题意:给定n*n乘法表,第a[i][j]=i*j,给定m=m1*m2,求m的所有因子中出现在表中的最小行的xor和。n<=1e9,m......
  • AQS 中的 ConditionObject 内部类
    AQS中的ConditionObject内部类ConditionObject内部类主要用作条件等待队列,条件等待队列为单项链表加入条件队列的过程:等待队列的节点调用await()从同步队列移......
  • 训练日记 2018.12.14
        哎,这几天被树形背包搞懵了,一开始感觉没学到啥,做一个题看一个题解,每个题单个来看都能看懂,但是遇到一个新题就不会了,而且你用上一个题的做法做,依旧不对,网上的题解......
  • Codeforces Round #514 (Div. 2).A. Cashier 做了个优化 还行
    A.Cashiertimelimitpertest2secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputVasyahasrecentlygotajobasacashieratal......
  • 洛谷 P2014 选课 树形依赖背包
    题目描述在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习。现在有N门功......
  • 14.shell概念
                 直接写·文件名容易被认为是命令,需要写成./文件名     直接敲bash会生成子shell,此时输入其他命令是在子shell中进行......
  • PostgreSQL-14 安装配置-wsl_v1_ubuntu22.04
    环境准备pgAdmin:Cisco2022postgrep数据库:leiwang6:Leiwang6_2023postgres:Postgres_2023installhttps://learn.microsoft.com/en-us/windows/wsl/tutorials/w......