Screen Shot Handling


#1

Hi…,

I am writing the script for web testing. And I have code around 100 lines where with set text, click the buttons and etc.
Now the question is, I wanted to handle the screen shot when the script is failed. I wanted to handle this without auto screen shot. Screenshot name should contain with the date & time also.
where do we write the code to take the screen shot when I have 100 lines of code as we cannot one after the every line of the code. I am coding with the CONTINUE_ON_FAILURE option. In this case, how to identify particular test case is failed. Can someone please share the code for taking the screenshot !!

Thanks in advance & Appreciate your help.


#2

@Russ_Thomas
@Dave_Evers
@kazurayam
@Ibus

Pumping this up as I am sure someone somewhere can help on this faster than I can.


#3

The only solution I know is to write tests the way I write them - but it’s not for everyone.

The idea is to wrap all test case steps in try/catch(). All failures must come from throw. In the catch() you can add any failure handling, including screenshots.

public class Test extends basePage {
  
  Test() {
    WebUI.whatever()
    // More steps
  }

}

try {
  new Test()
  passThisTest()
} catch(Exception e) {
  failThisTest(e.message)
  // Do Screenshot
}

Otherwise, I guess we’re talking about metaprogramming, and for that @kazurayam is your man.


#4

I think listeners can help with this, found some discussion on github. e.g sort of:

	@AfterTestCase
	def sampleAfterTestCase(TestCaseContext testCaseContext) 
	{
		if (testCaseContext.getTestCaseStatus() == 'PASSED')
		{
			doSomething()
		}
		else if (testCaseContext.getTestCaseStatus() == 'FAILED')
		{
			doTheSShotJob()
		}
	}

source: https://github.com/katalon-studio-samples/katalon-web-automation/issues/2


#5

The downside with listeners is, they are not in the same scope (or execution context) as the groovy script class that hosts your Test Case - the script class run() method[1].

But what do I know… this may be exactly what OP needs.


[1] The more I stumble across this, the more I think the devs got this part of the design wrong. I’d appreciate a new set of hooks, something like this pseudocode:

object run( ... ) {
  ifExists(runStart) runStart(context)

  // call Test Case Steps

  ifExists(runEnd) runEnd(context)

}

#6

Why? What is the reason why the auto screen shot is not enough for you? In other words, what problem do you want to solve by taking screen shots in your own way?


#7

Really thanks to @ThanhTo, @Russ_Thomas, @Ibus, @kazurayam for looking into this and for guiding.

Actually, I have the code for example 100 lines between try…catch statement. When I execute the testsuite, I can check the ProjectSettings -> Report -> TakeScreenshot check box. But that screenshot will not have test case number, data & time. So, I wanted to handle it separately with the script.
So, when I have 100 lines of code between the try…catch statements, after what line I need to write the screenshot code and how to write that ?

Coming to the CONTINUE_ON_FAILURE, all my coding statements will have WebUI.XX(findTestobject(xx, CONTINUE_ON_FAILURE) because we don’t want the browser to get close until the suite is completed. So, how to identify particular line is failed and if it is failed, how to store it into variable and update the status sheet with the failed status.

The above two are my queries. Can you please look into this and guide me to proceed further ?
Please let me know if you have any questions in this.

Example:

try {
— --- CODE -----
— --- CODE -----
}catch (StepErrorException e) {
this.println(e) }
catch (Exception e) {
this.println(“General Issue Occurs”) }
finally {
this.println(“Navigate to Page”)
}


#9

you may have to move the closebrowser() also in the listener, using@afterSuite

@Russ_Thomas slowly but slowly you will move to python. pytest has tones of hooks and if not enough, you can make custom ones :smiley:


#10

So… you all suggesting me to use the listeners for handling the above both ?
Can you please provide me sample example with the code ?

Appreciate your help.


#11

nope. try/catch will work too. so … you have to share your code and we will came with detailed solutions top of it :slight_smile:


#12

@Ibus … Thank You. Here I am sharing my code. Can you please guide me to proceed further.


> import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
> import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
> import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
> import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
> import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
> import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
> import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
> import com.kms.katalon.core.model.FailureHandling as FailureHandling
> import com.kms.katalon.core.testcase.TestCase as TestCase
> import com.kms.katalon.core.testdata.TestData as TestData
> import com.kms.katalon.core.testobject.ConditionType as ConditionType
> import com.kms.katalon.core.testobject.TestObject as TestObject
> import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
> import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
> import internal.GlobalVariable as GlobalVariable
> import com.kms.katalon.core.exception.StepErrorException
> import com.kms.katalon.core.webui.driver.DriverFactory
> import com.kms.katalon.core.webui.exception.WebElementNotFoundException
> import com.kms.katalon.core.testobject.TestObjectProperty
> import com.kms.katalon.core.testobject.RequestObject
> import com.kms.katalon.core.testobject.ResponseObject
> import org.openqa.selenium.WebElement
> import org.openqa.selenium.WebDriver
> import org.openqa.selenium.By
> 
> 
> try {
>    String intemp = ""
>    String inquiryt1 = ""
>    def valstore = new String[5]
>    int intal = 0
>    
> String objv = WebUI.getText(findTestObject('Object Repository/ORTC01/Page_View and Analyze Govt Bills/a_Home'), FailureHandling.CONTINUE_ON_FAILURE)
> 
> WebUI.click(findTestObject('Object Repository/ORTC01/Page_View and Analyze Govt Bills/a_Govt Inquiry'), FailureHandling.CONTINUE_ON_FAILURE)
> WebUI.verifyTextPresent("Inquiry", false, FailureHandling.CONTINUE_ON_FAILURE)
> 	  
> WebUI.selectOptionByValue(findTestObject('Object Repository/ORTC01/Page_Govt Inquiry/select_All ContractsAll non-Networx'), contract, false, FailureHandling.CONTINUE_ON_FAILURE)
> 
>   WebUI.click(findTestObject('Object Repository/ORTC01/Page_Govt Inquiry/input_Choose Contract Number_button'), FailureHandling.CONTINUE_ON_FAILURE)
> 	  
>   WebUI.click(findTestObject('Object Repository/ORTC01/Page_Govt Inquiry/a_Create Inquiry'), FailureHandling.CONTINUE_ON_FAILURE)
>   WebUI.verifyTextPresent("Create Inquiry", false, FailureHandling.CONTINUE_ON_FAILURE)
> 	  
>  WebUI.setText(findTestObject('Object Repository/ORTC01/Page_Create Inquiry/input_ Title _ttl_short_desc_text'), title, FailureHandling.CONTINUE_ON_FAILURE)
> 
>  WebUI.setText(findTestObject('Object Repository/ORTC01/Page_Create Inquiry/input_ AHC _ahc_code'), ahc, FailureHandling.CONTINUE_ON_FAILURE)
> 
>   WebUI.setText(findTestObject('Object Repository/ORTC01/Page_Create Inquiry/input_ Invoice No _full_invoice_id'), invoice, FailureHandling.CONTINUE_ON_FAILURE)
> 	  
>    WebUI.uploadFile(findTestObject('Object Repository/ORTC01/Page_Create Inquiry/input_Attach a file_file_attach_text'), intemp, FailureHandling.CONTINUE_ON_FAILURE)
> 
>    WebUI.click(findTestObject('Object Repository/ORTC01/Page_Create Inquiry/input_Attach a file_addattach'), FailureHandling.CONTINUE_ON_FAILURE)
> 
>    WebUI.selectOptionByIndex(findTestObject('Object Repository/ORTC01/Page_Commu_mode/select_Make a Selection  Web onlyWeb and phoneWeb and fax'), 2) 
>   
>    WebUI.setText(findTestObject('Object Repository/ORTC01/Page_Create Inquiry/input_ Requestor Name _extrnl_customer_name'), requestor, FailureHandling.CONTINUE_ON_FAILURE)
> 	  
> 	  
> WebUI.click(findTestObject('Object Repository/ORTC01/Page_Create Inquiry/input_Agency Inquiry ID _button'), FailureHandling.CONTINUE_ON_FAILURE)
> 
> TestObject testObject = new TestObject().addProperty("xpath", ConditionType.EQUALS, "//td[@class='pageTitle'][1]")
> inquiryt1 = WebUI.getText(testObject)
> WebUI.click(findTestObject('Object Repository/ORTC04/Page_Govt Inquiry/a_Home'), FailureHandling.CONTINUE_ON_FAILURE)
> 	 
> WebUI.click(findTestObject('Object Repository/ORTC03/Page_Govt Inquiry/a_ReportsSearch'), FailureHandling.CONTINUE_ON_FAILURE)
> 
>  WebUI.click(findTestObject('Object Repository/ORTC03/Page_ReportsSearch/a_QuickSearch'), FailureHandling.CONTINUE_ON_FAILURE)
> 
>  WebUI.setText(findTestObject('Object Repository/ORTC03/Page_Quick Search/input_Submitter Like_submitter'), submitQS, FailureHandling.STOP_ON_FAILURE)
> 	 
>  WebUI.click(findTestObject('Object Repository/ORTC03/Page_Quick Search/input_Agency Inquiry ID_button'), FailureHandling.CONTINUE_ON_FAILURE)
> 
> WebUI.verifyTextPresent("Quick Search Results", false, FailureHandling.CONTINUE_ON_FAILURE)
> 
> WebUI.click(findTestObject('Object Repository/ORTC03/Page_Govt Inquiry/a_ReportsSearch'), FailureHandling.CONTINUE_ON_FAILURE)
> 
> WebUI.click(findTestObject('Object Repository/ORTC03/Page_ReportsSearch/a_QuickSearch'), FailureHandling.CONTINUE_ON_FAILURE)
> 
> WebUI.setText(findTestObject('Object Repository/ORTC03/Page_Quick Search/input_Agency Inquiry ID_agencyInqID'), agncyIQS, FailureHandling.CONTINUE_ON_FAILURE)
> 	 
> 
> } catch (StepErrorException e) {
	   this.println(e) }
       catch (Exception e) {
 	   this.println("General Issue Occurs") }
      finally {
 	   this.println("Navigate to Page") 
      }

#13

Hi…@Russ_Thomas, @Dave_Evers, @kazurayam, @Ibus,
Can you please someone look into this to proceed further !!


#14

@vdavuluri2 as i mentioned in the private message you sent me … we are here for fun, not employees. and we have jobs too.
so please be patient until we find time to analyse your issue and mock-up some code. thank you!