前言
Selenium 是一個(gè)瀏覽器自動(dòng)化框架,專門為 W3C WebDriver 規(guī)范提供了一個(gè)與所有主要的 web 瀏覽器兼容的平臺(tái)和語言中立的編碼接口。github 地址:https://github.com/SeleniumHQ/selenium
Selenium IDE 是作為 Selenium 在瀏覽器 Firefox 和 Chrome 的插件,用于記錄、重放測試腳本,并且腳本也可以導(dǎo)出到 C#,Java,Ruby 或 Python 等編程語言。github 地址:https://github.com/SeleniumHQ/selenium-ide
一、安裝及界面
1.1 安裝
Chrome 安裝: https://chrome.google.com/webstore/detail/selenium-ide/mooikfkahbdckldjjndioackbalphokd
Firefox 安裝:https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/
1.2 界面
在安裝完插件后,以Firefox為例,下面是演示界面
新建新的項(xiàng)目demo002后,就是下面界面,最重要的就是下面三個(gè)紅框內(nèi)容:命令、目標(biāo)、值
二、常用命令
所有命令可以通過點(diǎn)擊上面Command 下拉框來選擇想要的命令。這些命令包括很多assert命令,這些是用來做測試用的,還有一些輸入文本,鼠標(biāo)點(diǎn)擊,腳本代碼執(zhí)行控制流命令等等。
所有命令官方文檔:https://www.selenium.dev/selenium-ide/docs/en/api/commands,下面會(huì)介紹一些常用的命令
2.1 通用
- click 點(diǎn)擊一個(gè)元素
- execute script 執(zhí)行js腳本,返回?cái)?shù)據(jù)需要使用 return
- double click 雙擊一個(gè)元素
- mouse down/mouse up/mouse over 鼠標(biāo)按下、抬起、懸停
- open 打開一個(gè)URL
- send keys 鍵盤輸入
2.2 表單
- check/uncheck 選擇 checkbox/radio
- type 輸入文本
- select 從下拉菜單中選擇一個(gè)元素
- submit 提交表單
2.3 流程控制
- do/repeat if 創(chuàng)建do循環(huán)
- end 結(jié)束 if, while, times 流程控制
- for each 遍歷
- if/else if/else 條件判斷
- times 循環(huán)n次
- wait for element editable 等待元素可編輯
- wait for element not present 等待元素不出現(xiàn)在頁面上
- wait for element visible 等待元素可視
- while 創(chuàng)建循環(huán)
三、常用操作
3.1 命令操作
下面是一個(gè)例子,使用Selenium IDE通過百度搜索Selenium IDE。通過這個(gè)小例子來介紹常用的open/type/click的用法,其它命令需要讀文檔練習(xí)
-
在輸入命令之前需要輸入base url,就是想要操作網(wǎng)頁的地址
-
第一個(gè)命令一般就是打開網(wǎng)頁,需要使用open命令
-
命令的添加和刪除
- 在Command 直接選擇想要的命令,這適用于輸入下一條命令
- 如果想要插入一條,可以在舊命令上右鍵菜單操作,注意: 插入新命令會(huì)在舊命令上方
- 刪除會(huì)刪除選中的命令
-
使用 type 命令向百度搜索框輸入文字,選擇type 命令,然后 點(diǎn)擊Target 箭頭,選擇百度界面的文本框,Value輸入想要搜索的文字。到這里就可以點(diǎn)擊上方的運(yùn)行按鈕測試命令執(zhí)行了
-
點(diǎn)擊搜索按鈕 這就需要click 命令,依舊需要使用Target 箭頭點(diǎn)擊百度一下這個(gè)按鈕
- 最終出現(xiàn)了百度Selenium IDE的搜索結(jié)果
3.2 js腳本
Selenium IDE 支持執(zhí)行js腳本,使用 execute script 命令,支持自定義變量,使用變量時(shí)需要使用${}包裹。
3.3 錄制
Selenium IDE 提供錄制功能方便命令執(zhí)行。當(dāng)點(diǎn)擊錄制時(shí),會(huì)先輸入要打開的網(wǎng)站地址,然后會(huì)記錄操作,這對(duì)新手很有幫助。
但是錄制功能很有限,它會(huì)將你做的操作會(huì)記錄成命令,但是有一些操作是需要耗時(shí),需要用到命令中的wait等命令等待,錄制功能有時(shí)候并沒有這么智能,能夠識(shí)別需要等待,所以一般在錄制后需要修改錄制生成的命令。
還有,錄制功能只能重復(fù)一遍操作,如果需要重復(fù)100次,則需要添加腳本控制。
使用右上角的紅色REC開始錄制,再次點(diǎn)擊結(jié)束
3.4 導(dǎo)出
在測試用例上,右鍵支持導(dǎo)出其他語言的代碼,這對(duì)做自動(dòng)化測試很有用
四、實(shí)際操作例子
4.1 紅黑樹插入可視化
- 腳本
- 執(zhí)行結(jié)果
文章來源:http://www.zghlxwxcb.cn/news/detail-434948.html
4.2 github下載
目標(biāo):通過 github 搜索openjdk stars:>1000的項(xiàng)目,第一頁10個(gè)下載。
實(shí)現(xiàn):通過Selenium IDE 錄制了一些操作, 導(dǎo)出為python 腳本,進(jìn)行一定修改,使用pytest執(zhí)行
局限:因?yàn)樵L問github速度太慢,下面的代碼時(shí)靈時(shí)不靈,以后再修改文章來源地址http://www.zghlxwxcb.cn/news/detail-434948.html
# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class TestDemo001():
def setup_method(self, method):
self.driver = webdriver.Firefox()
self.vars = {}
def teardown_method(self, method):
self.driver.quit()
def test_demo001(self):
self.driver.get("https://github.com/")
WebDriverWait(self.driver, 50).until(expected_conditions.element_to_be_clickable((By.NAME, "q")))
self.driver.find_element(By.NAME, "q").click()
self.driver.find_element(By.NAME, "q").send_keys("openjdk stars:>1000")
self.driver.find_element(By.NAME, "q").send_keys(Keys.ENTER)
time.sleep(5)
self.vars["i"] = self.driver.execute_script("return 1")
while self.driver.execute_script("return (arguments[0] <= 10)", self.vars["i"]):
self.driver.find_element(By.CSS_SELECTOR, ".repo-list-item:nth-child(" + str(self.vars["i"]) + ") .v-align-middle").click()
# time.sleep(5)
WebDriverWait(self.driver, 50).until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, ".position-relative > .btn-primary")))
self.driver.find_element(By.CSS_SELECTOR, ".position-relative > .btn-primary").click()
self.driver.find_element(By.CLASS_NAME, "octicon-file-zip").click()
time.sleep(1)
self.vars["i"] = self.driver.execute_script("return arguments[0]+1", self.vars["i"])
self.driver.execute_script("window.history.back()")
time.sleep(10)
# 等待下載完成
time.sleep(10 * 60)
參考
- https://www.selenium.dev/selenium-ide/docs/en/introduction/getting-started
到了這里,關(guān)于瀏覽器自動(dòng)化利器Selenium IDE使用指南的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!