I am building an automation framework in JavaScript for WebdriverIO.
The "out-of-the-box" error messages aren't too helpful and I would like to add the class name and method name as part of a prefix to the error message which is thrown when the method/function fails.
I have managed to call the class name with ClassName.name
However, I have not found a solution to reference the method name without using some hardcoded value.
Below is a summary of the changes I would like to make.
Before:
setElementValue(element, value){
try{
this.waitForElementAndScroll(element);
$(element).setValue(value);
}
catch(error){
throw `${BasePage.name}.setElementValue: ${error.message}`;
}
}
After:
setElementValue(element, value){
try{
this.waitForElementAndScroll(element);
$(element).setValue(value);
}
catch(error){
throw `${BasePage.name}.${setElementValue.name}: ${error.message}`;
}
}
Where ${setElementValue.name} is the method of referencing the name of the method/function.
Many thanks in advance!
I have tried the following with no success:
method.name //ReferenceError: method is not defined
(method).name //ReferenceError: method is not defined
Function.name //Returns "Function"
Function.value //Returns "undefined"
constructor.name //Returns "Object"
setElementValue.name //ReferenceError: setElementValue is not defined
I found this (very obvious) solution to work for me:
this.setElementValue.name
where the thrown error message is:
throw `${BasePage.name}.${this.setElementValue.name}: ${error.message}`;
which outputs the following error log when the method fails:
`BasePage.setElementValue: element ("#submit-button") still not displayed after 500ms`