首页 > 代码库 > Python+Selenium 自动化实现实例-模块化调用

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()

 

Python+Selenium 自动化实现实例-模块化调用