Tuesday, 2 September 2014

JUnit is a testing Framework for the Java Programming Language.

In order to Run a Selenium WebDriver Tests using JUnit, we have to perform the following thing first:

  • We have to use JUnit annotations in our Selenium WebDriver Tests

What are JUnit Annotations?

JUnit has before and after Annotations for:

  • Method
  • Class
Other Testing Frameworks ?

TestNG is an another testing Framework which can be used as an alternative to JUnit testing Framework and is also more powerful and has advantages over JUnit testing Framework.

JUnit is easy to use in Eclipse IDE and it comes by default with Eclipse IDE (i.e. No need of Installing JUnit separately if you have installed Eclipse IDE). Where as in order to use TestNG, we've to install it separately in Eclipse IDE (i.e. It wont come by default with Eclipse IDE).

As JUnit testing Framework is simple and easy to use. Hence I will be using JUnit testing Framework to explain the Selenium WebDriver tests and there after will introduce you to the TestNG framework when the time comes.

JUnit Annotations:

Out of all the JUnit Annotations we are going to use only the following five annotations in our Selenium Automation Tests:

  1. @Test
  2. @Before
  3. @After
  4. @BeforeClass
  5. @AfterClass

I will explain the above listed JUnit annotations by using them in the Selenium WebDriver automation tests.

And also its not required to learn JUnit testing Framework in depth, as we will any way move ourselves to TestNG testing Framework in future. So don't learn other things regarding JUnit testing Framework that are not mentioned in my posts.


Monday, 1 September 2014

Prerequisites:

1. Create Selenium WebDriver Test as specified in Post# 6 Create a JUnit Selenium WebDriver Test using Selenium IDE
2. Ensure a Compatible Firefox Browser is Installed on your machine as explained in Post#7 Ensure Compatible Firefox Browser is installed 

1. Run the Selenium WebDriver Test

1. In Eclipse IDE, right click on the 'ImFeelingLucky' class and select 'Run As' -> 'JUnit Test' as shown below:




2. See how our automation test runs on Firefox Browser from the below video:

Click here to watch the video

2. Check the Results after the test run is finished

1. After the test run is finished as shown in the above video, we've to find out whether the automation test run by Selenium WebDriver is passed or failed
2. In order to do that, view the Eclipse IDE after the test run and view the test results as shown below:




3. Red color bar will be displayed instead of Green color bar if the test gets failed.

Thats it !! This is how we run the Selenium WebDriver tests in Eclipse IDE.

Sunday, 31 August 2014

Before Running the created Selenium RC Test we've to verify whether a compatible Firefox Browser is installed on your machine.

1. Firefox browser version you are using in your computer machine should be compatible with the Selenium WebDriver version you are using

1. View the version of the Selenium WebDriver you are using as shown below: (i.e. the version of the Selenium WebDriver we have downloaded and extracted in our previous post #2 Downloading the Selenium WebDriver




2. Go to http://selenium.googlecode.com/git/java/CHANGELOG and start reading from the first line to find out the first occurrence of "Firefox Version Support" text as shown below:

 

3. I am writing this post on  May 27th 2013, after I read the above change log from the beginning  I found   "Firefox 18 Support" as the first occurrence of the "Firefox Version Support" text.  And from this log its very clear that Firefox 18 Support is available for the selenium versions starting from 2.29.0 and above as shown below:




4. As I'm using Selenium WebDriver version 2.32.0  , which is higher than 2.29.0 version and Firefox 18 support is there for 2.29.0 and higher versions. Hence Firefox 18 browser is compatible with 2.32.0 version

5. But can I use Firefox 19 browser with Selenium 2.31.0 version as it is available currently. Will my test run ?  The answer is No. Though the latest Firefox version is available, your test wont run if the browser version is not compatible with the selenium version you are using.


2. How to install an older version of Firefox that is compatible with Selenium server version you are using

1. Suppose I've Firefox 19 installed on my machine currently but I need Firefox 18 version which supports the Selenium Server version I'm using.
2. I will first of all uninstall the Firefox 19 installed on my machine
3. In order to get the older releases of Firefox, I will open http://filehippo.com/download_firefox/ in my browser and click on 'Firefox 18' version as shown below:


4. In 'Firefox 18' page of  "Filehippo.com" website, click on the 'Download this Version' option as shown below:



5. Install the newly downloaded Firefox 18.exe  file
6. But on launching the Firefox Browser the Firefox version will get upgraded to the latest version. To avoid this go through the following steps immediately after launching the Firefox browser
7. Click on the 'Tools' Menu and select 'Options' option


8. Select the 'Advanced' tab in the displayed 'Options' dialog and select 'Never check for Updates' option as shown below and click on 'OK' button:


9. Click on Firefox Browser -> 'Help' Menu and click on 'About Firefox' option as shown below:


10. Ensure that 'About Firefox' dialog is displayed and confirm that you have "Firefox 18" version in the details as shown below:


Prerequisites:

1. Record a Test in Selenium IDE

1. Launch Firefox Browser and click on 'Tools' Menu -> 'Selenium' IDE option as shown below:



2. Ensure that Selenium IDE is launched as shown below:



3. Open any website say www.google.com in Firefox Browser as shown below:


4. Click on I'm Feeling Lucky " button as shown below:



5. Turnoff the 'Recording' button on the Selenium IDE to stop recording as shown below:



6. Ensure that the recorded commands are displayed under the table tab as shown below:




2. Enable 'Experimental Features' Option

1. In 'Selenium IDE',  click on 'Options Menu' and select 'Options' option as shown below:


2. In 'Selenium IDE Options' dialog, select the 'Enable Experimental Features' check box option  and click on 'OK' button as shown below:




3. Change the Recorded code format from HTML to 'Java / Junit4 / Remote Control ' 

1. Click on 'Options' menu and select the 'Format' option to view the 'Format' Sub menu options   and ensure that the 'HTML' option is selected by default a shown below:


2. Now change the selection from HTML to Java/ Junit 4 / WebDriver as shown below: (If you want your tests to be written using  Selenium WebDriver commands & Java Language and you want your tests to be Run using Junit 4 framework, then you have to select this option else you have select other options based on your requirement)




3. Click 'OK' button on the displayed '[JavaScript Application]' dialog as shown below:


4. Ensure that Junit 4 -Selenium WebDriver - Test  java code is displayed in the Selenium IDE's Source tab as shown below:




4. Create a Package named "firstPackage" under the 'Selenium WebDriver' configured project 

1.  Create and configure a project as explained in Post# 3 Configure Projects in Eclipse IDE to work with Selenium WebDriver
2.   Right click on the Project and select 'Package' option as shown below:




3. Ensure that 'New Java Package' dialog is displayed, enter "firstPackage" as Package Name into the 'Name' text box and click on 'Finish' button as shown below:




4. Ensure that the New Package got added as shown below:



5. Create a New Class under the above created Package

1.  Right click on the newly created package and select 'Class' option as shown below:




2. Ensure that 'New Java Class' dialog is displayed, type "ImFeelingLucky" class name into the 'Name' text box and click on 'Finish' button as shown below:




3. Ensure that the New Class file named as "ImFeelingLucky' got added under the "firstPackage" as shown below:



6. Copy the code that is generated and converted to Java/Junit4/WebDriver  in Selenium IDE and paste into Eclipse IDE's newly created class

1. Copy the source code that is generated by recording and converted to Java/Junit4/WebDriver format as shown below:




2. Paste the source code without removing the existing code in the newly created class file as shown below:




3. Ensure that few Eclipse Errors got displayed after pasting the source code as shown below:





7. Lets Rectify the first Eclipse Error - Package error

1. As only one package declaration is allowed in a class, but after copying the source code there are now two packages as shown below:




2. As 'Copied code's package' is not available in the Eclipse IDE, lets remove or comment it to resolve this error as shown below:




8. Rectify the second Eclipse Error - Two Class Names Error

1. A single Class File can have only one Class declared inside it, but after copying the source code there are now two class declarations with different names as shown below:



2. Remove or Comment  the existing class declaration as it doesn't contain any code or we are not going to write any code into it (as shown below):




9. Rectify the third kind of Eclipse Error - Class declaration name in the Class file should have the same name as the Class file Name  error

1. Observe that the Class file name  is different from the Class declaration name as shown below:




2. Change the Class declaration name as Class File Name as shown below:




3. Ensure that all the errors got resolved and no others errors exist in the source code.

10. Remove the warning Messages

1. Observe that there are three warning message highlighted in yellow color icon  as shown below:




2. In this case, the warning message are displayed for the three import statements as these import statements are never used in this source code (You can know this by mouse over this warning icons) as shown below:




3. Remove or comment these import statements to resolve (as shown below):




4. Ensure that the warning messages with the import statements got resolved now.

5. Observe that there are two methods that are throwing warning messages as they are never used locally as shown below:



6. Comment these methods to resolve the warning messages as shown below:



7. After commenting the above two methods, observe that there is one more warning message displayed as shown below:



8. Comment the above displayed warning message to resolve it as shown below:




9. Now ensure that there are no more warning messages and finally select 'Save All' button on the Eclipse IDE  to save all the changes as shown below:



11. Remove the selenium command, so that after running the test, the selenium server wont close the browser on which it runs the test 

1. Remove or Comment  the  "driver.quit( )' in the code ( Will explain about this command in upcoming posts, for now comment it blindly):




So we have finally finished creating a Selenium WebDriver test by following the below steps:


1. Creating a Selenium WebDriver test by using Selenium IDE's recording feature
2. Converted the automation code generated by the Selenium IDE from HTML format to the desired JUnit 4/Java/WebDriver format.
3. Copied the converted code and pasted it into the Eclipse IDE
4. Resolved the error messages and warning messages in the Eclipse IDE

Before running this code, we have  to make sure whether a compatible firefox browser is installed to run this code. Lets do this in the next post and there after run this code.

Download this Project:

Click here to download this project and import into Eclipse IDE on  your machine.
The following are the differences between the Selenium RC and Selenium Web Driver:

Difference 1:

Selenium RC
Selenium Server is required to start.
Explanation:
Selenium RC don't call the browsers directly instead it uses Selenium Server to communicate with the Test Browsers on the same machine or different machine. So we've to start the Selenium Server before running the Selenium RC tests.
How Selenium RC uses Selenium Server ?
Selenium RC works the same way for each supported browser by injecting the JavaScript functions into the browser using Selenium Server when the browser is loaded and also drives the application under test in the browser using Selenium Server (Selenium Server uses Java Script to driver the application under test in the browser).
Drawbacks:
Selenium RC server doesn't use the Browser's built in support for test automation. Hence it has to use Selenium Server as a mediator.
Selenium Web Driver
Selenium Server is not required to start.
Explanation:
Selenium Web Driver makes direct calls to the Test Browsers on the same machine without using Selenium Server as a mediator.
Exception Case:
Selenium  Server will be used as a mediator by Selenium Web Driver when the Selenium Automation tests need be run on a different machine (i.e. While implementing Selenium Grid concept which distributes the Automation tests over multiple machines. This concept may require the Selenium WebDriver to communicate with the test browsers not on the current machine. Hence it uses Selenium Server to communicate with all the test browsers which are not on the current machine.)
How can the Selenium Web Driver make direct calls to the test browsers ?
Selenium Web Driver uses Browser's native support/built in support for test automation. i.e. While writing the Selenium Web Driver automation tests we also use driver code of the browser to be tested. Suppose if we have to run our tests on Firefox Browser, then we will use Firefox Driver code in our Selenium WebDriver automation tests. (Browser Drivers will be explained in the upcoming posts). Hence usage of Selenium Server is not required when the Automation tests and the test browsers are on the same machine.


Difference 2:

Selenium RC
Selenium RC code looks different from the Selenium WebDriver code (Selenium RC uses different commands to perform the operations when compared to Selenium Web Driver )
Selenium Web Driver
Selenium WebDriver code looks different from the Selenium RC code (Selenium Web Driver uses different commands to perform the operations when compared to Selenium RC )


Difference 3:

Selenium RC
Simple and compact API when compared to Selenium Web Driver API.
Selenium Web Driver
Complex and a bit large API when compared to Selenium RC API.


Difference 4:

Selenium RC
Less object oriented API.
Selenium Web Driver
Complete Object Oriented API.


Difference 5:

Selenium RC
Less object oriented API.
Selenium Web Driver
Complete Object Oriented API.


Difference 6:

Selenium RC
Cannot automate to move the mouse cursor using Selenium RC automation
Selenium Web Driver
Can automate to move the mouse cursor using Selenium RC automation.


Difference 7:

Selenium RC
Cannot test iPhone/Android applications
Selenium Web Driver
Can test iPhone/Android applications.


Go through the following links to get more clarity and more differences:

http://www.guru99.com/introduction-webdriver-comparison-selenium-rc.html
http://www.vietnamesetestingboard.org/zbxe/?document_srl=641375