0

I have written a reusable function in Cypress:

fill_prices_ht_nt(until, AP, GP, AP_NT, new_bonus){
        this.add_range().click({force:true})
        this.range_until().type(until)
        this.ap().type(AP)
        this.gp().type(GP)
        this.ap_nt().type(AP_NT)
        this.new_bonus().type(new_bonus)

Now I want to use that function to type in values, but unfortunately I need to type in decimal values in European format (with comma instead of periods), like e.g. "9,35" or "6,47"

        pmmt.fill_prices_ht_nt(99999, 10, 9,35, 6,47, 100)

This is of course not working as Cypress/JS treats every comma as a separator.

Is there a somewhat easy workaround to this problem? Otherwise I will have to dump the reusable function and hard-code the values.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
cypher_null
  • 632
  • 8
  • 22
  • *"I need to type in decimal values in European format"* Why? If the syntax needs a dot as a decimal separator then use a dot. – gre_gor Feb 15 '22 at 07:57
  • Because when I type ```6.47``` in the field on the website I am testing it is treated like ```647``` – cypher_null Feb 15 '22 at 08:16
  • You either want raw input which are strings or already parsed data which are floats with a dot, never floats with a comma. – gre_gor Feb 15 '22 at 08:29

3 Answers3

2

You can supply the numbers as strings, like this:

pmmt.fill_prices_ht_nt("99999", "10", "9,35", "6,47", "100")

Then write a utility function to convert those strings to numbers:

function toNumber(val) {
    if (typeof val !== "string") {
        return val;
    }
    return +val.replace(/,/g, ".");
}

(Unary + is just one way to do string-to-number conversion, I provide a list with pros and const in this answer.)

And use it in your function:

fill_prices_ht_nt(until, AP, GP, AP_NT, new_bonus) {
    until = toNumber(until);
    AP = toNumber(AP);
    GP = toNumber(GP);
    AP_NT = toNumber(AP_NT);
    new_bonus = toNumber(new_bonus);
    this.add_range().click({force:true});
    this.range_until().type(until);
    this.ap().type(AP);
    this.gp().type(GP);
    this.ap_nt().type(AP_NT);
    this.new_bonus().type(new_bonus);
}

If you don't mind using a temporary array, you can use destructuring to do the conversions all in one:

function toNumberList(...vals) {
    return vals.map(toNumber);
}

Then using it:

fill_prices_ht_nt(until, AP, GP, AP_NT, new_bonus) {
    [until, AP, GP, AP_NT, new_bonus] = toNumberList(until, AP, GP, AP_NT, new_bonus);
    this.add_range().click({force:true});
    this.range_until().type(until);
    this.ap().type(AP);
    this.gp().type(GP);
    this.ap_nt().type(AP_NT);
    this.new_bonus().type(new_bonus);
}

That assumes an ES2015+ environment (because it uses destructuring).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks, I will definitely use the function toNumberList in the future as I often need to convert decimal values from comma to periods! – cypher_null Feb 15 '22 at 12:37
1

How about you just use the pmmt.fill_prices_ht_nt with all numbers as strings.

 pmmt.fill_prices_ht_nt('99999', '10', '9,35', '6,47', '100')

Because when I see the cypress type, in syntax it says, type accepts these two formats:

.type(text)
.type(text, options)
Alapan Das
  • 17,144
  • 3
  • 29
  • 52
1

Here's one more option, using toLocaleString()

fill_prices_ht_nt(until, AP, GP, AP_NT, new_bonus) {
  this.add_range().click({force:true})
  this.range_until().type(until)
  this.ap().type(AP.toLocaleString('de-DE'))  
  this.gp().type(GP.toLocaleString('de-DE'))   // AP.toLocaleString('de-DE') = 9,35
  this.ap_nt().type(AP_NT.toLocaleString('de-DE'))
  this.new_bonus().type(new_bonus.toLocaleString('de-DE'))

In the test, pass with decimal and it will be converted as you .type()

pmmt.fill_prices_ht_nt(99999, 10, 9.35, 6.47, 100)

Or, you can add a method to set the format if you have other locale's to handle

set_number_format(number_format) {
  this.number_format = number_format
}

fill_prices_ht_nt(until, AP, GP, AP_NT, new_bonus) {
  this.add_range().click({force:true})
  this.range_until().type(until)
  this.ap().type(AP.toLocaleString(this.number_format))  
  this.gp().type(GP.toLocaleString(this.number_format))
  this.ap_nt().type(AP_NT.toLocaleString(this.number_format))
  this.new_bonus().type(new_bonus.toLocaleString(this.number_format))

In the test,

pmmt.set_number_format('de-DE')
pmmt.fill_prices_ht_nt(99999, 10, 9.35, 6.47, 100)
...
pmmt.set_number_format('en-IN')
pmmt.fill_prices_ht_nt(99999, 10, 9.35, 6.47, 100)
Fody
  • 23,754
  • 3
  • 20
  • 37