zoukankan      html  css  js  c++  java
  • Python+Selenium 自动化实现实例-模块化调用

    public 目录存一些公共模块,供用例调用。login.py 内容如下:

    # coding=utf-8
    import time
    
    
    # login
    
    def login(driver):
        driver.find_element_by_class_name("ui-dialog-close").click()  # 关闭弹窗
        driver.find_element_by_xpath("//*[@id='topbar_nav']/li[1]/a[1]").click()  # 点击登录按钮
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("18055352262")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("hj123456")
        driver.find_element_by_xpath("//input[@class='btn']").click()  # 点击确认登录按钮
    
    
    # logout
    
    
    def logout(driver):
        time.sleep(2)
        driver.find_element_by_link_text(u"退出").click()

    接下来login_lizi_public 文件引用login.py 中所定义的函数,代码如下:

    #coding=utf-8
    from  selenium import webdriver
    from public import login
    import unittest
    
    class LoginTest(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Chrome()
            self.base_url = "http://www-test.lizi.com"
            self.driver.implicitly_wait(5)
    
        def test_lizi(self):
            driver = self.driver
            driver.get(self.base_url)
            #调用登录函数
            login.login(driver)
            text = driver.find_element_by_css_selector('.name').text
            print text
            self.assertEqual(text,u"被风吹过的夏天",msg="error")
            #调用退出函数
            login.logout(driver)
    
        def tearDown(self):
            self.driver.quit()
    
    if __name__ == "__main__":
        unittest.main()
  • 相关阅读:
    docker 加速器配置目录
    php 超时设置笔记
    php socket通过smtp发送邮件(纯文本、HTML,多收件人,多抄送,多密送)
    fabric 安装
    centos7下使用yum安装pip
    【转】linux tar 压缩
    ASP.NET MVC 5 默认模板的JS和CSS 是怎么加载的?
    NHibernate with ASP.NET MVC 入门示例
    Ajax入门
    NHibernate入门
  • 原文地址:https://www.cnblogs.com/forcepush/p/6688103.html
Copyright © 2011-2022 走看看