Reading JSON data in Angular JS (Part-1)



Scenario: - How to fetch JSON data from backend into User Interface (UI) of application using Angular JS?

Solution:-
We have divided our solution approach into 2 parts; in part-1, tutorial we would fetch the data in a straight flow, without thinking about the framework level things.

Flow:-
Reading json data using service

Code:-
JS File

//Creat app module
var app = angular.module('myApp',[])
//Creating the controller constructor
app.controller('familyCtrl',function($scope,$http){
 //Calling the get method of service 
 $http.get("data/family.json").then(function(response){
  $scope.myData = response.data;
 });

});

html file

<!DOCTYPE html>
<html ng-app="myApp">
 <head>
  <script type="text/javascript" src="js/angular.min.js"></script>
  <script type="text/javascript" src="js/controllerRead.js"></script>
  <link rel="stylesheet" type="text/css" href="css/family.css">
 </head>
 <body>
  <div  ng-controller="familyCtrl">
   <p>Reading JSON data using AngularJS Service component</p> 
   <ul>
     <li ng-repeat="x in myData.family">
       {{ x.age + ', ' +  x.role }} 
     </li>
     <li>
      House No = {{myData.house[0].no}}
     </li>
      <li>
       Income = {{myData.income}}
     </li>
   </ul>

  </div>
 </body>
</html>

Explanation:-

JS File:- it contains the business logic, which uses the get method of the service ($http), to fetch data from JSON, on success it binds data to scope (myData).

JSON File:-It consists of our JSON data. Few points to know for beginners in JSON
HTML File:-Consists of ng-app (bootstrap angular), ng-controller (using this directive to link to controller - controllerRead.js), ng-repeat (to repeat it for all the values of json element) and lastly we use the scope to display the fetched data from JSON.








Importing an library in Robot Framework comes in RED color.

Issue: - We tried adding the Selenium2Libray in robot framework but its shown in red color(in settings) and we are not able to implement any of the keywords.


library comes in red in robot framework


Root cause: - Anything coming in red in settings after importing generally means that particular library is not properly installed.

Let’s check whether we were able to install selenium2library.
Type python in cmd and type "import selenium2library", in our case it would give the error message something call stack error.
We can visualize the same error after importing selenium2library in ride and going to Tools > View in ride.
View log in Robot framework

We found some files were missing like in our case its decorator dependency that RIDE is not able to locate when loading "Selenium2Library"
No module decorator robot framework
Note: - Most of the time it’s some missing files that are not installed which results in libraries name coming in red on adding to RIDE. Like same can happen on importing "ExcelLibrary" or "Database library"

Solution:- 
Go ahead and install the missing dependency, in our case its "decorator"
Download the dependency
Open the command prompt to the extracted downloaded file
setup.py install

And we are done.

Note:- As discussed same solution can be applied to any external library that is coming as red on importing into RIDE inside settings tab.

Learn Robot Framework

New input types in HTML5

Stop managing your time. 
Start managing your focus.

We already discussed the new form elements in HTML5. Today we would highlight the new input types in HTML5. 

How many of them was introduced?
13 New Input Types.

Follow us to learn more .. FbG+,Twitter

Does my browser Supports them?
Check the compatibility chart. If any of the browser does not supports it would be rendered as text type(default type for a input type).






Functionality:- 



Robot Framework: - Cannot import name XmlElementHandler Error

After upgrading to the latest version of Robot Framework and RIDE and rerunning existing scripts, we were surprised to get the “Cannot import name XmlElementHandler”


Solution:-
Step1:- Try to locate the missing file in robot setup that is installed on the system.
In our case it was XmlElementHandler, after some investigation (by searching the folders of robotframework installation :) ) we located the file :-
Path in our case: - “C:\Python27\Lib\site-packages\(robotframework-version)\robot\result

Step2:- Open the erroneous file (XmlElementHandler.py)

Step3:- In our case it was empty, so we googled it out to find the latest file
Step4:- Replaced the file and our scripts are up and running :p


Note:- Incase the copied files from robotframework setup location on internet contains some extra character do remove them before running ( in our case [docs] was coming extra)

Learn Robot Framework
Learn Selenium

Robot Framework- Working with Excel Library(Part-3)

After our installation and fetching data from excel we would move towards writing data into excel.
Robot Framework with Excel Library

Scenario:- How to publish robot framework run results into Excel?

We can connect on Fb,G+,Twitter to find what we do next?.

Solution:- 
Step1:- Create excel at run time with given heading inside Results folder.(At "Suite Setup")
Step2:- Add test details (at Test Case "Setup") to newly created excel
Step3:- Append the results(pass/fail) to the excel after the run of the Test case in Robot Framework.(at Test Case "Teardown")



Code:-

*** Settings ***
Suite Setup       Create Result File
Library           ExcelLibrary
Library           OperatingSystem
Library           robot.api.logger
Library           Selenium2Library
Library           String

*** Variables ***
${sFileName}      ${EXECDIR}\\Data\\TestSheet.xls    # DataSheet
${sResultFilePath}    ${EXECDIR}\\Results\\Results    # ResultFile

*** Test Cases ***
TC01_Fetch Data From Excel & Write Results
    [Setup]    Append Test Details to Results File
    Open given Excel file    ${sFileName}
    ${sURL}    Get data from Excel with given column    Data    TC_02    Url
    Log    ${sURL}
    [Teardown]    Publish Test Case Result

*** Keywords ***
Open given Excel file
    [Arguments]    ${sFileName}
    #Check that the given Excel Exists
    ${inputfileStatus}    ${msg}    Run Keyword And Ignore Error    OperatingSystem.File Should Exist    ${sFileName}
    Run Keyword If    "${inputfileStatus}"=="PASS"    info    ${sFileName} Test data file exist    ELSE    Fail    Cannot locate the given Excel file.
    Open Excel    ${sFileName}

Get data from Excel with given column
    [Arguments]    ${sSheetName}    ${sTestCaseNo}    ${sColumnName}
    log    ${sColumnName}
    ${colCount}    Get Column Count    ${sSheetName}
    : FOR    ${y}    IN RANGE    0    ${colCount}
    \    ${header}    Read Cell Data By Coordinates    ${sSheetName}    ${y}    0
    \    #Check if this is the given header
    \    Run Keyword If    "${header}"=="${sColumnName}"    Set Test Variable    ${colNum}    ${y}
    log    ${colNum}
    #Get the total rows in the Sheet
    ${iTotalRows}    ExcelLibrary.Get Row Count    ${sSheetName}
    : FOR    ${iRowNo}    IN RANGE    1    ${iTotalRows}+1
    \    ${TC_Num}    Read Cell Data By Coordinates    ${sSheetName}    0    ${iRowNo}
    \    #Incase TestCase No is same , fetch the data from same row and given column No
    \    ${sSearchedData}    Run Keyword If    "${sTestCaseNo}"=="${TC_Num}"    ExcelLibrary.Read Cell Data By Coordinates    ${sSheetName}    ${colNum}
    \    ...    ${iRowNo}
    \    Run Keyword If    "${sTestCaseNo}"=="${TC_Num}"    Exit For Loop
    [Return]    ${sSearchedData}

Create Result File
    [Documentation]    Create a result file at runtime
    ${sResultFile}    Add TimeStamp to File    ${sResultFilePath}
    #Create Result file at the given location
    Create File    ${sResultFile}
    Append to file    ${sResultFile}    TC_Number\tTC_Desc\tStatus\tComments\n
    Set Global Variable    ${sResultFile}    ${sResultFile}

Add TimeStamp to File
    [Arguments]    ${sResultFilePath}
    [Documentation]    Creating the Name of the Results file based on the current TimeStamp
    ${date}    OperatingSystem.Run    echo %date%
    ${yyyy}    ${mm}    ${dd}    Split String    ${date}    /
    ${time}    OperatingSystem.Run    echo %time%
    ${hh}    ${min}    ${sec}    Split String    ${time}    :
    #Creating TimeStamp
    ${timestamp}    Set Variable    ${mm}${dd}${yyyy}_${hh}${min}
    log    ${timestamp}
    ${sfileName}    Set Variable    ${sResultFilePath}_${timestamp}.xls
    [Return]    ${sfileName}

Append Test Details to Results File
    [Documentation]    Adding Test Case Details to Results File
    #Fetch TC No
    ${sTCNo}    ${sTestDesc}    Split String    ${TESTNAME}    _
    append to file    ${sResultFile}    ${sTCNo}\t${sTestDesc}\t

Publish Test Case Result
    [Documentation]    Adding TC fail/pass results to Result File
    append to file    ${sResultFile}    ${TEST STATUS}\t${TEST MESSAGE}\n
    Close All Browsers


Learn more Robot Framework
Automation Testing using Selenium                                                                               

Robot Framework- Working with Excel Library(Part-2)

Life is short..share while you still have knowledge.

We started with installation of Excel Library in Robot Framework , today we would focus on utilizing the keywords present in the Excel Library.
Next is publishing run results into Excel

We can connect on Fb,G+,Twitter to spread more learning.

Scenario:- How we can fetch data from Excel in Robot framework to parameter our test cases.

Solution:- We need to implement ExcelLibrary keywords.

Inbuilt Keywords Used in our Example:-

  • Open Excel
  • Get Column Count
  • Read Cell Data by Coordinates
  • Get Row Count
Download code from GitHub


Code:-
*** Settings ***
Library           ExcelLibrary
Library           OperatingSystem
Library           robot.api.logger

*** Variables ***
${sFileName}      ${EXECDIR}\\Data\\TestSheet.xls    # DataSheet

*** Test Cases ***
TC_Fetch Data from Excell
    [Documentation]    Fetching the data from given Row and Column Name
    Open given Excel file    ${sFileName}
    #Passing SheetName,RowDetails and ColumnName, to fetch data
    ${sData}    Get data from Excel with given column    DataSheet    TC_02    UserName
    Log    ${sData}

*** Keywords ***
Open given Excel file
    [Arguments]    ${sFileName}
    #Check that the given Excel Exists
    ${inputfileStatus}    ${msg}    Run Keyword And Ignore Error    OperatingSystem.File Should Exist    ${sFileName}
    Run Keyword If    "${inputfileStatus}"=="PASS"    info    ${sFileName} Test data file exist    ELSE    Fail    Cannot locate the given Excel file.
    Open Excel    ${sFileName}

Get data from Excel with given column
    [Arguments]    ${sSheetName}    ${sTestCaseNo}    ${sColumnName}
    log    ${sColumnName}
    ${colCount}    Get Column Count    ${sSheetName}
    : FOR    ${y}    IN RANGE    0    ${colCount}
    \    ${header}    Read Cell Data By Coordinates    ${sSheetName}    ${y}    0
    \    #Check if this is the given header
    \    Run Keyword If    "${header}"=="${sColumnName}"    Set Test Variable    ${colNum}    ${y}
    log    ${colNum}
    #Get the total rows in the Sheet
    ${iTotalRows}    ExcelLibrary.Get Row Count    ${sSheetName}
    : FOR    ${iRowNo}    IN RANGE    1    ${iTotalRows}+1
    \    ${TC_Num}    Read Cell Data By Coordinates    ${sSheetName}    0    ${iRowNo}
    \    #Incase TestCase No is same , fetch the data from same row and given column No
    \    ${sSearchedData}    Run Keyword If    "${sTestCaseNo}"=="${TC_Num}"    ExcelLibrary.Read Cell Data By Coordinates    ${sSheetName}    ${colNum}
    \    ...    ${iRowNo}
    \    Run Keyword If    "${sTestCaseNo}"=="${TC_Num}"    Exit For Loop
    [Return]    ${sSearchedData}

Learn RobotFramework
Selenium Turorials

Robot Framework- Working with Excel Library(Part-1)

In part-1 we would discuss on Installing Excel Library to work with Robot Framework.

True Success is balance. For me, this means Health, Wealth, Happiness.
So I try and get 1% better each week in each of these areas.
Lets learn the robot framework integration with Robot Framework to enhance our skills.

Scenario: - How we can install Excel Library to work with excel files using Robot Framework.
Solution:-
Download:-
Dependencies:-

After extracting all the files we need to install all of them:-
Downloaded files of Excel library



Installation:-
Using command prompt browse to the location where files are located and use the command
Setup.py install

Installing Excel Library in Robot Framework

Repeat the above process for all the dependencies.

Verify:-
We can check that we have successfully installed all the individual components by typing python, followed by import “folder Name” in the command window after the installation.

For example:-

ExcelLibrary

Verify Excel library is installed

natsort

Verify Excel library dependencies are installed



Note: - We cannot use .xlsx extensions, due to limited support thus we need to stick to .xls extension(Excel 97-2003).

In our next post we are going to explore more on Excel integration with Robot Framework.