在我上一篇關於 Selenium 的文章中,我描述了軟件提供的用於測試 Web 應用程序的工具。本文介紹了這些工具的安裝以及您可以使用以下工具執行的一些基本測試:

  • 硒 IDE
  • 硒網格
  • Selenium 網絡驅動程序

每個 Selenium 工具都按下面列出的順序進行描述。現在讓我們從安裝 Selenium IDE 開始。在此之後,我們將解釋每個工具的基本用法。

我需要

服務器規格:

  • CPU核心:2
  • 內存:2 GB
  • 操作系統:使用 GUI 全新安裝 Ubuntu 18.04

硒 IDE

我需要: Selenium IDE 是 Chrome / Firefox 瀏覽器擴展,因此除了瀏覽器本身需要之外沒有特殊的依賴項。它適用於所有支持安裝這些瀏覽器的操作系統。

火狐安裝

Step 1. 首先,點擊此鏈接進行安裝 Selenium 瀏覽器插件..安裝後,您的瀏覽器將如下所示:

第2步。在那之後,[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]

第二步:使用以下命令安裝:

[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に追加]單擊按鈕。

第一個 Chrome 屏幕

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

添加到 Chrome

這應該是!如果一切順利並且您沒有看到任何錯誤,則該擴展程序應該可以在 Chrome 中運行。

如何使用 Selenium IDE

使用 Firefox 擴展來顯示更多任務。

步驟 1. 首先,您需要創建一個項目。[新しいプロジェクトの作成]點擊開始。

首次發射

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

商業

Step 3. 指定項目名稱後[OK]點擊[プロジェクト]顯示頁面。

第 2 項

右上角應該有一個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.

第2步。接下來,讓我們使用以下命令更新存儲庫。

[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. 下載 Chrome 驅動程序

[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。然後解壓縮下載的 Chrome 驅動程序文件。

[email protected]:~$ unzip chromedriver_linux64.zip
Archive:  chromedriver_linux64.zip
  inflating: chromedriver

步驟 4. 依次運行以下命令,準備 Chrome 驅動程序。我們把解壓後的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 服務器運行 Chrome 驅動程序。

[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 和 Firefox Drive 安裝 Selenium。

硒 RC

Selenium Remote Control (RC) 已停產,並已採用 Selenium Web Driver。在大多數情況下,測試人員現在主要使用 Selenium Grid 和 WebDriver 以獲得更快的結果。因此,本教程不涵蓋 Selenium 的這一部分。

總之

Selenium 是一組功能強大的測試工具,集成到一個套件中。使用它的測試人員比在不同環境中手動運行測試的效率要高得多。 Selenium 還可用於在多個系統上的不同瀏覽器中測試大量 Web 應用程序。

你看?

請致電 800.580.4985 或 聊天 或諮詢我們知識淵博的解決方案或經驗豐富的託管顧問,了解如何利用這一技術知識。