Tuesday, May 6, 2014

Getting Standard/Inner Text

The code below shows how you can get the text from any standard object using diff way

Standard Examples
'Get text from a ListView - report style (for example, Windows Explorer)
Msgbox Window("C:\Program Files\Mercury").WinListView("SysListView32").GetSubItem(1,2)

'Get text from a toolbar
Msgbox Window("C:\Program Files\Mercury").WinToolbar("ToolbarWindow32").GetItem(1)

'Text of a message-box, the Static control is not in the object repository/shared object ' 'repository
set StatObj = Dialog("DlgTB").Dialog("DlgTB").Static("nativeclass:=Static","Index:=1")
Msgbox StatObj.GetROProperty("text")

Web Example
' An example for a Web Table (in the Mercury Tour site)
Set Cell = Browser("Welcome: Mercury Tours").Page("Select a Flight: Mercury").WebTable("RETURN").ChildItem(3, 2, "WebElement", 0)
Msgbox Cell.GetROProperty("innertext")

Visual Basic Example
'Retrieving the text of a label in a VB form: (There is no Test Object for a label)
Msgbox VbWindow("VBForm1").Object.Controls("Label1").caption

.NET Example
'Get the text of items in a CheckListBox (has no Test Object)
'Get run-time object reference
Set CustList = SwfWindow("Win").SwfObject(ìlst").Object

'Get Items collection
Set ListItems = CustList.get_Items()

'Get Items count
ItemsCount = ListItems.Count

'Loop over all items
For i = 0 to ItemsCount-1
text = text & ListItems.Item(i)
Next

ActiveX Example
This example will retrieve the text from the Visual Basic Flight 1A sample application's flight grid
This retrieves the flight number from the 3rd row.

VbWindow("VbWindow").VbWindow("VbWindow").AcxTable("Grid Control").Object.Row = 3
VbWindow("VbWindow").VbWindow("VbWindow").AcxTable("Grid Control").Object.Col = 0

Msgbox VbWindow("VbWindow").VbWindow("VbWindow").AcxTable("Grid Control").Object.Text

Count & Close All Browsers

QTP Script to get total count, names of all open browsers and to close them using descriptive programming.

Set ab=Description.Create
ab("micclass").value="Browser"
Set obj=Desktop.ChildObjects(ab)
Msgbox obj.count
For i=0 to obj.count-1
                c=obj(i).getroproperty("name")
                Msgbox(c)
                obj(i).Close

Next

Wednesday, April 30, 2014

How to search for a specific string in a MS Word document

The following example uses Word object methods to open a Microsoft Word Document and retrieve paragraphs from it. Then the InStr VBScript method is used to check for the word “contract.”


Example:

Dim wrdApp
Dim wrdDoc
Dim tString, tRange
Dim p, startRange, endRange
Dim searchString

‘Create the Word Object
Set wrdApp = CreateObject(”Word.Application”)
Set wrdDoc = wrdApp.Documents.Open(”C:\Temp\Word_Doc.doc”)     ‘replace the file with your MSDoc
searchString = “contract”                                   ‘replace this with the text you’re searching for

With wrdDoc
     For p = 1 To .Paragraphs.Count
          startRange = .Paragraphs(p).Range.Start
          endRange = .Paragraphs(p).Range.End
          Set tRange = .Range(startRange, endRange)
          tString = tRange.Text
          tString = Left(tString, Len(tString) - 1)                           ‘exclude the paragraph-mark
          If InStr(1, tString, searchString) > 0 Then                     ‘check if the text has the content you want
               
               msgbox “Yes! ” & searchString & ” is present”
         Exit For
          End If
     Next
.Close                    ‘close the document
End With

wrdApp.Quit      ‘close the Word application

Set wrdDoc = Nothing

Sunday, April 27, 2014

Java best practice for Selenium

1. Package declaration should start with com. and followed by your companyname
2. Class name should start with capital letter
3. Method name should start with small letter
4. Global variable name should be in capital letter and each word in name should be separated by '_'  like MAX_CAPACITY
5. Follow the following prefix data types
string - str
int - i,
float - f
long - l
double - d
List  - list
Map - map
string array -  strArr
int array - iArr

6. Variable name should be in CamelCase like getRowId, strUserNamen
7. Every object in selenium is webelement. However to use approriately in script while you have number of weblements present in pagefactory class please follow the following naming convention.

webedit - txt
webbutton - btn
link    - lnk
combo box - cbk
weblist - lst
check box - chk
frame - fra
Dialog - dlg
image - img



Friday, April 4, 2014

How to Identify objects/Web Elements Properties in Selnium

In qtp we all know how to get object/web elements properties -  using object spy.

Same with the selenium its quite easy please see diff way to find web elements properties:

1. Using Crome

This is the fast and most efficient way to find web elements. Nagivate to your app url --> right click on the web element --> select Inspect Element. please find the below screen shot.




2. Using Firefox

Right Click on web element and select Inspect Element.




You can install fire-bug add in on fire fox and find properties by right clicking on web element.



with firebug you can also copy xpath and css values by right clicking on firebug pan.
You can also use IDE. Record the business process and ide will store all object properties.

3. Using IE:

Open IE--> Navigate to URL--> Press F12

Please see the screen shot with number tag.



Thursday, April 3, 2014

Vbscript to copy files from source to dest folder


Have you ever faced a situation where you need to copy files from one location and paste them to destination location repetitively with some specific constrains. Like

I had an requirement where i need to copy 200 files from source and paste them to destination folder, only if there is no file present in dest folder. Here dest folder process the file one by one and load it to database. once all files are processed again i need to copy 200 files and paste it. continues human attention required to check destination file count reach to zero. To overcome this,  here is a simple vbscript code

This vbscript program  will check if there any file on dest folder. if it found no file then it will copy files from source to given destination path. once 200 files are copied then it will break as it goes into  if condition. You can schedule this program to run specified time.

========================================================================


Const destPATH = "D:\dest\Files\"
Const sourcePATH = "D:\source\Files\"
dim fso
dim destfolder,sourcefldr,i
i=0
set fso = CreateObject("Scripting.FileSystemObject")
set destfolder = fso.getFolder(destPATH)
set sourcefolder = fso.getFolder(sourcePATH)
               
If destfolder.files.Count <= 0 then
                                Set sourcefldr=fso.getFolder(sourcePATH)
                                                if sourcefldr.files.count > 0 then           
                                                                for each file in sourcefldr.files              
                                                                                fso.MoveFile file, destfolder + "\"                                                                        
                                                                                i=i+1
                                                                                                if i >=200 then                                                                                       
                                                                                                                exit for
                                                                                                end if
                                                                next
                                                end if      

end if

set fso = nothing
set destfolder = nothing
set sourcefolder = nothing

Read & Update Excel file in Selenium

To load and use test data in automation script is a key element in  Automation script execution.  Today we are going to discuss about loading excel files in selenium. There are number of API  are there to support Excel like jExcelAPi, Apache POI.   I found using JDBC connection is the most flexible and method rich option.

How to load excel file in memory:

by giving path of excel file we can load n number of diff excel files to memory for further use:

public void loadExcel(String path) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
this.con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};Dbq=" + path + ";DriverID=790;READONLY=false;");
} catch (Exception e) {
e.printStackTrace();

}
}


How to read full Row in Excel:

You need to give two parameters sheet name and row id. So you can retrieve any row id from given sheet.

public Map<String, String> getRowbyID(String sheet, String id) throws SQLException {
Map<String, String> map = new HashMap<>();
String strQuery = "SELECT * FROM [" + sheet + "$] WHERE [ID]= '" + id + "'";
try {
st = con.createStatement();
resultSet = st.executeQuery(strQuery);
ResultSetMetaData rmd = resultSet.getMetaData();
while (resultSet.next()) {
int columntCount = rmd.getColumnCount();
for (int i = 1; i <= columntCount; i++) {
map.put(rmd.getColumnName(i), resultSet.getString(i));
}
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return map;
}

This will return Map having two values one for column and second for value. You can retrieve row value by giving column name.

How to update Excel sheet:

You can write any costume query in the following method and update the appropriate value. Like the following example update the status to yes after given module execution.


public void updateExecutionConfig(String strModuleName, String strTestCategory) throws SQLException {
String strQuery = "update [Sheetname$] set Executed = 'Yes' where ModuleName = '" + strModuleName + "' and TestCategory = '" + strTestCategory + "'";
st = con.createStatement();
int updateRow = st.executeUpdate(strQuery);
System.out.println("updateRow = " + updateRow);
}


These are some examples you can write any costume method appropriate to your selenium framework need.