WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
Using this code to take snapshots using selenium web-driver. This code only stores snaphots locally on my PC. If i want to run it automatically from Jenkins, is there any way to store that snapshots somewhere else so that if anyone runs it either though Jenkins or locally from their PC, they don't have to change the link(("c:\tmp\screenshot.png") every time.
You could make the location of the output file something that it controlled by a setting - either a command-line argument to the tool running this code (if you can modify that), or an environment variable that you can read from your section of code above. You could also have a default location that always exists and should be writable like the user's home directory, rather than an absolute path to c:\tmp.
In jenkins, I would have a step (in an ant script, shell script, or whatnot) create a folder called "screenshots" below $WORKSPACE, and then tell the tool that's going to run your code about that location by one of the methods suggested above. This will also be handy if you want to make the screenshots part of the job's output.
Also, unless you really only ever need the latest file (or have downstream code consuming the screenshot and expecting a specific name), I would introduce a timestamp or some other variable file-naming for the png in the code above, e.g. screenshot-2014-05-16_12-15-37.png, so that if you run the tool twice it doesn't just overwrite the file that was there before.
hth
Related
I know there are 2 ways of writing a pathway for a file while reading file in JAVA.
//1st way
scanner = new Scanner(new File("C:\\Users\\User\\IdeaProjects\\EDSS\\src\\file\\JobList.txt"));
//2nd way
scanner = new Scanner(new File("src/file/JobList.txt"));
But when I try to convert my format from .java to .exe(Application) with the 1st way in IntelliJ via something called "Build Artifacts", I can open the application smoothly. However, with 2nd way, I cannot open the Application and I found out that it is because of the pathway. And 1st way only allow me to run the Application in my computer only. If I copy the file to my friend's laptop with different username, for example ("C:\Users\Bernard..."), the application cannot work too and I have to manually modify the pathway in the codes to run the Application.
Is there any way to search for a pathway accurately without manually type the full length out and at the same time, the Application (.exe) can work with it?
You run into the problems with absolute and relative paths.
Your first option is an absolute path. which means you will always grab the file in a particular location on the computer but if that location does not exist you can't find it
Your second option uses relative path which will look for a file based on the starting location of your program so an exe might start from a different location then the command line you build java from.
The solution is to use relative paths and to make sure the Joblist.txt file is always in one location relative to your run location.
If you want to know where the starting path of your program is you can run the following code:
System.out.println(new File(".").getAbsolutePath())
So I have this code that works fine, it launch the .jar file from another machine that I have configure in my pc as a red ubication
Runtime.getRuntime().exec("java -jar Z:\\AAA\\BBB\\CCC\\ZZZ.jar");
But now I want to launch the .jar from that external path without using the shortcut before (so I can launch it with this code in a machine that dont have that red ubication configured)
Runtime.getRuntime().exec("java -jar MMM\\NNN LLL\\OOO\\AAA\\BBB\\CCC\\ZZZ.jar");
But doent work (I can access and open the file manually without a problem).
When I enter the java -jar MMM\\NNN LLL\\OOO\\AAA\\BBB\\CCC\\ZZZ.jar in the Command prompt it return me Error: Unable to access jarfile MMM\NNN, so perhaps one problem is that the path have a space in the folder name, but I think that may be something else too.
The question is, if the problem is the space, how I can solve it? I cant find a way. And in the other hand, how I can run it in another machine? I have to use that red ubication IP in some way instead?
PD: Using this code, it return me true
File f = new File("\\\\MMM\\NNN LLL\\OOO\\ZZZ.jar");
System.out.println(f.exists()); //--> true
So looks like the spaces dont interfere in the path (the four "\" doesnt seem to do anything in the tests when launching)
I have heard other people having such problems. The main reason for that is that probably Java exec method is not network (SMB) aware. So it doesn't even try to open from the network.
Anyway running the code like that from the network might not be the best solution. First of all the network might be unavailable, or the java file coming might be corrupted. If you want to do it properly you have several options:
Simple option that can work:
Create a bat file that works and exec that one - you can even copy the file locally first to make sure it is available first (if it is big and the network fails)
A better solution :
Use java to copy the file to the working directory and execute it from there.
A plus to do it like that by downloading is that you can maintain a version of the file (hash?) so you don't download it if it is the same. Or you can have fallback - execute the last downloaded version if the network drive is unavailable.
Use a local maven repository and dependency for that jar :)
This way it will keep the version of the jar locally and won't have to download it every time. It will also download a new version if available and the code will be more mainstream (for example not platform / pc dependent)
The answer give by #MadProgrammer works fine!
ProcessBuilder builder = new ProcessBuilder("java", "-jar", "MMM\\NNN LLL\\OOO\\AAA\\BBB\\CCC\\ZZZ.jar");
try {
builder.start();
} catch (IOException e) {
e.printStackTrace();
}
Lot of thanks! In any case going to check the ideas posted by Veselin Davidov
I have developed an executor (jar file) for my automation framework, this executor connects to a remote machine and executes my script written in eclipse and gets the results back to my local machine. Right now I have to mention the name of file and it's path before I trigger the executor in eclipse using Ctrl+F11
Modification:
I want it to run the script which is open in eclipse when I run my executor using Ctrl+F11, e.g. I open myscript.txt in eclipse and hit run and it should execute myscript.txt.
For this I would need the name of file which is currently open in eclipse.
NOTE:
I have not developed a plugin it is just a simple core Java code.
I have tried the below code:
if(PlatformUI.isWorkbenchRunning()) {
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
String name = activePage.getActiveEditor().getEditorInput().getName();
System.out.println(name);
}
else
System.out.println("Work Bench Does not exist");
But it always returns me that Work Bench does not exist.
I am new to it, please let me know if something does not make sense here.
-Thanks in advance
When you run an application via a run configuration Eclipse starts an own javaw process for that. Therefore you cannot access the environment of Eclipse from inside that process in the way you tried.
There might be a simpler solution to your problem:
Make your application expect the opened file to be passed as commandline argument to its main method.
Enter ${resource_loc} in the "program arguments" section of the run configuration. ${resource_loc} translates into the absolute file system path of the selected resource.
According to the Java Info.plist Key Reference for Mac, you're supposed to be able to use the $APP_PACKAGE variable to be able to access the root directory of a Mac Application bundle. So I figure that I can store a file in the Contents/Resources/ folder and access it by calling
new File("$APP_PACKAGE/Contents/Resources/MyFile.txt")
However, this doesn't seem to work and I simply get a file not found error. Moreover, I've tried the following to see if I could narrow down the problem:
new File("$APP_PACKAGE/Contents").exists() //Returns false
System.out.printline(new File("$APP_PACKAGE").getParent()) //Returns an empty string
I did generate the Mac OS X bundle using Eclipse's Export to Mac OS X application bundle, if that matters. Any help (or a suitable workaround) would be greatly appreciated!
I figured it out! Even though the app had a hard time telling me what the working directory is, I managed to figure out that it is the folder that the application is in. Then I was able to solve it by referring to the app as a directory:
new File("MyApp.app/Contents/Resources/MyFile.txt").exists() //Returns true!!!
I'd also like to add that I found the suggestion on this blog post to add the following to my info.plist file:
<key>WorkingDirectory</key>
<string>$APP_PACKAGE/Contents/Resources</string>
Unfortunately, this doesn't seem to work on Mac OS 10.7.5 and the working directory just gets reset to the folder that the application is in.
If the value is set as as an environment variable, you may use System.getEnv() to first get the actual value of APP_PACKAGE then create the File object using it.
String appPackage = System.getEnv("APP_PACKAGE");
new File(appPackage + "/Contents/Resources/MyFile.txt");
That said, from the reading of your given link I wonder if this variable is just expended while read from the plist file, not in your Java process. If the variable is not actually given as an environment variable to your Java program, you can retrieve your file in the Contents directory easily since as I remember the packaging of an app on Mac should have the following architecture
/Contents
/MacOS
YourBinary
/Resources
YourFile.txt
Depending on the current working directory of your app (I think it should default to /Contents/MacOS) you can retrieve the correct path using ../Resources/YourFile.txt. If you don't know the current working directory you can print the value of new File(".").getAbsolutePath()
I am trying to use IM4J (a Java wrapper for ImageMagick) to create thumbnails of JPEGs and it is my first experience (ever) with both libraries. Please note that this is a hard requirement handed to me by my tech lead (so please don't suggest to use anything other than an IM4J/ImageMagick) solution - my hands are tied on the technology choice here!
I am getting a FileNotFoundException on the and convert command which tells me I don't have one of these libraries (or both) setup correctly.
On my computer, here is my directory structure:
C:/
myApp/
images/ --> where all of my JPEGs are
thumbnails/ --> where I want ImageMagick to send the converted thumbnails to
imageMagickHome/ --> Where I downloaded the DLL to
ImageMagick-6.7.6-1-Q16-windows-dll.exe
...
In my Java project, I make sure that the IM4J JAR (im4java-1.2.0.jar) is on the classpath at runtime. Although I am required to use the 1.2.0 version of IM4J, I have the liberty to use any version of ImageMagick that I want. I simply chose this version because it seemed like the most current/stable version for my Windows 7 (32-bit) machine. If I should use a different version, please send me a link to it from the ImageMagick downloads page in your answer!
As for ImageMagick, I just downloaded that EXE from here and placed it in the folder mentioned above - I didn't do any installation, wizard, MSI, environment variable configuration, etc.
Then, in my Java code:
// In my driver...
File currentFile = new File("C:/myApp/images/test.jpg"); --> exists and is sitting at this location
File thumbFile = new File("C:/myApp/thumbnails/test-thumb.jpg"); --> doesnt exist yet! (destination file)
Thumbnailer myThumbnailer = new Thumbnailer();
myThumbnailer.generateThumbnail(currentFile, thumbFile);
// Then the Thumbnailer:
public class Thumbnailer
{
// ... omitted for brevity
public void generateThumbnail(File originalFile, File thumbnailFile)
{
// Reads appConfig.xml from classpath, validates it against a schema,
// and reads the contents of an element called <imPath> into this
// method's return value. See below
String imPath = getIMPathFromAppConfigFile();
org.im4java.core.IMOperation op = new Operation();
op.colorspace(this.colorSpace);
op.addImage(originalFile.getAbsolutePath());
op.flatten();
op.addImage(thumbnailFile.getAbsolutePath());
ConvertCmd cmd = new ConvertCmd();
cmd.setSearchPath(imPath);
// This next line is what throws the FileNotFoundException
cmd.run(op);
}
}
The section of my appConfig.xml file that contains the imPath:
<imPath>C:/myApp/imageMagickHome</imPath>
Please note - if this appConfig.xml is not well-formed, our schema validator will catch it. Since we are not getting schema validation errors, we can rule this out as a culprit. However, notice my file path delimiters; they are all forward slashes. I did this because I was told that, on Windows systems, the forward slash is treated the same as a *nix backslash, in reference to file paths. Believe it or not, we are developing on Windows
machines, but deploying to linux servers, so this was my solution (again, not my call!).
IM4J even acknowledges that Windows users can have trouble sometimes and explains in this article that Windows developers might have to set an IM4JAVA_TOOLPATH env var to get this library to work. I tried this suggestion, created a new System-wide environmental variable of the same name and set its value to C:\myApp\imageMagickHome. Still no difference. But notice here I am using backslashes. This is because this env var is local to my machine, whereas the appConfig.xml is a config descriptor that gets deployed to the linux servers.
From what I can tell, the culprit is probably one (or more) of the following:
I didn't "install" the ImageMagick EXE correctly and should have used an installer/MSI; or I need to add some other environmental variables for ImageMagick (not IM4J) itself
Perhaps I still don't have IM4J configured correctly and need to add more environmental variables
Could be the Windows/*nix "/" vs. "" issue from my appConfig.xml file as mentioned above
I'm also perplexed as to why I'm getting a FileNotFoundException on a file named "convert":
java.io.FileNotFoundException: convert
I assume this is a batch/shell file living somewhere inside the IM4J jar (since the only thing I downloaded for ImageMagick was the EXE). However, if I extract the IM4J jar I only see classes inside of it. I see "script generator" classes, so I assume these kick off before my cmd.run(op) call and create the convert file, and maybe that's what I'm missing (perhaps I need to manually kick off one of these generators, like CmdScriptGenerator prior to executing my Thumbnailer methods. . Or, maybe my download is incomplete.
Either way, I'm just not versed enough with either library to know where to start.
Thanks for any help with this.
Run the 'ImageMagick-6.7.6-1-Q16-windows-dll.exe' installer first to install the imagemagick libraries. Then make sure your environment path includes the location of the installed binaries ('convert.exe', 'mogrify.exe', etc)
Make sure u have Set the environment-variable IM4JAVA_TOOLPATH.