Showing posts with label Selenium training. Show all posts
Showing posts with label Selenium training. Show all posts

Monday, 24 November 2014

Creating Hierarchy of Packages in Selenium

We can create packages at different hierarchy level (package and its sub-package etc). Lets create the packages at different hierarchy levels as shown below -
Lets implement this on Eclipse IDE -
1. Launch Eclipse IDE and create a Java Project ‘Project 41′ as shown below -
2. Now lets change the display mode of the package hierarchy by following the below steps.
3. Select the ‘view’ menu icon of the ‘Package Explorer’ tab as shown  below -
4. In ‘View Menu’ options change the Package Presentation option from Flat to Hierarchical as shown below -
Now with this step, the package display mode got changed to hierarchical mode. Lets create hierarchy of packages by following the below steps.
5. Create two packages ‘package1′ and ‘package2′ as shown below -
Now lets create the packages ‘packageA’ and ‘packageB’ as sub-packages under ‘package2′ by following the below steps.
6. Now right click on the ‘package2′ and select ‘New’ -> ‘Package’ from the right click options as shown below -
7. In New Package dialog, enter the package name as package2.packageA and click on ‘Finish’ button  as shown below -
8. Observe that the package ‘package2′ got changed to ‘package2.packageA’ as shown below -
When you create a single sub-package, the original package will get replaced with a single package ‘package.subpackage’. But when you create more than one sub-packages for a package, you can clearly see the hierarchy. Please follow the below steps to create another sub-package for ‘package2′.
9. Right click on the ‘package2.packageA’ package and select ‘New’ -> ‘Package’ from the right click options as shown below -
10. Ensure that the ‘New Java Package’ dialog is displayed with ‘package2.packageA’ in the name field as by default as shown below -
11. Change the packageA to packageB in the default displayed name and click on ‘Finish’ button as shown below -
12. Observe that the two sub-packages ‘packageA’  and ‘packageB’ are displayed under the ‘package2′ package as shown below -
This is how we create packages at different hierarchy levels and view them in the Hierarchical package display mode.
Now lets find out the package statements created in the Class files which are created under the package which is not having any sub-pakcage by following the below steps.
13. Create a Class ‘ClassOne’ under ‘package1′ page and observe that the package statement is displayed in the Class file as shown below -
Hence when you create a Class file under a package directly, the package statement syntax will be package packageName; 
Now lets create a Class file under a sub-package ‘packgeB’ of ‘package2′ package by following the below steps.
14. Create a Class ‘ClassX’ under ‘packageB’ of ‘package2′ and observe that the package statement is displayed in the Class file as shown below -
Hence when you create a Class file under a sub-package of any package, the package statement syntax will be package packageName.subPackageName; 
 Now lets change the package view from Hierarchical mode, back to the Flat mode by following the below steps.
15. Select ‘Flat’ option from the ‘View Menu’ options as shown below -
16. Observe that the hierarchical mode got changed to the Flat mode as shown below -
When you create a package in Eclipse IDE, a folder will be created in the workspace in your computer (i.e. Inside Eclipse IDE Workspace -> Project folder). Now lets view the workspace of this project to verify the same by following the below steps.
17. Right Click on the Project, select ‘Properties’ option and view the location of your project on your computer as shown below -
18. Navigate to the location on your computer and view the Project files as shown below -
19. Open the ‘src’ folder and observe that the Packages ‘package1′ and ‘package2′ are displayed as folder as shown below -
20. Open the ‘package2′ folder and observe that ‘packageA’ and ‘packageB’ sub folder for the sup-packages in Eclipse IDE are displayed as shown below -
21. Open the ‘packageB’ sub-folder and observe that ClassX created under the sub-package ‘packageB’ is displayed as shown below -

How to Rename the Packages in Hierarchy - QuontraSolutions

we can rename the packages which are not in hierarchy without any problems. But when you want to rename a package which has sub-packages under it, it is not possible to do it from Eclipse IDE. Instead we have to go to the Project location on your computer, rename the folder with package name outside Eclipse IDE and refresh the Project in Eclipse IDE.
Lets implement this on Eclipse IDE -
1. Launch Eclipse, Create a Java Project ‘Project 42′ as shown below -
Now lets create a package under Project 42 and rename it by following the below steps. 2. Create a Package ‘packageA’ as shown below -
3. Now Right click on the Package and select ‘Refactor’ -> ‘Rename’ option as shown below -
4. Observe that ‘Rename Package’ dialog is displayed with the ‘packageA’ name as shown below -
5. Now change the package name in the ‘New Name’ field from ‘packageA’ to ‘packageX’ and click on ‘OK’ button as shown below -
6. Observe that the package name got changed from ‘packageA’ to ‘packageX’ as shown below -
Now lets create sub-packages under the package ‘packageX’ and try to rename it by following the below steps. 7. Create two packages package1 & package2 under ‘packageX’ as sub-packages to it as shown below -
In Eclipse IDE, rename the package ‘packageX’ , which is having two sub-packages ‘package1′ and ‘package2′ by following the below steps. 8. Right click on the ‘packageX’ and select ‘Refactor’ -> ‘Rename’ option as shown below -
9. Observe that ‘Rename Pakcage’ dialog is displayed with ‘packageX’ name as shown below -
10. Change the package name displayed in the ‘New Name’ field from ‘packageX’ to ‘packageG’ and click on ‘OK’ button as shown below -
11. Observe that a new package ‘packageG’ got added instead of renaming the ‘packageX’ to ‘packageG’ as shown below -
Hence we are not able to rename the package having the sub-packages using Eclipse IDE. So renaming the packages having the sub-packages from Eclipse IDE is not possible. Now lets find out the project location and change the package folder name to change it by following the below steps. 12. Right click on the Project and select ‘Properties’ option as shown below -
13. In the ‘Properties for Project 42′ dialog, select ‘Resources’ option from the left and view the location of the project on your computer as shown below -
14. Go to the Project location on your computer and open the ‘src’ folder to view the package folder ‘packageX’ inside it as shown below -
15. Rename the ‘packageX’ folder manually to ‘packageM’ as shown below -
16. Observe that the package name is still not changed in the Eclipse IDE as shown below -
Package name wont be changed in Eclipse IDE until you refresh the Project. Refresh the project by following the below steps. 17. Right click on the Project in Eclipse IDE and select ‘Refresh’ option as shown below -
18. Observe that the package name got changed from ‘packageX’ to ‘packageM’ in Eclipse IDE as shown below -
Hence renaming the package having the sub-packages from Eclipse IDE is not possible. In order to rename, we have to change the name of the package folder from the Project location in our computer and refresh the Project in Eclipse IDE to see the changes. Can we rename any package having Class files under it from Eclipse IDE ?
Yes, we can rename the package name in Eclipse which has any Class files under it. But we cannot rename the package name in Eclipse IDE if it has any sub-packages under it.
Lets implement this on Eclipse IDE -
1. In Eclipse IDE, create two Class files ‘ClassOne’ & ‘ClassTwo’ under the package ‘packageG’ in the existing Java Project ‘Project 42′ as shown below -
2. Right click on the ‘packageG’ which is having two Classes ‘ClassOne’ & ‘ClassTwo’ and select ‘Refactor’ -> ‘Rename’ option as shown below -
3. Observe the ‘Rename Package’ dialog is displayed with ‘packageG’ name as shown below -
4. Change the name of the package in ‘New Name’ field from ‘packageG’ to ‘packageS’ and click on ‘OK’ button as shown below -
5. Observe that the package name got changed from packageG to packageS without any problems in Eclipse IDE as shown below -
Hence we can renaming the packages having Class under it from Eclipse IDE without any problems.


Exception is an Object - Selenium

A Java exception is an object that describes an exceptional condition (i.e. error) that has occurred in the piece of code. When an exceptional condition arises, an object representing that condition is created and thrown.
What happens when we don’t handle that exception ?
The program execution will stop at the line of the code where the exceptional condition (i.e. error) got occurred.
How does the program execution stops when exceptional condition got occurred ?
As our program, don’t have any code to handle the occurred exceptional condition (i.e. error), JVM stops our program execution and prints what when wrong using the thrown exception (i.e object).
How to handle the exception ?
We have candle the exception using the try{ } catch{ } blocks.
What happens when we use the try{ } catch{ } blocks to handle the exceptions ?
When an exception occurs in try { } block,  instead of passing the exception (i.e. object) to JVM, the try{} block will pass it to its associated catch{ } block where we can write our code to handle the occurred exceptional condition. catch{ } block catches the exception (i.e. object) that describes the occurred exceptional condition in the program code of the try{ } block and executes the program code that is written in its block to handle the occurred exception condition (i.e. error).
Lets understand it using the below code -
try
{
    int a = 1/0;  //Gives Arithmetic exception
}
catch(Exception e)   //Here e is an object of Exception Class type which receives the exception (i.e. object) that is occurred in and passed by try block.
{
     System.out.println(” 1/0  is not a valid statement “);  //This statement will be printed and the program execution will continue
Lets understand the above code step by step -
1. int a = 1/0;  statement gives divide by zero error and hence throws an exception (i.e. object). But as we have provided the above statement in the try{ } block, instead of passing the exception (i.e. object) to the JVM and terminating the program, try{ } block will pass the exception (i.e. object) to its corresponding catch { } block where we have written the code to handle the exception (i.e. object) to overcome its exceptional condition (i.e. occurred error).
2. catch(Exception e) -> catch block catches the exception(i.e. object) passed by the try{ } block and assigns it to the Exception Class type object ‘e’. You can create Exception Class type object with any name. Lets say, If I want to give the object name as  ‘object1′ instead of ‘e’ , I can as -> catch(Exception object1). And also Exception is a Parent Class of all its Sub-Classes which handles different types of  Exceptions.
In this example, we are using Exception Class i.e parent class, as we know that arithmetic exception is thrown by the statement in the try{ } block, we can use the Class which can receive the exception directly instead of up-casting the received object to the Exception Parent Class and using it. So in order to deal with the exception (i.e. object) directly we can modify the statement as -> catch(ArithmeticException object1). I will explain the Exception Hierarchy in our upcoming posts to explain the parent classes and child classes which are used in catch{ } blocks to handle the exceptions.
3. No programmer will intentionally write the statement int a = 1/0; in his program code. I have intentionally used this statement to make the program throw an exception in this example. But in real time, when a User runs the program created by a programmer, where in the code, the programmer has specified the statement as int c = a/b; and while the User is executing the program created by the programmer, the User may be asked to input a value for a & b variables, and unknowing the User inputs the a variable as 1 and b variable as 0.
The exception will occur in the statement int a=b/c and the program created by the programmer may not be run properly at the User end. In order to avoid this kind of situations, the programmer will handle the exceptions in his code by printing the message ‘0 is not valid’ at User end. When User runs the properly handled code at his end, and in case he enter ‘0’ for b variable, the executing program asks the User not to enter the value ‘0’ for the variable b.

Tuesday, 5 August 2014

Manual and Automated Testing

There are mainly two broad types of software testing – Manual Testing and Automated Testing.
Manual Testing
Manual Testing involves testing the software without any automation script or any tool. Testers check the application or software by taking up the role of an end user. They try to find out if there is any unexpected behavior or failure in the application. Test Management can be taken care of by using test plans and test cases.

Automation testing

Automation testing process involves testing with the help of automation scripts and executing the scripts to run the application with the help of some automation tool. Once the script is ready then these tests can run quickly and efficiently.
Since the cost of automated testing is in the form of efforts and time required to create the scripts, not all tests can be converted to automated test. There should be a valid reason to pay that cost.
Reasons for Automation
1. Regression testing to confirm that new changes have not affected the application adversely. It considers already existing test cases for execution. This is an efficient process when we need to provide feedback to the developer immediately.
2. The test cases need to be iterated multiple number of times often with varying datasets to cover multiple workflow paths.
3. When we require support for agile methodologies.
4. Customized reports are required for monitoring.

Getting Started with Automated Testing

Once the need for automated testing has been established, it involves creation of relevant test scripts. Test script creation can be done only by a skilled testers having knowledge of testing, the suite of tools as well functionality under development. Such resources are costly and their time is a premium. Considering this fact, it is often not possible to budget the automation of all tests. Some of the major decision points while identifying cases for testing automation are
1. System modules where requirements do not change frequently
2. Ample time is at hand to describe a test via scripts
3. The application/software module is critical enough to justify the upfront cost of automation
4. After functional testing we want to do performance testing with multiple virtual users using the same test script.
With the scope of automation decided, next step is to pick the testing tool. The following checklist can help with the selection.
1. The tool should be able to easy to work with. It should execute test cases in unattended manner. It should provide interface to write scripts, efficient IDE and ease of test execution.
2. The tool should provide support to various technologies. It should support testing using different browsers, languages, and types of applications.
3. It should integrate with a software that does Application Lifecycle Management so that it can be used for running automated Build Verification Tests as well as the reports can be integrated with other reports created by ALM software

Automated testing frameworks

There are three automated testing tools namely Selenium, QTP (Quality Test Professional) and Coded UI Test (CUIT) .We will consider above mentioned main aspects of automation and see how these tools provide the support for each category.
Ease of Use
- Recording and Playback Functionality
Each of the testing tools has the ability of recording the actions and playback the recorded actions. Selenium provides the plug-in named Selenium IDE with Mozilla Firefox with which the actions can be recorded. QTP provides record button to record a new test. Recording for
- IDE and tools with which the tester can write the scripts
With Selenium IDE there is no special tool and specific technology to write the script. We can insert commands with ‘Table or Source View’ when required.
QTP provides Keyword View to display test steps graphically or Expert View which shows VB Script lines.
Selenium IDE comes as a plug-in with Mozilla Firefox. With this we can create a test suite which comprise of various test cases. With Selenium IDE is, you can convert recorded Selenium IDE scripts into different languages and after conversion you can run it in Selenium RC. Selenium RC has two components, one is “Selenium Server” and another is “Selenium Client”.
With QTP IDE for the first time 3 add-ins are provided ActiveX, Visual Basic and Web. Various links to best practices, new features for the current version are available with start page. We can either open existing test case or create a new one.
- Ease of Test Case execution
With Selenium IDE we have the option of executing the entire test suite already recorded or a test case at a time.
Depending upon the add-ins loaded in QTP IDE the record and run window shows tabs. Windows Application tab is always available. The tests can be executed with run button which in turn opens the run dialog box. We can specify the location for run specific results and provide parameters if any.
These tools can execute test cases without human intervention.

Comparison Between Selenium and QTP

Platform Support
-Language Support
Selenium uses Selenese, a high-level, cross platform language to write Selenium commands which is a domain specific language. There are 3 basic categories for the commands – named actions, accessors and assertions. To write tests there are a lot of programming languages like C#, Java, Perl, PHP, Python or Ruby.
QTP scripts can be written with VBScript which is a high-level language with support to everything except polymorphism and inheritance.
- Support for various application types
Selenium supports only Web applications.
QTP supports almost any kind of applications.
Selenium scores fewer points in this regard as it supports only web application. QTP supports almost all kinds of applications

- Support for various browsers
Selenium supports all versions of IE, Firefox, Safari and Opera and a few more browsers.
QTP supports IE & Firefox. But both do not provide full cross browser support.
Selenium is the clear winner in this respect
- Support for Data Driven Testing
Selenium IDE supports xml data source using user extensions.
Data Driven testing is implemented as Excel workbook that can be accessed by QTP. There are 2 types of data sheet global and local. Global sheet is a single one which can be accessed from every action in a test. There can even be a local data sheet associated with every action.
- Exception Handling
Selenium IDE does not support error handling particularly unexpected errors (as it supports only HTML language). Selenium RC will provide support for it (it supports languages with .NET, Java, Perl, Python, PHP, Ruby).
QTP provides VBScript with the help of which we can use On Error statements.
- Validations or Assertions
Selenium assertions can be used in 3 modes assert, verify and waitFor. When an “assert” fails, the test is aborted. When a “verify” (Soft Assertions) fails, the test will continue execution, logging the failure. This facility can be used with TestNg framework. The “waitFor” commands wait for some condition to become true. They will succeed immediately if the condition is already true. However, they will fail and halt the test if the condition does not become true within the current timeout period.
For QTP there are checkpoints: to verify application under test. These are of 10 types – Standard, Table, Image, Bitmap, Database, Text, Text Area, Page, Accessibility, and XML. A checkpoint is a verification point that compares the current value with the expected value. If the current and expected value match it generates a PASS status otherwise FAIL status.
- Support for Objects
Object properties are not supported by Selenium. Selenium objects can be managed by using UI element user extensions. QTP comes with in-built object repository.
QTP objects have user friendly names.
Integration with Application Lifecycle Management and going beyond
- ALM Integration
Selenium being an Open Source software can be integrated with other Open Source products for Application Lifecycle Management like QMetry. This in turn can provide platform for software development lifecycle platform in the form of Atlassian Jira (project tracking tool), FogBugz or Bugzilla (bug tracking tool).
QTP being a part of Quality Centre it supports requirement traceability matrix. QTP integrates seamlessly with QC. Test management and mapping the manual testing process with automation becomes a lot easier with this integration
QC is still not complete life cycle management tool. It does not provide support for efforts management, build management or support to different process templates. It supports only test management, bug management and requirement management.
- Going beyond
Selenium being Open Source a lot of plugins available. Selenium IDE has plug-ins for customization, for adding new functionality to API, changing existing functionality.
QTP provided plug-ins for ActiveX controls, web application and VB objects. Other than these plug-ins for other objects like Microsoft .NET, multimedia plug-ins and Windows Mobile are also available. These QTP plugins available at an additional cost.
Each of the tools keeps on adding features as per need. Selenium being open source there are a lot of plug-ins.

Selenium WebDriver Training

What is Selenium Webdriver?

Webdriver is known to be the latest version of Selenium i.e. Selenium 2.x. More powerful than the primitive Selenium rc 1.x and comes with lots and lots of libraries and new features to work with. It’s not mandatory to know Selenium RC to work with Webdriver. You can directly learn webdriver and start working on it. There is no need of selenium server if you are working with Webdriver API

What makes it different from Selenium RC?

  • More object oriented as compare to Selenium RC.
  • Has wider range of APIs
  • Interacts natively with browser where as Selenium RC is Javascript based.
  • Can test Iphone and Andriod based application which is not supported by Selenium RC.
  • Implements HTMLUnit driver which makes test execution really fast.
  • Unlike Selenium RC there is no server in Webdriver.
  • Supports almost all lastest web browsers where as Selenium RC does not works on latest versions on Firefox and IE.

WebDriver Architecture

Selenium WebDriver architecture

Element Identification using Selenium WebDriver: Questions and Answers

1: What is the method used to launch URL using Selenium WebDriver?
Answer: In Firefox, we can set the homepage as the required URL as shown in the code below. This will launch the URL set in homepage on launching the driver.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference(“browser.startup.homepage”,“http://www.google.com”);
//We will provide the profile used as argument for Firefox Driver.
WebDriver driver = new FirefoxDriver(profile);
In case, we do not want to use profile, we can use the get method to launch the URL.
WebDriver driver = new FirefoxDriver();
driver.get(“https://www.google.com”);
2: What is the difference between findElement and findelements method in Selenium?
Answer: To locate an element in the page, we use findElement method whereas findElements gives the collection of web elements in the Page.
Syntax for findElement is:
WebElement tableinfo = driver.findElement(By.className(“tableClsName”));
This will give the first element in the page with className as “tableClsName”. Now once the element is recognized, we can perform action on the WebElement. Suppose the element is a button, we can click on the button as shown below:
Tableinfo.click();
Suppose the WebElement is an input box, we can insert data in the input box as shown below:
Tableinfo.SendKeys(“This is a test”);
Syntax for findElements is:
List<WebElement> tabledata = tableinfo.findElements(By.tagName(“td”));
This will give collection of all elements in the page with className as “td”.
We can further work on the collection of the object and extract the required information or perform actions on the required webElement as shown below:
List<WebElement> tabledata = tableinfo.findElements(By.tagName(“td”));
for (int iCnt = 0;i<tabledata.size();iCnt++)
 {
String strData = tabledata.get(iCnt).getText() ;
If(strData.contentEquals(“this is the row”))
{
System.out.println(“Web Element Found”)
iCnt = tabledata.size()-1;
}
}
3: What are the value ways to identify an object in Selenium WebDriver?
Answer: Below are the various locators by which elements can be identified in Selenium WebDriver:
1. Id:  Identifies WebElement by the ‘Id’ attribute.
2. ClassName: Identifies webelement by the ‘class’ attribute.
3. cssSelector: identifies element based on the css of the webElement.
4. linkText: Identifies element by the actual text of the link. Text should match exactly with the link text.
5. Name: Identifies webelement by the ‘name’ attribute.
6. partiallinkText: Identifies element by the actual text of the link. The link is identified on partial match of text.
7. tagName: Identifies an object by the tagName of the webElement.
8. xpath: Identifies an object by the xpath of the object.

Selenium IDE: Interview Questions with Answers

1:  Explain the various features provided by Selenium IDE?
Answer: The layout of Selenium IDE is divided into following four areas:
1. MenuBar – Following are the menu options available in Selenium IDE:
 a.   File – Allows creating new test case, test suite, open existing test case, test suite, exporting tests to other formats.
 b.   Edit – Allows editing existing test steps, and adding new commands and comments.
 c.   Actions – Allows to record, play a test, execute command, toggle breakpoint and set entry point.
 d.  Options – Allows the changing of settings including set the timeout value for commands and specifying the format used for saving test cases.
 e.  Help – provides documentation for selenium IDE.
   2.  Toolbar – Provides buttons to manage test execution including test execution and test execution.
   3.  Test Case Pane – Test Case Pane shows the list of test case on the left and test steps in table or
source pane on the right. We can add/modify commands, target and value in the table for the test.
    4.  Log/Reference/UI-Element/Rollup Pane – This pane helps us to view logs of execution, reference explaining the selected command. We can also set to filter logs for info, warning, error and debug in this window.
 2: Explain what are the various items available from file menu
Answer: From the File Menu, We can perform various tasks
a.  Create New Test Case.
b.   Create new Test Suite.
c.   Save test cases and test suites.
  1.    Export Test cases/test suites to various formats. E.g : Ruby/Python/java/c# with WebDriver or RC.
  2.   Open existing test cases and test suites.
d.   Rename a test case.
3: Can the script prepared in Selenium IDE be used with webdriver/Remote control?
Answer: Yes, the script created in Selenium IDE can be exported to WebDriver/Remote Control in the languages java/c#/python/ruby that can be run using junit(java), RSpec(ruby), nUnit.
4: My Application is running slowly, can I change the default timeout value of recorder command?
Answer: We can reset the default timeout value for recorded command from Options Menu. By default the timeout is set as 30000 ms. In options, we can also define the recording settings and order of locator preference for object identification.
 5: What is the short cut key to create a new test in Selenium IDE?
Answer: The shortcut key to create a new test in Selenium IDE is Ctrl + N.
Some other useful shortcut keys in Selenium are:
Open a test – Ctrl + O :  Save a test – Ctrl + S  : Add test to test suite : Ctrl + D.
 6: What is the use of Select and find button in target in Selenium IDE?
Answer: On clicking on select, we can add the locator of an element in the page. Click on Select and click on an element in the page, the locator for the element will be added in the target. Find helps us to verify object exists in the page and highlights the object in the page if exists, else error is displayed in the log.

How to Configure Eclipse to work with Selenium

Before starting working with Selenium using Eclipse IDE, we did some configuration that needs to be done for starting and understanding Selenium. This post will explain step wise, how to start working with selenium using Eclipse:

Pre- Conditions: We will require following before beginning the configuration:

1. Java needs to be installed in the machine.
2. Eclipse IDE needs to be installed .
3. Standalone libraries of selenium, at the time of writing this post, I am using selenium-server-standalone-2.33.0.jar
4. Verify in Path variable in User defined variable for machine, is set to the path where standalone libraries is placed as shown below



Configuration Steps: 


Step 1: In eclipse, create a new java project as shown below from File>New Java Project

Create a new Java Project

Step 2: Once a java project is created, right click on project and select option “Build Path>Configure build path
Configure build path of project
Step 3: Click on Add External JAR’s and add the selenium standalone jar as shown below.
 Step 4: Now in src for the project, Add a new package, and add a new class as shown below.

Select methods stubs to create as public static void main(String[] args)

                                         
Step 5: Write the code in the class created above and execute. This will open Google in internet explorer . Write selenium in search and click on search.

Code to be placed in void main:

WebDriver ieDriver = new InternetExplorerDriver();
ieDriver.get(“http://www.google.com”);
WebElement element = ieDriver.findElement(By.name(“q”));
element.sendKeys(“selenium”);
element.submit();