Search This Blog

Friday, December 11, 2015

XML Basics

XML is the abbreviation for EXtensible Markup Language. It's used to store and transport data. It's both human-readable and meachine -readable format.

There is no pre-defined tags (like
in HTML) in XML. Here, the developer must define the tags and document structure.


XML documents are extensible meaning adding/removing tags to the XML document will not impact the application that process the document.

XML Tree Structure: XML docs are formed as ELEMENT TREES. They have root element and child elements.

<root>
  <child>
    <subchild>.....</subchild>
  </child>
</root>


XML doc components:
1. XML Prolog (Optional) . It contains two parts.
             1. XML Declaration(Example: <?xml version="1.0" encoding="UTF-8"?>)
             2.DTD Declaration
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2. root element (Mandatory)  (Document)
3. child elements
4. xml data

XML Namespaces: 
If you use same element name twice with different child elements, there will be conflict with element name. XML Namespace is used to avoid the conflict. use a prefix and define xmlns attribute with unique URL(any dummy URL) value.

Example 1:
<root>

<h:table xmlns:h="http://www.w3.org/TR/html4/">
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table xmlns:f="http://www.venkat.com/furniture">
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>

</root>

Example 2:
The namespace can also be declared at root element level:

<root
xmlns:h=
"http://www.w3.org/TR/html4/"
xmlns:f=
"http://www.w3schools.com/furniture">

<h:table>....

..............

Example 3(Default namesapce):

<table xmlns="http://www.w3.org/TR/html4/">
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table>

<table xmlns="http://venkat.com/furniture">
  <name>African Coffee Table</name>
  <width>80</width>
  <length>120</length>
</table>


XSD:
 It's the equivalent of DTD. It's the .xsd file that is defined for xml file. In xml file, xsd is referenced as shown below:

<?xml version="1.0"?>

<note xmlns="http://www.w3schools.com/venkat"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.w3schools.com/venkat note.xsd">
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

Explanation:
xmlns -  default namespace
schemaLocation - (defalut namespace) (path of .xsd file)

Valid vs Well-formed documents:
If the XML document follows the syntax, it's called well-formed document.
If the XMl document is validated against DTD or XML Schema(xsd), then it;s called both 'well-formed' and 'valid' document.

XPath:  It is a language for finding information in an XML doc.

XML Viewer: You can use any browser to view XML file.

How do you create xml from xsd using JAXB:
Sometimes, you will be getting only .xsd file and test data. You need to generate the xml file from the xsd file and test data.

1. Generate java classes from the xsd file using xjc utility
2. write a java classto set data to the generated java classes and to marshall the java objects to xml file using JAXB classes.

Static Code Analysis using SonarQube

SonarQube is a open source Quality Management Platform. Download SonarQube from http://www.sonarqube.org/downloads/ (Downloaded Zip file contains Web Server that comes with Embedded Database).

The best method to run the SonarQube platform is to set up the VM (preferabley Windows Server). Then install the SonarQube in the VM and run it there. Ensure that Firewall settings of the VM allows inbound connections.

Install SonarQube:
Unzip the downloaded .zip file and place them in your drive. If you want to configure different port, please edit sonar.properties file under /conf folder.

Run SonarQube:
Navigate to /SonarQube-5.1/bin/windows-x86-64/ folder and run startsonar.bat file. (It runs the WebServer at port number 9000).
Navigate to URL: http://localhost:9000

Analyze the code:
Install SonarQube Scanner:
SonarQube comes with separate tool for analyzing the source code. Download SonarQube Scanner (command line tool) from http://docs.sonarqube.org/display/SONAR/Installing+and+Configuring+SonarQube+Scanner

Unzip the zip file and place them in your drive.

Configure SonarQube Scanner:

Open /conf/sonar-runner.properties
Edit
#----- Default SonarQube server
sonar.host.url=http://vm-name:9000

Add the /bin directory to your path variable.

Open command prompt
type >sonar-runner -version

Run SonarQube Scanner:
Create a configuration file (sonar-project.properties) in the root directory of the java project. Specify project details, source code location (src folder path) and module names if the application has multiple modules (like web, service, common, etc).

Open command prompt. Navigate to the root directory of the java project. run >sonar-runner
This will run sonar analysis and push the analysis data to the sonarqube server's embedded database.

Navigate to the SonarQube URL and see the results in dashboard.

Note: If you want to use your own database such as oracle, MySQL, etc, please refer SonarQube Documentation for configuration steps.
If you want to run sonarqube analysis as part of Continuous Integration using Jenkins server, you can configure it in Jenkins server job.

Unit Tests Suceess report in SonarQube: SonarQube Scanner(Runner) can not execute the Unit test cases (Junit java classes), it can only analyze the testing reports. You have to run the Junit Test cases with maven or your preferred build tool (Note that running JUnit in Eclipse will not generate report) to generate the report. Then provide the report path in the sonar-project.properties. Then run the sonar analysis using SonarQube scanner. SonarQube runner uses the Java Plugin installed in SonarQube server to analyze the Junit report.

Sample configuration in Sonar-project.properties;

sonar.tests=src/test/java
#Tells SonarQube where the unit tests execution reports(Maven generated report) are 
sonar.junit.reportsPath=target/surefire-reports

Unit Test Code Coverage report in SonarQube: To collect the Unit Test Coverage, you need to launch JaCoCo tool along with your Maven build that runs Junit testcases.  Use this command: mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=true
Once you run the maven build with the above command, jacoco.exec file will be generated at folder /target/jacoco.exec

Now run the Sonar Runner with the following configuration in sonar-project.properties:

#Location of class files are required for java plugin(in SonarQube Server) to generate report out of jacoco.exec file
sonar.binaries=target/classes
#Tells SonarQube where the jacoco coverage data file(Maven generated file) are 
sonar.jacoco.reportsPath=target/jacoco.exec

Sample report at Sonar Dashboard:


Monday, October 12, 2015

Unix Shell Scripting

Unix uses Shells to accept commands given by users.

Commonly used shells: SH(Bourne SHell) CSH(C SHell) and KSH(Korn SHell
Other shells: KSH is based on SH and so is BASH(Bourne again shell). TCSH(Extended C SHell) is based on CSH.

To check which shell is installed:  echo $SHELL

Shell's command line gives interface between users and UNIX Kernel.

Frequently used commands:
sudo su -
gzip -d xyz.gz
pwd
ls -a
cd /home
./sample.sh (to execute a file)
cp
mv
date "%y,%m,%d,%h"
whoami
ps -e | grep parse
kill
hostname
ifconfig



General:
Use semicolon(;) to separate commands, backslash(\) to continue the commands in new line and pipe(|) to pass the output of one command as input to another command.

Conditional Operators:
&& - A command runs only the previous command is successful
|| - A command runs only the previous command is failed.

Group Commands:
Braces ({})  -Command runs in current shell and returns one exit status for the entire block of the command with in the braces.

Round Brackets(()) - Commands execute is subshell as single block.

Redirection:
> - redirect the output of the command to a new file (ls  > output.log)
< - pass the file as input to the command (grep venkat )

>> - redirect the output of the command to append to the existing file

File Descriptors:
0 - standard input
1 - standard output
2 - standard error ( ls 2> log1.log) - output the error to log1.log file


Substitution & patterns:
Filename substitution:
* - match any string
? - match any single character
[abcd]- match any of these characters for a single character
[a-d] - match any of the characters in the range for a single character.

Command Substitution: Output of one command as input for another command(use single quotes)
cat 'find . -name venkat*.txt' (dump all the files starts with venkat that exists in current directory to the screen)


Thursday, October 8, 2015

REST API - Fundamentals

REST is known as Representational State Transfer meaning a Representational State of the resource(Image, html document, etc) is transferred from the Server(Web Server) to Client (Web Browser). REST is an Architectural Style to design network applications. REST also has Architectural constraints, but they are not enforced. For example, in REST, POST is supposed to create a new record in the server. But we can use POST to query records from the server.

SOAP is a XML based RPC (Remote Procedure Call) over HTTP protocol using HTTP Post method. Also SOAP can be used with FTP, JMS and SMTP Transport protocols.

REST-based architectures communicate primarily through the transfer of representations of resources". This is fundamentally different from the Remote Procedure Call (RPC) approach that encapsulates the notion of invoking a procedure on the remote server. Hence, RPC messages typically contain information about the procedure to be invoked or action to be taken. This information is referred to as a verb in a Web service request. In the REST model, the only verbs allowed are GET, POST, PUT, and DELETE. In the RPC approach, typically many operations are invoked at the same URI. This is to be contrasted with the REST approach of having a unique URI for each resource.

REST has the following architectural constraints:
stateless
cachable
uniform interface (only through GET/POST/PUT/DELETE)
client-server
layered system


REST vs SOAP:
SOAP uses only POST
REST uses GET, POST, PUT, DELETE
SOAP uses only XML Message
REST uses XML, JSON, Text, csv etc.
In SOAP, Interface definition is defined in WSDL file.
In REST, there is no interface definition file. Data can be set in HTTP parameters (URL Query parameters) of GET or in HTTP Body(payload) of POST.

REST Service format:
REST service has three components:
1. Service name (or) Service Endpoint (maps.googleapis.com)
2.Unique URI(path) to identify the Resource (/maps/api/geocode/xml)
3. Query parameters if it's GET method (?param1=sx, param2=123)

Example:
Resource 1: http://maps.googleapis.com/maps/api/geocode/xml ? param1=sx, param2=123
Resource 2: http://maps.googleapis.com/maps/api/geocode/json

Sample REST raw GET request:

GET http://maps.googleapis.com/maps/api/geocode/xml?address=Parkway%2C%20Mountain%20View%2C%20CA&sensor=false HTTP/1.1
Accept-Encoding: gzip,deflate
Host: maps.googleapis.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

Sample REST Raw GET Response:

HTTP/1.1 200 OK
Date: Thu, 15 Oct 2015 09:08:16 GMT
Expires: Fri, 16 Oct 2015 09:08:16 GMT
Cache-Control: public, max-age=86400
Vary: Accept-Language
Access-Control-Allow-Origin: *
Content-Encoding: gzip
Server: mafe
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Content-Length: 555
Content-Type: application/xml; charset=UTF-8
Connection: keep-alive

XML Document>
 .......
...........

Sample SOAP Web Service Request (always use POST method):



Monday, October 5, 2015

JSON Basics

JSON is the abbreviation for  Javascript Object Notation. It's a lightweight data interchange format - Text Format that is independent of any programming language.  It's also a syntax for storing and retrieving javascript objects.

JSON is built on two structures:
1. Name/Value Pairs (something similar to Object in Java)
2. An ordered list of values (something similar to Array in Java)

Example 1:
{"firstName":"venkat", "lastName","chinna"}

Example 2 (Arrays):
"employees": [{"firstName":"venkat", "lastName","chinna"}, {"firstName":"john", "lastName","miller"}]

Example 3 (JSON Object):
"window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    }

Data types:
int, string, float, boolean, array, object, null

JSON vs XML:

JSON
XML
Provides automatic way of serializing/de-serializing javascript objects
Need to write code to do the same.
Format follows simple name/value pairs.
Complex, verbose format with tags and namespaces.
It’s data oriented
It’s document oriented.

Monday, September 28, 2015

Selenium-Cucumber with Ruby

Install Ruby:
Install Ruby from http://rubyinstaller.org/. This webpage has the link to download Ruby Installer that installs Ruby Language and Execution Environment. While installing Ruby, choose the check box to add Ruby to PATH variable:



Configure Ruby (if you don't choose option as above during installation):
Add 'C:\Ruby200-x64\bin' to PATH variable.

Check the installation:
Open CMD
type >ruby -v  (This will show Ruby version)

Install Ruby Devkit:
Go to URl http://rubyinstaller.org/downloads/ and download appropriate Devkit.



Double click the Devkit  exe file and extract the content to C:\devkit

Initialize and install the Devkit:
Open CMD
>cd c:\devkit
>dk.rb init
>dk.rb install
The above steps install gem(package manager for ruby) which is part of devkit.

Install Selenium-cucumber(uses gem):
Open CMD
>gem install selenium-cucumber
>gem list (to check if selenium-cucumber is installed properly)
>selenium-cucumber version

Install Sublime Text Editor:
Download from http://www.sublimetext.com/2 and install it.

Download Cucumber Sublime Bundle that provides syntax coloring and snippets for cucumber nd gherkin language from (https://github.com/drewda/cucumber-sublime-bundle)
Download it as ZIP.

Open Sublime
Preferences->Browse Packages...
It will open the package folder  (C:\Users\\AppData\Roaming\Sublime Text 2\Packages).
Copy the downloaded files here.
Close and open Sublime again
View->syntax->Cucumber-selenium->Gherkin

Writing test scripts:
Set up your project:
Open CMD
create your project folder
change directory to project folder
creature feature skeleton (>selenium-cucumber gen)











Open my_first.feature file in sublime.

save the file as MyFirstScript.feature
modify the file using predefined steps as defined in https://github.com/selenium-cucumber/selenium-cucumber-ruby .
Also you can find the predefined steps here: https://github.com/selenium-cucumber/selenium-cucumber-ruby/blob/master/doc/canned_steps.md
Types of predefined steps:

Sample MyFirstScript.feature:
Feature: Login feature

  Scenario: As a valid user I can log into my web app
    Given I navigate to "http://www.rediff.com"
    Then I should see page title having partial text as "Rediff.com:"

Run the script (feature file):
Open CMD
Navigate to the root folder of the project
>cucumber (if you have single feature file)
or
>cucumber features\MyFirstScript.feature (if you have multiple feature files)

Custom Steps:
If you don't find Predefined steps, you can write custom steps using RUBY language  in step_definitions/custom_steps.rb file

Step definition is similar to Method Definition or Function Definition in any kind of OO/script languages. They can take 0 or more arguments. We can use Selenium-cucumber Ruby APIs to write custom step definitions (https://github.com/selenium-cucumber/selenium-cucumber-ruby/blob/master/doc/selenium-cucumber-API.md).

Sample MyFirstScript.feature (with custom steps)

Feature: Login feature

  Scenario: As a valid user I can log into my web app
    Given I navigate to "http://www.rediff.com"
    Then I should see page title having partial text as "Rediff.com:"
    And I should open yahoo
    

   Scenario: As a valid user I can log google
    Given I open google

sample custom_steps.rb (with custom step definitions)

require 'selenium-cucumber'

# Do Not Remove This File
# Add your custom steps here
# $driver is instance of webdriver use this instance to write your custom code
And(/^I should open yahoo$/) do
navigate_to("http://www.yahoo.com")
end

Given(/^I open google$/) do
navigate_to("http://www.google.com")

end