[英]How to run selenium webdriver test cases from command line without testng/maven
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. 我没有使用任何testng或maven。 How can I run from cmd? 如何从cmd运行?
Create a new Java project in your favorite IDE platform (Eclipse, Netbeans, Intellij ...). 在您喜欢的IDE平台(Eclipse,Netbeans,Intellij ...)中创建一个新的Java项目。
Download and unpack Selenium Java language bindings from here: http://www.seleniumhq.org/download/ 从此处下载并解压缩Selenium Java语言绑定: http : //www.seleniumhq.org/download/
It contains all required libraries (jar files) and also Firefox driver. 它包含所有必需的库(jar文件)以及Firefox驱动程序。
Add all libraries (jar files) to your project to the classpath. 将所有库(jar文件)添加到项目中的类路径。 Don't forget to add also all jar files from lib
subdirectory. 不要忘了还要从lib
子目录添加所有jar文件。
Refer to documentation of your IDE to know how to do it. 请参阅您的IDE的文档以了解操作方法。
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 您还可以将项目配置为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文件,选择“将所需的库打包到生成的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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.