How to make generic function for different type of controls

Class library usage, coding and language questions.
omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

How to make generic function for different type of controls

Post by omayer » Tue Sep 11, 2012 3:42 am

how to create generic functions that will work for all same type controls like enter on textfield, click on radio button since i am running into lots of controls into a page which i don't want to create each functions for each controls. Thank you in Advance
Tipu

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: How to make generic function for different type of controls

Post by sdaly » Tue Sep 11, 2012 8:17 am

You can achieve this by using the Adapter type -

Code: Select all

Button b = "/button[@text='Start']";
DoSomething(b);

public static void DoSomething(Adapter a){
	a.Click();
}
Not a generic method, but should work for you!

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: How to make generic function for different type of controls

Post by omayer » Wed Sep 12, 2012 2:48 am

Thank you Sdaly to look into it.
testcase 1. need to create customer > enterfname, enterlname, enteraddress, click on save button
how will i make reusable function that will do enterfname, enterlname, enteraddres
currently i have - enterfname(string fname);
public void enterfname(string fname)
{
inputtag fnameField = "/[email protected]";
fnameField .value=fname;

}

but the problem i have to create four separate functions
Tipu

sham526
Posts: 34
Joined: Wed Jul 07, 2010 7:12 am
Location: Hyderabad(INDIA)

Re: How to make generic function for different type of controls

Post by sham526 » Wed Sep 12, 2012 6:45 am

U can try like this:
Pass the inputTag also as the parameter, so that it can be a reusable function.

public void enterfname(string fname,InputTag myTag)
{
inputtag fnameField = myTag
fnameField .value=fname;
-------------
-------------
}

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: How to make generic function for different type of controls

Post by omayer » Wed Sep 12, 2012 3:31 pm

Thank you so much, this is very helpful with sample code.

is this how will i call the function
enterfname("joe",fnamefield)

public void enterfname(string fname,InputTag myTag)
{
inputtag fnameField = myTag ----- where do i defined the rxpath or how to find the fnamefield
fnameField .value=fname;
-------------
-------------
}
Tipu

sham526
Posts: 34
Joined: Wed Jul 07, 2010 7:12 am
Location: Hyderabad(INDIA)

Re: How to make generic function for different type of controls

Post by sham526 » Thu Sep 13, 2012 6:22 am

If the elements were stored in Repository then simply call the function as below.
enterfname("joe",Recording1.Repo.------.MyinpTag)

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: How to make generic function for different type of controls

Post by omayer » Fri Sep 14, 2012 5:37 pm

elements were NOT stored in Repository
Tipu

sham526
Posts: 34
Joined: Wed Jul 07, 2010 7:12 am
Location: Hyderabad(INDIA)

Re: How to make generic function for different type of controls

Post by sham526 » Tue Sep 18, 2012 6:39 am

Sorry for the late response.In that case search for each element before calling the function.
Example(In Vb.Net)
Dim btn_ok as InputTag=nothing
Dim bool as boolean=Host.Local.TryfindSingle("./dom[]//InputTag[@InnerText='ok']",btn_ok)

If bool then
'Call the Function that clicks the btn.
enterfname("joe",btn_ok)
Else
'OK Button was NOT Found.
'Report it to the result and log files.
EndIf

'Same case for Cancel button also.
Dim btn_Cancel as InputTag=nothing
Dim bool as boolean=Host.Local.TryfindSingle("./dom[]//InputTag[@InnerText='cancel']",btn_cancel)

If bool then
'Call the Function that clicks the btn.
enterfname("joe",btn_cancel)
Else
'Cancel Button was NOT Found.
'Report it to the result and log files.
EndIf

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: How to make generic function for different type of controls

Post by omayer » Wed Sep 19, 2012 5:03 pm

Thank you Sham526, this is very helpful
Tipu

sham526
Posts: 34
Joined: Wed Jul 07, 2010 7:12 am
Location: Hyderabad(INDIA)

Re: How to make generic function for different type of controls

Post by sham526 » Thu Sep 20, 2012 6:17 am

:D