Skip to content
Advertisement

how extract value from calc function in tests

I am trying to create a test in which I want to compare the width of an element after it has been resized by dragging it. The problem is that the resize library returns the width value as

JavaScript

The only thing that changes is this percentage value and this is what I want to compare in the test if it is bigger or smaller depending on the mouse movement to the right or left.

However, I do not know how to extract only the percentage value or possibly the subtraction result from this string.

JavaScript

Expected: “50%” Received: “calc(50% – 4px)”

Advertisement

Answer

You could use a regular expression to get the percentage from your string, here’s an example:

JavaScript

Remember to add checks to make sure your regular expression matched something, this is just an example!

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement