Select Box and onchange

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
thecrazykaktus
Posts: 13
Joined: Wed Feb 19, 2014 12:24 pm

Select Box and onchange

Post by thecrazykaktus » Thu May 22, 2014 3:51 pm

Hello,

I have a problem with the Selectbox.
Sorrow I can select a list item, but but the onchange do not work.

Code: Select all

 
public void test()
        {
SelectTag someSelectTag = "/dom[@domain='www.....com']//div[#'ifrdiv_intranet']/iframe[@id='ifr_intranet']//frameset[#'main']/frame[@id='DVAGcontent']//div[#'searchbox']/select[@name='ebene1']";  
  		  
OptionTag optTag = someSelectTag.FindSingle(".//option[@value='Bankprodukte']");  

optTag.Selected = true;

optTag.Click();
          
Here is the HTML Code

Code: Select all

<select name="ebene1" class="searchBox" style="width: 145px; font-size: 8pt; margin-bottom: 10px;" onchange="initEbene2(this.options.selectedIndex)" size="1">
Can any one tell me how I trigger the onchange?

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Select Box and onchange

Post by krstcs » Fri May 23, 2014 2:05 pm

You will probably need to use keyboard input (up/down keys and enter) to do this. I have found that attempting to do this with direct manipulation (by setting the values) does not fire the events correctly or at all.

See this thread for more info:
http://www.ranorex.com/forum/unable-to- ... t3413.html

If you look at the bottom of the first page you will see my post with the code for doing this. The code will select the correct optiontag no matter where it is in the list or what is selected, and it will press enter. It works for me every time, no matter what browser.
Shortcuts usually aren't...

thecrazykaktus
Posts: 13
Joined: Wed Feb 19, 2014 12:24 pm

Re: Select Box and onchange

Post by thecrazykaktus » Mon May 26, 2014 9:16 am

First of all thanks for the hint,
But unfortunately I do not know exactly how I need to implement it. I hope you can help me with this?
I'm unfortunately a novice programmer :)

This is my current code.

Code: Select all

public void Alle_Kategorien()
        {
SelectTag someSelectTag = "//div[#'ifrdiv_intranet']/iframe[@id='ifr_intranet']//frameset[#'main']/frame[@id='DVAGcontent']//div[#'searchbox']/select[@name='ebene1']";  
OptionTag optTag = someSelectTag.FindSingle(".//option[@value='Bankprodukte']");  
optTag.Selected = true;
optTag.Click();  	 	
       }


public void Auswahl_Zeitraum()
        {	
 SelectTag someSelectTag = "//div[#'ifrdiv_intranet']/iframe[@id='ifr_intranet']//frameset[#'main']/frame[@id='DVAGcontent']//div[#'searchbox']/select[@name='ebene2']";  
OptionTag optTag = someSelectTag.FindSingle(".//option[@value='Investment']");  
optTag.Selected = true;
optTag.Click();     	
        }
And this is supposed to be the code that helps

Code: Select all

public static void SelectOption(SelectTag selectTag, OptionTag optionTag, Duration dur) {
   List<string> options = GetOptionsText(selectTag);

   int desiredIndex = options.IndexOf(optionTag.TagValue);

   DoSelectAction(selectTag, desiredIndex, options, dur);
}

private static List<string> GetOptionsText(SelectTag selectTag) {
   List<string> options = new List<string>();

   foreach (OptionTag ot in selectTag.Find<OptionTag>(".//option")) {
      options.Add(ot.TagValue != null ? ot.TagValue : "");
   }

   return options;
}


private static void DoSelectAction(SelectTag selectTag, int desiredIndex, List<string> options, Duration dur) {
   int selectedIndex = 0;

   if (selectTag.TagValue != null) {
      List<OptionTag> optiontags = new List<OptionTag>(selectTag.Find<OptionTag>(".//optiontag[@TagValue='" + selectTag.TagValue + "']"));

      if (options.Count > 0) {
         OptionTag selectedOptionTag = optiontags[0];

         selectedIndex = options.IndexOf(selectedOptionTag.TagValue);
      }
   }
   
   Keyboard.PrepareFocus(selectTag);

   Duration oldKeyPressTime = Keyboard.DefaultKeyPressTime;
   Keyboard.DefaultKeyPressTime = 15;
   Duration keyPressDuration = dur;

   if (desiredIndex < selectedIndex) {
      for (int i = desiredIndex; i < selectedIndex; i++) {
         //up
         Report.Log(ReportLevel.Info, "Keyboard", "Key 'Up' Press with focus on '" + selectTag.GetPath() + "'.");
         Keyboard.Press(System.Windows.Forms.Keys.Up, Keyboard.DefaultScanCode, keyPressDuration, 1, true);//Keyboard.DefaultKeyPressTime, 1, true);
      }
   } else if (selectedIndex < desiredIndex) {
      for (int i = selectedIndex; i < desiredIndex; i++) {
         //down
         Report.Log(ReportLevel.Info, "Keyboard", "Key 'Down' Press with focus on '" + selectTag.GetPath() + "'.");
         Keyboard.Press(System.Windows.Forms.Keys.Down, Keyboard.DefaultScanCode, keyPressDuration, 1, true);//Keyboard.DefaultKeyPressTime, 1, true);
      }
   }

   Report.Log(ReportLevel.Info, "Keyboard", "Key 'Enter' Press with focus on '" + selectTag.GetPath() + "'.");
   Keyboard.Press(System.Windows.Forms.Keys.Enter, Keyboard.DefaultScanCode, keyPressDuration, 1, true);//Keyboard.DefaultKeyPressTime, 1, true);

   Keyboard.DefaultKeyPressTime = oldKeyPressTime;
}
Unfortunately, I do not know how I can implement the code with my existing code. I hope you could help me.

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Select Box and onchange

Post by krstcs » Tue May 27, 2014 2:28 pm

In your usercode module, just copy and paste the code I gave you inside the class.

Then in your action table create a usercode action.

In the usercode action, you can type the following:

Code: Select all

SelectOption(<YourRepoSelectTagObject>, <YourRepoOptionTagObject>, new Duration(<SearchDuration>));
Just replace each of the <...> tags above with the actual object reference or number you need.
Shortcuts usually aren't...