zoukankan      html  css  js  c++  java
  • 0721复习

    要把这些内容导入自己的电脑上,下节课使用

    一步步导入调用,填坑,最终在ClientSelenium.py中实例化,拉起webdriver和浏览器和打开目标url

    通过本次复习收获,课堂的视频一定再看一遍并按照步骤重新搭建框架,才能把知识点真正搞明白

    config.ini

    [Driver]
    chrome = E:PageObjectdriverchromedriver.exe
    firefox = E:PageObjectdrivergeckodriver.exe
    
    [Url]
    ali_url = https://618.tmall.com
    baidu_url = https://www.baidu.com/

    setting.py

    import os
    #E:PageObject
    project_path = os.path.dirname(os.path.abspath("."))
    driver_path = project_path + "\driver"
    ini_path = project_path +"\Conf\config.ini"

    ReadIni.py

    from configparser import ConfigParser   # 可以把类名重命名 as cf
    from Conf.setting import *
    from time import sleep
    
    #util/ReadIni.py
    
    #读取文件 project/Conf/config.ini
    #封装函数 类名
    
    #ConfigParser() 实例化一个读取ini的赋予对象
    #新建一个类读取ini的类 类里面的成员方法cf  cf方法绑定ConfigParser()
    #cf读取testdata下面的绝对路径,encoding="utf-8" :编译格式
    
    #定义了两个函数方法,完全解耦的
    #第一个函数作用是拿到某个集合名称下面的所有内容
    #这个通过传统json格式返回对象items,包裹dict()
    #第二个函数作用是一层层拿,根据某个集合名称下面的options的key拿到最终的value
    #根据传入的参数
    
    class ReadIni(object):
        def __init__(self,ini_path):  #project_path + "\..."
            self.cf = ConfigParser()   #configparser库的类名 ,少了()没有参数
            self.cf.read(ini_path, encoding="utf-8")   #ini_path读出来以后=cf.read  返回是cf
    
        def get_item_section(self,sectionName):    # sectionName 读取什么里面的内容?
            "获取配置文件指定的section下面的所有内容"
            optionDict = dict(self.cf.items(sectionName))
            return optionDict  #字典
    
        def get_option_value(self, sectionName, optionName):
            "返回对应option键值对的value ??"
            value = self.cf.get(sectionName,optionName)
            return value
    
    #实例化
    #第一件事情拿到Url下面所有的集合
    #第二件事情拿到阿里url的value
    if __name__ == '__main__':    #隔离一下
        ri = ReadIni(ini_path)    #这里给参数,是因为有构造函数吗?
        print(ri.get_item_section("Url"))
        print(ri.get_option_value("Url","baidu_url"))
        print(ri.get_item_section("Driver"))
        print(ri.get_option_value("Driver", "chrome"))

    Base.py

    # 只有用到的部分
    from Conf.setting import *  #其他都被这个文件导入了, from后面跟着是文件夹绝对路径,可以想想比如os
    from util.ReadIni import ReadIni #也可以放入Confsetting.py
    pc =ReadIni(ini_path)
    class Base(object):
        def chrome_path(self):
            "谷歌的浏览器驱动"
            #读取驱动
            return pc.get_option_value("Driver", "chrome") #把driver目录拷贝到一级目录下。 这里之前写错了,传错了一个函数
    
        def firefox_path(self):
            "火狐的浏览器驱动"
            return pc.get_option_value("Driver", "firefox")
    
        def baidu_url(self):
            "百度的网址"
            return pc.get_option_value("Url", "baidu_url")
    
        def ali_url(self):
            "天猫的网址"
            return pc.get_option_value("Url", "ali_url")
    
    # b = Base()
    # print(b.chrome_path())
    # print(b.baidu_url())
    ClientSelenium.py
    from selenium import webdriver
    from util.Base import Base
    base = Base()   #调用类,没有参数()
    
    #只开放了基础api的,定位器请自己学习封装完成作业,下节课在讲带保护的高级封装
    class ClientSelenium(object):
        """通过封装把Page的行为压缩在这里"""
        def get_driver(self,driver, url):
            "拉起webdriver和浏览器和打开目标url   可以用于setUpClass的初始化"
            if driver == 'firefox' or driver == 'Firefox' or driver == 'F' or driver == 'f':
                exe_path = base.firefox_path()  #第二个功能是包裹在第一个功能下面的   base代码稍等
                self.driver = webdriver.Firefox(executable_path=exe_path)
            elif driver == 'Chrome' or driver == 'chrome' or driver == 'Ch' or driver == 'ch':
                exe_path = base.chrome_path()
                self.driver = webdriver.Chrome(executable_path=exe_path)
            else:
                print("输入在预期以外")
    
            url = base.baidu_url()
            self.driver.get(url) #区域3
            return self.driver
    
    
    
        def quit(self):
            self.driver.quit()
    
    
    if __name__ == '__main__':
        a = ClientSelenium()
        a.get_driver("chrome", "baidu_url")   #调用config中的url,需要相同作用的函数导出路径
        a.quit()

     test_baiduV_logina.py

    用unittest.TestCase方式实现,这里不应该这样用,否则前面的配置就无意义了。

    应该要使用suite 来写case吗?

    import unittest  #好像不该用unittest,ClientSelenium中的设置没怎么起作用
    from selenium import webdriver  #这里应该用不到,ClientSelenium.py中配置过了
    from util.ClientSelenium import ClientSelenium
    import time
    
    class BaiduV_Login(unittest.TestCase):
    
        @classmethod
        def setUpClass(cls):
            open_baidu = ClientSelenium()
            cls.driver = open_baidu.get_driver("chrome", "baidu_url")
            return cls.driver
    
        # def test_a_open_baiduV(self):
        #     self.driver.find_element_by_name("tj_trvideo").click()
    
        def test_b_open_login(self):  #加判断,断言等,
            "找到打开登陆界面的方法,很多疑惑应该就可以解决了"
            # self.driver.find_element_by_partial_link_text()
            # self.driver.find_element_by_link_text()
            # self.driver.find_element_by_xpath('//*[@id="loginbtn"]').click()
            self.driver.find_element_by_xpath('//*[@id="u1"]/a[7]').click()  #百度主页面打开登陆窗口,网不好替代
            time.sleep(2)   #需要等待,否则一闪而过。。。
    
        def test_c_normal(self):   #加判断,断言等,
            self.driver.find_element_by_id("TANGRAM__PSP_10__footerULoginBtn").click()
            time.sleep(2)
    
        def test_d_userName(self):    #加判断,断言等,
            self.driver.find_element_by_id("TANGRAM__PSP_10__userName").send_keys("testerhome")
            time.sleep(2)
    
        def test_e_password(self):     #加判断,断言等,
            self.driver.find_element_by_id("TANGRAM__PSP_10__password").send_keys("testerhome")
            time.sleep(5)
    
    
    
    
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
    
    
    if __name__ == '__main__':
        unittest.main()
    
    #测试过程中遇到网络很差,一直不能刷新跳转,该用哪些方法处理?
    #有时直接不能调起浏览器,是什么情况?
    学习的是为了更快、更好的解决问题,不要让学习本身成为问题!
  • 相关阅读:
    mysql常用基本命令
    mysql8.0.13下载与安装图文教程
    k8s ingress 增加跨域配置
    Jenkins 备份恢复插件 thinBackup 使用
    k8s HA master 节点宕机修复
    nginx 跨域问题解决
    mongodb 3.4.24 主从复制
    k8s 线上安装 jenkins并结合 jenkinsfile 实现 helm 自动化部署
    k8s helm 运用与自建helm仓库chartmuseum
    centos6 源码安装 unzip
  • 原文地址:https://www.cnblogs.com/testerhome-yizhou2018/p/9368633.html
Copyright © 2011-2022 走看看