Skip to main content
Astronaut
Dec 9, 2024 6:21 PM UTC
Question

Decimal precision validation

I'm trying to do two things with my test script.

  1. I would like to determine the decimal precision of an extracted value
  2. I would like to compare that precision value to the value of setting to determine if the precision value is being respected.

Does anyone have any idea on how I can go about this?
Thanks!

6 replies

Ramanan
Ace Pilot
Dec 10, 2024 10:46 AM UTC

I'm trying to do two things with my test script.

  1. I would like to determine the decimal precision of an extracted value
  2. I would like to compare that precision value to the value of setting to determine if the precision value is being respected.

Does anyone have any idea on how I can go about this?
Thanks!

Hello ​@bikeguychicago ,

When working with decimal precision validation in test scripts, you have several effective approaches to achieve your goals. Let me walk you through a detailed solution that covers both determining decimal precision and comparing it against expected settings.

Approach 1: Custom JavaScript Function for Precision Checking

function getDecimalPrecision(value) {

  // Convert value to string to handle precision

  const stringValue = value.toString();

  // Split the number at decimal point

  const parts = stringValue.split('.');

  // Return length of decimal part, or 0 if no decimal

  return parts.length > 1 ? parts[1].length : 0;

}

// Usage example

function validateDecimalPrecision(extractedValue, expectedPrecision) {

  const actualPrecision = getDecimalPrecision(extractedValue);

  return actualPrecision === expectedPrecision;

}

 

Approach 2: Regular Expression Method

function checkDecimalPrecision(value, expectedPrecision) {

  const precisionRegex = new RegExp(`^\\d+\\.\\d{${expectedPrecision}}$`);

  return precisionRegex.test(value.toFixed(expectedPrecision));

}

Practical Testim Implementation

Here's a comprehensive test script integrating these techniques:

testim.test({

  name: 'Decimal Precision Validation',

  steps: [

    {

      type: 'extract',

      value: '//input[@id="price"]',

      store: 'extractedPrice'

    },

    {

      type: 'custom',

      fn: function(context) {

        const extractedValue = context.store.extractedPrice;

        const expectedPrecision = 2; // Two decimal places

        

        // Method 1: Precision Check

        const precisionCheck = getDecimalPrecision(extractedValue) === expectedPrecision;

        

        // Method 2: Regex Validation

        const regexCheck = checkDecimalPrecision(extractedValue, expectedPrecision);

        // Assert both checks pass

        if (!precisionCheck || !regexCheck) {

          throw new Error(`Decimal precision validation failed. 

            Extracted Value: ${extractedValue}, 

            Expected Precision: ${expectedPrecision}`);

        }

      }

    }

  ]

});

Key Considerations

1. **Flexible Validation**: The approaches work with various numeric inputs

2. **Handles Different Scenarios**: Works with integers, decimals

3. **Precise Error Reporting**: Provides detailed error messages

 Pro Tips

- Use `toFixed()` for consistent decimal representation

- Always handle potential type conversions

- Consider rounding logic for complex numeric scenarios

Bonus Recommendation

For more complex financial or scientific calculations, consider using libraries like `decimal.js` which offer advanced precision handling.

 

By implementing these strategies, you'll create robust decimal precision validation in your Testim scripts. The methods are flexible, easy to understand, and provide comprehensive coverage for most precision validation needs.

 

Hope this helps,

 

Thanks,

Ramanan

Hunt the bugs, ensure the hugs. Quality is everything.
Ramanan
Ace Pilot
Dec 11, 2024 2:48 AM UTC

I'm trying to do two things with my test script.

  1. I would like to determine the decimal precision of an extracted value
  2. I would like to compare that precision value to the value of setting to determine if the precision value is being respected.

Does anyone have any idea on how I can go about this?
Thanks!

Hello ​@bikeguychicago ,

Did you try the above approach?

Thanks,

Ramanan

Hunt the bugs, ensure the hugs. Quality is everything.
Astronaut
Dec 11, 2024 3:22 PM UTC

I'm trying to do two things with my test script.

  1. I would like to determine the decimal precision of an extracted value
  2. I would like to compare that precision value to the value of setting to determine if the precision value is being respected.

Does anyone have any idea on how I can go about this?
Thanks!

Hello ​@bikeguychicago ,

Did you try the above approach?

Thanks,

Ramanan

 

Ramanan, 

I am trying to implement the JavaScript method you provided through a set of custom action.

For the decimal precision function, my custom action looks like this:

 

I removed the value.toSting function as the value I am trying to parse -- decimalValue --- is already in string format. decimalVaule is fetched via an Extract Value step earlier in the test. 

 

When I run the action, it passes. However, no value is returned, as shown below. 

What do I need to do in order to persist the return value so that it can be used by the validation function?

 

Thank you so much for your help. I’m a bit of a newbie with custom actions so I need a bit of hand-holding in order to get things working. 

 

Keith

 

 

Astronaut
Dec 11, 2024 3:52 PM UTC

I revised the function after I realized the validation function called the other function. However, the step still passes, even if the decimal precision (2) doesn’t match the setting for the decimal precision in our app (3). 

 

The function looks like this: 

 

Any ideas?

Thanks!

Ramanan
Ace Pilot
Dec 12, 2024 3:10 AM UTC

I revised the function after I realized the validation function called the other function. However, the step still passes, even if the decimal precision (2) doesn’t match the setting for the decimal precision in our app (3). 

 

The function looks like this: 

 

Any ideas?

Thanks!

Hello ​@bikeguychicago ,

I understand your concern about the decimal precision not matching the app's setting. Let's take a closer look at the code and see.

The `getDecimalPrecision` function you provided appears to be correctly splitting the decimal value and returning the length of the decimal part. However, the `validateDecimalPrecision` function is comparing the actual precision to the `precisionSettingValue`, which is set to 2 in your implementation.

To ensure the decimal precision matches the app's setting of 3, you can modify the `validateDecimalPrecision` function like this:

function validateDecimalPrecision(extractedValue, precisionSettingValue) {
  const actualPrecision = getDecimalPrecision(extractedValue);
  return actualPrecision === precisionSettingValue;
}

Now, the function will return `true` only if the actual precision matches the `precisionSettingValue` of 3.

Another approach could be to update the `getDecimalPrecision` function to round the decimal value to the desired precision before returning the length:

function getDecimalPrecision(decimalValue) {
  const parts = decimalValue.split('.');
  // Round the decimal part to the desired precision
  const roundedDecimalPart = parseFloat(parts[1]).toFixed(3);
  return roundedDecimalPart.length;
}

This way, the function will always return the correct precision, regardless of the original decimal value.

Let me know if either of these solutions works for you or if you have any other questions!

 

Hope this helps!

 

Thanks ,

Ramanan

Hunt the bugs, ensure the hugs. Quality is everything.
Ramanan
Ace Pilot
Dec 22, 2024 2:53 AM UTC

I revised the function after I realized the validation function called the other function. However, the step still passes, even if the decimal precision (2) doesn’t match the setting for the decimal precision in our app (3). 

 

The function looks like this: 

 

Any ideas?

Thanks!

Hello ​@bikeguychicago ,

Did you try the above?

Thanks,

Ramanan

Hunt the bugs, ensure the hugs. Quality is everything.