How can I Modularize this code?

Hi
How would you modularize this code?

Thanks

//Step 2- Click CTU homepage
Click "CTU"
Wait(3)
//Checking to see if the word Development is on the page to verify you are on the CTU homepage
put readtext("CTU_development_A","CTU_development_B")into myReadText
If myReadText is equal to "Development" then
//Closing CTU homepage
Click "iinDevasp"
end if

//step 3 - Click on Ambulatory Care Nursing link
Wait(3)
Click "AmbulatoryC"
Wait(3)
put readtext("CTU_ambulatory_A","CTU_ambulatory_B")into myReadText
If myReadText is equal to "Ambulatory" then
//Closing Ambulatory homepage
Click "ACN"
end if

//step 4 - Click on CA Newborn Hearing Screening Program link
Wait(3)
Click "HearingScreening"
Wait(3)
put readtext("oldnurs_newborn_hearing_A","oldnurs_newborn_hearing_B")into myReadText
If myReadText is equal to "https://icms.dhcs.ca.gov is correct." then
//Closing CA Newborn homepage
Click "httpsicmsdhcscagoviscorrect"
end if

Comments

  • EggplantMattEggplantMatt ForumAdmin admin
    I see why you would think that this code could be refactored because it has a very repetitive structure, but in truth I think that it's already about as good as it's going to get. The reason that you can't really perform a useful refactoring is that you are supplying the parameters for every command and they are all diffferent. If the search area for the readText() call was being derived from the image used in the initial Click command, then you could write a handler that received the image to click as an argument and then did the rest dynamically based on that image, but you're hard-coding everything so that won't work. You would have to pass all the images as arguments, and I think rather than making things easier and more readable, it would make your code less clear.

    If you did refactor based on your existing code, here's what it would look like:
    //Step 2- Click CTU homepage
    checkText "CTU","CTU_development_A","CTU_development_B","Development","iinDevasp"
    
    //step 3 - Click on Ambulatory Care Nursing link
    checkText "AmbulatoryC","CTU_ambulatory_A","CTU_ambulatory_B","Ambulatory","ACN"
    
    //step 4 - Click on CA Newborn Hearing Screening Program link
    checkText "HearingScreening","oldnurs_newborn_hearing_A","oldnurs_newborn_hearing_B","https://icms.dhcs.ca.gov is correct.","httpsicmsdhcscagoviscorrect"
    
    on checkText sectionToClick,ulImage,lrImage,validationString,exitclick
    	Wait 3
    	Click sectionToClick
    	Wait 3
    	//Checking to validation text
    	put readtext(ulImage,lrImage)into myReadText
    	If myReadText is equal to validationString then
    		// closing page
    		Click exitclick
    	end if
    end checkText
    

    I don't personally think that's a great improvement.
Sign In or Register to comment.