How to call certain steps from a test case to another case

Sanil Thomas said:

Marek Melocik said:

Of course. This is very easy test case (displayed in Script mode):

General.login()WebUI.click(findTestObject("Objects/Object1"))WebUI.click(findTestObject("Objects/Object2"))WebUI.click(findTestObject("Objects/Object3"))WebUI.click(findTestObject("Objects/Object4"))WebUI.click(findTestObject("Objects/Object5"))WebUI.click(findTestObject("Objects/Object6"))WebUI.click(findTestObject("Objects/Object7"))WebUI.click(findTestObject("Objects/Object8"))WebUI.click(findTestObject("Objects/Object9"))WebUI.click(findTestObject("Objects/Object10"))General.logout()

Now, let's say you want to reuse actions 2 to 8. So you simply copy them out and go to **Keywords** in left menu, create a new keyword there and create a new method:

public class YourKeywordName { public static void clickFrom2to8() { WebUI.click(findTestObject(“Objects/Object2”))
WebUI.click(findTestObject(“Objects/Object3”)) WebUI.click(findTestObject(“Objects/Object4”)) WebUI.click(findTestObject(“Objects/Object5”)) WebUI.click(findTestObject(“Objects/Object6”)) WebUI.click(findTestObject(“Objects/Object7”)) WebUI.click(findTestObject(“Objects/Object8”)) }}


And now, simply call this method in your original test case:

General.login()WebUI.click(findTestObject(“Objects/Object1”))YourKeywordName.clickFrom2to8()WebUI.click(findTestObject(“Objects/Object9”))WebUI.click(findTestObject(“Objects/Object10”))General.logout()


And that's it, you can use the same code in multiple test cases without duplicating it. Do not forget to import your new keyword (it behaves as normal Java class).

  

you are awesome sir !
Thank you

Indeed. That’s one of the clearest answers to a question with a potentially long and dense answer I’ve seen on this forum. Kudos to @Marek.

@Sanil - why don’t you click “Best Answer” ?

1 Like