How selenium can test if it has read access to a file - java

Our test-app runs on multiple Virtual Machines through Selenium Remote Control.
The App sits on a test controller Server.
The test-app is used to test a third party online application.
How can I test to see if on certain VM Selenium-RC has read access to a file or folder.
Is there anything like file.canRead(filepath) kind of thing for selenium too?
Before you respond:
File's canRead(filepath) will only test if the file is readable from a test controller server, not able to say anything if it is readable on VM where actual browsers are opening(testing) third-party-online-application.
Basically, I want to upload some file to the third-party-online-application through selenium.
Before doing an upload, I want to make sure that the file is available for upload (on VMs).

A solution would be to create a download link in the application and then attempt to download the file via Selenium. That way, you get a user-representative experience.
If you want to be really fancy, have the Application create a file with the current date and then let the test download the file (simple text file) and check if the file contains the date. Then you test application writing a file and user reading the file, which covers access rights as well.

Which scripting language you are using? If assuming that your file to upload resides under "./data" directory then in java you can check with following steps
File file = new File("./data/myfile.ext");
boolean canUpload = file.exists() && file.canRead();
String fileToUpload = file.getCanonicalPath(); //file name with full path

File file = new File("Folder_Location"); // Folder path if file name not known
boolean canUpload = file.listFiles()[index].canRead();
Note : For latest downloaded file use
int size=file.listFiles().length-1;
boolean canUpload = file.listFiles()[size].canRead();

Related

How can I make my java code machine independent

I have written a very small java code on Eclipse which will automate a small process of logging into a web system. The employees of my company use this web system to connect to office network if they are working from home.
I have converted my java project on Eclipse into an exe file, my intention is to log into that system by just a double click on the exe file.I have parameterized the userID and password and have stored it in an excel file on my local machine.
The problem am having is, My exe file will not run in any other systems except mine as my code is referring to the excel file(which has userID and password) path on my local machine. I would greatly appreciate the developers on this forum who could help me out to come up with a solution for this problem.
What about looking for the excel file in a well defined folder like C:\Users\\my-tool\credentials.xls. Or maybe look for it in the same dir as the executable?
You can get the path of the home folder of the current user with this command:
String homeDir = System.getProperty("user.home");
With that you cann assemble your custom lookup path:
Path xlsPath = Paths.get(System.getProperty("user.home"))
.resolve("my-tool")
.resolve("credentials.xls");

java desktop and web application using the same filepath

I wrote a desktop java application with a class (say ClassA) that reads the content of a file, processes it and returns some results. The filename was specified relative to the project using
File input = new File("config.xml");
Now, I want to upgrade the project into a web project. I wrote a servlet which calls the same java class (i.e. ClassA) for reading the content of the same file but this time I get an error message saying file not found.
How do I refactor my code so that both the desktop and the web versions run smoothly.
Just copy the file config.xml into the proper location on the web server e.g. public_html/www/
The "working directory" of a web application is different - depends on the configuration of the web server you are deploying it to.
If you read a file without specifying a path, it is read from the current dir, which you can access with System.getProperty("user.dir");
So you can try to find out what value is returned by System.getProperty("user.dir") in your web app and place the file there.
But this may differ depending on the environment and servlet server (Tomcat etc.) and may be not a reliable solution.
Another way is to change your code, so it reads the file from the user.home directory and place the file there.

Passing a .JAR program (ran from terminal) a text file as input

I have created a java program that other testers will use to help with their testing. I will be sending them a zip file with the .jar, a readme.txt, and main.properties.txt file.
The main.properties.txt file is a template for the testers to input their DB access credentials. They will update the main.properties file with their db cred's and then attempt to run the .jar from the terminal or command line. The issue I am running into is this. My program needs this updated main.properties.txt file so it can create the connections to our DB's.
What instructions do I need to give in my readme so my program can successfully find the main.properties.txt? Does the main.properties need to be in the same directory as the .jar? Can the testers just create a file on their desktop or documents folders to put the .jar and main.props?
The other question I have is how do I pass this file to my program once its ran from the terminal? Currently it is really easy, because the main.props is part of my program and I can just do something like
Properties prop = new Properties();
FileInputStream in = new FileInputStream("src/main/resources/main.properties");
prop.load(in);
in.close();
But now main.properties is not part of the project anymore. I don't know how to change the code above so that it can find the text from a directory on the local. The location in which they wish to put their main.properties is out of my control so writing a static path will not work. Please help!
There are many ways, I'll show you two.
You need a File object that points to the main.properties file. Then you create a stream on this object new FileInputStream(File) , as you already did by using a String.
The problem of course is to get a relative path to main.properties.txt which works on all systems, regardless where the jar-File is located.
1. Desktop
In this case the main.properties.txt is located at the users desktop. Here is how you access it:
File desktop = new File(System.getProperty("user.home"), "Desktop");
File target = new File(desktop, "main.properties.txt");
Alernativly, if you plan to distribute configuration and property files that do not require user interaction, you may want to use locations like Temp or Documents (Windows).
2. Relative to the jar
Probably one of your best options. Assume the target is in the same folder than the jar-File (or at least in a fix structure relative to the jar). Here is how you access it (related question: how-to-get-the-path-of-a-running-jar-file):
CodeSource codeSource = YourMainClass.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
File jarDir = jarFile.getParentFile();
File target = new File(jarDir, "main.properties.txt");

Not able to read file in jenkins using java code

I am able to read a file using java,File is in eclipse workspace.
My file location is:src\test\resources
So,I have given like this.
String filePath = "\src\test\resources\ARImport_Copy3.csv";
This is working fine but when i am running in jenkins ,I am getting not able to load file messages.
Please provide me the solution.
Reading file directly from filesystem is not flexible. You can't control what working directory would be when executed by different tools in different environments.
It is more reliable to read a resource file from classpath like this:
URL url = this.getClass().getResource("/ARImport_Copy3.csv");
File testFile = new File(url.getFile());
A broader discussion you will find here: Easy way to get a test file into JUnit

How to refer a file system in Cloudbess?

I'm new to Cloudbees. I just opened an account and trying to upload a .JAR file which basically downloads a file to the location mentioned by user (through java command line arguments). So far I have run the .JAR in my local. So far, I was referring to my local file system to save the file. If I deploy my .JAR file to Cloudbees SDK, where can I save the downloaded file (and then process it).
NOTE: I know this is not a new requirement in java if we deploy the jar in UNIX/WINDOWS OS where we can refer the file system w.r.t to home directory.
Edit#1:
I've seen couple of discussions about the same topic.
link#1
link#2
Everywhere they are talking about the ephemeral (temporary) file system which we can access through System.getProperty("java.io.tempDir"). But I'm getting null when I access java.io.tempDir. Did anyone manage to save files temporarily using this tempDir?
You can upload a jar with the java stack specifying the class and classpath (http://developer.cloudbees.com/bin/view/RUN/Java+Container)
Our filesystem however is not persistent, so if you are talking about saving a file from within your application, you could save it in this path
System.getProperty("java.io.tmpdir")
but it will be gone when your application hibernates, scales-up/down or is redeployed.
If you want a persistent way to store file/images, you can use AmazonS3 from your cloudbees application: uploading your files there will ensure their persistence.
You can find an example of how to do that in this clickstart:
http://developer-blog.cloudbees.com/search?q=amazon+s3
More information here: https://wiki.cloudbees.com/bin/view/RUN/File+system+access

Categories