• Selenium Video Tutorials

IntelliJ 用於 Selenium



我們需要使用編輯器配置 Selenium 才能執行自動化測試。有多個可用的編輯器,例如:Eclipse、IntelliJ、Atom、Sublime 等。使用這些編輯器,我們可以開始使用 Java 專案來啟動測試自動化。

設定 IntelliJ for Selenium 的先決條件

使用以下連結下載並安裝 Java(版本高於 17),並使用以下命令驗證它是否可用:

java –version

https://www.oracle.com/java/technologies/downloads/.

有關 Java 配置的更多資訊,請參考以下連結:

https://www.youtube.com/watch?v=bxIZ1GVWYkQ.

使用以下連結下載並安裝 Maven,並使用以下命令驗證它是否可用:

mvn -version

https://maven.apache.org/download.cgi.

有關 Maven 配置的更多資訊,請參考連結 Maven 環境設定

為 Selenium 設定 IntelliJ

步驟 1 - 使用以下連結導航到 IntelliJ(Jetbrains 的產品)的官方網站,然後點選下載:

https://www.jetbrains.com/idea/.

Selenium IntelliJ 1

步驟 2 - 導航到下一頁後,我們將獲得在各種作業系統(如 Windows、macOS 和 Linux)中下載 IntelliJ 的選項。根據當前作業系統點選相應的選項卡。

IntelliJ 有兩個版本,付費版和社群版(免費)。

Selenium IntelliJ 2

步驟 3 - 我們將下載社群版。為此,我們將向下移動到 IntelliJ IDEA 社群版部分,然後點選下載。

Selenium IntelliJ 3

步驟 4 - IntelliJ 的 logo 應該會顯示幾秒鐘,然後會出現 JETBRAINS COMMUNITY EDITION TERMS。選中複選框以接受條款和條件,然後點選繼續。

步驟 5 - 歡迎使用 IntelliJ IDEA 應該會顯示。點選新建專案按鈕。

步驟 6 - 在名稱:欄位下輸入名稱。將語言選擇為 Java,構建系統選擇為 Maven,並選擇 JDK 版本,然後點選建立。

步驟 7 - 輸入 ArtifactId,然後點選建立。

步驟 8 - IntelliJ 編輯器設定應該已成功完成。

步驟 9 - 從連結 Selenium Java 將 Selenium Maven 依賴項新增到 pom.xml 檔案中。

Selenium IntelliJ 4

步驟 10 - 在中央選項卡下選擇並點選版本連結。我們導航到 Selenium Java >> 頁面。複製 Maven 選項卡下的依賴項。

Selenium IntelliJ 5

依賴項示例:

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-java</artifactId>
   <version>4.11.0</version>
</dependency>

步驟 11 - 將步驟 9 中複製的依賴項貼上到 pom.xml 檔案中(位於 IntelliJ 工作區中建立的 Maven 專案下)。

步驟 12 - 在 Main.java 檔案中新增以下程式碼。

程式碼實現

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
   public static void main(String[] args) throws InterruptedException {
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.google.com");
      System.out.println("Browser title: " + driver.getTitle());
   }
}

新增到 pom.xml 檔案中的依賴項:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.example</groupId>
   <artifactId>SeleniumJava</artifactId>
   <version>1.0-SNAPSHOT</version>
   
   <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>

   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.11.0</version>
      </dependency>
   </dependencies>
</project>

步驟 13 - 右鍵點選並選擇執行“Main.main()”選項。等待執行完成。

步驟 14 - Chrome 瀏覽器啟動,我們在控制檯中獲得了輸出 - 瀏覽器標題:Google,以及訊息“程序已完成,退出程式碼為 0”,表示程式碼已成功執行。

此外,Chrome 瀏覽器啟動時顯示訊息Chrome 正在由自動化測試軟體控制

結論

本教程全面介紹了 Intellij Selenium 教程,到此結束。我們首先介紹了設定 IntelliJ for Selenium 的先決條件,並逐步介紹瞭如何設定 IntelliJ 以及 Selenium。這使您能夠深入瞭解 Intellij Selenium。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並擴充套件您的視野。

廣告

© . All rights reserved.