如何正确设置 Java/Selenium 配置以运行自动化测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33701682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to properly set up Java/Selenium configuration to run automated tests?
提问by Dear1ofGdBear
I am trying to set up selenium webdriver to work together with Browserstack with Java for automated testing. I installed the Selenium for java and I copied and pasted the code from browserstack's site https://www.browserstack.com/automate/java#configure-capabilitiesto set up an example automation test.
我正在尝试设置 selenium webdriver 与 Browserstack 一起使用 Java 进行自动化测试。我为 Java 安装了 Selenium,并从浏览器堆栈的站点https://www.browserstack.com/automate/java#configure-capabilities复制并粘贴了代码以设置示例自动化测试。
I ran javac -classpath selenium-server-standalone-2.48.2.jar JavaSample.java
from my terminal (JavaSample.java is the file with the selenium configuration code with the sample test) and I get the following error:
我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
I'm not sure how to go about this being as I just followed the instructions on Browserstack and I have very little background in Java.
我不知道如何解决这个问题,因为我只是按照 Browserstack 上的说明进行操作,而且我对 Java 的了解很少。
采纳答案by Umang Sardesai
You will have to download the "Selenium Client & WebDriver Language Bindings" for Javafrom Selenium Downloads. You can download directly by clicking the link here.
您必须从Selenium 下载 下载Java的“Selenium Client & WebDriver Language Bindings” 。您可以通过单击此处的链接直接下载。
Include all the JAR files that are present in the downloaded ZIP file. To include multiple JARs in Java classpath, you can check the link here.
包括下载的 ZIP 文件中存在的所有 JAR 文件。要在 Java 类路径中包含多个 JAR,您可以查看此处的链接。
The selenium-server-standalone JAR
is required if you are running your tests locally. Executing the command java -jar selenium-server-standalone-2.48.2.jar
will start a Selenium server, which required to launch Selenium tests locally. You need not use it, if you are running tests on BrowserStack.
selenium-server-standalone JAR
如果您在本地运行测试,则这是必需的。执行该命令java -jar selenium-server-standalone-2.48.2.jar
将启动一个 Selenium 服务器,它需要在本地启动 Selenium 测试。如果您在 BrowserStack 上运行测试,则不需要使用它。
Would also recommend using an IDE for Java. The most commonly recommended ones are IntelliJ Idea, Eclipse, and Netbeans.
还建议使用适用于 Java 的 IDE。最常推荐的是IntelliJ Idea、Eclipse和Netbeans。