Help please.how to make loop to check excel data binding

Class library usage, coding and language questions.
mauriPS
Posts: 6
Joined: Fri Jul 11, 2014 8:46 pm

Help please.how to make loop to check excel data binding

Post by mauriPS » Sat Jul 12, 2014 8:45 pm

Hello,

I have a doubt and I need help since I am a beginner at using Ranorex Studio.

So in the Samples folder I am using the KeePassTest sample.
But I want to do an automated test where the mouse clicks on the password input textfield in the main app window, then starts putting some handmade passwords written in an external xml file in my computer,

and IF, the password is wrong (message dialog comes on where it says the password is wrong) then the mouse clicks again on the input textfield and puts the following password after that. And so on until it finds the password "rx" then it access to another window, finally clicks on the "file tab" and then on the "exit" menuitem.
app closes, and automation test is ended.

In my recording I just opened the application, the click on the textfield, put text inside (xml is named "passwords" and is already binded correctly to my recording variable), then click "OK" button, then File tab, then exit menuitem.



Here is my Code Module (which I use to custom my personal recoerding behavior thorugh C#):

Code: Select all

/*
 * Created by Ranorex
 * User: mpimienta
 * Date: 7/12/2014
 * Time: 11:04 AM
 * 
 * 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 KeePassTest
{
    /// <summary>
    /// Description of ToolbarButtons.
    /// </summary>
    [TestModule("DFB6335C-EFD9-4E92-BE6A-1E7373723CF4", ModuleType.UserCode, 1)]
    public class ToolbarButtons : ITestModule
    {
        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        public ToolbarButtons()
        {
            // 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()
        {
            Mouse.DefaultMoveTime = 300;
            Keyboard.DefaultKeyPressTime = 100;
            Delay.SpeedFactor = 1.0;
            
            
            //start here
            Report.Log(ReportLevel.Info, "Application", "Run application 'C:\\Program Files (x86)\\Ranorex 5.1\\Samples\\KeePassSample\\C#\\KeePassTestSuite\\KeePass\\KeePass.exe' with arguments '' in normal mode.", new RecordItemIndex(0));
            Host.Local.RunApplication("C:\\Program Files (x86)\\Ranorex 5.1\\Samples\\KeePassSample\\C#\\KeePassTestSuite\\KeePass\\KeePass.exe", "", "C:\\Program Files (x86)\\Ranorex 5.1\\Samples\\KeePassSample\\C#\\KeePassTestSuite\\KeePass", false);
            Delay.Milliseconds(0);
            
            
            //start here
            var repo = KeePassTestRepository.Instance;
          
            var mBtnOK = repo.KeyPromptForm.MBtnOK;
            var text = repo.KeyPromptForm.Text;

            //Here I want to know how to make a loop so 
            //the machine displaces through my excel list of paswords
            //and keep trying new paswords to access the program
            
            text.TextValue = "rx" ;
            mBtnOK.Click();
            
            
            
            //file -> exit. app closes
            MenuItem file = "/form[@controlname='MainForm']/?/?/menuitem[@accessiblename='File']";
            file.Click();
            MenuItem exit = "/form[@controlname='MainForm']/?/?/menuitem[@accessiblename='File']/menuitem[@accessiblename='Exit']";
            exit.Click();
            
            
            
        }
    }
}

mauriPS
Posts: 6
Joined: Fri Jul 11, 2014 8:46 pm

Re: Help please.how to make loop to check excel data binding

Post by mauriPS » Mon Jul 14, 2014 6:10 pm

Okay, so I updated my code.

Even though It is still not working, it stops when opening the app. But it did recognize the KeePass error menu when Manually clicked on the OK button from the KeyPromptForm with no password.

When I force my way through the test manually and putting everything manually then it throws warnings, I need help to fix these.

here is my updated code. please help I think my "while" statement loop is wrong:

Code: Select all

/*
 * Created by Ranorex
 * User: mpimienta
 * Date: 7/12/2014
 * Time: 11:04 AM
 * 
 * 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 KeePassTest
{
    /// <summary>
    /// Description of ToolbarButtons.
    /// </summary>
    [TestModule("DFB6335C-EFD9-4E92-BE6A-1E7373723CF4", ModuleType.UserCode, 1)]
    public class ToolbarButtons : ITestModule
    {
        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        public ToolbarButtons()
        {
            // 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()
        {
            Mouse.DefaultMoveTime = 300;
            Keyboard.DefaultKeyPressTime = 100;
            Delay.SpeedFactor = 1.0;
            
            
            //start here
            
            //opens app
            Report.Log(ReportLevel.Info, "Application", "Run application 'C:\\Program Files (x86)\\Ranorex 5.1\\Samples\\KeePassSample\\C#\\KeePassTestSuite\\KeePass\\KeePass.exe' with arguments '' in normal mode.", new RecordItemIndex(0));
            Host.Local.RunApplication("C:\\Program Files (x86)\\Ranorex 5.1\\Samples\\KeePassSample\\C#\\KeePassTestSuite\\KeePass\\KeePass.exe", "", "C:\\Program Files (x86)\\Ranorex 5.1\\Samples\\KeePassSample\\C#\\KeePassTestSuite\\KeePass", false);
            Delay.Milliseconds(1);
            
            //variables
            //Global
            var repo = KeePassTestRepository.Instance;
            String[] arrayOfPasswords = {"rr", "ry", "rx"};
            var counter = -1;
            
            //Belong to KeyPromptForm window
            Button buttonOfKeyPromptForm = "/form[@controlname='KeyPromptForm']/button[@controlname='m_btnOK']";
            Text MasterPassword = "/form[@controlname='KeyPromptForm']/?/?/text[@accessiblerole='Text']";

            
            //Belong to keePass window
            Form keePass = "/form[@title='KeePass']";
            Button buttonOK = "/form[@title='KeePass']/button[@text='OK']";
            	
            	
            	
            
            while(counter < 3){
            	counter++;
            	MasterPassword.TextValue = arrayOfPasswords.GetValue(counter).ToString();
            	buttonOfKeyPromptForm.MoveTo();
            	buttonOfKeyPromptForm.Click();
            	Delay.Milliseconds(10);
            	if(keePass.EnsureVisible()){
            		Report.Info("Invalid Password entered");
            		buttonOK.MoveTo();
            		buttonOK.Click();
            		continue;
            	}
            	else{
            		break;
            	}
            }
        	
           
            
            
            //Host.Local.CloseApplication(repo.MainForm.Self);
            //file -> exit. app closes
      
            MenuItem file = "/form[@controlname='MainForm']/?/?/menuitem[@accessiblename='File']";
            file.Click();
            MenuItem exit = "/form[@controlname='MainForm']/?/?/menuitem[@accessiblename='File']/menuitem[@accessiblename='Exit']";
            exit.Click();
            
            
        }
    }
}
:cry:

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Help please.how to make loop to check excel data binding

Post by Support Team » Fri Jul 18, 2014 12:47 pm

Hello mauriPs,

Unfortunately, we cannot provide the code for your test, but generally you need to ensure that the element you try to instantiate is available. I’m referring to

Code: Select all

 Button buttonOK = "/form[@title='KeePass']/button[@text='OK']";
Additionally, it will be necessary to reload the elements (e.g., buttonOK) while iterating over them since the application “changes” after the Click()-method. If you try to access the button again you will get an exception since the RxPath remains the same, but the link to the live object is lost.
Hope this information helps you.

Kind regards,
Robert
.
Image