• selenium po模式实战一


    一、抽离出 basePage 的版本

    mySettings.py (utils)
    # 项目网址
    url = "http://120.55.190.222:38090/#/login"
    # 账号密码
    username = "测试"
    password = "123456"
    # 智能等待超时时间
    time_out = 10
    # 智能等待轮询时间
    poll_frequency = 0.5
     
    myDriver.py (utils)
    from foo.songQing.seleniumV2.day5.utils.mySettings import url
    from selenium import webdriver
    class Driver:
    # 初始化为 None
    _driver = None
    @classmethod
    def get_driver(cls, browser_name="chrome"):
    """
    获取浏览器驱动对象
    :param browser_name: 浏览器名称
    :return:
    """
    # 如果不为空就不需要新建了
    if cls._driver is None:
       if browser_name == 'chrome':
          cls._driver = webdriver.Chrome()
    elif browser_name == 'firefox':
         cls._driver = webdriver.Firefox()
    elif browser_name == 'safari':
         cls._driver = webdriver.Safari()
    elif browser_name == 'opera':
         cls._driver = webdriver.Opera()
    elif browser_name == 'edge':
         cls._driver = webdriver.Edge()
    elif browser_name == 'ie':
         cls._driver = webdriver.Ie()
    cls._driver.implicitly_wait(5)
    cls._driver.maximize_window()
    cls._driver.get(url)
    cls.__login()
    return cls._driver
    @classmethod
    def __login(cls):
    """
    私有方法, 只能在类里边使用
    类外部无法使用, 子类不能继承
    解决登录问题
    :return:
    """
    cls._driver.find_element_by_id("username").send_keys("测试")
    cls._driver.find_element_by_id("password").send_keys("123456")
    cls._driver.find_element_by_id("btnLogin").click()
     
    if __name__ == '__main__':
    # 无论调用多少次, 都只有一个浏览器
    Driver.get_driver()
    Driver.get_driver()
    Driver.get_driver()
    Driver.get_driver()
     
    basePage.py (pages)
    from foo.songQing.seleniumV2.day5.utils.mySettings import time_out, poll_frequency
    from foo.songQing.seleniumV2.day5.utils.myDriver import Driver
    from selenium.webdriver.support import expected_conditions as ec
    from selenium.webdriver.support.wait import WebDriverWait
    import time
    class BasePage:
          
          def __init__(self):
            # 获取浏览器对象
              self.driver = Driver.get_driver()
          
          def get_element(self, locator):
              
    """
    根据表达式匹配单个元素
    :param locator: 元素定位表达式, 以元组形式传入, 示例: (By.ID, "abc")
    :return:
    """
       
        # 判断元素是否存在
         WebDriverWait(
        # 传入浏览器对象
        driver=self.driver,
        # 传入超时时间
        timeout=time_out,
        # 设置轮询时间
        poll_frequency=poll_frequency).until(
        # 检测定位的元素是否可见
        ec.visibility_of_element_located(locator))
    # 返回元素对象, 元组传参
    return self.driver.find_element(*locator)
     
    def to_page(self, url):
        time.sleep(1)
        self.driver.get(url)
     
    if __name__ == '__main__':
    x = BasePage()
     
    addProductPage.py  (pages)
     
    from foo.songQing.seleniumV2.day5.pages.basePage import BasePage
    import time
    class AddProductPage(BasePage):
          
          def __init__(self):
          super().__init__()
          self.url = "http://120.55.190.222:38090/#/pms/addProduct"
          
          def product_classification_select_box(self):
              """商品分类下拉框, 外框"""
               return self.driver.find_element_by_css_selector("form > div:nth-child(1) .el-cascader__label")
           
          def product_classification_select_box_idx1(self, idx1):
               """商品分类下拉框, 一级分类"""
              return self.driver.find_element_by_css_selector("ul.el-cascader-menu > li:nth-child(%s)" % idx1)
          
        def product_classification_select_box_idx2(self, idx2):
    """商品分类下拉框, 二级分类"""
    return self.driver.find_element_by_css_selector("ul + ul.el-cascader-menu > li:nth-child(%s)" %idx2)
          def product_name_input_box(self):
          """商品名称输入框"""
          return self.driver.find_element_by_css_selector("label[for="name"] + div input")
        def product_subtitle_input_box(self):
          """副标题输入框"""
          return self.driver.find_element_by_css_selector("label[for="subTitle"] + div input")
        def product_brand_select_box(self):
          """商品品牌下拉框外框"""
          return self.driver.find_element_by_css_selector("label[for="brandId"] + div input")
     
        def product_brand_select_box_option(self, idx):
          """商品品牌下拉框, 一级分类"""
          return self.driver.find_element_by_css_selector("body > div:nth-child(8) ul > li:nth-child(%s)" %idx)
        def next_step_commodity_promotion_button_box(self):
          """下一步, 填写商品促销按钮"""
          return self.driver.find_element_by_xpath("//*[text()="下一步,填写商品促销"]")
     
        def is_herald_box(self):
            """预告商品开关"""
           return self.driver.find_element_by_xpath("//*[text()="预告商品:"]/..//span")
        def next_step_product_attribute_button_box(self):
            """下一步, 填写商品属性按钮"""
           return self.driver.find_element_by_xpath("//*[text()="下一步,填写商品属性"]")
        def next_step_choose_product_related_button_box(self):
            """下一步, 选择商品关联按钮"""
          return self.driver.find_element_by_xpath("//*[text()="下一步,选择商品关联"]")
        def submit_product_button_box(self):
            """完成, 提交商品按钮"""
           return self.driver.find_element_by_xpath("//*[text()="完成,提交商品"]")
        def confirm_submission_box(self):
           return self.driver.find_element_by_css_selector(
                     "[class="el-button el-button--default el-button--small el-button--primary "]")
    class AddProductPageAction(AddProductPage):
     
    def add_product_action(self, idx1, idx2, product_name, subtitle, brand_select_idx):
       self.to_page(self.url)
       # 点击商品分类下拉外框
       self.product_classification_select_box().click()
       # 选择一级分类
       self.product_classification_select_box_idx1(idx1).click()
       # 选择二级分类
       self.product_classification_select_box_idx2(idx2).click()
         # 输入商品名称
       self.product_name_input_box().send_keys(product_name)
       # 输入副标题
       self.product_subtitle_input_box().send_keys(subtitle)
       # 点击商品品牌下拉框外框
       self.product_brand_select_box().click()
       # 选择商品品牌一级分类
       self.product_brand_select_box_option(brand_select_idx).click()
       # 点击[下一步,填写商品促销]按钮
       self.next_step_commodity_promotion_button_box().click()
       # 点击是否预告商品开关
       self.is_herald_box().click()
       # 点击[下一步, 填写商品属性]按钮
       self.next_step_product_attribute_button_box().click()
       # 点击下一步, 选择商品关联按钮
       self.next_step_choose_product_related_button_box().click()
       time.sleep(0.3)
       # 点击完成,提交商品按钮
       self.submit_product_button_box().click()
       # 确认提交
       self.confirm_submission_box().click()
    AddProductPageActionObj = AddProductPageAction()
    if __name__ == '__main__':
    AddProductPageActionObj.add_product_action(1, 1, "针不戳羊毛衫", "羊毛姗姗", 1)
    case.py
    import time
     
    from study.seleniumStu.day5.抽离出 basePage 版本.addProductPage import AddProductPageActionObj as
    APP
    from study.seleniumStu.day5.抽离出 basePage 版本.productLIstPage import ProductLIstPageActionObj as
    PLP
    
    
    def addProductCase():
        product_name = "%s" % time.time()
        # 添加一个商品
        APP.add_product_action("1", "1", product_name, product_name, 1)
        # 进入商品列表页面
        PLP.to_page(3, PLP.url)
        # 获取商品列表,第一个商品的商品名称
        first_product_name = PLP.first_tr_product_name_box().text
        if product_name == first_product_name:
          print("测试通过")
        else:
          print("测试失败")
    
    
    addProductCase()


      
          
    世界上最美的风景,是自己努力的模样
  • 相关阅读:
    求欧拉回路的算法学习
    2020牛客暑期多校训练营(第六场 )C Combination of Physics and Maths(思维)
    2020牛客暑期多校训练营(第六场)E.Easy Construction(思维构造,附图解)
    CF1038D Slime(思维+枚举+贪心)(来自洛谷)
    CF1250B The Feast and the Bus(贪心+枚举)(来自洛谷)
    Codeforces Round #659 (Div. 2) A.Common Prefixes
    IDEA本人亲测可用的破解方法
    Codeforces Round #658 (Div. 2)(A,B博弈,C1,C2)
    2020牛客暑期多校训练营(第四场)B.Basic Gcd Problem(数学)
    2020牛客暑期多校训练营(第三场)B.Classical String Problem(思维)
  • 原文地址:https://www.cnblogs.com/xiong-hua/p/15068150.html
Copyright © 2020-2023  润新知