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();
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?
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>
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();
}
}
You can then run this class in the IDE to test if it woks - before saving it to a runnable jar file.
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").
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