在我之前關於 Selenium 的文章中,我討論了該軟件提供的用於測試 Web 應用程序的工具。本文將引導您安裝這些工具以及您可以使用它們執行的一些基本測試。
- 硒 IDE
- 硒網格
- Selenium 網絡驅動程序
每個 Selenium 工具都按以下順序描述。讓我們從安裝 Selenium IDE 開始。在此之後,我將解釋每個工具的基本用法。
我需要
服務器規格:
- CPU 核心數:2
- 內存:2GB
- 操作系統:全新安裝帶有 GUI 的 Ubuntu 18.04
硒 IDE
我需要: Selenium IDE 是一個 Chrome/Firefox 瀏覽器擴展,因此除了瀏覽器本身需要的那些之外,它沒有特殊的依賴關係。在支持安裝這些瀏覽器的操作系統上可用。
安裝火狐
Step 1. 首先,點擊這裡的鏈接進行安裝 Selenium 瀏覽器插件安裝後,您的瀏覽器應如下所示:
第二步。在那之後,[Firefox に追加]單擊按鈕後,將出現以下屏幕。
步驟 3。[追加]點擊。如果一切順利,您的擴展程序將被安裝並可以使用。
第 4 步。當擴展程序第一次啟動時,您將看到以下屏幕提示。

下面繼續上面的過程 部門.
安裝 Chrome CLI
您可以使用以下命令從命令行在 Ubuntu 18.04 上安裝 Chrome。
第 1 步:獲取軟件包的最新穩定版本。
[email protected]:~$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
--2019-12-28 21:07:45-- https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
Resolving dl.google.com (dl.google.com)... 216.58.211.14, 2a00:1450:400f:80d::200e
Connecting to dl.google.com (dl.google.com)|216.58.211.14|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 62181264 (59M) [application/x-debian-package]
Saving to: 'google-chrome-stable_current_amd64.deb'
google-chrome-stable_current_amd64.deb 100%[=============================================>] 59.30M 2.86MB/s in 21s
2019-12-28 21:08:07 (2.78 MB/s) - 'google-chrome-stable_current_amd64.deb' saved [62181264/62181264]
第2步:使用以下命令安裝:
[email protected]:~$ sudo dpkg -i google-chrome-stable_current_amd64.deb
Selecting previously unselected package google-chrome-stable.
(Reading database ... 129294 files and directories currently installed.)
Preparing to unpack google-chrome-stable_current_amd64.deb ...
Unpacking google-chrome-stable (79.0.3945.88-1) ...
Setting up google-chrome-stable (79.0.3945.88-1) ...
update-alternatives: using /usr/bin/google-chrome-stable to provide /usr/bin/x-www-browser (x-www-browser) in auto mode
update-alternatives: using /usr/bin/google-chrome-stable to provide /usr/bin/gnome-www-browser (gnome-www-browser) in auto mode
update-alternatives: using /usr/bin/google-chrome-stable to provide /usr/bin/google-chrome (google-chrome) in auto mode
Processing triggers for gnome-menus (3.13.3-11ubuntu1.1) ...
Processing triggers for desktop-file-utils (0.23-1ubuntu3.18.04.2) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
如果一切順利,您應該會在 Show Applications 中看到 Chrome。

在 Chrome 中安裝 Selenium IDE
在 Chrome 中安裝擴展程序與在 Firefox 中相同。這個 Chrome 的 Selenium 擴展 有(這裡 Selenium Firefox 擴展)。
步驟1:[Chrome に追加]單擊按鈕。

第2步。[拡張機能の追加]點擊。

這應該是!如果一切順利並且您沒有看到任何錯誤,那麼您的擴展程序應該可以在 Chrome 中運行。
使用 Selenium IDE
使用 Firefox 擴展來顯示更多任務。
步驟 1. 首先,我們需要創建一個項目。 單擊創建新項目開始。

第二步。創建新項目後,您需要為其命名。使用 SeleniumLW 作為項目名稱。

步驟 3. 為您的項目指定名稱後,[OK]點擊[プロジェクト]顯示一個頁面。

右上角應該有一個REC按鈕。按錄音,會彈出如下畫面。 (在 https://CodePre.com 上運行測試作為測試)。

步驟 5。按開始錄製後,將在新窗口中打開一個鏈接,在網站上執行的操作將被記錄並在下一個屏幕上列出。

步驟 6. 在此屏幕下方,您將看到從測試期間執行的命令中提取的各種信息。錄製停止後,您可以運行一個測試來複製之前在您的站點上執行的所有任務。發現的任何問題都將記錄在擴展程序的“日誌”部分。

第 7 步。下一個選項卡稱為參考。在此頁面上,您可以看到測試期間記錄的每個命令的描述。 Selenium IDE 不僅用於可視化測試,還用於創建測試原型。這是因為這些測試可以導出到 WebDriver 應用程序。保存測試後,右鍵單擊[エクスポート]您可以通過選擇該選項來導出您的測試。可以按命令或按項目進行導出。這將在導出選項期間顯示一些其他選項。

第 8 步。接下來,將輸出導出到 Python(或您喜歡的語言)。保存輸出後,WebDriver Python 測試輸出如下所示:
# 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 TestTestLW():
def setup_method(self, method):
self.driver = webdriver.Firefox()
self.vars = {}
def teardown_method(self, method):
self.driver.quit()
def test_testLW(self):
# Test name: TestLW
# Step # | name | target | value | comment
# 1 | open "https://www.CodePre.com/" |
self.driver.get("https://www.CodePre.com/")
# 2 | setWindowSize | 1853x920 | |
self.driver.set_window_size(1853, 920)
# 3 | click | css=.menu-item-has-children:nth-child(1) > a | |
self.driver.find_element(By.CSS_SELECTOR, ".menu-item-has-children:nth-child(1) > a").click()
# 4 | click | css=.sub-nav-wrap:nth-child(1) .menu-item:nth-child(5) span | |
self.driver.find_element(By.CSS_SELECTOR, ".sub-nav-wrap:nth-child(1) .menu-item:nth-child(5) span").click()
# 5 | runScript | window.scrollTo(0,191) | |
self.driver.execute_script("window.scrollTo(0,191)")
# 6 | runScript | window.scrollTo(0,399) | |
self.driver.execute_script("window.scrollTo(0,399)")
# 7 | runScript | window.scrollTo(0,741) | |
self.driver.execute_script("window.scrollTo(0,741)")
# 8 | runScript | window.scrollTo(0,1311) | |
self.driver.execute_script("window.scrollTo(0,1311)")
# 9 | runScript | window.scrollTo(0,1968) | |
self.driver.execute_script("window.scrollTo(0,1968)")
# 10 | click | css=#fl-accordion-5c2d26f14835f-tab-49 > .fl-accordion-button-label | |
self.driver.find_element(By.CSS_SELECTOR, "#fl-accordion-5c2d26f14835f-tab-49 > .fl-accordion-button-label").click()
正如我在上一篇文章中所說,Selenium IDE 在所有情況下都很棒,但是它缺少一些選項和配置,因此主要用於原型設計。
安裝硒網格
Selenium Grid 允許您同時在多個瀏覽器和環境中運行測試。這大大減少了測試所需的時間。
安裝依賴項
步驟 1. 首先,安裝 software-properties-common,以便更輕鬆地添加更多存儲庫。
[email protected]:~$ sudo apt-get install software-properties-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
software-properties-common is already the newest version (0.96.24.32.11).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
第二步。現在讓我們使用以下命令更新存儲庫:
[email protected]:~$ sudo apt-get update
Hit:1 https://ppa.launchpad.net/webupd8team/java/ubuntu bionic InRelease
Hit:2 https://archive.ubuntu.com/ubuntu bionic InRelease
Hit:3 https://archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:4 https://archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:5 https://security.ubuntu.com/ubuntu bionic-security InRelease
Ign:6 https://dl.google.com/linux/chrome/deb stable InRelease
Hit:7 https://dl.google.com/linux/chrome/deb stable Release
Reading package lists... Done
步驟 3。然後使用以下命令安裝 Java JDK 8:
sudo apt 安裝默認 jre
[email protected]:~$ sudo apt install default-jre
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
ca-certificates-java default-jre-headless fonts-dejavu-extra java-common libatk-wrapper-java libatk-wrapper-java-jni libgif7 openjdk-11-jre openjdk-11-jre-headless
Suggested packages:
fonts-ipafont-gothic fonts-ipafont-mincho fonts-wqy-microhei | fonts-wqy-zenhei
The following NEW packages will be installed:
ca-certificates-java default-jre default-jre-headless fonts-dejavu-extra java-common libatk-wrapper-java libatk-wrapper-java-jni libgif7 openjdk-11-jre openjdk-11-jre-headless
0 upgraded, 10 newly installed, 0 to remove and 223 not upgraded.
Need to get 39.6 MB of archives.
After this operation, 179 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
…
…
…
…
Setting up default-jre (2:1.11-68ubuntu1~18.04.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for gnome-menus (3.13.3-11ubuntu1.1) ...
Processing triggers for ca-certificates (20180409) ...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
done.
Processing triggers for hicolor-icon-theme (0.17-2) ...
Processing triggers for fontconfig (2.12.6-0ubuntu2) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for desktop-file-utils (0.23-1ubuntu3.18.04.2) …
[email protected]:~$
步驟 4. 如果一切順利且沒有任何錯誤,您可以使用以下命令檢查您的 Java 版本:
[email protected]:~$ java -version
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-post-Ubuntu-0ubuntu1.118.04)
OpenJDK 64-Bit Server VM (build 11.0.5+10-post-Ubuntu-0ubuntu1.118.04, mixed mode, sharing)
安裝硒網格
步驟 5. 接下來,下載 Selenium 分發 jar 文件。
[email protected]:~$ wget https://selenium-release.storage.googleapis.com/3.10/selenium-server-standalone-3.10.0.jar
--2019-12-28 22:51:56-- https://selenium-release.storage.googleapis.com/3.10/selenium-server-standalone-3.10.0.jar
Resolving selenium-release.storage.googleapis.com (selenium-release.storage.googleapis.com)... 172.217.22.176, 2a00:1450:400f:80b::2010
Connecting to selenium-release.storage.googleapis.com (selenium-release.storage.googleapis.com)|172.217.22.176|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23427664 (22M) [application/java-archive]
Saving to: 'selenium-server-standalone-3.10.0.jar'
selenium-server-standalone-3.10.0.jar 100%[===========================================>] 22.34M 3.94MB/s in 5.5s
2019-12-28 22:52:02 (4.03 MB/s) - 'selenium-server-standalone-3.10.0.jar' saved [23427664/23427664]
啟動硒網格
第 6 步。現在您可以使用 java 命令啟動 Selenium。
[email protected]:~$ java -jar selenium-server-standalone-3.10.0.jar -role hub
22:54:44.323 INFO [GridLauncherV3.launch] - Selenium build info: version: '3.10.0', revision: '176b4a9'
22:54:44.377 INFO [GridLauncherV3$2.launch] - Launching Selenium Grid hub on port 4444
2019-12-28 22:54:45.881:INFO::main: Logging initialized @2350ms to org.seleniumhq.jetty9.util.log.StdErrLog
22:54:46.200 INFO [Hub.start] - Selenium Grid hub is up and running
22:54:46.204 INFO [Hub.start] - Nodes should register to https://10.0.2.15:4444/grid/register/
22:54:46.205 INFO [Hub.start] - Clients should connect to https://10.0.2.15:4444/wd/hub
步驟 7. 現在集線器已經啟動,我們可以在每台機器上配置節點。為此,請使用以下設置創建一個名為 config.json 的文件:
{
"capabilities":
[
{
"browserName": "firefox",
"marionette": true,
"maxInstances": 5,
"seleniumProtocol": "WebDriver",
"version": "58.0.2"
}
],
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"maxSession": 5,
"port": 5555,
"register": true,
"registerCycle": 5000,
"hub": "https://10.0.2.15:4444",
"nodeStatusCheckTimeout": 5000,
"nodePolling": 5000,
"role": "node",
"unregisterIfStillDownAfter": 60000,
"downPollingLimit": 2,
"debug": false,
"servlets" : [],
"withoutServlets": [],
"custom": {}
}
第 8 步。現在您可以通過鍵入以下內容來啟動節點:
[email protected]:~$ java -jar selenium-server-standalone-3.10.0.jar -role node -nodeConfig config.json
23:05:21.922 INFO [GridLauncherV3.launch] - Selenium build info: version: '3.10.0', revision: '176b4a9'
23:05:21.935 INFO [GridLauncherV3$3.launch] - Launching a Selenium Grid node on port 5555
2019-12-28 23:05:22.387:INFO::main: Logging initialized @1264ms to org.seleniumhq.jetty9.util.log.StdErrLog
23:05:22.728 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 5555
23:05:22.729 INFO [GridLauncherV3$3.launch] - Selenium Grid node is up and ready to register to the hub
23:05:22.747 INFO [SelfRegisteringRemote$1.run] - Starting auto registration thread. Will try to register every 5000 ms.
23:05:22.767 INFO [SelfRegisteringRemote.registerToHub] - Registering the node to the hub: https://10.0.2.15:4444/grid/register
23:05:23.853 INFO [SelfRegisteringRemote.registerToHub] - Updating the node configuration from the hub
23:05:24.012 INFO [SelfRegisteringRemote.registerToHub] - The node is registered to the hub and ready to use
由於您已經配置了集線器和節點,您應該能夠從以下鏈接中看到它們:https://10.0.2.15:4444/grid/console

如需進一步閱讀,您可以查看許多其他內容 硒網格選項 可配置。
安裝 Selenium WebDriver (ChromeDriver)
步驟 1. 由於您已經安裝了 selenium-server-standalone 框架,您無需再次重複這些步驟。 只需為 Chrome 安裝新驅動程序即可。首先,我們需要安裝一些依賴項。
[email protected]:~$ sudo apt-get install -y unzip xvfb libxi6 libgconf-2-4
Reading package lists... Done
Building dependency tree
Reading state information... Done
libxi6 is already the newest version (2:1.7.9-1).
unzip is already the newest version (6.0-21ubuntu1).
The following additional packages will be installed:
gconf-service gconf-service-backend gconf2-common
The following NEW packages will be installed:
gconf-service gconf-service-backend gconf2-common libgconf-2-4 xvfb
0 upgraded, 5 newly installed, 0 to remove and 223 not upgraded.
Need to get 1,628 kB of archives.
After this operation, 10.3 MB of additional disk space will be used.
Get:1 https://archive.ubuntu.com/ubuntu bionic/universe amd64 gconf2-common all 3.2.6-4ubuntu1 [700 kB]
Get:2 https://archive.ubuntu.com/ubuntu bionic/universe amd64 libgconf-2-4 amd64 3.2.6-4ubuntu1 [84.8 kB]
Get:3 https://archive.ubuntu.com/ubuntu bionic/universe amd64 gconf-service-backend amd64 3.2.6-4ubuntu1 [58.1 kB]
Get:4 https://archive.ubuntu.com/ubuntu bionic/universe amd64 gconf-service amd64 3.2.6-4ubuntu1 [2,036 B]
Get:5 https://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 xvfb amd64 2:1.19.6-1ubuntu4.3 [783 kB]
Fetched 1,628 kB in 2s (981 kB/s)
Selecting previously unselected package gconf2-common.
(Reading database ... 129820 files and directories currently installed.)
Preparing to unpack .../gconf2-common_3.2.6-4ubuntu1_all.deb ...
Unpacking gconf2-common (3.2.6-4ubuntu1) ...
Selecting previously unselected package libgconf-2-4:amd64.
Preparing to unpack .../libgconf-2-4_3.2.6-4ubuntu1_amd64.deb ...
Unpacking libgconf-2-4:amd64 (3.2.6-4ubuntu1) ...
Selecting previously unselected package gconf-service-backend.
Preparing to unpack .../gconf-service-backend_3.2.6-4ubuntu1_amd64.deb ...
Unpacking gconf-service-backend (3.2.6-4ubuntu1) ...
Selecting previously unselected package gconf-service.
Preparing to unpack .../gconf-service_3.2.6-4ubuntu1_amd64.deb ...
Unpacking gconf-service (3.2.6-4ubuntu1) ...
Selecting previously unselected package xvfb.
Preparing to unpack .../xvfb_2%3a1.19.6-1ubuntu4.3_amd64.deb ...
Unpacking xvfb (2:1.19.6-1ubuntu4.3) ...
Setting up gconf2-common (3.2.6-4ubuntu1) ...
Creating config file /etc/gconf/2/path with new version
Setting up xvfb (2:1.19.6-1ubuntu4.3) ...
Setting up libgconf-2-4:amd64 (3.2.6-4ubuntu1) ...
Setting up gconf-service-backend (3.2.6-4ubuntu1) ...
Setting up gconf-service (3.2.6-4ubuntu1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
步驟 2. 下載 ChromeDriver
[email protected]:~$ wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
--2019-12-28 23:24:32-- https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
Resolving chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)... 216.58.211.144, 2a00:1450:400f:80c::2010
Connecting to chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)|216.58.211.144|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3944714 (3.8M) [application/zip]
Saving to: 'chromedriver_linux64.zip'
chromedriver_linux64.zip 100%[===========================================>] 3.76M 3.69MB/s in 1.0s
2019-12-28 23:24:34 (3.69 MB/s) - 'chromedriver_linux64.zip' saved [3944714/3944714]
步驟 3。然後解壓下載的 ChromeDriver 文件。
[email protected]:~$ unzip chromedriver_linux64.zip
Archive: chromedriver_linux64.zip
inflating: chromedriver
步驟 4. 按順序運行以下命令,準備使用 ChromeDriver:讓我們將解壓後的 chromedriver 文件移動到 /usr/bin/ 文件夾。然後確保 root 用戶擁有它。然後用 chmod 命令添加執行權限標誌,最後檢查版本。
[email protected]:~$ sudo mv chromedriver /usr/bin/chromedriver
[email protected]:~$ sudo chown root:root /usr/bin/chromedriver
[email protected]:~$ sudo chmod +x /usr/bin/chromedriver
[email protected]:~$ chromedriver --version
ChromeDriver 2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706)
步驟 5。然後使用此命令通過 Selenium 服務器運行 ChromeDriver。
[email protected]:~$ xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone-3.10.0.jar
23:32:09.057 INFO [GridLauncherV3.launch] - Selenium build info: version: '3.10.0', revision: '176b4a9'
23:32:09.062 INFO [GridLauncherV3$1.launch] - Launching a standalone Selenium Server on port 4444
2019-12-28 23:32:10.051:INFO::main: Logging initialized @1951ms to org.seleniumhq.jetty9.util.log.StdErrLog
23:32:10.687 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444
基本上,WebDriver 只是 Web 瀏覽器的參考,所以最初的網格設置展示瞭如何使用 ChromeDriver 和 FirefoxDrive 安裝 Selenium。
硒 RC
Selenium 遠程控制 (RC) 已被棄用,取而代之的是 Selenium WebDriver。在大多數情況下,測試人員主要使用 Selenium Grid 和 WebDriver 來加速結果。因此,本教程不涵蓋 Selenium 的這一部分。
總之
Selenium 是一套功能強大的測試工具。使用它的測試人員比在不同環境中手動運行測試更有效率。 Selenium 還可用於在多個系統上的不同瀏覽器中測試大量 Web 應用程序。
正如你看到的?
致電 800.580.4985 或 和…聊天 或者聯繫我們知識淵博的解決方案或經驗豐富的託管顧問,了解如何將這些技術知識運用到今天。