Creating Powershell Cmdlets

Ask general questions here.
jjorgens
Posts: 14
Joined: Wed Apr 01, 2009 7:32 pm

Creating Powershell Cmdlets

Post by jjorgens » Fri Jun 26, 2009 5:41 pm

I am trying to create powershell cmdlets to use. Everything seems to work just find except for TDTags and Spantags. I took my C# code and put it into my cmdlet. I have tried with the full RxPath and with the relative path. When I run it in C# it works fine, however when I run it as a cmdlet, it can't find td tags or span tags. I have other cmdlets I have made and they work as I have wanted them to. Any ideas?
windowsForm.EnsureVisible();
TdTag setupTag = windowsForm.FindSingle<TdTag>(new RxPath(".//td[@InnerText~'Setup']"),new Duration(90000));
setupTag.Click();
This code works but not in the cmdlet.
Platform: XP Pro with SP3
Ranorex Version: 2.1.0.6243

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Creating Powershell Cmdlets

Post by Support Team » Mon Jun 29, 2009 1:13 pm

It could be a problem with the apartment state of the running thread. Please, make sure that in your code the System.Threading.Thread.CurrentThread.GetApartmentState() returns ApartmentState.STA. If it returns MTA, you need to start a separate STA thread from your CmdLet and let that STA thread carry out the Ranorex code.
You can find an example of how to create a separate STA thread with CmdLets on the following website:
http://weblogs.asp.net/pschneider/archi ... ntact.aspx

Regards,
Alex
Ranorex Support Team

chris
Posts: 17
Joined: Fri Mar 27, 2009 6:10 pm

Re: Creating Powershell Cmdlets

Post by chris » Wed Jul 15, 2009 8:29 pm

I am also using cmdlets with Ranorex, but I am having trouble getting a menu item. Here is the code I use to make sure it's apartment state is STA:

Code: Select all

            Thread newThread = new Thread(delegate()
            {
                SetUpDefaultParameters();
                adapter = DelegateAction();
            });
            newThread.SetApartmentState(ApartmentState.STA);

            newThread.Start();
            // Wait for newThread to start and go to sleep.
            newThread.Join();
Here is the DelegateAction method which contains the Ranorex code:

Code: Select all

protected override Adapter DelegateAction()
        {
            Ranorex.MenuItem menuItem = null;
            try
            {
                menuItem = container.FindSingle<Ranorex.MenuItem>(new RxPath(".//menuitem[@accessiblename~'" + name + "']"),
                    new Duration(timeOut));
                menuItem.EnsureVisible();
                menuItem.Click();
            }
            catch (ElementNotFoundException ex)
            {
                exceptionInformation = ex.ToString();
            }
            return menuItem;
        }
I am using version 2.1.2.6480 of Ranorex, and I am working on Windows 2003 R2 Enterprise edition with Service Pack 2.
For some reason I keep getting a blank menu item. Any thoughts on what I am doing wrong? Suggestions for troubleshooting?
Thanks.
You do not have the required permissions to view the files attached to this post.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Creating Powershell Cmdlets

Post by Support Team » Thu Jul 16, 2009 10:34 am

Hello Chris!

Thank you for the post but the data in your snapshot file is currupt. To know how to create a snapshot from menu items please read : :!:
http://www.ranorex.com/support/user-gui ... html#c2072 .
For some reason I keep getting a blank menu item
In your code, you set the menu item 'NULL' before try to find the element. If the element could not found, the 'ElementNotFoundException' will be called. You ignore the exception and return menu item ('NULL').
Try in Ranorex Studio to change the 'ElementNotFoundException' like this:
catch (ElementNotFoundException ex)
            {
                Report.Error(ex.ToString());
                Report.Screenshot();
                Report.End();
            }
So you can see in Report, why the menu item could not be found.

Regards,
Mhosen
Ranorex Support Team

chris
Posts: 17
Joined: Fri Mar 27, 2009 6:10 pm

Re: Creating Powershell Cmdlets

Post by chris » Thu Jul 16, 2009 8:10 pm

I'm not throwing away the exception, I'm assigning it to exceptionInformation. Anyway, there is no exception being thrown. The values I get for the menu item are:
Checked : False
Image :
Text :
Element : {MenuItem:None}
UseEnsureVisible :
Children : {}
FlavorName : msaa
ScreenRectangle : {X=0,Y=0,Width=0,Height=0}
Visible : True
HasFocus : False
Enabled : True
IsSnapshot : False
Valid : False
I cannot save a valid snapshot with Ranorex spy using the any method I have found in the users guide. I tried using ctrl+leftwindow button, that tracks the menu item on the context menu, but when I click to save, the context menu goes away. Then I get the corrupt file. I have tried taking a picture when the error occurs, but since there is no exception thrown that doesn't work when using the error reporting in the exception.
I have no problem when I run it outside of a cmdlet, so I thought it might be a threading issue. The thread is running in STA mode though. Any ideas? I am trying to right click on a button in the system tray. The item in the system tray is a program called virtual clone drive. The menu item I am trying to click on is the drive that it creates so I can mount an ISO.
Thanks.
You do not have the required permissions to view the files attached to this post.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Creating Powershell Cmdlets

Post by Support Team » Fri Jul 17, 2009 12:23 pm

Hello chris!
As explained in the UserGuide(Snapshots from popup windows) use the "<SCROLL>" key to save the snapshot.
http://www.ranorex.com/support/user-gui ... html#c2072

Regards,
Christian
Support Team

chris
Posts: 17
Joined: Fri Mar 27, 2009 6:10 pm

Re: Creating Powershell Cmdlets

Post by chris » Fri Jul 17, 2009 6:08 pm

Sorry, I've never seen the notation <SCROLL> before. I incorrectly assumed you meant the scroll button on the mouse instead of the scroll lock key. Here are the snapshots.
You do not have the required permissions to view the files attached to this post.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Creating Powershell Cmdlets

Post by Support Team » Mon Jul 20, 2009 7:30 am

Hello chris,
if your test works fine outside powershell it seems to be a rxpath problem.
compare the menu item rxpath from the application hosted by powershell with the normal one (matching attributes, path structure).

Regards,
Christian
Support Team

chris
Posts: 17
Joined: Fri Mar 27, 2009 6:10 pm

Re: Creating Powershell Cmdlets

Post by chris » Wed Jul 22, 2009 10:19 pm

After doing some trial and error, we figured out that using ApartmentState.MTA (or the default settings of powershell) makes it work. We decided to make two sets of cmdlets, ones that are web based (ApartmentState.STA), and ones that are form based(ApartmentState.MTA). So far this approach is working.
Thanks,