Forum Discussion

ijaz5's avatar
ijaz5
Occasional Contributor
6 years ago

Unable to perform action on second date-picker of the range value

To fetch a report I am giving date range using two datepickers . first one is "From date" and 2nd is "To date". I have a function of date picker which selects a date from date-picker.My function easily selects first date i.e From date but selecting To Date  2nd date picker gives me the error (attached) which is window is invisible and thus cannot be activated.

Testcomplete is expecting first datepicker i.e From Datepicker to be reopen to perform action on it. and thats becuase Dev team is using same datepicker control for both dates (from and to) and thus my function expects first control found to be reopened. So both controls have same object name.

 

What else should I do to select a date from "To datepicker" using my function? keeping in mind following things:

- Its not a textbox to insert a date directly by changing text. its a dropdown not a textbox. screenshot attached.

- I tried changing text aswell, it does change the text on the dropdown button but its only change the text and calendar still shows the previous date. Anything I can do to change text only and gives me the effect on calendar aswell ?

 

 

 

 

3 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    > What else should I do to select a date from "To datepicker" using my function? 

    Have absolutely no idea without knowing at least something about your function.

    Standard approach:

    a) Investigate both controls in the Object Browser, find what properties are different and use them to distinguish between controls during runtime;

    b) Try to record the script and investigate the result.

     

    • ijaz5's avatar
      ijaz5
      Occasional Contributor

      Alex thanks for your reply !

      Will try standard approches you shared. For now only difference I found is "Focused Day" property and "index" property but these object does not support Key stroke methods.

       

      As I am using Right or Left keystrokes in my function by adding or subtracting user date with Focused date.

       

      Below is my function:

       

       

      function datepicker(Month,Day,year)
      
      {
        
        var date_pickercontrol, Calender_object,Focus_day,newcalnderobject;
        var dd,mm,yyyy;
         
           date_pickercontrol = FindObject("WinFormsControlName","cntrlDateTimePicker");  
           Calender_object = date_pickercontrol.FindChild("WinFormsControlName","Month");
       
           Focus_day = Calender_object["FocusDay"];
           dd = Focus_day["Day"];
           mm = Focus_day["Month"];
           yyyy = Focus_day["Year"];
         
           
           
           if(year == yyyy) 
           {
             Log["Message"]("Same year")     
           
           }
           
           else if(year > yyyy)
           {
             var btnright = FindObject("WinFormsControlName","btnYearRight");      
             newyear = year - yyyy;   
             
             for (var i=1;i<= newyear;i++)
              {
                  btnright["Click"]();                 
              }
           }
           
            else if(year < yyyy)
           {
             var btnright = FindObject("WinFormsControlName","btnYearLeft");      
             newyear = yyyy  - year; 
               
             for (var i=1;i<= newyear;i++)
              {
                  btnright["Click"]();         
              }          
           }
           
       /////////////////////////////MONTH///////////////////////////////////////////
           
           
           if(Month == mm )
           {
                Log["Message"]("Same Month")   
           
           }
           
           else if(Month > mm)
           {
             var btnright = FindObject("WinFormsControlName","btnMonthRight");      
             newmonth = Month - mm;   
             
             for (var i=1;i<= newmonth;i++)
              {
                  btnright["Click"]();                 
              }
           
           }
           
            else if(Month < mm)
           {
             var btnright = FindObject("WinFormsControlName","btnMonthLeft");      
             newmonth = mm  - Month; 
               
             for (var i=1;i<=newmonth;i++)
              {
                  btnright["Click"]();         
              }          
           }
           
      
      ////////////////////////////////DAY/////////////////////////////////////////////////
         
           
           if (Day == dd)    
           {
              Log["Message"]("Same Day")     
           }             
           else if(Day > dd)
           {
            
      		newday = Day - dd;   
      		var  date_pickercontrol = FindObject("WinFormsControlName","cntrlDateTimePicker");  
      		var Calender_object2 = date_pickercontrol.FindChild("WinFormsControlName","Month");
      		for (var i=1;i<= newday;i++)
              {
                 
                  Calender_object2["Keys"]("[Right]")                 
              }
           }
           
            else if(Day < dd)
      	 {
             
             newday = dd - Day; 
      		for (var i=1;i<=newday;i++)
      			{
                   
      				Calender_object2["Keys"]("[Left]") ;         
      			}          
           }
          Sys["Keys"]("[Enter]");
      
          
      return;
      }

       

       

       

       

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        I am pretty sure that you need to adjust this line of code:

        date_pickercontrol = FindObject("WinFormsControlName","cntrlDateTimePicker");

        I think so because identification criteria look too generic and I am quite sure that both Date From and Date To controls have the same control name. In the worst case you will need to use the value of their Index property to distinguish between them. This might lower code stability, but on the other hand, I am also quite sure that test code will be quite stable until developers change something in the layout (move/add another date controls).

         

        P.S. Navigation and selection in the Calendar controls is not that easy usually. You may consider slightly different approach that I use quite often and it works :

        a) The major condition is to be able to manually enter text (date) into the field itself (Date From or Date To);

        b) If the above is possible, then focus control (fieldFrom.SetFocus() and enter the date (fieldFrom.Keys('...'));

        c) The above sequence usually opens calendar with the entered date already pre-selected;

        d) Depending on the implementation of the date-picker control, send Enter key to the proper object (e.g. fieldFrom.Keys('[Enter]' or fieldFromCalendar.Keys('[Enter]')). This usually confirms date selection and (again, depending on the implementation of the given date-picker control, triggers all internal events that might not trigger as expected by developers if only text is entered into the date field.).

         

        Hope, this will help.