Basic steps to run your first selenium program-1
In this section we will follow 7 step by step instructions to run your first selenium program. Selenium supports multiple language but here I am focusing on selenium with java. So for that you need to have java in your machine before you start writing an automation scripts.
You can go through the video or use git repository for more details.
First lets get java into our machines.
1. Java installation
Go to your browser and simply type java jdk download. www.oracle.com/technetwork/java/javase/downloads/index.html is the web site which you need to navigate to download java.
You have to download jdk file based on your operating system. Once you download .exe you can install it.
After installing your jdk file, you can check where exactly your java file got stored. For that you can got to C drive and select program files and if you see java folder there that means java is successfully stored in your machine.
2. Configure java path in system variables
Next step is configuring java path in system variables.
Why do we need to configure java path in system variables ?
When you run any program, that program first talk to the system variables and see that related software is present. When you run your selenium program, it find the java software in your machine. By default selenium doesn’t have knowledge to find the place where exactly java located in your machine, so it directly comes to your system variables and read the system variables and identify the place where java is located, go to that specific path and execute java .exe file to run your selenium test.
Go inside the jdk folder and copy the home path. So to set the variables, go to Control Panel and in the control panel go to Advance system settings, this will navigate you to system properties and go to Environment Variables.
Now you need to create new system variable and tell where java is located. So you can click on new and that will provide you a new system variable. Then type JAVA_HOME in variable name and carefully pass the home path and click on Okay.
Apart form above system variable there is one more system variable. Go to bin directory, copy the bin path and select the path variable which is available under system variables. Select and edit the path, add bin path and click on Okay.
Lets see whether you have successfully configured java in your computer. Go to command prompt and type java -version. Then you will get some information like this.
java version “1.8.0_181”
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode).
3. Eclipse installation and create new eclipse project
Go to eclipse web site and download eclipse to your machine. If it in zip folder just unzip it and install it.
Go to File > New and select Project > Java project.Then you can see a popup where you can give a Project name and click on Finish. SRC is the folder which automatically created with the project. You can see that under package explorer, project is created.
Project is a test suite which can store collection of test cases. To write selenium we are using java. You can write java code using java class file. Java class file is a test case.
Right on source folder New and go to Class and give a Name and select the option “public static void main”, because writing any java, that part is common and click on Finish.
4. Download and configure selenium jar files
Now you have a dummy template to write your selenium code.
Next you need to download selenium and create connect between selenium and your project, then project will recognize selenium and execute your test case.
You can download selenium from https://www.seleniumhq.org/download/ selenium official website. You can download java client libraries available in the site.
Once you open the selenium folder you can see two executable jar files, when you open lib folder there are few more jar files present. These jar files are important to run our selenium test cases.
Right click the project and go to Properties, select Properties and click Java Build Path and come to the Libraries tab. Click on Add External JARs then reach out the folder and select the two jar and click on open.Also import the jars available in lib folder.
5. Chose browser to run
Go to java doc in selenium and select web driver, there you can see ChromeDriver, EdgeDriver, FirefoxDriver etc. They are the classes you will use if you want to automate your test cases on chrome browser, internet explorer or in Firefox driver.
6. Create driver object based on your browser
First, we need to create a driver object .You have to do it as step one before you write any code.
This drive object, drive all your automation. Driver object is is specific to each and every browser object. If your willing to automate using chrome browser, then you can create drive object based on chrome browser.
When you move your cursor on ChromeDriver you can see an import link, also you can see an error. Once you get the link import, you can see something will return on to the top. Package in the top came through your selenium jars (which you added to your build path).
Above step is not sufficient to invoke chrome browser in to your machine, because the third party browsers which not belong to selenium. These third party browsers restricted selenium to invoke their browsers automatically.Due to some securities issues third party browsers will not allow any external APIs to invoke their browsers automatically.So for that reason, we need to write one more step to invoke those third party browser.
Each and every browser gives you an .exe file to selenium and ask selenium team to invoke that .exe file first.
7. Set system property of browser and run your first program
To invoke browser you need to add one more step before the driver object class. In selenium official web site , you can see “Third Party Drivers, Bindings, and Plugin” which are not developed by seleniumhq. So if your working with chrome you have to download google chrome driver, if your working with Firefox then you have to go with Mozilla Gecko Driver.
Download the .exe file from selenium official website.
Selenium has set some properties from chrome, the property is webdriver.chrome.driver, this property expect the location of chrome driver .exe file.We need to set this property with chrome driver location.
//invoke .exe file
System.setProperty("webdriverchrome.driver","C:\\Chromedriver.exe");
When you run first it go and identify chrome driver store in your machine and it invoke that file. Once that file is invoked, the browser will get open.
Type driver. , you will get all the methods which web driver support.
//Implement the method of web drive
//Go to the url
driver.get("https://www.findmyfare.com/");
In this argument you need to pass the url which you want to hit on the browser.Right click and run the java application, then you will see chrome browser successfully open and hit “https://www.findmyfare.com/”.
If you want to get the title of the browser, type
//Get the title and print it
driver.getTitle();
If you want to see, the title as an output you can simply say
System.out.println(driver.getTitle());
Once you create the driver object, whatever the browser that you use, you can simply copy paste and need not to change anything because the first test you write defines which browser you execute.So you need to change “ChromeDriver” in to “Firefox” web driver.
Similar to chrome class, there should be a class for Firefox and provide object name as driver or you can give any name you want.
That’s all you want to do ,just change the name of the class. If it is internet explorer just use that class.
Import the package “firefox Driver” and download .exe file. The file you need to download is the geko driver.
If you want to get page source, there is one method which you can retrieve the code from browser when you landed.
driver.getPageSource();
If you want to see it visually in the console you can write :
//Print page source
System.out.println(driver.getPageSource());
If you want to move back from one browser to other :
//Navigate back
driver.navigate().back();
If you want to navigate forward:
//Navigate forward
driver.navigate().forward();
If you want to close opening browser you can close them using a simple code :
close will close the current browser.
//Close current browser
driver.close();
Quit will close all the browsers open by selenium script.
//Close all the browsers
driver.quit();
Until we meet again…………Happy coding!!!