• Selenium Video Tutorials

Selenium WebDriver - Safari 選項



Selenium Webdriver 可用於在 Safari 瀏覽器上執行自動化測試。某些功能和特性僅適用於 Apple 使用者的 Safari。Safari 是一款流行的瀏覽器,由 Apple 裝置預設提供。對於 Safari 瀏覽器版本 10 及更高版本,safaridriver 會自動安裝,無需單獨安裝。

safari 驅動程式預設安裝在作業系統上,而 Firefox 和 Chromium 瀏覽器則不是這樣。我們需要進行一些初始設定,在 Safari 瀏覽器中啟用“開發”選單,然後才能在其上實際執行測試。預設情況下,“開發”選單不可見。

Selenium Safari Options 1

為此,我們需要開啟 Safari,然後單擊“設定”。接下來,我們需要轉到“高階”選項卡,並選中“顯示 Web 開發人員功能”選項。

Selenium Safari Options 2

要開始在 Safari 瀏覽器上執行自動化測試,我們需要從終端執行命令:

safaridriver  --enable

示例

讓我們以以下頁面為例,我們在此啟動一個 Safari 驅動程式會話以及 SafariOptions。然後,我們將獲取瀏覽器標題“Selenium Practice - Student Registration Form”和當前 URL Selenium Automation Practice Form

Selenium Safari Options 3

要在 SafariDriver 以及 SafariOptions 類一起使用,我們需要新增匯入語句:

import org.openqa.selenium.safari.SafariOptions
import org.openqa.selenium.safari.SafariDriver

程式碼實現

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariOptions;
import java.util.concurrent.TimeUnit;

public class SafariOpts {
   public static void main(String[] args) throws InterruptedException {

      // Using the Safari options
      SafariOptions opts = new SafariOptions();
      WebDriver driver = new SafariDriver(opts);

      // adding an implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // opening the Safari browser and launch a URL
      driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");

      // get the page title
      System.out.println("Browser title is: " + driver.getTitle());
      
      // get the current URL
      System.out.println("Current URL is: " + driver.getCurrentUrl());

      // closing the browser
      driver.quit();
   }
}

輸出

Browser title is: Selenium Practice - Student Registration Form
Current URL is: 
https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php

Process finished with exit code 0

在上面的示例中,我們使用 SafariOptions 啟動了 Safari 瀏覽器,然後分別獲取了瀏覽器標題和當前 URL,並在控制檯中顯示訊息 - “瀏覽器標題為:Selenium Practice - Student Registration Form 和當前 URL 為:” Selenium Automation Practice Form

Safari 瀏覽器始終將日誌(如果已啟用)儲存在以下位置 - ~/Library/Logs/com.apple.WebDriver/。與 Chrome、Firefox 等其他瀏覽器不同,我們無法選擇將日誌及其級別分別儲存到其他位置和值。

示例 - 捕獲 Safari 日誌

讓我們以一個啟用 Safari 日誌的示例為例。

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariDriverService;
import org.openqa.selenium.safari.SafariOptions;
import java.util.concurrent.TimeUnit;

public class SafariOptsLogs {
   public static void main(String[] args) throws InterruptedException {

      // Using the Safari options
      SafariOptions opts = new SafariOptions();
      WebDriver driver = new SafariDriver(opts);

      // adding an implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // opening the Safari browser and launch a URL
      driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");
      
      // enabling the Safari logs
      SafariDriverService service = new SafariDriverService.Builder().withLogging(true).build();

      // get the page title
      System.out.println("Logging enabled: " + service);

      // close the browser
      driver.quit();
   }
}

輸出

Logging enabled:
org.openqa.selenium.safari.SafariDriverService@3eeb318f

示例 - 停用 Safari 日誌

讓我們以一個停用 Safari 日誌的示例為例。

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariDriverService;
import org.openqa.selenium.safari.SafariOptions;
import java.util.concurrent.TimeUnit;

public class SafariOptsLogsFalse {
   public static void main(String[] args) throws InterruptedException {

      // Using the Safari options
      SafariOptions opts = new SafariOptions();
      WebDriver driver = new SafariDriver(opts);

      // adding an implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // opening the Safari browser and launch a URL
      driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");

      // disabling the Safari Logs
      SafariDriverService service = new SafariDriverService.Builder()
         .withLogging(false)
         .build();

      // get the page title
      System.out.println("Launched Browser title is: " + driver.getTitle());
      
      // get the current URL
      System.out.println("Current URL is: " + driver.getCurrentUrl());

      // close the browser
      driver.quit();
   }
}

輸出

Browser title is: Selenium Practice - Tool Tips
Current URL is: 
https://tutorialspoint.tw/selenium/practice/tool-tips.php

結論

本教程全面介紹了 Selenium WebDriver Safari 選項,到此結束。我們從描述 SafariOptions 類開始,並逐步講解了如何將 SafariOptions 與 Selenium Webdriver 一起使用的示例。這使您深入瞭解了 Selenium Webdriver 中的 SafariOptions 類。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。

廣告