0

I have a Spring Boot project which is using selenium for automation testing of different application. The output archive of project is a JAR file. I have below code to initiate chrome browser.

static {
    try {
        Resource resource = null;
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.contains(IAutomationConstans.WINDOWS_OS_NAME)) {
            resource = new ClassPathResource("chromedriver.exe");
        } else if (osName.contains(IAutomationConstans.LINUX_OS_NAME)) {
            resource = new ClassPathResource("chromedriver");
        }
        System.setProperty("webdriver.chrome.driver", resource.getFile().getPath());
        ChromeOptions capabilities = new ChromeOptions();
        webdriver = new ChromeDriver(capabilities);
        Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
    } catch (Exception e) {
        System.out.println("Not able to load Chrome web browser "+e.getMessage());
    }
}

Apart form this I have below Spring Boot code which executes automation code.

@SpringBootApplication
@ComponentScan("com.test.automation")
@PropertySource(ignoreResourceNotFound = false, value = "classpath:application.properties")
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, 
DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class TestAutomation{

public static void main(String[] args) {
    System.out.println("&&&&&&&&&&&&&&&&&&&&&");
    SpringApplication.run(TestAutomation.class, args);
    System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%");
}
}

I have placed chromedriver.exe under src\main\resources. If I am executing TestAutomation class from eclipse by doing right click everything is working fine.

But If I generate jar file by mvn package command and executing JAR file I am getting below error.

Not able to load Chrome web browser class path resource [chromedriver.exe] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/user/automation/target/automationapp-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/chromedriver.exe
GD_Java
  • 1,359
  • 6
  • 24
  • 42

2 Answers2

1

resource.getFile() expects the resource itself to be available on the file system, i.e. it can't be nested inside a jar file. In such cases, resource.getInputStream() will work. You need to modify your code, because this line of your code System.setProperty("webdriver.chrome.driver", resource.getFile().getPath()); will not work if you're trying to load a resource and package it in a jar using org.springframework.core.io.Resource.

Refer to this : Classpath resource not found when running as jar.

Abhijay
  • 278
  • 1
  • 8
0

Try to use ResourceLoader;

@Autowired
ResourceLoader resourceLoader;

...
Resource resource = resourceLoader.getResource("classpath:chromedriver.exe")
...
Alexey.S
  • 129
  • 4