如何正确设置 Java/Selenium 配置以 运行 自动化测试?

How to properly set up Java/Selenium configuration to run automated tests?

我正在尝试设置 selenium webdriver 以与 Browserstack 一起使用 Java 进行自动化测试。我为 java 安装了 Selenium,并从 browserstack 的站点 https://www.browserstack.com/automate/java#configure-capabilities 复制并粘贴了代码以设置示例自动化测试。

I 运行 javac -classpath selenium-server-standalone-2.48.2.jar JavaSample.java 从我的终端(JavaSample.java 是带有示例测试的 selenium 配置代码的文件),我收到以下错误:

JavaSample.java:1: error: package org.openqa.selenium does not exist
import org.openqa.selenium.By;
                      ^
JavaSample.java:2: error: package org.openqa.selenium does not exist
import org.openqa.selenium.Platform;
                      ^
JavaSample.java:3: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebDriver;
                      ^
JavaSample.java:4: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebElement;
                      ^
JavaSample.java:5: error: package org.openqa.selenium.remote does not exist
import org.openqa.selenium.remote.DesiredCapabilities;
                             ^
JavaSample.java:6: error: package org.openqa.selenium.remote does not exist
import org.openqa.selenium.remote.RemoteWebDriver;
                             ^
JavaSample.java:18: error: cannot find symbol
DesiredCapabilities caps = new DesiredCapabilities();
^
symbol:   class DesiredCapabilities
location: class JavaSample
JavaSample.java:18: error: cannot find symbol
DesiredCapabilities caps = new DesiredCapabilities();
                               ^
symbol:   class DesiredCapabilities
location: class JavaSample
JavaSample.java:25: error: cannot find symbol
WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
^
symbol:   class WebDriver
location: class JavaSample
JavaSample.java:25: error: cannot find symbol
WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
                       ^
symbol:   class RemoteWebDriver
location: class JavaSample
JavaSample.java:27: error: cannot find symbol
WebElement element = driver.findElement(By.name("q"));
^
symbol:   class WebElement
location: class JavaSample
JavaSample.java:27: error: cannot find symbol
WebElement element = driver.findElement(By.name("q"));
                                        ^
symbol:   variable By
location: class JavaSample

我不确定如何解决这个问题,因为我只是按照 Browserstack 上的说明进行操作,而且我在 Java 方面的背景知识很少。

您必须从 Selenium Downloads. You can download directly by clicking the link here.

下载 Java 的 "Selenium Client & WebDriver Language Bindings"

包括下载的 ZIP 文件中存在的所有 JAR 文件。要在 Java 类路径中包含多个 JAR,您可以检查 link here

如果您 运行 在本地进行测试,则 selenium-server-standalone JAR 是必需的。执行命令 java -jar selenium-server-standalone-2.48.2.jar 将启动一个 Selenium 服务器,这需要在本地启动 Selenium 测试。如果您在 BrowserStack 上进行 运行 测试,则无需使用它。

还建议对 Java 使用 IDE。最常推荐的是IntelliJ Idea, Eclipse, and Netbeans