简体   繁体   中英

How to include Chrome Driver in executable Jar file when I export my tests to a Executable jar

I'm trying to export my test case into an executable jar so that I can run them any where or from any machine, but I got the problem of Chrom Driver is not set by the system.properity. I need a solution where I can export my code into executable jar and include Chrome Drivre so that it could run on any machine even it doesn't have Chrome Drivre on it.

I tried to include Chrome Driver on environment variables and Selenium Path. I also tried to add Chrome Driver under project resources but nothing worked.

            public WebDriver OpenCPURL()  {


    PropertyConfigurator.configure("Log4j.properties");
    //System.setProperty("webdriver.chrome.driver",  
            "./chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("disable-infobars");
    options.addArguments("--start-maximized");

    WebDriver driver = new ChromeDriver(options);

    //driver = new ChromeDriver();

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    //driver.manage().window().maximize();
    wait = new WebDriverWait(driver, 60);
    baseUrl = "http://3ddxtesting/newcp/";
    driver.get(baseUrl);
    user_name = driver.findElement(By.name("username"));
    Pass = driver.findElement(By.name("password"));
    user_name.sendKeys(username);
    Log.info(username);
    Pass.sendKeys(Password);
    WebElement LoginButton = driver
            .findElement(By.xpath("//*[@id=\"contentDiv\"]/form/table/tbody/tr[4]/td[4]/input"));
    LoginButton.click();
    String Pageheader = driver.findElement(By.cssSelector("#body > nav > a > font")).getText();
    assertEquals(Pageheader, "3D | Diagnostix");

    return driver;

}

And here I call this method:

public class Add_3D_Printers_Order {

CommonLogin Login = new CommonLogin();
WebDriver driver = Login.OpenCPURL();
 driver.findElement(By.id("printerCheck")).click();
    Thread.sleep(2000);
    String Pageheader_printer = driver.findElement(By.cssSelector("#locationId")).getText();
    assertEquals(Pageheader_printer, "Add Order > Printer & Acc");
    System.out.println("the assertion is done and the application navigate to the 3d printer page");
    Log.info("the application navigate to the 3d printer page succssfully");

This code runs wherever any machine has the Chrome driver on the same path but on other machines it doesn't work

You can make a folder in your project with a name driver and can insert your chromedriver in that folder and you can pick the chromedriver from that location using System.getProperty("user.dir")

For example, use this line of code after making a folder named driver and then adding chromedriver in it: System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/driver/chromedriver");

Here System.getProperty("user.dir") would give you the project directory path and then appending it with /driver/chromedriver would give you the exact path of the chromedriver and then it could be used in any machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM