Here's a list of Sample Custom Functions created for Testsigma


1. Custom Function to check specified error(given as Argument) is present partially or fully in Console log

2. Custom Function to change attribute value for HTML Element

3. Custom Function to update the Console log errors in Test Step Results Metadata

4. Custom Function to update Runtime Test Data by replacing given text


1. Custom Function to check specified error is present partially or fully in Console log


import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import com.testsigma.customfunc.common.TestsigmaCustomFunctions;
import com.testsigma.customfunc.common.CustomTestStep;
import com.testsigma.customfunc.result.ResultConstants;
import com.testsigma.customfunc.result.TestStepResult;

/* The step with this function should fail if the Console log contains specified error message 
and pass if error message is not present in the Console log or if the Console log is empty. 
Add this as a minor step if you don't want the Test Case to fail even if the step fails.*/

public class ConsoleLogs extends TestsigmaCustomFunctions{
  @CustomTestStep
  public TestStepResult verifyErrorMsgIsPresent(String errorMessage){
    
//your custom code starts here
    TestStepResult result = new TestStepResult();
    LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
    for (LogEntry entry : logEntries) {
      if(entry.getMessage().contains(errorMessage)) {
        result.setMessage("Specified error message present in Console Logs");
        result.setStatus(ResultConstants.FAILURE);
        break;
      }
      else{
        result.setStatus(ResultConstants.SUCCESS);
        result.setMessage("Specified error message absent in Console Logs or Console Logs Empty");
      }
    }
    return result;
  }
}


2. Custom Function to change attribute value for HTML Element


import java.lang.Exception;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import com.testsigma.customfunc.common.TestsigmaCustomFunctions;
import com.testsigma.customfunc.common.CustomTestStep;
import com.testsigma.customfunc.result.ResultConstants;
import com.testsigma.customfunc.result.TestStepResult;

/*
The step containing this function should modify the attribute 'attribute_name'
of the Web Element located by the xpath 'locator' with the value 'final_value'.
If the modification of attribute value fails, the step will fail else it passes. Create
this step as minor step if the Test Case shouldn't fail even if modify attribute fails.
*/

public class ModifyHTMLAttribute extends TestsigmaCustomFunctions{
  @CustomTestStep
  public TestStepResult modifyAttribute(String locator, String attribute_name, String final_value){
    //your custom code starts here
    TestStepResult result = new TestStepResult();
    try{
      WebElement element=driver.findElement(By.xpath(locator));
      JavascriptExecutor js=(JavascriptExecutor)driver;
      js.executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);", element, attribute_name, final_value);
      result.setMessage("Attribute value changed successfully");
      result.setStatus(ResultConstants.SUCCESS);
    }catch(Exception e){
      result.setMessage("Attribute value not changed");
      result.setStatus(ResultConstants.FAILURE);  
    }
    finally{
    return result;
    }
  }
}


3. Custom Function to update the Console log errors in Test Step Results Metadata


import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import com.testsigma.customfunc.common.TestsigmaCustomFunctions;
import com.testsigma.customfunc.common.CustomTestStep;
import com.testsigma.customfunc.result.ResultConstants;
import com.testsigma.customfunc.result.TestStepResult;

/*
The step containing this function should export the Browser Console logs
to the Test Step metadata. If there is some issue with exporting the log,
the Test Step should fail. Create this as minor step if the Test Case should
pass even if the export doesn't work.
*/

public class ConsoleLogsOutput extends TestsigmaCustomFunctions{
  @CustomTestStep
  public TestStepResult printConsoleErrorMsgsToMetadata(){
    //your custom code starts here
    TestStepResult result = new TestStepResult();
    try{
      LogEntries logentries = driver.manage().logs().get(LogType.BROWSER);
      result.getMetadata().put("console_log", logentries.getAll());
      result.setStatus(ResultConstants.SUCCESS);
      result.setMessage("Console log exported to Metadata successfully");
    }
    catch(Exception e)
    {
      result.setStatus(ResultConstants.FAILURE);
      result.setMessage("Failed to export Console log to Metadata"); 
    }
    finally{
    return result;
    }  
  }
}






4. Custom Function to update Run-time Test Data by replacing text

import com.testsigma.customfunc.common.CustomTestStep;
import com.testsigma.customfunc.result.ResultConstants;
import com.testsigma.customfunc.result.TestStepResult;
import com.testsigma.customfunc.common.TestsigmaCustomFunctions;
import com.testsigma.testengine.exceptions.TestEngineException;

public class RuntimeDataUpdate extends TestsigmaCustomFunctions {

  @CustomTestStep
  public TestStepResult updateRuntimeData(String runtimeVariableName, String searchtext, String replacetext) throws TestEngineException {
    TestStepResult result = new TestStepResult();
    try{
      String inittext = getRuntimeData(runtimeVariableName);
      if(inittext.contains(searchtext)){
        String finaltext = inittext.replace(searchtext, replacetext);
        setRuntimeData(runtimeVariableName, finaltext);
        result.setStatus(ResultConstants.SUCCESS);
        result.setMessage("Given search text '"+searchtext+"' replaced with '"+replacetext+"' in runtime variable '"+runtimeVariableName+"'");
      }
      else{
        result.setStatus(ResultConstants.FAILURE);
        result.setMessage("Given search Text '"+searchtext+"' not found in runtime variable with value '"+inittext+"'");
      }
    }catch(TestEngineException testengex){
      result.setStatus(ResultConstants.FAILURE);
      result.setMessage("Failed to update runtime variable - TestEngineException");
    }
  return result;
  }
}