Starting with Selenium WebDriver…

As you might be aware that Selenium is used for Test Automation for Web Applications. Selenium is composed of multiple software tools like Selenium IDE, Selenium WebDriver, Selenium Grid etc. We will be concentrating more on Selenium WebDriver as this is very powerful and commonly used for test automation. Selenium WebDriver is also compatible with multiple browsers like Google Chrome, Firefox, IE, Safari etc. I will also share details of Selenium Grid in upcoming posts which is mainly used for running scripts parallely in multiple environments.

You can go to below link and get complete details of Selenium:
Selenium Documentation

Create our first Selenium Script:

Here I will be mainly focusing on creating our first Selenium script using JAVA language and by using Eclipse as our IDE. You can simply follow the step by step approach which I will be taking. They are very easy to follow…

  • First of all open Eclipse and select the default workspace as your working directory. You can change the location if required.
  • Go to File -> New -> Java Project to create a new Java Project. Enter the Project Name as “FirstSeleniumProject”. You can provide any name as your liking.
  • You can see by default the JRE environment is selected. In my case its Java SE 1.8
  • Click Next and Finish to complete the Java Project creation. This is very much same to our last “TestMadness” project creating.
  • Right Click on the “src” folder to create a new package. src -> New -> Package.
  • Enter the package name as “com.selenium.firstscript”. As mentioned earlier, package will create a folder heirarchy ‘com -> selenium -> firstscript’ and store the class file in it. Click Finish to complete the Package creating.
  • Right Click on the Package name and create a new class file. Package -> New -> Class.
  • Name the class as “FirstScript” and select the “public static void main(String[] args)” option. For simplicity we will be creating our first Selenium script inside the main method. When a program starts executing, it has to start execution from somewhere. That somewhere is called main method. Click Finish to complete the class creation.
screen-shot-2017-02-19-at-12-17-25-pm
This how FirstSeleniumProject looks like

Now that we have completed our Java Project creation, lets start creating creating our first Selenium Script inside the main method.

  • Enter the following code “WebDriver driver = new ChromeDriver()” in the main method as  shown below. As you enter the code, you will see an error coming “- WebDriver cannot be resolved to a type”. Don’t worry on the error. This is because we haven’t added the Selenium Jars into our project. We will have to import the Selenium Jars into our project in order to work with Selenium.
Screen Shot 2017-02-19 at 12.27.04 PM.png
Error in our first script

Next steps are to add the Selenium API into our Java Project.

  • Go to Download section of the Selenium.org website and download the Selenium Standalone Server as shown below.

URL: Selenium Download

screen-shot-2017-02-19-at-12-34-34-pm
Click 3.0.1 to download Selenium Standalone Server
  • Also download the Chrome Driver from ‘Third Party Drivers, Bindings, and Plugins’ in the same page. We will be executing our first Selenium script in the Chrome Browser. Chrome Driver is an executable file and it depends on the OS and configuration of the machine. Since I am working on a Mac 64 bit machine, I will be downloading the chromedriver_mac64.zip version.
Screen Shot 2017-02-19 at 12.37.31 PM.png
Click Google Chrome Driver from Third Party Drivers section
Screen Shot 2017-02-19 at 12.43.23 PM.png
Download the appropriate Driver version

Next step is to add the downloaded Jar file into our Java project.

  • Right Click the Java Project name “FirstSeleniumProjects” and select properties.
  • Select the Java Build Path in the Properties pop up.
  • Click on Add External Jars to import the downloaded Selenium Standalone Jar.
  • Go the correct location of the downloaded Selenium Standalone Jar and click OK to finish the import.
Screen Shot 2017-02-19 at 1.02.49 PM.png
Selenium Standalone Server Jar import
  • Once the jar is imported, you can see Referenced Libraries is added in our project. Referenced Libraries contains the Selenium Standalone Server Jar that we imported.
Screen Shot 2017-02-19 at 1.06.06 PM.png
Referenced Libraries
  • Now go to our “FirstScript.java” and click on the ‘WebDriver’ error as displayed. You can see a new option ‘import WebDriver’ is displayed. Click on this option to import all the required packages and classes for writing our first Selenium Script. Eclipse will do the import process. You can see a new code “import org.openqa.selenium.WebDriver” is added in our script.
    Screen Shot 2017-02-19 at 1.10.09 PM.png
    Click import WebDriver

    Similarly we can remove the ChromeDriver error by clicking on the error and importing the necessary packages for Chrome Driver. Both imports are from Selenium Standalone Server Jar that we imported into our project. Save the project and can see all the errors are removed now.

    Screen Shot 2017-02-19 at 1.21.11 PM.png
    Error removed

    Congrats! We started with our first Selenium script. Although this script is not validating anything but its great that we reached till this point. Please note to execute the Selenium script in Chrome browser, the browser need to be installed in your Windows or Mac machine.

    I will continue with the scripting in the next blog. So stay tuned…

    Additional Information:

    What is a WebDriver?

    As you have seen that in our first script we have used the command ‘WebDriver’. Many of you have wondered what WebDriver is actually. WebDriver is an Interface, and we are defining a reference variable (driver) whose type is an interface.Now any object we assign to it must be an instance of a class (For example ChromeDriver) that implements the interface.

    Now obviously the next question comes, What’s an Interface?

    Interface is OOPS concept. It is like a blueprint of Class. It contains variables and body less methods (Abstract methods), where we just declare methods but we implement them inside the class which inherit Interface. It always helps in understanding the Selenium script exercises if you have knowledge in OOPS concept.

     

    What are different types of Drivers?

    As mentioned, different Drivers contains the implementation of WebDriver Interface. Based on the browser requirement, we can use any of the following Drivers:

  • FirefoxDriver – For Firefox browser.
  • InternetExplorerDriver – For IE browser.
  • ChromeDriver – For Chrome browser.
  • SafariDriver – For Safari browser.
  • OperaDriver – For Opera browser.
  • AndroidDriver – For Android devices.
  • IPhoneDriver – For iPhone devices.
  • HtmlUnitDriver – For headless browser. This is a web-browser without a graphical user interface. It will behave just like a browser but will not show any GUI.

 

Learning JAVA…

JAVA is a big topic and my intention here is not to teach JAVA. You can get loads of free materials from internet for learning JAVA. The last couple of posts were just to make you comfortable with the word JAVA and to give a high level idea on the IDE as we will be using both JAVA and IDE in all coming posts.

To work with Selenium, you need to have some understanding on any one of the Object Oriented Programming languages like JAVA, Python, C# etc.. I chose JAVA because JAVA is freely available and you can get lot of tutorial contents and support. Learning the concepts of JAVA is not a big task and I can assure you that to learn a programming language does not require too many skills. It just requires your patience and a desire to learn.

Here I will just list out the topics in Core JAVA that you need to know before comfortably working in Selenium:

OOPs Concepts:

  • Polymorphism
  • Inheritance
  • Abstraction
  • Encapsulation
  • Aggregation
  • Composition
  • Association

JAVA Topics:

  • Datatypes
  • Literals
  • Variables
  • Operators
  • Control Statements
  • Classes
  • Methods
  • Methods Overiding, Overloading
  • Class Inheritance
  • Interfaces, Packages and Access Control
  • Exceptions
  • Strings
  • Collections Framework

JAVA topics doesn’t end here but I think knowledge on the above topics will help you to get started with Selenium Automation.

Install IDE and writing our first JAVA program…

Before going into details, let me give you a brief idea on Integrated Development Environment or IDE. IDE is a software application that has all the basic tools developers need to write and test software. Typically it contains a code editor, a compiler or interpreter and a debugger that the developer accesses through a single graphical user interface or GUI. Eclipse and NetBeans are the popular IDEs that most JAVA developers use and they are free to download. IDEs have lots of features that makes a developers life easy…

I will be using Eclipse as I am familiar with this IDE.

Download Eclipse:

Open Browser and navigate to Google. Type in “Eclipse download” and go to downloads page in eclipse.org website. Select the latest version for Java Enterprise Edition (EE) and download. You have to select correct version based on your Windows or Mac machine. Once download is complete in your local machine, you need to click on eclipse executable file. This will open the Eclipse IDE and a Eclipse Launcher prompt will come asking to confirm on the directory for Workspace.  You can either go with the default location or else you can provide your own location. Workspace is where all your source code will be placed.

screen-shot-2017-02-13-at-1-54-21-pm

Click Ok to open up the main window. The steps are pretty much same for Windows and Mac machine.

Writing a simple program in Eclipse:

  • Now the Eclipse download is complete, lets write our first HelloWorld! program using Java as our programming language and Eclipse as our IDE.
  • Open Eclipse and go to File -> New and select Project option.
  • Select “Java Project” from the wizard and click on Next.
  • Enter Project name as “TestMadness”. You can give your own name to your project.
  • You can see the the default location of your project. Click on Next and then Finish to complete the project creation.
  • By default JAVA Project will have “src” folder which will contain your source code and “JRE System Library” folder which is added by Eclipse IDE automatically on creating Java Projects. JRE System Library implements the JAVA API in Eclipse IDE. So all the predefined functions of Java can be accessed by the Java Programs written in Eclipse IDE because of this JRE System Library.

screen-shot-2017-02-13-at-2-15-43-pm

  • Right click “src” folder and click New -> Package. Enter the package name as “com.selenium.testmadness” and click Finish to create a new package. A package in JAVA can be defined as a grouping of related types like classes, interfaces, enumerations and annotations and providing them access protection and namespace management. Here in this case, folder structure will be created in the form of com -> selenium -> test madness.
  • Once a package is created, our next task is to create a class file. Right click on the package and click New -> Class. Enter the class name as “HelloWorld” and select the option “public static void main(String[] args)” and click on Finish to complete JAVA Class creation.

screen-shot-2017-02-13-at-7-14-33-pm

  • If you  directly go to Workspace location in your machine, you can see a folder structure is automatically created as TestMadness -> src -> com -> selenium ->test madness -> HelloWorld.java.
  • For writing a simple HelloWorld program, add “System.out.println(“HelloWorld!”)” in the public static void main method as shown below. This is a simple program to print “HelloWorld!” in the IDE console.

screen-shot-2017-02-13-at-7-50-15-pm

  • To execute the HelloWorld program, right click HelloWorld.java class file and click run as Java Application. You can see the “HelloWorld!” printed in the console.

Additional Notes:

What is public static void main(String[] args) method?

main method in Java is an standard method which is used by JVM to start execution of any Java program. It is the entry point for core JAVA applications. However you can compile a core JAVA application without a main method. main method is always public that is anyone can see or run it. static means that you can call the function without an instance of the class. void means that the method has no return type. String[] args is an array of command-line arguements and the only formal parameter to the method. Here in our case we are not passing any command line arguments. System.out.println command prints “HelloWorld!” on the screen and then adding a new line after it.

 

 

Writing our first JAVA program…

Now that JDK is installed and Environment Variables are set up in our machine, lets try to create and execute a simple “HelloWorld” program using Command Prompt or Terminal.

  • Using a text editor like Notepad or TextEdit write a simple program and then save the file as “HelloWorld” with .java extension. You can save it in any location.
screen-shot-2017-02-13-at-10-40-51-pm
HelloWorld in text editor
  • Open your Command Prompt or Terminal and go to the location where your .java file is saved.
  • Type in “javac -d . HelloWorld.java” in the Command Prompt or Terminal. Here “javac” refers to JAVA Compile command. As you remember in our previous post, we set our PATH to JDK Bin in our machine. Bin folder contains the executable files for “javac” and “java” commands. “-d” refers to Directory where “.class” files will be stored. When we run “javac” command, the javac compiler compiles your .java source files into .class files, and so that JavaVM can execute (interpret) those files. “.” refers to current working directory and “HelloWorld.java” is the file that we just created.
  • As you see we created a package “com.selenium.test” in our first JAVA program. What is a package? I know many will have this question. A package in JAVA can be defined as a grouping of related types like classes, interfaces, enumerations and annotations and providing them access protection and namespace management. In simple terms, it will store the source file inside a folder structure. Here in this case, folder structure will be created in the form of com -> selenium -> test. When we run this command, system will automatically create a folder structure com -> selenium -> test and store the “.class” file inside the “test” folder.
screen-shot-2017-02-14-at-12-03-16-pm
com folder is created inside HelloWorld folder
  • Once the compilation is complete, next step is to execute the “.class” file. Type in “java com.selenium.test.HelloWorld” in the Command Prompt or Terminal and hit Enter. This time we don’t have to use the file extension. And as you see below, the execution is success.
screen-shot-2017-02-14-at-12-08-42-pm
Hello World!
  • If we are not using any package is our program, then we can simply go to our source folder and run the below commands.

Screen Shot 2017-02-14 at 12.14.23 PM.png

Additional Notes:

Command Prompt is Windows based DOS application and Terminal is Mac Os based Linux application. The purpose is same but the commands are bit different. You can get a complete list of required commands for both Windows and Mac machine from below URL:

DOS Linux Commands

 

 

Setting up Environment Variables…

Once the JDK installation is complete, the next big step is setting the Environment Variables in your machine. Environment Variables setting is different for Windows and Mac machines. I will walk you through in setting up the Environment Variables in both machines.

Before setting up the Environment Variables, let me give you a basic idea on what are Environment Variables?

Environment variables are, in short, variables that describe the environment in which programs run in.Examples of environment variables in an operating system include PATH, HOME etc.

To execute JAVA console based programs in Windows or Mac environment we have to use javac and java commands. These commands are unknown for Windows or Mac till we do not specify explicitly where those executable resides. This is the reason while setting the PATH we specify path of bin folder(bin contains all the binary executable).

Mac OS:

  • Open Terminal in Mac OS similar to command prompt in Window
  • Type in the following command  “vim .bash_profile”. This will open the VIM editor in terminal. bash_profile is a configuration file for bash shell. When .bash_profile is invoked as an interactive login shell it first reads and executes commands from ~/.bash_profile. This file can be used to export variables in shell.
  • Type ‘i’ to insert into the VIM Editor.
  • Type in  “export JAVA_HOME=$(/usr/libexec/java_home)” to set up JAVA_HOME.
  • Type in “export PATH=${JAVA_HOME}/bin:$PATH” to set up PATH.
  • To Save the entires in VIM Editor, hit Escape, type in: “:wq + Enter”. To Quit without saving the editor, hit Escape, type in “:q!”.
  • Type in “source .bash_profile”. This command will save the entries in .bash_profile.
  • Type in “echo $JAVA_HOME” to verify JAVA_HOME Environment Variable.
  • Type in “echo $PATH” to verify PATH Environment Variable.
Screen Shot 2017-02-11 at 9.51.56 PM.png
VIM Editor in Mac

Please ignore M2_HOME as of now. This is for setting up the MAVEN Home which I will explain in future blogs.

Windows OS:

  • Type “advanced system settings” in the search box (beside the Windows start button), clicks View advanced system settings.
  • Select Advance tab, clicks Environment Variables.
  • In System variables, add a new “JAVA_HOME” variable and point it to the JDK installed folder which by default is “C:\Program Files\Java\jdk1.8.0_111”.
  • In System variables, find “PATH”, clicks edit and append this “%JAVA_HOME%\bin” to the end.
  • Type in “echo %JAVA_HOME%” to verify JAVA_HOME Environment Variable.
  • Type in “echo %PATH%” to verify PATH Environment Variable.
Screen Shot 2017-02-11 at 10.10.02 PM.png
Environment Variable Setting in Windows 1o

Now that we have completed the Environment Variable settings in our machine, we can move on to the next topic…

Please note that you need to be an Admin in your machine or else you won’t have the privilege to set the Environment Variables.

Installing Java Development Kit…

Before you start working with Java, the first step is to check whether Java is already installed on your machine or not. There are different ways to check if Java is already installed.

The easiest way is to open the Command Prompt/Terminal in Windows/Mac machine and type in “java -version” and hit enter. If Java is already installed on your machine, then below message will be displayed as you hit enter.

screen-shot-2017-02-10-at-10-30-04-am
Terminal in Mac Machine

If Java is not installed, then below message will be displayed in Command Prompt or Terminal:

“‘Java’ is not recognized as an internal or external command”.

This is good news as now we will start with JDK installation…

Open Browser and type in “jdk download”. Click on the Java SE Development Kit 8 link to go to Oracle Website download section. Java 8 is the latest version. However, lower versions are also available for download. For now we will download the Java 8 version.

Once you are in below downloads page, select one of the options depending on the machine you are using. Depending on your Windows machine 32 or 64 bit, select Window x86 or Windows x64 option or else if its Mac, select the Mac OS X option. Click to download the exe or dmg file into your machine.

screen-shot-2017-02-10-at-12-13-44-pm
Oracle Downloads Page

Once after the successful download, click on the file in the Download folder of your machine to proceed with the installation. Installation is straight forward and you will be taken through various prompts to complete the installation. In Windows machine, JDK and JRE will be installed in C:\Program Files folder. In Mac, it will be in /Library/Java/JavaVirtualMachines folder.

Once after complete installation of JDK, you can confirm the installation by going to Command Prompt or Terminal and type in java -version” and hit Enter.

And now the JDK installation is complete, we will move on to the next topic…