Popular Releases
Popular Libraries
New Libraries
Top Authors
Trending Kits
Trending Discussions
Learning
13.6.0 | |
v1.21.1 | |
v1.1.0 | |
4.0.0 | |
20.1.0 |
puppeteer 13.6.0 |
playwright v1.21.1 |
headless-recorder v1.1.0 |
forever 4.0.0 |
awx 20.1.0 |
1
17 Libraries
314
2
16 Libraries
78
3
14 Libraries
355
4
12 Libraries
39314
5
12 Libraries
1660
6
11 Libraries
264
7
10 Libraries
236
8
9 Libraries
707
9
8 Libraries
165
10
8 Libraries
145
1
17 Libraries
314
2
16 Libraries
78
3
14 Libraries
355
4
12 Libraries
39314
5
12 Libraries
1660
6
11 Libraries
264
7
10 Libraries
236
8
9 Libraries
707
9
8 Libraries
165
10
8 Libraries
145
QUESTION
In JavaScript, can you reference a method/function name within that method/function?
Asked 2022-Mar-28 at 10:35I 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:
1setElementValue(element, value){
2 try{
3 this.waitForElementAndScroll(element);
4 $(element).setValue(value);
5 }
6 catch(error){
7 throw `${BasePage.name}.setElementValue: ${error.message}`;
8 }
9}
10
After:
1setElementValue(element, value){
2 try{
3 this.waitForElementAndScroll(element);
4 $(element).setValue(value);
5 }
6 catch(error){
7 throw `${BasePage.name}.setElementValue: ${error.message}`;
8 }
9}
10setElementValue(element, value){
11 try{
12 this.waitForElementAndScroll(element);
13 $(element).setValue(value);
14 }
15 catch(error){
16 throw `${BasePage.name}.${setElementValue.name}: ${error.message}`;
17 }
18}
19
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
ANSWER
Answered 2022-Mar-28 at 10:35I found this (very obvious) solution to work for me:
1setElementValue(element, value){
2 try{
3 this.waitForElementAndScroll(element);
4 $(element).setValue(value);
5 }
6 catch(error){
7 throw `${BasePage.name}.setElementValue: ${error.message}`;
8 }
9}
10setElementValue(element, value){
11 try{
12 this.waitForElementAndScroll(element);
13 $(element).setValue(value);
14 }
15 catch(error){
16 throw `${BasePage.name}.${setElementValue.name}: ${error.message}`;
17 }
18}
19this.setElementValue.name
20
where the thrown error message is:
1setElementValue(element, value){
2 try{
3 this.waitForElementAndScroll(element);
4 $(element).setValue(value);
5 }
6 catch(error){
7 throw `${BasePage.name}.setElementValue: ${error.message}`;
8 }
9}
10setElementValue(element, value){
11 try{
12 this.waitForElementAndScroll(element);
13 $(element).setValue(value);
14 }
15 catch(error){
16 throw `${BasePage.name}.${setElementValue.name}: ${error.message}`;
17 }
18}
19this.setElementValue.name
20throw `${BasePage.name}.${this.setElementValue.name}: ${error.message}`;
21
which outputs the following error log when the method fails:
1setElementValue(element, value){
2 try{
3 this.waitForElementAndScroll(element);
4 $(element).setValue(value);
5 }
6 catch(error){
7 throw `${BasePage.name}.setElementValue: ${error.message}`;
8 }
9}
10setElementValue(element, value){
11 try{
12 this.waitForElementAndScroll(element);
13 $(element).setValue(value);
14 }
15 catch(error){
16 throw `${BasePage.name}.${setElementValue.name}: ${error.message}`;
17 }
18}
19this.setElementValue.name
20throw `${BasePage.name}.${this.setElementValue.name}: ${error.message}`;
21`BasePage.setElementValue: element ("#submit-button") still not displayed after 500ms`
22
Community Discussions contain sources that include Stack Exchange Network
QUESTION
In JavaScript, can you reference a method/function name within that method/function?
Asked 2022-Mar-28 at 10:35I 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:
1setElementValue(element, value){
2 try{
3 this.waitForElementAndScroll(element);
4 $(element).setValue(value);
5 }
6 catch(error){
7 throw `${BasePage.name}.setElementValue: ${error.message}`;
8 }
9}
10
After:
1setElementValue(element, value){
2 try{
3 this.waitForElementAndScroll(element);
4 $(element).setValue(value);
5 }
6 catch(error){
7 throw `${BasePage.name}.setElementValue: ${error.message}`;
8 }
9}
10setElementValue(element, value){
11 try{
12 this.waitForElementAndScroll(element);
13 $(element).setValue(value);
14 }
15 catch(error){
16 throw `${BasePage.name}.${setElementValue.name}: ${error.message}`;
17 }
18}
19
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
ANSWER
Answered 2022-Mar-28 at 10:35I found this (very obvious) solution to work for me:
1setElementValue(element, value){
2 try{
3 this.waitForElementAndScroll(element);
4 $(element).setValue(value);
5 }
6 catch(error){
7 throw `${BasePage.name}.setElementValue: ${error.message}`;
8 }
9}
10setElementValue(element, value){
11 try{
12 this.waitForElementAndScroll(element);
13 $(element).setValue(value);
14 }
15 catch(error){
16 throw `${BasePage.name}.${setElementValue.name}: ${error.message}`;
17 }
18}
19this.setElementValue.name
20
where the thrown error message is:
1setElementValue(element, value){
2 try{
3 this.waitForElementAndScroll(element);
4 $(element).setValue(value);
5 }
6 catch(error){
7 throw `${BasePage.name}.setElementValue: ${error.message}`;
8 }
9}
10setElementValue(element, value){
11 try{
12 this.waitForElementAndScroll(element);
13 $(element).setValue(value);
14 }
15 catch(error){
16 throw `${BasePage.name}.${setElementValue.name}: ${error.message}`;
17 }
18}
19this.setElementValue.name
20throw `${BasePage.name}.${this.setElementValue.name}: ${error.message}`;
21
which outputs the following error log when the method fails:
1setElementValue(element, value){
2 try{
3 this.waitForElementAndScroll(element);
4 $(element).setValue(value);
5 }
6 catch(error){
7 throw `${BasePage.name}.setElementValue: ${error.message}`;
8 }
9}
10setElementValue(element, value){
11 try{
12 this.waitForElementAndScroll(element);
13 $(element).setValue(value);
14 }
15 catch(error){
16 throw `${BasePage.name}.${setElementValue.name}: ${error.message}`;
17 }
18}
19this.setElementValue.name
20throw `${BasePage.name}.${this.setElementValue.name}: ${error.message}`;
21`BasePage.setElementValue: element ("#submit-button") still not displayed after 500ms`
22
Community Discussions contain sources that include Stack Exchange Network
Tutorials and Learning Resources are not available at this moment for Automation