FayazMd
FayazMd

Reputation: 386

Running selenium testng executable jar is failing

I have written a simple Selenium + TestNG maven project (say, in directory I:\Projects\) using Java in Eclipse IDE, when I right click the project and run testng.xml file able to launch the Chrome browser I have now exported this project as 'Runnable JAR file' and saved the jar on Desktop (C:\Users\Programmer\Desktop).

Now,

  1. If I run the saved executable jar from my project directory and double click the jar, able to launch the Chrome browser, that is, I:\Projects>java -jar "C:\Users\Programmer\Desktop\runnable.jar", but
  2. If I save the executable jar from desktop unable to launch the browser, that is C:\Users\Programmer\Desktop>java -jar runnable.jar

My simple selenium code,

public class GoogleSearch {

WebDriver driver;

@Test
public void launchBrowser() {
    String PWD = System.getProperty("user.dir");
    System.out.println(">>>>>>" + PWD);
    System.setProperty("webdriver.chrome.driver", PWD + "\\lib\\chromedriver");
    driver = new ChromeDriver();
    driver.get("https://www.google.com");
    WebElement search = driver.findElement(By.name("q"));
    search.sendKeys("Selenium monk");
}

@Test
public void tearDown() {
    if (driver != null) {
        driver.close();
        driver.quit();
    }
}
}

Project directory structure..

enter image description here

I understood like, when I am running from Desktop, jar is unable to find the chromedriver in lib folder.? If yes, how to make it work.?

What else is I am missing here?

Upvotes: 1

Views: 1053

Answers (2)

FayazMd
FayazMd

Reputation: 386

I resolved on my own, since the problem was, "when I am running from Desktop, jar is unable to find the chromedriver in lib folder."

I need to set the chrome driver as part of environment variables, and I have updated the chrome driver property by reading the env var, System.getenv("chrome_driver_path");

After which my problem resolved.

Thank you all for those who took your time to answer the question.

Upvotes: 1

Bigman
Bigman

Reputation: 1473

When your jar is on the desktop, try to run this (assuming your are running under C:\Users\you\Desktop),

java -cp runnable.jar;path-to-your-proj\lib\* org.testng.TestNG path-to-your-proj/testng.xml -Duser.dir=path-to-your-proj

Upvotes: 1

Related Questions