Loop problem

Best practices, code snippets for common functionality, examples, and guidelines.
yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Loop problem

Post by yaro2o » Mon Feb 12, 2018 10:19 pm

Hi, I'm just starting to learn programming and i heave small problem with loop.

I heave a app where sometimes a window will pop up and i need check if pop up equals true do something else do...

i try If, while, and nothing maybe im doing something wrong

Code: Select all

if (MyRepo.FormWallpaperSettings.Equals(MyRepo.FormWallpaperSettings))
           	{
            
            	MyRepo.FormWallpaperSettings.BtnOk.Click();
            	
            }
            else
            {
            	MyRepo.FormWallpaper.BtnLoadImage.Click();
            }


if i write something that, if popup exists it work fine but if pop up dont exist ranorex do loop all time and they dont do else

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Loop problem

Post by odklizec » Tue Feb 13, 2018 8:28 am

Hi,

Have you examined Ranorex popup watcher? It's probably exactly what you are looking for?...
https://www.ranorex.com/help/latest/cod ... tedDialogs
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Re: Loop problem

Post by yaro2o » Tue Feb 13, 2018 9:22 am

Code: Select all

/*
 * Created by Ranorex
 * User: jaroslaww
 * Date: 6.02.2018
 * Time: 12:29
 * 
 * To change this template use Tools > Options > Coding > Edit standard headers.
 */

using System;
using System.Threading;
using System.Drawing;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Reporting;
using Ranorex.Core.Testing;

namespace WM
{
    class Program
    {
    	public static WMRepository repo = WMRepository.Instance;
    	
        [STAThread]
        public static int Main(string[] args)
        {
            // Uncomment the following 2 lines if you want to automate Windows apps
            // by starting the test executable directly
            //if (Util.IsRestartRequiredForWinAppAccess)
            //    return Util.RestartWithUiAccess();
            
            PopupWatcher myPopupWatcher = new PopupWatcher();
            
            myPopupWatcher.Watch(repo.PopUP.Uwaga, ConfirmDialog);
            
            
            myPopupWatcher.Start();
            
        
            Keyboard.AbortKey = System.Windows.Forms.Keys.Pause;
            int error = 0;

            try
            {
                error = TestSuiteRunner.Run(typeof(Program), Environment.CommandLine);
            }
            catch (Exception e)
            {
                Report.Error("Unexpected exception occurred: " + e.ToString());
                error = -1;
            }
            return error;
        }
        public static void ConfirmDialog(Ranorex.Core.Repository.RepoItemInfo repoItemInfo, Ranorex.Core.Element repoElement)
        {Report.Screenshot(ReportLevel.Warn, "Error", "An error occured", repoElement, false , null);
        
        if (repoItemInfo == repo.PopUP.Uwaga)
        {
        repo.PopUP.Uwaga.ButtonOK.Click();	
        }
    }
    }
}
I now i heave problem with message : i cant use == operator for arguments like Ranorex.Core.Repository.ReportItemInfo

yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Re: Loop problem

Post by yaro2o » Tue Feb 13, 2018 9:23 am


User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Loop problem

Post by odklizec » Tue Feb 13, 2018 9:36 am

Hi,

Could you please post a Ranorex snapshot of the dialog in question?

Maybe I'm misunderstanding something, but there should be no need to use this condition...

Code: Select all

if (repoItemInfo == repo.PopUP.Uwaga)
This line of code should be enough...

Code: Select all

myPopupWatcher.Watch(repo.PopUP.Uwaga, ConfirmDialog);
Where the first parameter is "watched" dialog and second parameter is the name of method, in which you handle the dialog, e.g. click a button to close it.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Re: Loop problem

Post by yaro2o » Tue Feb 13, 2018 9:52 am

Or i try like that

Code: Select all

 public class UserCodeModule1 : ITestModule
    {
        /// <summary>
        WMRepository repo = WMRepository.Instance;
        /// </summary>
        public UserCodeModule1()
        {
            // Do not delete - a parameterless constructor is required!
        }

        /// <summary>
        /// Performs the playback of actions in this module.
        /// </summary>
        /// <remarks>You should not call this method directly, instead pass the module
        /// instance to the <see cref="TestModuleRunner.Run(ITestModule)"/> method
        /// that will in turn invoke this method.</remarks>
        void ITestModule.Run()
        {
          PopupWatcher myPopupWatcher = new PopupWatcher();
          
          myPopupWatcher.Watch("/form[@title='Uwaga']/button[@text='OK']", Close); 
          myPopupWatcher.Start();  
   
}  
  
public static void Close(Ranorex.Core.Repository.RepoItemInfo myInfo, Ranorex.Core.Element myElement)  
{  
 myElement.As<Ranorex.Button>().Click();  
}  
  
public static void Close(Ranorex.Core.RxPath myPath, Ranorex.Core.Element myElement)  
{  
 myElement.As<Ranorex.Button>().Click();  
}  
        }
    }

yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Re: Loop problem

Post by yaro2o » Tue Feb 13, 2018 10:03 am

This is a snapshot
You do not have the required permissions to view the files attached to this post.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Loop problem

Post by odklizec » Tue Feb 13, 2018 10:09 am

OK so what's the problem? It does not work or what? In case it does not work, could you please post a sample solution as well?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Re: Loop problem

Post by yaro2o » Tue Feb 13, 2018 10:20 am

does not work, does not close the window, only generate report success :/

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Loop problem

Post by odklizec » Tue Feb 13, 2018 10:26 am

It's hard to tell what's wrong without seeing your app and solution. I would suggest to try splitting the click action into "moveto" and "click" and eventually add a report action into "Close" method so you are sure the popup watcher found the watched element.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Re: Loop problem

Post by yaro2o » Tue Feb 13, 2018 10:47 am

now it works, the problem was that my window was already shown and it must be displayed, human learn by his mistakes.

Now i heave another question how add IF

if popup 1 do this
if popup 2 do this
etc...

??

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Loop problem

Post by odklizec » Tue Feb 13, 2018 12:12 pm

Well, in this case, you need to implement multiple watchers, pointing either to separate methods or the same method with conditions. This post should give you a clue...
https://www.ranorex.com/forum/pop-up-ha ... tml#p38346

Basically, you need a popoup watcher, like this (where repo.PopUP.Uwaga should have xpath /form[@title='Uwaga']):

Code: Select all

myPopupWatcher.Watch(repo.PopUP.Uwaga, Close);
myPopupWatcher.Watch(repo.PopUP.Uwaga2, Close);
...
public static void Close(Ranorex.Core.Repository.RepoItemInfo myInfo, Ranorex.Core.Element myElement) 
{ 
    if (myInfo== repo.PopUP.Uwaga.SelfInfo)
    {
        repo.PopUP.Uwaga.OK_Btn.Click(); 
    }
    else if (myInfo== repo.PopUP2.Uwaga.SelfInfo)
    {
        repo.PopUP2.Uwaga.OK_Btn.Click(); 
    }
}  
Hope this helps?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Re: Loop problem

Post by yaro2o » Tue Feb 13, 2018 12:55 pm

Now I have this problem:
for a non-static field, method or property "WM.UserCodeModule1.repo" a reference to the object is required. (CS0120) - C: \ Users \ JaroslawW \ Documents \ Ranorex \ RanorexStudio Projects \ Prints \ WM \ UserCodeModule1.cs: 55,15

the error marks all repo. ....

Code: Select all

/*
 * Created by Ranorex
 * User: jaroslaww
 * Date: 13.02.2018
 * Time: 09:36
 * 
 * To change this template use Tools > Options > Coding > Edit standard headers.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;

namespace WM
{
    /// <summary>
    /// Description of UserCodeModule1.
    /// </summary>
    [TestModule("F242FEC7-2230-4211-8C77-11A97B3DD218", ModuleType.UserCode, 1)]
    public class UserCodeModule1 : ITestModule
    {
        /// <summary>
        WMRepository repo = WMRepository.Instance;
        /// </summary>
        public UserCodeModule1()
        {
            // Do not delete - a parameterless constructor is required!
        }

        /// <summary>
        /// Performs the playback of actions in this module.
        /// </summary>
        /// <remarks>You should not call this method directly, instead pass the module
        /// instance to the <see cref="TestModuleRunner.Run(ITestModule)"/> method
        /// that will in turn invoke this method.</remarks>
        void ITestModule.Run()
        {
          PopupWatcher myPopupWatcher = new PopupWatcher();
          
          myPopupWatcher.Watch("/form[@title='Uwaga']/button[@text='OK']", Close); 
          myPopupWatcher.Watch("/form[@title='Blad']/button[@text='OK']", Close);
          myPopupWatcher.Start();  
          
}  
  
public static void Close(Ranorex.Core.Repository.RepoItemInfo myInfo, Ranorex.Core.Element myElement)  
{  
 if (myInfo== repo.PopUP.Uwaga.SelfInfo)
    {
 	
        repo.PopUP.Uwaga.ButtonOK.Click(); 
    }
    else if (myInfo== repo.PopUP.Błąd.SelfInfo)
    {
    	repo.PopUP.Błąd.ButtonOK.Click();
        
    } 
}  
  
public static void Close(Ranorex.Core.RxPath myPath, Ranorex.Core.Element myElement)  
{  
 myElement.As<Ranorex.Button>().Click();  
}



        }
    }

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Loop problem

Post by odklizec » Tue Feb 13, 2018 1:01 pm

There is no point using xpath in popup watcher, if you already have both dialogs defined in repo? I would suggest to use code like this:

Code: Select all

	{
	  PopupWatcher myPopupWatcher = new PopupWatcher();
	 
	  myPopupWatcher.Watch("repo.PopUP.Uwaga", Close);
	  myPopupWatcher.Watch("repo.PopUP.Błąd", Close);
	  myPopupWatcher.Start(); 
	} 
	 
	public static void Close(Ranorex.Core.Repository.RepoItemInfo myInfo, Ranorex.Core.Element myElement) 
	{ 
	if (myInfo== repo.PopUP.Uwaga.SelfInfo)
		{
			repo.PopUP.Uwaga.ButtonOK.Click();
		}
		else if (myInfo== repo.PopUP.Błąd.SelfInfo)
		{
			repo.PopUP.Błąd.ButtonOK.Click();
		}
	} 
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

yaro2o
Posts: 95
Joined: Mon Jan 29, 2018 11:19 am

Re: Loop problem

Post by yaro2o » Tue Feb 13, 2018 2:07 pm

but i still heave a problem

for a non-static field, method or property "WM.UserCodeModule1.repo" a reference to the object is required. (CS0120) - C: \ Users \ JaroslawW \ Documents \ Ranorex \ RanorexStudio Projects \ Prints \ WM \ UserCodeModule1.cs: 55,15