如何在没有testng/maven的情况下从命令行运行Selenium Webdriver测试用例

如何在没有testng/maven的情况下从命令行运行Selenium Webdriver测试用例

问题描述:

我正在使用Selenium Webdriver编写测试用例.

I am writing test cases using selenium webdriver.

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");// Open the URL.
driver.manage().window().maximize(); // Maximize the window
driver.quit();

现在,我想从命令行运行此测试并创建一个批处理文件.我没有使用任何testng或maven.如何从cmd运行?

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?

在您喜欢的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

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

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.53.0</version>
</dependency> 


接下来使用main函数创建Java类:


Next create java class with main function:

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();
    }
}

然后,您可以在IDE中运行此类以测试它是否可以正常工作-在将其保存到可运行的jar文件之前.

You can then run this class in the IDE to test if it woks - before saving it to a runnable jar file.

接下来构建项目,然后将其导出到可运行的jar文件-请参阅您的IDE文档以了解如何进行操作(在Eclipse中单击选项:File/Export/Java/Runnable JAR文件,选择打包"选项所需的库到生成的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时的目录,然后使用以下命令运行它:

And finally open a command prompt, change a current directory to the directory when the generated jar has been saved, and run it using:

java -jar name_of_jar_file.jar