How to use Gecko driver in Dynamic Web Application - java

I am using Selenium to crawl a website using Java and Gecko driver. When I run the project on a local Tomcat v.9 server I am able to get the path of the Gecko driver directly. But what I want is to be able to run the Gecko exe from within the project folder. The image below shows the project hierarchy. I have added the exe in the "resources" folder under "src". How do I get the path of the driver to use it in my code?
Project hierarchy

Turns out it was pretty easy. I noticed that Catalina base has a similar directory to where WEB-INF is. So I did the following;
String catalinaBase= System.getProperty("catalina.base");
so the output is;
C:\Users\<user>\EE
Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1
Which means I would have to keep this Path up until the ".metadata" part (excluding it).
catalinaBase = catalinaBase.split(".metadata")[0];
I then excluded anything after the "EE Workspace" by splitting the Path String.
mainPath = catalinaBase + "\\Crawler\\src\\main\\webapp\\WEB-INF\\geckodriver.exe";
I concatenated the Path String I got from step no.2 and the rest of the path I know is the same.
System.setProperty("webdriver.gecko.driver",mainPath);
Last but not least I set the system property for gecko driver using the final Path String I created on step 3.
Bear in mind. I am unaware if this is working when you extract the WAR file. For a local Tomcat server it will work.
If someone can let me know if this works when a WAR is exported and
deployed on a real Tomcat server I would appreciate it.

Related

chrome and firefox webdriver can not be found under src/main/resources?

I am using Netbeans 8.2 and i am trying to put the chrome and firefox webdrivers under src/main/resources, so that my teat can be run from any other PC, but when i set the system Property to point to src/main/resources/chrome/chromedriver, it gives me the error The driver executable does not exist: /src/main/resources/chromedriver.
But when i put the full path to the webdriver it woks fine like /home/username/NetBeansProjects/projectname/src/main/resources/chrome/chromedriver, the problem is that i want my tests to be runnable on different PCs without any changes, and the first part of the full path must be adjusted from PC to another, but the second part of the path /src/main/resources/chrome/chromedriver that is the one i want to use because that will make my tests runnable in different PCs without any changes in the system property, how can i do that and avoid the above error??
I have searched netbeans documentation but did not find any thing about that, i have also tried the solution in : How to start FireFoxDriver using Selenium 3.4.0 using Maven?
they are saying that it is important to have the drivers under /src/main/resources/ but they did not say any thing about how to avoid the above error
I have also tried How to get the path for .exe file which resides in src/main/resources
But I had the error utilities.Driver.Initialize(Driver.java:34)
thank you for your help in advance,
Try this when specifying your path for the driver
System.getProperty("user.dir")+"/src/main/resources/chromedriver/chromedriver.exe";

How can I resolve Windows vs Linux properties file paths?

I have a java project on a Windows box which builds successfully and all tests pass locally. This project is then checked in and built with Jenkins on a Linux box.
The problem I am having is related to path issues in my properties file which is used for running tests. I point to a resources dir for part of a file path like this: "./src/test/resources".
I am trying to access two files which reside in the same directory. File1 is accessed successfully. Then File2 access is attempted, but it returns a file not found exception.
I've tried using an absolute path like this: "/code/myproject/main/src/test/resources/..." This again works on my local box, but not on Jenkins because Jenkins only knows about" "/myproject/main/src/test/resources/..."
How can this be resolved?
Today I learned Linux is case sensitive. The file name extension is upper case, but I was pointing to a lower case one.
Depending on your version of Windows... Windows 7 (don't know since when this was implemented) understand linux-style / separator just fine, as long as the whole path is relative.

How can I create a file in my project folder through Servlet?

My objective is to create file in one of the folders of my Web-App such that when user clicks a download button, it's gonna download the file from that particular folder. For example, my file resides in "MyProjectName/WEB-INF/NewFolder/myfile.xls". Now, how do I create "myfile.xls" in that folder?
Here's what I have already tried:
I have set the file path to "/WEB-INF/NewFolder/" using this code in my Servlet:
String filePath = getServletContext().getRealPath("/WEB-INF/NewFolder/myfile.xls");
But the problem is, the value of "filePath" in the above code is:
C:\Users\MyUserName\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\MyProjetName\WEB-INF\NewFolder\myfile.xls
Hence "myfile.xls" is not getting created in "/MyProject/WEB-INF/NewFolder" and getting created in the metadata folder instead. Because of this, it's working only in my machine ie., localhost. When I am trying to access my application through another machine I am not able to download the file. I guess I am passing the absolute path to the JSP page which makes it impossible to download it from a different machine. So could anyone please let me know where I have gone wrong in doing this?
Your webapp is deployed to a web server on runtime and filePath points somewhere inside deployment folder not to a project one.
Anyway, why do you want to write into WEB-INF? If you for example redeploy application, then all files inside usually will be removed. Isn't better to use some folder inside server (for example in tomcat we can use ${catalina.home}/myfiles folder or some other outside web server?

Website directory structure different from eclipse

I am coding a website using java servlets and am using eclipse and tomcat. When I test it using localhost, it works fine. But when I am deploying it on my actual website, the directory structure is messed up and the files are not called properly.
My eclipse directory structure on localhost is
Project Name
.src/packageName/java files
.WebContent/HTML files.
When I make a call from the html files, I use the relative location and tomcat automatically knows to look in the src/packageName folder. For example, from the /WebContent/login.html page makes a onClick call as follows,
. This will automatically trigger the java file in /src/packageName/welcome
When I am deploying it in my actual website, the WebContent/login.html is throwing an error WebContent/welcome file is not found. How do I tell my website to search in /src/packageName folder?
Hmm...have you been sure to package the application as a war for deployment.

Source Folder File Java

I'm working on a project where I open a file, overwrite it, and save it as a new file. However, I'm having some difficulties with accessing the template file.
Right now, I believe my program is referencing the file using the path from my computer, however, if I were to export this program to a different computer, it probably couldn't find the file.
I have the file stored in a source folder for the project in eclipse. Is there a correct way to reference the file so that it will be able to be found on any computer?
I've attached an image on how my program is now referencing the file.
If the file is in the project folder, you don't need to specify a path at all. Simply using the filename "file.txt" can suffice where you would have normally put a path.
One alternative can be if you are using Tomcat server for deployment , then place this file in web-apps location of tomcat.
Accessing will be done using below code snippet
String filePath = System.getProperty("catalina.base") + "/web-apps";
This filePath will work on all computers because we set catalina base in all machines installing tomcat.

Categories

Resources