Selenium 简明教程
Selenium WebDriver - Safari Options
Selenium Webdriver 可用于在 Safari 浏览器上运行自动化测试。有些功能和特性仅适用于 Apple 用户的 Safari。Safari 是一款著名的浏览器,由 Apple 设备默认提供。对于 Safari 浏览器版本 10 及更高版本,safaridriver 会自动提供,不需要单独安装。
safari 驱动程序默认安装在操作系统上,而 Firefox 和 Chromium 浏览器则不然。我们需要进行初始设置,在 Safari 浏览器中启用开发菜单,然后才能实际在其中运行测试。默认情况下,开发菜单不会保持可见。
为此,我们需要打开 Safari,然后单击 Settings 。接下来,我们需要移动到 Advanced 选项卡,然后选中 Show Features for web developers 选项。
要开始在 Safari 浏览器上运行自动化测试,我们需要从终端运行以下命令−
safaridriver --enable
Example
让我们举一个下面页面的示例,在其中我们在 SafariOptions 上启动 Safari 驱动程序会话。然后我们将获取浏览器标题 Selenium Practice - Student Registration Form 和当前 URL Selenium Automation Practice Form 。
要将 SafariOptions 类与 SafariDriver 一起使用,我们需要添加导入语句 −
import org.openqa.selenium.safari.SafariOptions
import org.openqa.selenium.safari.SafariDriver
Code Implementation
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://www.tutorialspoint.com/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();
}
}
Output
Browser title is: Selenium Practice - Student Registration Form
Current URL is:
https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php
Process finished with exit code 0
在上面的示例中,我们用 SafariOptions 启动 Safari 浏览器,然后在控制台中用消息获取浏览器标题和当前 URL - Browser title is: Selenium Practice - Student Registration Form and Current URL is: Selenium Automation Practice Form 。
Safari 浏览器总是将日志(如果已启用)存储在以下位置 - ~/Library/Logs/com.apple.WebDriver/ 。与 Chrome、Firefox 等其他浏览器不同,我们无法将日志及其级别分别保存到其他位置和值。
Example - Capture Safari Logs
让我们举一个启用 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://www.tutorialspoint.com/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();
}
}
Example - Disable Safari Logs
让我们举一个禁用 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://www.tutorialspoint.com/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();
}
}