Questions ?

Thursday, August 8, 2013

Binding for separate threads

I had been in struggling with binding data into Grid using ObservableCollection because of exception "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread" then I just figured out that: try to adding items to this collection "ObservableCollection" out of the main thread, we will get the exception mentioned above. Searching around, found http://www.thomaslevesque.com/tag/mvvm/ this help me! Actually, I wanna to say big thanks Thomas to share his knowledge.

Here are solutions: using SynchronizationContext since it is the one collection was created. Below is implementation:

public class AsyncObservableCollection<T> : ObservableCollection<T>
{
    private SynchronizationContext _synchronizationContext = SynchronizationContext.Current;
 
    public AsyncObservableCollection()
    {
    }
 
    public AsyncObservableCollection(IEnumerable<T> list)
        : base(list)
    {
    }
 
    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        if (SynchronizationContext.Current == _synchronizationContext)
        {
            // Execute the CollectionChanged event on the current thread
            RaiseCollectionChanged(e);
        }
        else
        {
            // Post the CollectionChanged event on the creator thread
            _synchronizationContext.Post(RaiseCollectionChanged, e);
        }
    }
 
    private void RaiseCollectionChanged(object param)
    {
        // We are in the creator thread, call the base implementation directly
        base.OnCollectionChanged((NotifyCollectionChangedEventArgs)param);
    }
 
    protected override void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (SynchronizationContext.Current == _synchronizationContext)
        {
            // Execute the PropertyChanged event on the current thread
            RaisePropertyChanged(e);
        }
        else
        {
            // Post the PropertyChanged event on the creator thread
            _synchronizationContext.Post(RaisePropertyChanged, e);
        }
    }
 
    private void RaisePropertyChanged(object param)
    {
        // We are in the creator thread, call the base implementation directly
        base.OnPropertyChanged((PropertyChangedEventArgs)param);
    }
}

Note: instances of this class must be created on the UI thread (inside Dispatcher)

Friday, June 29, 2012

Why do I get 'Invalid procedure call or argument'?

After a long searching I have found this link: http://classicasp.aspfaq.com/files/directories-fso/why-do-i-get-invalid-procedure-call-or-argument.html

it pointed out some possible cause and I have found that the last one is useful for my case.

This often happens because you used a VBScript "friendly name" constant in place of its integer equivalent. Visual Basic understands these friendly names, such as FileSystemObject's 'forAppending' and 'forWriting' constants. 
 
An easy solution is to add this line to any include files you use in every page (or else add it at the top of every page, if you don't have a common header in your application): 

<%
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
%>
 


 
Another possible cause is trying to use a string operation (like Mid(), InStr(), Left() or Right()) on a NULL value. So for example: 
 
<%
    response.write Left(rs("column"), 10)
%>
 
Should be: 
 
<%
    if len(rs("column")) > 0 then
        response.write Left(rs("column"), 10)
    else
        response.write " "
    end if
%>
 
If you're having these problems with NULL values coming out of a database, see Article #2150
 

 
Another possibility is that you are coming over from JavaScript, or otherwise think that string lengths are 0-based. The following code sample will cause this error: 
 
<%
    str = "foo"
    response.write Mid(str, 0, 1)

%>
 
Similarly, passing a starting argument of 0 to the Instr() function will cause the same error. To solve this problem, always start at 1 for string parsing in VBScript. 
 

 
Still one more possibility is that you have unexpected characters in an FSO writeline call. I recently came across this when using MSXML to write out contents of dynamic ASP files into static HTML files. I had this: 
 
<%
    set fs = fso.CreateTextFile("blat.htm", true)
    fs.writeline xmlHTML
%>
 
When I ran this code, I received the above error. When I added the unicode format designator, as follows, everything worked fine: 
 
<%
    set fs = fso.CreateTextFile("blat.htm", true, -1)
    fs.writeline xmlHTML
%>

Monday, May 21, 2012

Some experience with QTP 11

I have encountered many QTP's issues since working with AUT (dot Net 4.0) both 32 bit and 64 bit, QTP will intend to crash after long run ...solution I found here it: update latest hotfix for QTP.






Monday, December 19, 2011

Select custom button in SwfToolbar

/[TestComplete JScript]==============================
// Function:    SelectMainMenu
// Description: selects a menu item from Infragistic Main menu
// Parameters:
//  -pProcess  - process of the application
//  -oMenu     - main menu, object
//  -sMenuItem - menu item to select, string 
// Return: <none>
// NOTES: menu items should be splitted with "|" symbol, e.g. "File|Options", "Administration|Edit|Account Roles" 
function SelectMainMenu(pProcess, oMenu, sMenuItem)
{
  var aMenu = sMenuItem.split("|");
  var i, j; // counter
  var iX = 0, iY = 0; // coordinates for clicking
  var iCount; // menu items count for each toolbar/popup menu
  var obj; // temporary object for different menuitems
  var iLevels = aMenu.length-1;
  
  // * * * Selecting top level menu item...
  iCount = oMenu.ToolbarsManager.Toolbars.Item(0).Tools.Count; // number of menu items in main menu
  for(i = 0; i < iCount; i++)
  {
    obj = oMenu.ToolbarsManager.Toolbars.Item(0).Tools.Item(i);
    Log.Message("Checking menu '" + obj.CaptionResolved.OleValue + "'");
    if(obj.CaptionResolved.OleValue.match(aMenu[0]))
    {
      oMenu.Click(iX + obj.WidthResolved/2, iY + obj.Height/2);
      break;
    }
    else
    {
      iX += obj.WidthResolved*obj.VisibleResolved;
    }
  }
  
  // * * * After top level menu selected selecting other menus...
  //for(j = iLevels; j > 0; j--)
  for(j = 1; j <= iLevels; j++)
  {
  iX = 0; iY = 0;
  obj = pProcess.WinFormsObject("DropDownForm", "", 1).WinFormsObject("PopupMenuControlTrusted", "");
  iCount = obj.MenuAgent.MenuItems.Count;
  for(i = 0; i < iCount; i++)
  {
    Log.Message("Checking menu item '" + obj.MenuAgent.MenuItems.Item(i).Text.OleValue + "'");
    if(StrTran (obj.MenuAgent.MenuItems.Item(i).Text.OleValue, "&", "").match(aMenu[j]))
    {
      obj.Click(iX + 10, iY + 10);
      break;
    }
    else
    {
      iY += 25;
    }
  }
  } // j= iLevels...
}

Sunday, December 18, 2011

Select all rows in Infragistics.Win.UltraWinGrid.UltraGrid

I have found the way to select all rows in table and check whether all that rows is selected or not.


Public Function Swf_SelectAllItems(ByRef objSwfTable)
Dim strStep: strStep= "Swf_SelectAllItems()"
Dim numRows,i
If objSwfTable.GetROProperty("enabled") Then
numRows = objSwfTable.RowCount

objSwfTable.Type micLCtrlDwn

For i = 0 to numRows-1
objSwfTable.SelectRow i
objSwfTable.Type micDwn
Next
objSwfTable.Type micLCtrlUp
Dim iSelectedRows: iSelectedRows = objSwfTable.Object.Selected.Rows.Count
If  numRows = iSelectedRows Then
            reporter.ReportEvent micDone,strStep,"Success: Successfully Select All Items"
Swf_SelectAllItems=True
        Else
            reporter.ReportEvent micWarning,strStep, "Fail: Does NOT select All Items"
Swf_SelectAllItems=False
End If
Else
reporter.ReportEvent micWarning,strStep, "The : table is NOT ENABLE."
End If

End Function

Wednesday, December 7, 2011

Sending mails using Outlook and SMTP

Here we will see how we can send the mail after the test run using outlook and also using SMTP. First let us see how to send the mail using Outlook. This method though useful has the following drawbacks:
1. The system or machine where you are running the scripts should have outlook configured in it
2. The email will be sent in the name of the person whose email has been configured in the outlook. 
3. Finally, outlook throws up a security message when you try to send an automated mail and to take care of this you will have to download a third party software called expressclick.

Well, if you still prefer to go by this method, then here is the code 

systemutil.Run "C:\Program Files\Express ClickYes\ClickYes.exe"
wait 5
set oMailobj=CreateObject("Outlook.Application")

set oSendmail=oMailobj.CreateItem(0)
oSendmail.To=""
'oSendmail.Cc=""
oSendmail.Subject="Test"
oSendmail.Body="Hi" & vbcrLf & vbCrlf & "PFA the Smoke Test Automation Results" & vbCrlf & vbCrlf & "Thanks" & vbcrlf & "Automation Team"
sAttachment = "D:\Results.zip"
If (sAttachment <> "") Then
oSendmail.Attachments.Add(sAttachment)

Else
Reporter.ReportEvent micInfo,"Sending mail:","Unable to send attachment,please verify your attachment file"
End If
oSendmail.Send
wait 3
set oSendmail=Nothing
set oMailobj=Nothing
SystemUtil.CloseProcessByName "ClickYes.exe"

All these problems can be overcome by using SMTP mail. The only thing that you need to know is the SMTP server name and the machine should be in the network. Once you use this code you will never go back to using Outlook.

Set oMessage = CreateObject("CDO.Message")

oMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

'SMTP Server
oMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="servername or ip"

oMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

oMessage.Configuration.Fields.Update
oMessage.Subject = "Test"
oMessage.Sender = "" ' any non-existing mail id will also work.
oMessage.To =""
'oMessage.CC = ""
'oMessage.BCC = ""

oMessage.AddAttachment "D:\Results.zip"
oMessage.TextBody = "Hi" & vbcrLf & vbCrlf & "PFA the Smoke Test Automation Results & vbCrlf & vbCrlf & "Thanks" & vbcrlf & "Automation Team" & vbCrlf & vbCrlf & vbCrlf &"*********************************************************************************************************" & vbcrlf & "This is an auto-generated email. Please do not reply to this email." & vbcrlf &"*********************************************************************************************************"
oMessage.Send

Set oMessage = Nothing



Source: http://qtp-learnings.blogspot.com/2010/08/sending-mails-using-outlook-and-smtp.html

How to send email with attachment from outlook using qtp


 Code:
Text1 ="The information contained in this message " _
    & "constitutes privileged and confidential information " _
    & "and is intended only for the use of and review by " _
    & "the recipient designated above."
 
Text2= "The information contained in this message " _
    & "constitutes privileged and confidential information " _
    & "and is intended only for the use of and review by " _
    & "the recipient designated above."
 
'Multiline body
Body= Text1 & vbCrLf & Text2
 
'path of the file sample.txt  to be attached
path="D:\sample.txt"
 
'Usage of Function  "Send_Testresults"
'Multiple email Id can be used  with  semicolon as Seperator
Send_Testresults"xx@mail.com;yy@abc.com","zz@xyz.com",,"Mail from QTPWorld.com",Body,path
 
'Function  to send email
Function  Send_Testresults(sTo,sCC,sBCC,sSubject,sBody,sAttachment)
 
'Open outlook if Outlook is not open
   systemUtil.Run "OUTLOOK.EXE"
   wait (10)
 'Create Outlook Object
Set oMail = CreateObject("Outlook.Application")
 
set  Sendmail=oMail.CreateItem(0)
      Sendmail.To=sTo
      Sendmail.CC=sCC
      Sendmail.BCC=sBCC
      Sendmail.Subject=sSubject
      Sendmail.Body=sBody
          If (sAttachment <> "") Then
         Sendmail.Attachments.Add(sAttachment)
         End If 
 
       Sendmail.Send
 
oMail.quit
 
set  Sendmail=Nothing
set oMail=Nothing
 
End Function









Source: http://qtpworld.com/index.php?cid=89