java 如何在没有 testng/maven 的情况下从命令行运行 selenium webdriver 测试用例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37225903/
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 run selenium webdriver test cases from command line without testng/maven
提问by Sowmya
I am writing test cases using selenium webdriver.
我正在使用 selenium webdriver 编写测试用例。
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");// Open the URL.
driver.manage().window().maximize(); // Maximize the window
driver.quit();
Now I want to run this test from command line and create a batch file. I am not using any testng or maven. How can I run from cmd?
现在我想从命令行运行这个测试并创建一个批处理文件。我没有使用任何 testng 或 maven。如何从cmd运行?
回答by krokodilko
Create a new Java project in your favorite IDE platform (Eclipse, Netbeans, Intellij ...).
Download and unpack Selenium Java language bindings from here: http://www.seleniumhq.org/download/
It contains all required libraries (jar files) and also Firefox driver.
Add all libraries (jar files) to your project to the classpath. Don't forget to add also all jar files from lib
subdirectory.
Refer to documentation of your IDE to know how to do it.
You can also configure your project as maven project and let Maven download all dependecies for you, this is a dependency definition from Selenium project page: http://www.seleniumhq.org/download/maven.jsp
在您喜欢的 IDE 平台(Eclipse、Netbeans、Intellij ...)中创建一个新的 Java 项目。
从这里下载并解压 Selenium Java 语言绑定:http: //www.seleniumhq.org/download/
它包含所有必需的库(jar 文件)和 Firefox 驱动程序。
将所有库(jar 文件)添加到您的项目的类路径中。不要忘记添加lib
子目录中的所有 jar 文件。
请参阅您的 IDE 的文档以了解如何执行此操作。
您还可以将您的项目配置为 maven 项目,让 Maven 为您下载所有依赖项,这是来自 Selenium 项目页面的依赖项定义:http: //www.seleniumhq.org/download/maven.jsp
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
</dependency>
Next create java class with main
function:
接下来用main
函数创建java类:
package mypackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MySeleniumTest {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");// Maximize the window.
driver.manage().window().maximize();
try {
// wait 4 seconds before closing the browser
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.quit();
}
}
You can then run this class in the IDE to test if it woks - before saving it to a runnable jar file.
然后,您可以在 IDE 中运行此类以测试它是否有效 - 在将其保存到可运行的 jar 文件之前。
Next build the project, and then export it to a runnable jar file - refer to your IDE documentation to know how to do it (in Eclipse click options: File/Export/Java/Runnable JAR file, choose the option "Package required libraries into generated JAR").
接下来构建项目,然后将其导出为可运行的 jar 文件 - 请参阅您的 IDE 文档以了解如何操作(在 Eclipse 中单击选项:File/Export/Java/Runnable JAR 文件,选择选项“Package required libraries into生成的 JAR”)。
And finally open a command prompt, change a current directory to the directory when the generated jar has been saved, and run it using:
最后打开命令提示符,将当前目录更改为保存生成的 jar 时的目录,并使用以下命令运行它:
java -jar name_of_jar_file.jar