He escrito una función reutilizable en 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)Ahora quiero usar esa función para escribir valores, pero desafortunadamente necesito escribir valores decimales en formato europeo (con coma en lugar de puntos), como por ejemplo "9,35" o "6,47"
pmmt.fill_prices_ht_nt(99999, 10, 9,35, 6,47, 100)Por supuesto, esto no funciona ya que Cypress/JS trata cada coma como un separador.
¿Hay una solución algo fácil para este problema? De lo contrario, tendré que volcar la función reutilizable y codificar los valores.
Puede proporcionar los números como cadenas, así:
pmmt.fill_prices_ht_nt("99999", "10", "9,35", "6,47", "100")Luego escribe una función de utilidad para convertir esas cadenas en números:
function toNumber(val) { if (typeof val !== "string") { return val; } return +val.replace(/,/g, "."); } (Unary + es solo una forma de hacer una conversión de cadena a número, proporciono una lista con ventajas y constantes en esta respuesta ).
Y úsalo en tu función:
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); }Si no le importa usar una matriz temporal, puede usar la desestructuración para hacer las conversiones todo en uno:
function toNumberList(...vals) { return vals.map(toNumber); }Luego usándolo:
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); }Eso supone un entorno ES2015+ (porque usa desestructuración).
Aquí hay una opción más, usando 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')) En la prueba, pase con decimal y se convertirá a medida que .type()
pmmt.fill_prices_ht_nt(99999, 10, 9.35, 6.47, 100)O bien, puede agregar un método para establecer el formato si tiene otras configuraciones regionales para manejar
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))En la prueba,
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)