Selenium 简明教程
Selenium WebDriver - Safari Options
Selenium Webdriver 可用于在 Safari 浏览器上运行自动化测试。有些功能和特性仅适用于 Apple 用户的 Safari。Safari 是一款著名的浏览器,由 Apple 设备默认提供。对于 Safari 浏览器版本 10 及更高版本,safaridriver 会自动提供,不需要单独安装。
Selenium Webdriver can be used to run automated tests on the Safari browser. There are certain functionalities and characteristics which are applicable to only Safari for Apple users. Safari is a prominent browser and is provided by default by Apple devices. For Safari browser versions 10 and greater than 10, the safaridriver comes automatically and is not required to be installed separately.
safari 驱动程序默认安装在操作系统上,而 Firefox 和 Chromium 浏览器则不然。我们需要进行初始设置,在 Safari 浏览器中启用开发菜单,然后才能实际在其中运行测试。默认情况下,开发菜单不会保持可见。
The safari driver is installed on the operating system by default which is not the case for Firefox and Chromium browsers. We would need to do initial setups enable Develop menu in the Safari browser before actually running the tests on it. By default, the Develop menu does not remain visible.
为此,我们需要打开 Safari,然后单击 Settings 。接下来,我们需要移动到 Advanced 选项卡,然后选中 Show Features for web developers 选项。
To do so, we have to open Safari, then click on Settings. Next, we would need to move to the Advanced tab, and check the option Show Features for web developers.
要开始在 Safari 浏览器上运行自动化测试,我们需要从终端运行以下命令−
To start running automation tests on Safari browser, we would need to run the command from the terminal −
safaridriver --enable
Example
让我们举一个下面页面的示例,在其中我们在 SafariOptions 上启动 Safari 驱动程序会话。然后我们将获取浏览器标题 Selenium Practice - Student Registration Form 和当前 URL Selenium Automation Practice Form 。
Let us take an example of the page below, where we initiate a Safari driver session along the SafariOptions. Then we would obtain the browser title Selenium Practice - Student Registration Form and current URL Selenium Automation Practice Form.
要将 SafariOptions 类与 SafariDriver 一起使用,我们需要添加导入语句 −
To use SafariOptions class along with SafariDriver, we would need to add the import statements −
import org.openqa.selenium.safari.SafariOptions
import org.openqa.selenium.safari.SafariDriver
Code Implementation
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 。
In the above example, we had launched the Safari browser with SafariOptions then obtained browser title and current URL with the message in the console - Browser title is: Selenium Practice - Student Registration Form and Current URL is: Selenium Automation Practice Form respectively.
Safari 浏览器总是将日志(如果已启用)存储在以下位置 - ~/Library/Logs/com.apple.WebDriver/ 。与 Chrome、Firefox 等其他浏览器不同,我们无法将日志及其级别分别保存到其他位置和值。
Safari browsers would always store the logs(if enabled) in the location - ~/Library/Logs/com.apple.WebDriver/. Unlike other browsers like Chrome, Firefox, and so on, we would not get the option to save the logs and its levels to other locations and values respectively.
Example - Capture Safari Logs
让我们举一个启用 Safari 日志的示例。
Let us take an example, where we would enable the Safari Logs.
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 日志的示例。
Let us take an example, where we would disable the Safari Logs.
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();
}
}
Conclusion
以上就是针对 Selenium WebDriver Safari Options 教程的综合介绍。我们从介绍 SafariOptions 类开始,然后介绍了如何将 SafariOptions 与 Selenium WebDriver 一起使用的示例。这使你能够深入了解 Selenium WebDriver 中的 SafariOptions 类。明智的做法是继续练习你所学的内容,并探索与 Selenium 相关的其他内容以加深你的理解并拓宽你的视野。
This concludes our comprehensive take on the tutorial on Selenium WebDriver Safari Options. We’ve started with describing a SafariOptions class, and walked through examples of how to use the SafariOptions along with Selenium Webdriver. This equips you with in-depth knowledge of the SafariOptions class in Selenium Webdriver. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.