Friday 5 April 2013

JUnit 4 Annotations


JUnit 4 Annotations : Overview
Following is a list of frequently used Annotations , which is available when you include junit4.jar in your Classpath:

@Before
@BeforeClass
@After
@AfterClass
@Test
@Ignore
@Test
(timeout=500)
@Test
(expected=IllegalArgumentException.class)


@Before and @After
In Junit4 there is no setup() or tearDown() method and instead of that we have @Before and @After annotations.
By using @Before you can make any method as setup() and by using @After you can make any method as teardown(). What is most important point to remember is @Before and @After annotated method will be invoked before and after each test case. So in case you have five test cases in your JUnit test file than just like setup() and tearDown() method annotated with @Before and @After will be called five times. Here is an example of using
@Before and @After Annotation :

    @Before
   
 public void setUp() {
       
 System.out.println("@Before method will execute before every JUnit4 test");
   
 }
 
 
    @After
   
 public void tearDown() {
       
 System.out.println("@After method will execute after every JUnit4 test");
   
 }


@BeforeClass and @AfterClass
@BeforeClass and @AfterClass JUnit4 Annotations are similar to @After and @Before with only exception that they
are called on per TestClass basis and not on per test basis. They can be used as one time setup and tearDown
method and can be used to initialize class level resources. here is an example of using @BeforeClass and @AfterClass Annotations in JUnit4, here is an example of @BeforeClass and @AfterClass Junit 4 annotation

    @BeforeClass
   
 public static void setUpClass() throws Exception {
       
 System.out.println("@BeforeClass method will be executed before JUnit test for"
                +
 "a Class starts");
   
 }

    @AfterClass
   
 public static void tearDownClass() throws Exception {
         System.
out.println("@AfterClass method will be executed after JUnit test for"
                +
 "a Class Completed");
   
 }


@Test
@Test is a replacement of both TestCase class and convention "test" which we prefix to every test method. for example to test a method  called calculateInterest() we used to create method testCalcuatedInterest() and our class needs to be extended from org.junit.TestCase class. Now with @Test annotation that is not required any more. You just need to annotate your test method with @Test Junit4 annotation and done. no need to extend from TestCase class and no need to prefix "test" to your method, here is an example of  JUnit 4 @Test annotation

 @Test
   
 public void testCalculateInterest() {
       
 System.out.println("calculateInterest");
        fail
("An Example of @Test JUnit4 annotation");
   
 }


@Ignore
JUnit 4 Annotations examples list meaningsSome time we add test method in JUnit test class but hasn't implemented that is causing your build to fail if JUnit testcase are integrated or embedded into build process. you can avoid that problem by marking your test method as @Ignore in Junit4. JUnit4 ignores method annotated with @Ignore and doesn't run during test. Here is an example of using @Ignore annotation in JUnit4 to exclude a particular Test from running:


 @Ignore("Not yet implemented")
    @Test
   
 public void testGetAmount() {
       
 System.out.println("getAmount");
        fail
("@Ignore method will not run by JUnit4");
   
 }


@Test(timeout=500)
Now with JUnit4 writing testcases based on timeout is extremely easy. You just need to pass a parameter timeout with value in millisecond to @Test annotation. remember timeout values are specified in millisecond and your JUnit4 timeout test case will help if it doesn't complete before timeout period. This works great if you have SLA(Service Level Agreement)  and an operation need to complete before predefined timeout.

  @Test(timeout = 500)
   
 public void testTimeout() {
       
 System.out.println("@Test(timeout) can be used to enforce timeout in JUnit4 test case");
       
 while (1 == 1) {
         
 
       
 }
   
 }

This JUnit4 test will fail after 500 millisecond.

@Test(expected=IllegalArgumentException.class)
Another useful enhancement is Exception handling testcases of JUnit4. Now to test Exception is become very easy and you just need to specify Exception class inside @Test annotation to check whether a method throws a particular exception or not. here is an example which test behavior of a method to verify whether it throws Exception or not,  when run with invalid input:

    @Test(expected=IllegalArgumentException.class)
   
 public void testException(int input) {
       
 System.out.println("@Test(expected) will check for specified exception during its run");
     
 
   
 }


These were list of frequently used JUnit 4 annotations and there meanings. In the course we have also learn how to use @Before , @After in place of setup() and teardown(). Code review and Unit testing is one of the best development practices to follow and we must try our best to incorporate that in our daily coding and development cycle.

Wednesday 3 April 2013

Selenium Remote Control(RC) Interview Questions


1.  What do you know about selenium RC (Remote Control)?A)  Selenium RC starts up browsers(one at a time) and then runs commands we pass along from our tests..  It allows us to use a programming language for maximum flexibility and extensibility in developing test logic.. It provides an API and library for each of its supports languages like Java, Ruby, Python, Perl and C#..-->Selenium RC has 2 components.. Selenium server and Client libraries.. Selenium Server launches and kills browser.. Client libraries provide the interface between each programming language and the Selenium RC server..
2. Briefly explain how Selenium RC executes your scripts ?A)  Client libraries communicate with the Server passing each selenium command for execution.. Then the server passes the selenium command to the browser using Selenium-Core Java Script commands.. The browser, using its JavaScript interpreter, executes the selenium commands.. This runs the Selenese actions or verifications you specified in your test script..
3. What are the requirements needed to run simple script in RC ?A)  A browser, command prompt, Selenium Server jar file are enough to run simple scripts in RC..Selenium Server is needed inorder to run selenium RC scripts..
4. How to set up selenium RC completely with eclipse to run junit tests ?A)    First we need to download Selenium Server jar file, Selenium client libraries, junit jar file, eclipse and java software.. There after, open eclipse and click on workbench and then create a java project with meaningful name... Set path to java and jar files... Drag and drop your test scripts(which are exported from IDE) to the package you have created in the project.. Before running any test you must start the server..
5. Why selenium RC is used ?A)   Selenium RC is used to automate web applications with more effective browser actions when compared to SIDE(Selenium IDE).. As selenium RC uses a programming language, we can overcome limitations of SIDE i.e we can handle multiple windows and pop-ups, we can use loops and conditions, capturing screenshot etc.. In addition to that, RC can perform Data-Driven(read/write data from external files) concept, decent report generation and mailing that report to concern person etc.. 
6. What are the languages and operating systems that support RC ?A)   RC supports languages like Java, C#, Perl, Ruby, Python and PHP.. Operating systems like Windows, Mac OS X, Linux, Solaris etc..
7.  What are the advantages and disadvantages of RC ?A)  advantages:·                     Selenium RC can be used for any java script enabled browser..·                     Support for many operating systems and Programing languages..·                     We can use loops and conditions for better performance and flexibility..·                     Decent report generation..·                     Can handle Dynamic objects and Ajax based UI elements..·                     Can read/write data from/to .txt, .xls, etc..disadvantages:·                      There are very limited features in RC when working with Ajax based UI elements..·                     Cannot handle on-load alerts..·                     Controlling multiple windows is somewhat difficult..
8.In Selenium what are the four parameters you have to pass?A)    Host, port number, browser and URL...
9.Can we handle pop-ups in RC ?A)    Yes, we can handle pop-ups in RC... Using selectWindow method pop-up window will be selected and windowFocus method will let the control from current window to pop-up window and perform some actions according to our script..
10.Which method will you use for mouse left click and right click ?A)    For mouse left click i use 'click' method and for right click i use 'keyDown' method followed by 'click' and 'keyUp' methods i.e 'keyDown' method will press 'control' key without releasing it yet and then click method will be executed, after that 'keyUp' method will release the 'control' key..... Code will be as follows..          left click      --->  selenium.click(locator)          right click  --->  selenium.keyDown(locator,keysequence)selenium.click(locator)selenium.keyUp(locator,keysequence)NOTE : Here, all the three locators belong to same element/object and keysequence will be ASCII value for 'control' key...
11.What is the use of 'chooseOkOnNextConfirmation()' ?A)    This command is used to select/click 'OK' button in the confirmation box and it must be placed before the occurrence of confirmation box...   
12.What is a framework and what are the frameworks available in RC ?A)    Framework is nothing but a structure that allows us to do things better and faster... It is a collection of libraries and classes and they are very helpful if testers want to automate test cases.. JUnit, NUnit, TestNG, Bromine, RSpec, unittest are some of the frameworks available in RC ..
13.How do you handle secured connection error in HTTPS ?A)    Create an object to RemoteControlConfiguration and use setTrustAllCertificate method and set boolean value as true i.eRemoteControlConfiguration r= new RemoteControlConfiguration();r.setTrustAllCertificate(true);
14.If the default port of selenium is busy then which port you will use ?A)    We can use any port number which is valid.. First create an object to remote control configuration. Use 'setPort' method and provide valid port number(4545,5555,5655, etc).. There after attach this remote control configuration object to selenium server..i.eRemoteControlConfiguration r= new RemoteControlConfiguration();r.setPort(4567);SeleniumServer s= new SeleniumServer(r);
15.How do you select second value from a drop down menu ?A)   Define an array of string type.. By using 'getSelectOptions' command provide locator for that particular drop down and then use 'select' command.. As 'select' command parameters are locator and the label, in-place of label, define array index... i.e,.String a[]=selenium.getSelectOptions(locator of drop down);selenium.select("locator of drop down", a[1]);note: If you want to select 5th value from drop down, then provide '4' in the index of an array because array index starts from 'zero'...
16.What is the differrence between sleep() and setSpeed() methods ?A)    Both will delay the speed of execution... When you use Thread.sleep(), then the execution of your test will be stopped untill the time you have provided in sleep method, it will wait only once where the command is usedwhere as using setSpeed() method we can set the time of delay which will follow each and every selenium command i.e if you set 5000 milliseconds then your test execution will wait 5 seconds after each and every selenium operation..

Selenium IDE Interview Questions

1)  What is Selenium IDE ?A)  Selenium IDE (Integrated Development Environment) is an ideal tool used to develop selenium test scripts... It is the only flavor of selenium which allows us to record user actions on browser window... Here, the scripts are recorded in 'Selenese'(a set of selenium commands like Click, assertTextPresent, storeText, etc,.).. It is not only a time-saver but also an excellent way of learning Selenium scripts syntax... It is a Firefox add-0n...

2)  How to install Selenium IDE ?A)  click here for detailed steps        I use seleniumhq.org site to download any software regarding selenium.. Here, if we click on the version number of selenium ide it starts downloading the add-ons, just we have to click on 'install now' button and restart the firefox browser.. With-in 5 minutes we can install selenium ide...  
3)  How do you open/start selenium-ide after installation ?A)  First launch/open firefox browser and then click on 'Tools' tab in the menu bar.. You can see an option called 'selenium ide' , click on it, a small window will be opened where you can start recording scripts... 4)  Features of selenium IDE..A)  click here for detailed features of selenium ide·                     Its main feature is record and playback..·                     Debugging features by setting breakpoints, startpoints, pause..·                     Identifies element using id, name, xpath etc..·                     Auto complete for all common selenium commands..·                     It has an option of asserting title of every page automatically..·                     Support for selenium user-extensions.js file..·                     Option to run single test or multiple tests(suite).·                     It has a feature of exporting testcase/suite into different formats like C#, Java, Ruby, Python..
5)  Advantages and disadvantages of selenium IDE..A)  Advantages : ·                     Freeware..·                     Easy to install..·                     It is the only flavor of selenium that allows us to record user actions on browser window.. ·                      Scripts recorded in IDE can be coverted into other languages like Java, C#, Python and Ruby..·                     It is not only a time saver but also an excellent way of learning scripts syntax..·                     We can insert comments in the middle of the script for better understanding and debugging..·                     Context menu, which allows us to pick from a list of assertions and verifications for the selected location..Disadvantages:·                     Main disadvantage of IDE is that it runs only in firefox browser..·                     Selenium IDE can execute scripts created in selenese only.·                     Cannot upload files..·                     It does not directly support loops and conditions..·                     Reading from external files like .txt, .xls is not 
possible..
6)  What are the selenium locators and what is the tool you  use to identify element ?A)  Selenium locators are the way of finding HTML element on the page to perform Selenium actions... We use firebug(for firefox) to identify elements as it is more popular and powerful web development tool.. It inspects HTML and modify style and layout in real-time.. We can edit, debug and monitor CSS, HTML and Javascript live in any web page.. ((click here to download firebug))-->For Internet Explorer we can choose debugbar.. It views HTML DOM tree, we can view and edit tab attributes.. ((click here to download debugbar))
7)  How do you locate elements in IDE ?A)  I will focus on the unique attribute values like id, name or other structural information that is stable enough to withstand frequent changes to the web application.. I strongly recommend CSS selectors as locating strategy.. They are considerably faster than xpath and can find the most complicated objects in any HTML document..
8)  What is selenese ?A)  Selenium set of commands that run our test is called Selenese.. A sequence of these commands is a test script.. There are three types of selenese..1.                   Actions : They perform some operations like clicking a link or typing text in text box or selecting an option from drop-down box etc..2.                  Assertions : They verify that the state of application conforms to what is expected.. Ex: 'verify that this checkbox is checked', 'make sure that the page title is X'..3.                  Accessors : Checks the state of application and store the results in a variable.. Ex: storeText, storeTitle, etc..
9)  How do you add check points or verification points ?A)  They are called as Assertions in selenium.. 'assert', 'verify' and 'waitFor' are the commands used to add check points..Ex: assertText, verifyElementPresent, waitForTextPresent, etc..
10)  Name some commands that you use frequently in IDE ?A)  Open, click, type, select, assertText, assertTitle, assertTextPresent, verifyText, verifyTextPresent, veriftTitle, waitForText, waitForTextPresent, waitForTitle, store, storeText, storeTitle, check, uncheck, pause, mouseover, etc..
11)  What is the difference between assert and verify ?A)   When an 'assert' fails, the test is aborted where as when 'verify' fails, the test will continue execution logging the failure.. 'assert' is used when the expected value is mandatory to continue with next set of steps.. However 'verify' is used when the expected value is optional to continue with the next set of steps..
12)  Difference between waitFor and pause commands ?A)  'waitFor' command waits for some condition to become true..For example, 'waitForPageToLoad(20000)'-- it will wait upto 20 thousand milliseconds to load required page.. If the page is loaded before 20,000ms then it jumps to next step to execute.. If the page is not loaded before 20,000ms then it stops the execution due to time-out error..--> pause command stops the execution of the test until the specified time..Ex:  pause(10000)-- It stops the execution of test for 1o thousand milliseconds.. After completing 10,000ms it jumps to next command to execute.. I prefer 'waitFor' command than 'pause'..
13)  How do you export tests from Selenium IDE to RC ?A)  First i will open the test in IDE, which should be exported to RC.. There after i'l select 'File' from the menu bar.. when we mouseover on 'Export Test Case As' in the file menu, we could see different languages like C#, Java, Python and Ruby.. Select the language you want to export your test and provide the name to save it..

Selenium Interview Questions


1)  What is Selenium ?A)  Selenium is a combination of different software tools with different approach to automate browsers..  Its primary purpose is to automate Web applications for testing purpose and it is developed in JavaScript...2)  What is the cost of Selenium ?A)  Since, selenium is an open source, it is free of cost.. So, we can download the software for free... 3)  What are the main components / flavors of Selenium ?·                     Selenium IDE (Integrated Development Environment)·                     Selenium RC (Remote Control)·                     Selenium Grid·                     Selenium Webdriver4)  What tests can selenium do ?A)  Selenium is mainly used for Functional testing, Regression testing and Load/Stress testing for web based applications...5)  What are the advantages and disadvantages of selenium ?A)  Advantages :·                     Its main advantage is that it is free of cost.·                     Installation of selenium software is easy.·                     Record and play back feature is available.·                     Selenium supports multiple operating systems and runs in many browsers.·                     It has a feature of converting scripts into other languages like Java, C#, Python, Ruby, etc..·                     Good support for Agile(methodology) projects.    Disadvantages :·                     Complete set-up of selenium is somewhat critical.·                     It is not suitable for Client-Server applications.·                     Boring error analysis.·                     Selenium does not support back end test.·                     Support provided for Selenium would be very less.6)  Name some browsers that support Selenium ?·                     Firefox·                     Internet Explorer·                     Safari·                     Opera·                     Google Chrome7)  Operating systems that support Selenium..·                     Windows·                     Mac·                     Linux·                     Solaris8)  Programming languages that support Selenium(with RC)..·                     Java·                     C#·                     Perl·                     PHP·                     Python·                     Ruby9)  Name some open source tools otherthan Selenium ?·                     WATIR- Web Application Testing In Ruby, is a functional testing tool for web applications which uses Ruby scripting language..·                     Sahi- It is also an automation tool used to automate web appications which is developed in java and javascript..·                     WET- Web Tester is also a web automation testing tool which uses Watir as the library to drive web pages..10)  Why you have chosen 'selenium' as your automation t00l ?A)  As our project owner is not much interested in investing amount on automation, he had suggested us to use an open source tool.. Selenium is perhaps(reasonably) the best option for automating web-based applications.. Due to its popularity with good features, it would be the first choice of a tester or a company for automation testing.. Since, our team has exposure on selenium, our project manager has decided to use 'Selenium' for automation..11)  Compare Selenium with QTP..·                     Selenium is an open source(free of cost) where as QTP(have to purchase) is commercial tool..·                     Selenium is used for testing only web-based applications where as QTP can be used for testing client-server applications also..·                      Selenium supports Firefox, Internet Explorer, Safari, opera on operating systems like Windows, Mac, linux etc., however QTP is limited to Internet Explorer on Windows..·                     Selenium tests has the flexibility to use many languages like Java, C#, Python, Ruby, Perl and PHP where as QTP mainly supports VB script..