Sunday, 29 December 2019

Iterator in Java

package collectionInterface;
import java.util.ArrayList;
import java.util.Iterator;

public class ListIteratorDemo {
public static void main(String[] args) {

ArrayList<String> al = new ArrayList<String>();

for (int i = 0; i < 5; i++) {
al.add(i + " item ");
}

Iterator il = al.iterator();

// #Printing all elements
while (il.hasNext()) {
System.out.println(il.next());
         }

System.err.println("________________________");
       

//Printing element based on condition
Iterator il2 = al.iterator();
while (il2.hasNext()) {

String str = (String) il2.next();
if (str.contains("4")) {

System.out.println(str);
}
}

}

}


/*OUTPUT########################################3
0 item
1 item
2 item
3 item
4 item
________________________
4 item
 */

ArrayList in Java

package collectionInterface;
import java.util.ArrayList;

public class ArrayListDemo {
public static void main(String[] args) {

/*ArrayList
1. Best for item retrieval
2. Worst for inserting item in the middle. LinkedList best for middle insertion.
3. Implements Random Access Interface which allows any index access with same speed
4. NOT THREAD SAFE. Multiple threads can access simultaneously. With Collections                           class thread safety can be achieved

5.   ArrayList<String> al=new ArrayList<String>(); 
      Creates empty array list with default initial capacity 10
     
      When arrayList reaches its maximum capacity new array list is created.Old elements                            are copied into it and references moved.
     
     new capacity=(current capacity *3/2)+1
*/



ArrayList<String> al=new ArrayList<String>();
System.out.println(al.size());     //0       Physical Size 0 as no item added
System.out.println(al.isEmpty());  //true    Since List is empty
    System.out.println(al.add("first"));  //true    Will add item at 0 index as first item
System.out.println(al.isEmpty());  //false   Since List is not empty now
   
System.out.println(al.size());    //1        Physical Size
al.add(1, "sagar");               //         Add "sagar" at index 1
System.out.println(al.size());  //2          Since 2 items now with index 0 and 1

System.out.println(al);        //[10, sagar]
al.add(1, "Sumit");
System.out.println(al);   ;  // [10, Sumit, sagar] Adding new element "Sumit" SHIFTS the                                                               list to right
/*
* Now list has two items (0,1) and we are trying to add value to index 3
* al.add(4,"test"); //Exception in thread "main"
* java.lang.IndexOutOfBoundsException: Index: 3, Size: 2
*
*/

/* Adding int data in String type arrayList will result in exception
System.out.println(al.add(10));  //Exception
The method add(int, String) in the type ArrayList<String> is not applicable for the arguments (int)
*/


System.out.println(al.contains("10")); //true
System.out.println(al.contains(8)); //false (No exception just warning when we use contains() for different data type)
System.out.println(al.indexOf("Sumit")); //1  first occurrence of "Sumit"
    System.out.println(al.indexOf("sumit")); //-1
    System.out.println(al.lastIndexOf("S"));//-1
       
    //Reading from arrayList
   
    System.out.println(al.get(0)); //first
   
    /* 
    System.out.println(al.get(-1)); //Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    System.out.println(al.get(5)); //Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 3
     */
   
    System.out.println(al.size()); //3
    // System.out.println(al.remove(5)); //Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 3
   
    System.out.println(al.remove(2)); //returns value at specified index or throws exception
    System.out.println(al.size()); // 2
   
   
    System.out.println(al);  //[first, Sumit]
   
 
   
    System.out.println(al.add("latest"));
    System.out.println(al);  //[first, Sumit, latest]
   
    System.out.println((al.set(2, "sagar"))); //  "Latest" returns old value at that index
   
    System.out.println(al); //[first, Sumit, sagar]
   
    // Looping######################################################33
   
    for(int i=0;i<al.size();i++) {
   
    System.out.println(al.get(i));
    //first
    //Sumit
    //sagar
    } 
   
    for(String s:al) {
    System.out.println(s);
    //first
    //Sumit
    //sagar      }
   
   
   // al.forEach((a)->System.out.println(a));
   
   
ArrayList<String> al2=new  ArrayList<String>();
al2=al;

System.out.println(al2.equals(al)); //true

ArrayList<String> al3=new  ArrayList<String>();
System.out.println(al3.equals(al)); //false


   
   
   
    }
}
}

Thursday, 18 July 2019

UFT:'ReadParameter_FN

'@Description This function reads the action parameter value and returns back.
'@Documentation This function reads the action parameter value and returns back.
Function ReadParameter_FN(str_Name)

Err.Clear
Dim tempValue
tempValue = str_Name
On Error Resume Next

dataSource = "Global"
ReadParameter_FN = Datatable.Value(str_Name,dataSource)

If Err.Number <> 0 Then
Err.Clear
dataSource = Environment.Value("ActionName")
If Environment.Value("localRowIndex") = 0 Then
Environment.Value("localRowIndex") = 1
End If
datatable.LocalSheet.SetCurrentRow Environment.Value("localRowIndex")
ReadParameter_FN = Datatable.Value(str_Name,dataSource)
End If


If Err.Number <> 0 Then
Call ReportEvent_FN("Fail", "Read Value from Datasheet", "Reading parameter : "&str_Name&" from Datasheet is failed. Description:"&Err.Description)
End If

Err.Clear

End Function

UFT: WriteParameter_FN

'@Description This function writes the action parameter value and returns back.
'@Documentation This function writes the action parameter value and returns back.
Function WriteParameter_FN(str_Name, str_Value)

Err.Clear
     On Error Resume Next
Datatable.Value(str_Name,dataSource) = str_Value

If Err.Number <> 0 Then
DataTable.getSheet(dataSource).AddParameter str_Name, str_Value
Err.Clear
End If

If dataSource <> "Global" Then
Datatable.Value(str_Name,"Global") = str_Value
If Err.Number <> 0 Then
DataTable.getSheet("Global").AddParameter str_Name, str_Value
Err.Clear
End If
End If
Err.Clear

End Function

UFT ValidateRegularPattern_FN function

Function ValidateRegularPattern_FN(str_Pattern, str_Value)
Err.Clear

Dim regEx, Match

Set regEx = New RegExp   ' Create a regular expression.
regEx.Pattern = str_Pattern   ' Set pattern.
regEx.IgnoreCase = True   ' Set case insensitivity.
regEx.Global = True   ' Set global applicability.
Match  = regEx.Test(str_Value)   ' Execute search.
 
ValidateRegularPattern_FN = Match

Err.Clear

End Function

Saturday, 25 May 2019

UFT: Using Class File in UFT Action

'###CLASS FILE

ExecuteGlobal "Dim Reporter"
Set Reporter = New HtmlReporter


Class HtmlReporter
 
Public Sub Class_Initialize(  )
   
    print "started"
   
    Systemutil.CloseProcessByName "iexplore.exe"
    Systemutil.Run "iexplore.exe"
End Sub
   
'When Object is Set to Nothing
public Sub Class_Terminate(  )
       
    Systemutil.CloseProcessByName "iexplore.exe"
   
    print "ended"
End Sub
   
End Class



'###ACTION


msgbox datatable.Value("Value")


'#DATATABLE
Value
1
2
3

Saturday, 11 May 2019

ADODB my SQL Connection with VbScript

Function test()

On Error Resume Next

Set obj = CreateObject("ADODB.Connection")
Set obj1 = CreateObject("ADODB.RecordSet")
Dim dbquery
dbquery = "INSERT INTO test (firstname,lastname) VALUES ('neha','deshmukh');"

obj.Open "Server=db4free.net;Data Source=mysql; Database=sumit1234; User=sumit1234; Password=sumit1234;"

MsgBox obj1.State & ""

MsgBox Err.Description
obj1.Open dbquery, obj

MsgBox Err.Description

obj.Close
obj1.Close
Set obj1 = Nothing
Set obj = Nothing


End Function

pic

 qt...