Selenium with Java
These past few day, I’ve been getting familiar with Selenium; a web testing tool that uses scripts to run tests directly inside a browser. During my training, I’ve had some challenges because of connectivity issues to the companies servers or some kind of admin requirement that I don’t have access to. Quite frankly, I’m still training and I’m pretty impatient so I figured I’d just set up my environment on my personal laptop. I’m suppose to use eclipse as my IDE but I’m going to be very honest here, I hate the way eclipse looks so we’re going to hop on back to vscode.
Install the Java Extension Pack
- Inside of Vscode, if you press (shift + command + x) you’ll open up the marketplace and inside of that, you’ll want to look for an extension called ‘Java Extension Pack’ and install.
- Great, now create a new Java project. Under your explorer, you should have a “Java Folder” with a button that says ‘Create Java Project’. Click that and use the ‘stand alone’ option. You’ll be asked to name your project and now you have your basic setup.
Set Up Selenium
- Download the latest stable Selenium WebDriver for Java and unzip it.
- Inside the folder you’ll find a few ‘jar’ files. You’ll need these as your project dependencies to your project so let’s add them.
- Under you Java folder, you’ll find a path called “Referenced Libraries” and next to it, there will be a “+”. Hit that and import all those jar files from the selenium website you downloaded earlier.
Set Up Drivers
So you will need to specify the browser you’ll be using for testing. It can be IE, Chrome, Edge, whatever it maybe and they all have a unique driver. I will be using the Chrome Driver for this setup.
- Download the ChromeDriver that matches your version of Chrome. I like to save it on to my desktop just for ease of access.
- Now, configure the ChromeDriver to be discoverable by your projects WebDriver.
To make it discoverable, on line 7 of the code above, I use the path to the Chrome Driver executable. Now to check that all is working well, just create a driver, test it on google.com and quit after. When you run this, you should see a chrome window running google.com that shortly closes after a few seconds.
Now you may encounter some issues where your mac does not want to run the executable because it may be ‘malware’. For this, just head into you System Preferences > Security and Privacy> General and allow it under the ‘Allow apps downloaded from’ section. Once that’s done, you should be good to roll.