App下載

Python Selenium框架搭建與使用

我正好喜歡 2023-06-25 09:59:35 瀏覽數(shù) (1428)
反饋

Selenium是一個用于測試Web應(yīng)用程序的框架,它可以直接在瀏覽器中運行,模擬真實用戶的操作。Selenium支持多種瀏覽器和語言,其中Python是最常用的一種。本文將介紹如何使用Python和Selenium搭建一個自動化測試框架,并實現(xiàn)一些基本的功能。

安裝Selenium

要使用Selenium,首先需要安裝Selenium庫和瀏覽器驅(qū)動。Selenium庫可以通過pip命令安裝:

pip install selenium

瀏覽器驅(qū)動需要根據(jù)不同的瀏覽器下載對應(yīng)的版本,例如Firefox瀏覽器需要下載geckodriver,Chrome瀏覽器需要下載chromedriver。下載后,需要將驅(qū)動文件放在系統(tǒng)的PATH目錄下,或者在代碼中指定驅(qū)動文件的路徑。

使用Selenium

要使用Selenium,首先需要導(dǎo)入selenium模塊,并創(chuàng)建一個webdriver對象,這個對象可以控制瀏覽器的行為。例如,下面的代碼創(chuàng)建了一個Firefox瀏覽器對象,并訪問了實驗樓網(wǎng)站:

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.shiyanlou.com")

Selenium提供了多種方法來定位網(wǎng)頁上的元素,例如通過id、name、class、tag、xpath等。定位到元素后,可以對元素進行操作,例如點擊、輸入、獲取屬性等。例如,下面的代碼定位到百度首頁的搜索框,并輸入“selenium”:

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.baidu.com")


search_box = driver.find_element_by_id("kw")
search_box.send_keys("selenium")

Selenium還提供了一些常用的瀏覽器操作,例如設(shè)置窗口大小、刷新頁面、后退、前進、關(guān)閉等。例如,下面的代碼設(shè)置了窗口大小為800x400,并刷新了頁面:

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("https://www.baidu.com")


driver.set_window_size(800, 400)
driver.refresh()

搭建測試框架

為了提高測試用例的可維護性和可讀性,我們可以使用Page Object模式來搭建測試框架。Page Object模式是將頁面的元素定位和業(yè)務(wù)操作分開,每個頁面對應(yīng)一個對象類,測試用例只調(diào)用對象類的方法,而不直接操作元素。這樣可以避免頁面變化導(dǎo)致測試用例頻繁修改的問題。

例如,我們可以為51Testing軟件測試論壇的首頁和登錄頁分別創(chuàng)建一個對象類,如下所示:

# home_page.py


from selenium import webdriver
from selenium.webdriver.common.by import By


class HomePage:


def __init__(self, driver):
self.driver = driver
self.url = "http://bbs.51testing.com/forum.php"
self.username_input = (By.ID, "ls_username")
self.password_input = (By.ID, "ls_password")
self.login_button = (By.XPATH, '//*[@id="lsform"]/div/div[1]/table/tbody/tr[2]/td[3]/button')


def open(self):
self.driver.get(self.url)


def input_username(self, username):
self.driver.find_element(*self.username_input).send_keys(username)


def input_password(self, password):
self.driver.find_element(*self.password_input).send_keys(password)


def click_login_button(self):
self.driver.find_element(*self.login_button).click()


def login(self, username, password):
self.input_username(username)
self.input_password(password)
self.click_login_button()

# login_page.py


from selenium import webdriver
from selenium.webdriver.common.by import By


class LoginPage:


def __init__(self, driver):
self.driver = driver
self.welcome_text = (By.CSS_SELECTOR, ".vwmy a")


def get_welcome_text(self):
return self.driver.find_element(*self.welcome_text).text

然后,我們可以編寫一個測試用例,使用unittest模塊來組織和執(zhí)行測試。例如,下面的代碼測試了登錄功能是否正常:

# test_login.py


import unittest
from selenium import webdriver
from home_page import HomePage
from login_page import LoginPage


class TestLogin(unittest.TestCase):


def setUp(self):
self.driver = webdriver.Firefox()
self.home_page = HomePage(self.driver)
self.login_page = LoginPage(self.driver)


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


def test_login_success(self):
self.home_page.open()
self.home_page.login("your_username", "your_password")
welcome_text = self.login_page.get_welcome_text()
self.assertEqual(welcome_text, "your_username")

運行上面的代碼,可以看到瀏覽器自動打開51Testing網(wǎng)站,輸入用戶名和密碼,點擊登錄按鈕,然后斷言歡迎文本是否與用戶名一致。

總結(jié)

本文介紹了如何使用Python和Selenium搭建一個自動化測試框架,并實現(xiàn)一些基本的功能。Selenium是一個強大而靈活的工具,可以用來測試各種Web應(yīng)用程序。通過使用Page Object模式,可以提高測試用例的可維護性和可讀性。當(dāng)然,Selenium還有很多其他的特性和用法,有興趣的讀者可以進一步學(xué)習(xí)和探索。

python相關(guān)課程推薦:python相關(guān)課程

0 人點贊