Posting code to improve

Hi,

Can we post our code to get looked at and see how we can improve it?

Thanks

Please feel free to post your code.

Dave is way nicer than me. But I’m more judgmental. Post away lol.

Best way to learn. Go for it.

Thank you, here is my code. I am creating a patient and scheduling an appointment and checking that appointment in.

put “yes” into newpatient //new patient will be created
put “80000” into Dept

//Call open_tst which opens tst and verifies it opens, pass Modeluser and dept as parameters
open_tst_for_appt ModelUser, Dept

//click appointment button
Wait[16]
typetext controlkey, “1”

if [newpatient =“no”] then
wait(2)
typetext mrn
Wait(1)
typetext returnkey
Wait(1)
typetext altkey, A
//Click on book it
Wait(5)
typetext altkey, B

else if [newpatient =“yes”] then
//new patient needs to be created

//Enter a name and add the date and time to make the name unique and sex, birthdate, and phone number
Wait(1)
enter_patient_info

//Click on book it
Wait(5)
typetext altkey, B

//enter address
wait[17]
typetext "123 Street"
wait(1)
typetext tabkey
wait(1)
//enter zipcode
typetext "90212", tabkey
wait(1)
//enter phone number
Click (Image:"a04/HomePhone", WaitFor:5)
wait(1)
typetext "954-444-2342"
wait(1)
typetext tabkey

//add a guarantor
Click (Image:"a04/PatGuarandCvg", WaitFor:4)
//click add guarantor
wait[3]
typetext altkey, r
//click search/create
wait[3]
typetext altkey, a
//click create
wait[3]
typetext altkey, r

//add a coverage
wait(2)
Click (Image:"a04/Coverages", WaitFor:4)
//click add coverage
wait(1)
typetext altkey, d
wait(1)
//type in a coverage
typetext "AAE", tabkey
wait(1)
//accept the coverage
typetext altkey, a
wait(1)
//click on patient as subscriber
typetext returnkey
//override query
wait(1)
typetext altkey, o
wait(8)


//click accept
Wait[3]
Click (Image:"a04/Accept_4", WaitFor:5)

//click continue anyway
wait(1)
Click (Image:"a04/ContinueAnyway_1", WaitFor:3)

end if

//tab to visit type
wait(10)
typetext tabkey
wait(1)
//enter new as the visit type
typetext “new”, tabkey
wait(1)
//enter all for the provider
if ImageFound(text:“Providers”, WaitFor:9) then
Wait(1)
typetext “all”, tabkey
wait(1)
typetext returnkey
wait(1)
//select a time
wait(1)
Click (Image:“a04/Selectatime”, WaitFor:5)
//type E1000 for fammd
wait(1)
typetext “E1000”, tabkey
wait(1)
//enter today for the appt date
typetext “t”
wait(1)
typetext tabkey
//enter now for the time
wait(1)
typetext “n”
wait(1)
typetext tabkey
wait(1)
//click check mark
wait(1)
Click {Image:“a04/checkmark”, WaitFor:4}
end if
if ImageFound(Image:“a04/personsicon”, WaitFor:9) then
//click the time field
Click {text:“Select a time”, WaitFor:5}
wait(1)
//enter today for the appt date
typetext “t”
wait(1)
typetext tabkey
//enter now for the time
wait(1)
typetext “n”
wait(1)
typetext tabkey
wait(1)
//click check mark if there
if ImageFound(Image:“a04/personsicon”, WaitFor:9) then
Click (Image:“a04/checkmark”, WaitFor:4)
end if

end if

wait(1)
//click continue
typetext altkey, n
//click schedule
wait[2]
typetext altkey, s
//click schedule
wait[2]
typetext altkey, s
//click accept
Click {Image:“a04/Accept_3”, WaitFor:20}

//click continue anyway

Click {Image:“a04/ContinueAnyway”, WaitFor:3}

(*
//If registration wizard shows up, click finish
if ImageFound(Image:“a04/RegistrationWizard”, WaitFor:9) then
Wait(1)
typetext altkey, f
end if*)

//press pagedown to go to latest appointment
wait(1)
typetext pagedown

//check in the patient
Wait(3)
Click (Image:“a04/CheckIn”, WaitFor:7)
Wait[2]
//add a visitor’s account
//click Enc Guar and Cvgs
Click (Image:“a04/EncGuarCvgs”, WaitFor:4)
wait(1)
//select the visit guarantor
Click (Image:“a04/VisitGuarantor”, WaitFor:4)

//add lanuage, ethnicity and race
wait(2)
//click on demographics
Click {text:“demographics”, WaitFor:4}
wait[4]
typetext pagedown, pagedown
wait(1)
//click on preferred language and enter a value
Click (Image:“a04/PreferredLanguage”, WaitFor:4)
wait(1)
typetext “17”, tabkey
wait(1)
//tab to ethnicity and enter a value
typetext tabkey, tabkey, tabkey, tabkey
wait(1)
typetext “2”, tabkey
//tab to race and enter a value
wait(1)
typetext tabkey, tabkey
wait(1)
typetext “1002”, tabkey

//click check in again
wait(2)
Click {Image:“a04/CheckIn1”, WaitFor:5}

//click ask later
wait(2)
if ImageFound(text:“Ask Later”, waitFor:7) then
Typetext altkey, L
end if

//click continue anyway
wait(2)
Click {Image:“a04/ContinueAnyway”, WaitFor:3}

//put mrn into variable
Wait[7]
typetext altkey, “f”
put readtext(“a13/MRN_a_TL”, “a13/MRN_a_BR”) into CreatedMRN
log createdMRN

//verify if patient shows arrived
if ImageFound(imageName:“a04/arrived”, waitFor:7) then
logsuccess “Patient is checked in and the appointment desk screen is displayed.”
else
logerror “Patient is not checked in and the appointment desk screen is displayed.”
end if

//click close epic
closeepic

//returning MRN
return createdMRN

Hey @shah,

Here are my findings and an optimized version.
As you did not mention what particularly you’d like to optimize in your code I generally reviewed it with common sense and based on best practices. Check this link Eggplant best practices

  1. Try to avoid using long “hard” waits. Better work with “waitfor” as it will continue once the content appears. Use the click/ action and validate method. You click and you validate that the expected text or image is on screen and continue.
  2. Eggplant by default waits 0.7 seconds between two commands that are executed on a SUT (System under test) Which means having a wait of 1 seconds is not really required. I’ve added set the remoteworkinternal to 1 at the beginning of your scripts which changes the default 0.7 to 1 seconds default waittime. Here you can find more details
  3. for long wait like 5,8,17 or similar: ask yourself why do I need them. What am I waiting for and make use of the click and validate. There is always something that indicates that the app accepts clicks and commands
  4. tabkey: You are making use of the tabkey a lot. I assume (as I do not know what the script actually does) That you are jumping from one entry field to another. This works as long as the form or UI does not change wrt to adding/ removing /different order of fields. You can use a function similar to this in order to define a field name and click at a distance (hospot)from the initially found location
textEntry "My Name", "Karsten","200,0" # This will click 100 pixel right to the center of the text "My Name"
to textEntry label, input, offset:(100,0)
	click text: label + offset
	typetext controlkey,"a" #fail safe if the box has already text in it 
	typetext input
end textEntry
  1. You do you a lot of keyboard short cuts to control you app. This is fine in general. In case you want to test you app from UI perspective you might want to use the actual on screen buttons to make sure they do work as expected. Might be that the shortcut works but your on screen button not (that’s what we want to ideally confirm and find out with UI based testing)
  2. You do have a couple of typetext with numbers like typetext “17”, tabkey wouldn’t it make sense to have these as variables available?
  3. In case you work for an active customer of ours. Kindly ask you to reach out to your Eggplant Account manager to ask for a dedicated Code review session. Which he/she will organize for you.

Here the reviewed code: (Welcome the others to post their review and feedback) There is more than one solution.

put "yes" into newpatient //new patient will be created
put "80000" into Dept
set the remoteworkinterval to 1 // Default is 0.7 set Seconds 
//Call open_tst which opens tst and verifies it opens, pass Modeluser and dept as parameters
open_tst_for_appt ModelUser, Dept

//click appointment button
Waitfor 20, Text:"Appointment"##wait 16 

typetext controlkey, "1"

if newpatient ="no" then
	wait 2
	typetext mrn
	#wait 1
	typetext returnkey
	#wait 1
	typetext altkey, A
	//Click on book it
	Waitfor 5, text:"Text"#wait 5
	typetext altkey, B
	
else #if newpatient ="yes" then Not required if its just yes or no
	//new patient needs to be created
	
	//Enter a name and add the date and time to make the name unique and sex, birthdate, and phone number
	#wait 1
	enter_patient_info
	
	//Click on book it
	Waitfor 5, text:"Text"
	typetext altkey, B
	
	//enter address
	Waitfor 20, IMage:"Address"#wait 17
	typetext "123 Street"
	#wait 1
	typetext tabkey
	#wait 1
	//enter zipcode
	typetext "90212", tabkey
	#wait 1
	//enter phone number
	Click (Image:"a04/HomePhone", WaitFor:5)
	#wait 1
	typetext "954-444-2342"
	#wait 1
	typetext tabkey
	
	//add a guarantor
	Click (Image:"a04/PatGuarandCvg", WaitFor:4)
	//click add guarantor
	Waitfor 5, image:"AddedGuarantorIcon"#wait 3
	typetext altkey, r
	//click search/create
	waitfor 20, image:"CreatePopup"#wait 3
	typetext altkey, a
	//click create
	Waitfor 5, image:"validatorPic"#wait 3
	typetext altkey, r
	
	//add a coverage
	Waitfor 5, image:"validatorPic"#wait 2
	Click (Image:"a04/Coverages", WaitFor:4)
	//click add coverage
	#wait 1
	typetext altkey, d
	#wait 1
	//type in a coverage
	typetext "AAE", tabkey
	#wait 1
	//accept the coverage
	typetext altkey, a
	#wait 1
	//click on patient as subscriber
	typetext returnkey
	//override query
	#wait 1
	typetext altkey, o
	Waitfor 8, image:"validatorPic"#wait 8
	
	
	//click accept
	#wait 3
	Click {Image:"a04/Accept_4", WaitFor:5} // This commands waits already for the image. no need to add another hard wait before. It will search multiple times within the 5 seconds until it finds it. 
	
	//click continue anyway
	#wait 1
	Click (Image:"a04/ContinueAnyway_1", WaitFor:3)
end if

//tab to visit type
Waitfor 10, TExt: "My visit"#wait 10
# Tabkey is not ideal. In case your UI changes you might end up another field and enter the wrong data
typetext tabkey
#wait 1
//enter new as the visit type
typetext "new", tabkey
#wait 1
//enter all for the provider
if ImageFound(text:"Providers", WaitFor:9) then
	#wait 1
	typetext "all", tabkey
	#wait 1
	typetext returnkey
	#wait 1
	//select a time
	#wait 1
	Click (Image:"a04/Selectatime", WaitFor:5)
	//type E1000 for fammd
	#wait 1
	typetext "E1000", tabkey
	#wait 1
	//enter today for the appt date
	typetext "t"
	#wait 1
	typetext tabkey
	//enter now for the time
	#wait 1
	typetext "n"
	#wait 1
	typetext tabkey
	#wait 1
	//click check mark
	#wait 1
	Click {Image:"a04/checkmark", WaitFor:4}
end if

if ImageFound(Image:"a04/personsicon", WaitFor:9) then
	//click the time field
	Click {text:"Select a time", WaitFor:5}
	#wait 1
	//enter today for the appt date
	typetext "t"
	#wait 1
	typetext tabkey
	//enter now for the time
	#wait 1
	typetext "n"
	#wait 1
	typetext tabkey
	#wait 1
	//click check mark if there
	if ImageFound(Image:"a04/personsicon", WaitFor:9) then
		Click (Image:"a04/checkmark", WaitFor:4)
	end if
	
end if

#wait 1
//click continue
typetext altkey, n
//click schedule
wait 2
typetext altkey, s
//click schedule
wait 2
typetext altkey, s
//click accept
Click {Image:"a04/Accept_3", WaitFor:20}

//click continue anyway

Click {Image:"a04/ContinueAnyway", WaitFor:3}

(*
//If registration wizard shows up, click finish
if ImageFound(Image:"a04/RegistrationWizard", WaitFor:9) then
#wait 1
typetext altkey, f
end if*)

//press pagedown to go to latest appointment
#wait 1
typetext pagedown

//check in the patient
wait 3
Click (Image:"a04/CheckIn", WaitFor:7)
#wait 2 The waitfor in the click already has an included wait. 
//add a visitor's account
//click Enc Guar and Cvgs
Click (Image:"a04/EncGuarCvgs", WaitFor:4)
#wait 1
//select the visit guarantor
Click (Image:"a04/VisitGuarantor", WaitFor:4)

//add lanuage, ethnicity and race
wait 2
//click on demographics
Click {text:"demographics", WaitFor:4}
wait 4
typetext pagedown, pagedown // validate that the content is visible . in the future it might be 2 pagedown
#wait 1
//click on preferred language and enter a value
Click (Image:"a04/PreferredLanguage", WaitFor:4)
#wait 1
typetext "17", tabkey
#wait 1
//tab to ethnicity and enter a value
typetext tabkey, tabkey, tabkey, tabkey # .... Use text or images to identify the fields you'd like to click and fill. You can use the hostspot to click at another location in respect to the found center location 
#wait 1
typetext "2", tabkey
//tab to race and enter a value
#wait 1
typetext tabkey, tabkey
#wait 1
typetext "1002", tabkey

//click check in again
wait 2
Click {Image:"a04/CheckIn1", WaitFor:5}

//click ask later
wait 2
if ImageFound(text:"Ask Later", waitFor:7) then
	Typetext altkey, L
	# Alternative in case the text "ask later " is clickable
	# click foundimagelocation() # this contains the last image search location, In this case the one from the imagefound within the if statement
end if

//click continue anyway
wait 2
Click {Image:"a04/ContinueAnyway", WaitFor:3}

//put mrn into variable
waitfor 7, text: "Mytest" #Wait 7
typetext altkey, "f"
put readtext("a13/MRN_a_TL", "a13/MRN_a_BR") into CreatedMRN
log createdMRN

//verify if patient shows arrived
if ImageFound(imageName:"a04/arrived", waitFor:7) then
	logsuccess "Patient is checked in and the appointment desk screen is displayed."
else
	logerror "Patient is not checked in and the appointment desk screen is displayed."
end if

//click close epic
closeepic // assume this is another handler you haven't shared

//returning MRN
return createdMRN # Why do you return it at the end of the script you do log it already? 

Thank you Karsten for the review. Appreciate it, very helpful!

1 Like