Due to a project requirement I had to move from Selenium 2.0 to Watir and Cucumber framework. Earlier we were using Java and often set the default file path as:
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"/browsers/chromedriver.exe");
Now I am trying to set up something similar in Ruby also, but being new to Ruby, I'm failing to get the root directory of the project. It only gets the path of the file which the current user is working using __FILE__ or Dir.pwd.
My Watir project structure is like this:
root
-config
-features
-pages
-step_definitions
-support
-hooks.rb
-browsers
...
...
I want to specify in hooks.rb that if the parameter passed is "Chrome", then run the Chrome browser, and when it is "FF" run the Firefox browser, but it always gives me the current working directory path and I need the root directory.
I am on WINDOWS 7, Using Ruby version 1.9.3p551.
Ruby's File class has several different methods that are useful for what you're trying to do, so read the documentation for absolute_path, expand_path, realdirpath and realpath.
I'd recommend starting with absolute_path:
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point. If the given pathname starts with a “~” it is NOT expanded, it is treated as a normal directory name.
I built the path from your hooks.rb to the root directory in my tests directory on my Desktop, and added this code to hooks.rb:
PATH_TO_ROOT = File.absolute_path('../..', File.dirname(__FILE__))
Running it shows:
PATH_TO_ROOT # => "/Users/ttm/Desktop/tests/ruby/root"
You'll always have to maintain the '../..' relative string but that is easier than hard-wiring your code with a fixed absolute path.
I tend to put code similar to this in any project that has multiple directories, especially when I'm calling library files. Write your code correctly, and you can do it once assigning the value to a constant, and you can then reference that constant wherever you need to know the absolute path to the installation.
book = Roo::Excel.new(File.join(File.absolute_path('../..', File.dirname(__FILE__)),"config/data/test.xls"))
It's a lot easier than that:
path_to_book = File.absolute_path('../../config/data/test.xls', File.dirname(__FILE__))
path_to_book # => "/Users/ttm/Desktop/tests/ruby/root/config/data/test.xls"
File.dirname(__FILE__) is the anchor for your relative path. Simply define the relative path to the file you want and let Ruby do the rest.
"Relative path to your project directory" talks about relative pathing in Ruby.
The answer to your question is going to be specific to the code that you are trying to use in your hooks.rb. If you could post a sample of what code you are using for this from your hooks.rb, that would be great. But the answer to your question is likely going to be some combination of the following:
File.join(File.dirname(__FILE__), '../../')
Where the first parameter is your current directory (root\features\support) and the second parameter is going to be the relative path to your root directory ('../../').
Related
I am writing a library that contains references to a few non-java binary files. (The java library is used to make sub-proc calls to these).
I has specified the path to the folder containing these files using this line of code:
String binaryFolder = System.getProperty("user.dir") + "/externalbinaries/";
Of course this path does not hold when the library is included in another java application.
Is there a way to specify the path relative to the project root?
I have seen answers using the classloader:
binaryFolder = classLoader.getResource("bin/ext/").getPath();
but this path to the resource folder also does not hold when imported as a .jar library in an application.
I am using java 1.8.
Any ideas?
what you describe with the user.dir is not really a relative path, because the result of that statement is actually an absolute path, but it is not a "fixed" path because the user working directory can change, i assume thats what you meant with "relative"?
what about user.home instead, that should be a fixed path?
I know there were several similar questions, however, examples in them don't make things clear or I can't make profit of them - Shame on me.
So my problem is with loading images in simple app with GUI.
e.g.:
I got images in "D:\javaeclipseprog\Graphics\src\images", class and java files in "D:\javaeclipseprog\Graphics\src\app"
When I use direct path: "D:/javaeclipseprog/Graphics/src/images/icon.jpg" everything works, but as good practice I would like to get them from relative path, which as far as I know should be: "./images/icon.jpg".
Unfortunately it doesn't work.
Any help appreciated, thanks in advance.
When you are running it in eclipse, your default working directory in the project directory. That is the directory where srcis located in. In your example the project directory is:
D:/javaeclipseprog/Graphics
Therefore the correct path is:
./src/images/trophy.png
Edit: Just want to add that you could also load a file via a path relative to the class location by using the getResource method.
../../images/icon.jpg should work fine
You're going two folders up and go straight to the right folder.
Paths
A simple way to check this would be to use the Paths and Path classes and methods.
Path p1 = Paths.get("D:\\javaeclipseprog\\Graphics\\src\\app\\java.class");
Path p2 = Paths.get("..\\..\\images\\icon.jpg");
System.out.println(p1.resolve(p2).normalize()); // D:\javaeclipseprog\Graphics\src\images\icon.jpg
I'll use the inverted slash because it seems that you use windows. In this case .\ indicates that the same directory where the code is, will have the file you want to use. If you want to jump into the father of that directory, the one that contains the source, you'll use ..\
You can even do it more tan once, for example ..\..\ would be a valid path. Try adding quantities of ..\ in order to look for the directory you want. In this chase ..\src\images\icon.jpg (the parent on a java project is src)
Another important thing is that you're using / instead of \\ that would be the symbol of the directory separator on windows (\ is an special char that must be scaped using an aditional \) For portability i'd use:
String sep = System.getProperty("file.separator");
String path = ".."+sep+"src"+sep+"images"+sep+"icon.jpg"
I think what you need to use is "../images/icon.jpg".
Using it like you have it will look in the current directory, which unless I'm understanding it incorrectly, is "D:\javaeclipseprog\Graphics\src\app".
I want to use the relative path in xml files in our project. I have the files in the following location.
D:/SDC-Builds/SRDM2.3.0/SRDM/Svr/IdP/IdPserver/conf/attribute-r.xml
I have other xml file which needs to ref the above location, I use the following relative path to be independent of machines and folder names.
In D:/SDC-Builds/SRDM2.3.0/SRDM/Svr/IdP/IdPserver/others/service.xml, i am using the code like below
service.xml
<srv:ConfigurationResource="../../../../../../IdP/IdPserver/conf/attribute-r.xml">
</srv>
Please tell me am i using proper convention to refer the attribute-r.xml ?
If your Project Root Directory is SRDM, then you need to get back from your executable path.
Say you have your exe file at SRDM/Svr/bin/EXECUTABLE.exe then,
you need to mention in xml as
<srv:ConfigurationResource="../IdP/IdPserver/conf/attribute-r.xml"></srv>
ie. Current working directory is SRDM/Svr/bin/ and from that you need to get back up to common junction[Svr] in your case.
I have a folder called WalnutiQ. Inside this folder is is a file at WalnutiQ/train/model/MARK_II/Save.java
Save.java
JsonFileInputOutput.saveObjectToTextFile(myObjectJson,
"Digits.txt");
which works! However, the file Digits.txt is unfortunately saved in WalnutiQ/Digits.txt
How do I save the file Digits.txt at WalnutiQ/train/model/MARK_II/Digits.txt???
I am programming in java in eclipse in windows. I have tried
JsonFileInputOutput.saveObjectToTextFile(myObjectJson,
"/train/model/MARK_II/Digits.txt");
JsonFileInputOutput.saveObjectToTextFile(myObjectJson,
"\\train\\model\\MARK_II\\Digits.txt");
but neither work.
judging from the result you are getting your current directory is pointed at WalnutIQ. You might try using .\train\model\MARK_II\Digits.txt. Windows treats the . (period) as a token for "current directory". Your other attempt would have tried to find the train directory in the root of C because the \ (backslash) is a token for the root (c:). It likely fails because that folder does not exist - unless it created it... might go look :) I don't use Json in eclipse which is why I'm not answer your question with code.
Have you considered using the full absolute file path to the text file, rather than the relative one you are currently using? This may not be practical, depending on how you plan on using your program, but it might be worth a shot.
Depending on how the library saves files (I imagine it's using File behind the scenes), you should be able to supply an absolute path to the location you want it to save to.
JsonFileInputOutput.saveObjectToTextFile(myObjectJson,
"/path/to/WalnutiQ/train/model/MARK_II/Digits.txt");
I'm newbie to java.
I have some directory structure
product/
conf/
classes/com/../..
conf/ contains some configuration file, while under classes/ I have my application.
How can I ensure from inside java code that I'm able to find file in conf/ despite way I'm executing it (e.g. from eclipse, from different directories, from crontab etc.).
P.S.
Files in conf/ are not resources, since required to be edited by user.
Is there're way to know where my .class, so I canuse relative path form that directory to reach my directory (e.g. MY_CLASS_DIR/../../../../conf)
I would put the conf directory into the class path. That way you can always find them by:
YourClass.class.getClassLoader().getResource("conf/....");
You can use the absolute path, including the way to product.
Or you may use a configuration setting, by starting your program like
java -DXY_HOME=/some/path/product ...
From the javacode, you use it:
String xyHome = System.getProperty ("XY_HOME")
Or you use a kind of inifile in your home directory, where you specify where to look for the conf-directory.
Rereading your question multiple times, it is unclear to me what your goal is. To find the conf dir independently from where you are (eclipse, crontab, ...)? But the headline asks for the CWD, which is the opposite - the directory, depending on where you are.
Both is possible, but you have to decide what you want.
Its safe to use relative paths than absolute paths. Even if you JAR your classes tomorrow it will work as is,
Put you configuration files in classpath during deployment.(Please note that
project directory structure can be different from that of deployment directory structure)
product/
classes/com/../..
classes/conf/some_conf.properties
Then you can use Apache common configuration to get the URL of file
URL urlOfFile = org.apache.commons.configuration.
ConfigurationUtils.locate("conf/some_conf.properties");
The other alternative you can try is,
URL urlOfFile = <SomeClassFromClassesFolder>.class.
getClassLoader().getResource(resourceFile);
Once you get the URL of your configuration file getting stream out of it very simple,
InputStream stream = urlOfFile.openStream();
Good luck.
For you understanding you can refer the following as well,
http://bethecoder.com/applications/tutorials/showTutorials.action?tutorialId=Java_IO_CurrentWorkingDirectory
http://bethecoder.com/applications/tutorials/showTutorials.action?tutorialId=Java_Reflection_WheretheClassloadedfrom
Good luck.
you can find out what is the absolute path of the working dir by:
String str = new File("").getAbsolutePath()