| 1 |
/* global posmodel */ |
| 2 |
|
| 3 |
import * as Numpad from "@point_of_sale/../tests/generic_helpers/numpad_util"; |
| 4 |
import * as Dialog from "@point_of_sale/../tests/generic_helpers/dialog_util"; |
| 5 |
import * as PartnerList from "@point_of_sale/../tests/pos/tours/utils/partner_list_util"; |
| 6 |
import * as NumberPopup from "@point_of_sale/../tests/generic_helpers/number_popup_util"; |
| 7 |
|
| 8 |
/** |
| 9 |
* Clicks on the payment method and then performs checks if necessary. |
| 10 |
* |
| 11 |
* @param {string} name - The name of the payment method to click on. This name is used to identify the corresponding element in the user interface. |
| 12 |
* @param {boolean} [isCheckNeeded=false] - Indicates whether additional checks are necessary after clicking on the payment method. If `true`, additional verification steps will be added to ensure that the expected changes (such as the remaining amount, change, or selected amount) are correctly applied. |
| 13 |
* @param {Object} [options={}] - An object containing additional options for the checks. The options include: |
| 14 |
* @param {string|null} [options.remaining=null] - The expected remaining amount after selecting the payment method. If provided and `isCheckNeeded` is `true`, a check will be performed to ensure this remaining amount is correct. |
| 15 |
* @param {string|null} [options.change=null] - The expected change amount after selecting the payment method. If provided and `isCheckNeeded` is `true`, a check will be performed to confirm this change amount. |
| 16 |
* @param {string|null} [options.amount=null] - The specific amount associated with the selected payment method. If provided and `isCheckNeeded` is `true`, a check will ensure that the selected amount is correctly displayed. |
| 17 |
* |
| 18 |
* |
| 19 |
* @example |
| 20 |
* // Clicks on the "Cash" payment method without additional checks |
| 21 |
* clickPaymentMethod("Cash"); |
| 22 |
* |
| 23 |
* // Clicks on the "Bank" payment method and checks the remaining amount and change |
| 24 |
* clickPaymentMethod("Cash", true, { remaining: "50.20", change: "10.50" }); |
| 25 |
* |
| 26 |
* // Clicks on the "Cash" payment method and checks the amount to be paid |
| 27 |
* clickPaymentMethod("Cash", true, { amount: "10.20" }); |
| 28 |
*/ |
| 29 |
export function clickPaymentMethod(name, isCheckNeeded = false, options = {}) { |
| 30 |
const { remaining = null, change = null, amount = null } = options; |
| 31 |
|
| 32 |
const step = [ |
| 33 |
{ |
| 34 |
content: `click '${name}' payment method`, |
| 35 |
trigger: `.paymentmethods .button.paymentmethod .payment-name:contains("${name}")`, |
| 36 |
run: "click", |
| 37 |
}, |
| 38 |
]; |
| 39 |
|
| 40 |
if (isCheckNeeded) { |
| 41 |
if (remaining) { |
| 42 |
step.push(...remainingIs(remaining)); |
| 43 |
} |
| 44 |
if (change) { |
| 45 |
step.push(...changeIs(change)); |
| 46 |
} |
| 47 |
if (amount) { |
| 48 |
step.push(...selectedPaymentlineHas(name, amount)); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return step; |
| 53 |
} |
| 54 |
/** |
| 55 |
* Delete the paymentline having the given payment method name and amount. |
| 56 |
* @param {String} name payment method |
| 57 |
* @param {String} amount |
| 58 |
*/ |
| 59 |
export function clickPaymentlineDelButton(name, amount, mobile = false) { |
| 60 |
return [ |
| 61 |
{ |
| 62 |
content: `delete ${name} paymentline with ${amount} amount`, |
| 63 |
trigger: `.paymentlines .paymentline .payment-infos:contains("${name}"):has(.payment-amount:contains("${amount}")) ~ .delete-button`, |
| 64 |
run: "click", |
| 65 |
}, |
| 66 |
]; |
| 67 |
} |
| 68 |
export function clickCancelButton() { |
| 69 |
return [ |
| 70 |
{ |
| 71 |
content: "Cancel the ongoing payment request currently being processed.", |
| 72 |
trigger: ".paymentlines .paymentline .send_payment_cancel", |
| 73 |
run: "click", |
| 74 |
}, |
| 75 |
]; |
| 76 |
} |
| 77 |
export function clickRetryButton() { |
| 78 |
return [ |
| 79 |
{ |
| 80 |
content: "Retry sending the payment request using the payment terminal.", |
| 81 |
trigger: ".paymentlines .paymentline .send_payment_request:contains('Retry')", |
| 82 |
run: "click", |
| 83 |
}, |
| 84 |
]; |
| 85 |
} |
| 86 |
export function clickRefundButton() { |
| 87 |
return [ |
| 88 |
{ |
| 89 |
content: "Initiate a refund request for the selected order.", |
| 90 |
trigger: ".paymentlines .send_refund_request:contains('Refund')", |
| 91 |
run: "click", |
| 92 |
}, |
| 93 |
]; |
| 94 |
} |
| 95 |
/** |
| 96 |
* Click the paymentline having the given payment method name and amount. |
| 97 |
* @param {String} name payment method |
| 98 |
* @param {String} amount |
| 99 |
*/ |
| 100 |
export function clickPaymentline(name, amount) { |
| 101 |
return [ |
| 102 |
{ |
| 103 |
content: `click ${name} paymentline with ${amount} amount`, |
| 104 |
trigger: `.paymentlines .paymentline .payment-infos:contains("${name}"):has(.payment-amount:contains("${amount}"))`, |
| 105 |
run: "click", |
| 106 |
}, |
| 107 |
]; |
| 108 |
} |
| 109 |
export function clickInvoiceButton() { |
| 110 |
return [ |
| 111 |
{ |
| 112 |
content: "click invoice button", |
| 113 |
trigger: ".payment-buttons .js_invoice", |
| 114 |
run: "click", |
| 115 |
}, |
| 116 |
]; |
| 117 |
} |
| 118 |
export function clickValidate() { |
| 119 |
return [ |
| 120 |
{ |
| 121 |
content: "validate payment", |
| 122 |
trigger: `.payment-screen button.validation-button.next`, |
| 123 |
run: "click", |
| 124 |
}, |
| 125 |
]; |
| 126 |
} |
| 127 |
/** |
| 128 |
* Press the numpad in sequence based on the given space-separated keys. |
| 129 |
* Note: Maximum of 2 characters because NumberBuffer only allows 2 consecutive |
| 130 |
* fast inputs. Fast inputs is the case in tours. This method is only for the |
| 131 |
* desktop environment. The mobile environment doesn't work exactly the same way |
| 132 |
* so we have to call fillPaymentLineAmountMobile to have the same behaviour. |
| 133 |
* |
| 134 |
* e.g. : |
| 135 |
* PaymentScreen.enterPaymentLineAmount("Cash", "70"), |
| 136 |
* PaymentScreen.remainingIs("2.0"), |
| 137 |
* PaymentScreen.clickNumpad("0"), <- desktop: add a 0 |
| 138 |
* PaymentScreen.fillPaymentLineAmountMobile("Cash", "700"), <- mobile: rewrite the amount |
| 139 |
* PaymentScreen.remainingIs("0.00"), |
| 140 |
* PaymentScreen.changeIs("628.0"), |
| 141 |
* |
| 142 |
* @param {String} keys space-separated numpad keys |
| 143 |
*/ |
| 144 |
export function clickNumpad(keys) { |
| 145 |
return keys.split(" ").map((key) => ({ ...Numpad.click(key), isActive: ["desktop"] })); |
| 146 |
} |
| 147 |
export function clickBack() { |
| 148 |
return [ |
| 149 |
{ |
| 150 |
content: "click back button", |
| 151 |
trigger: ".back-button", |
| 152 |
run: "click", |
| 153 |
}, |
| 154 |
]; |
| 155 |
} |
| 156 |
export function clickBackToProductScreen() { |
| 157 |
return [ |
| 158 |
{ |
| 159 |
content: "click back to product screen", |
| 160 |
trigger: ".payment-screen .back-button", |
| 161 |
run: "click", |
| 162 |
}, |
| 163 |
]; |
| 164 |
} |
| 165 |
export function clickTipButton() { |
| 166 |
return [ |
| 167 |
{ |
| 168 |
trigger: ".payment-screen .button:contains('Tip')", |
| 169 |
run: "click", |
| 170 |
}, |
| 171 |
]; |
| 172 |
} |
| 173 |
/** |
| 174 |
* Enter an amount for a specified payment line and then perform checks if necessary. |
| 175 |
* |
| 176 |
* This function performs the entry of an amount on a payment line in the user interface. It can also check for expected conditions such as the remaining amount, change, or the selected amount after the entry. |
| 177 |
* |
| 178 |
* @param {string} lineName - The name of the payment line where the amount needs to be entered. This name helps to identify the target payment line in the user interface. |
| 179 |
* @param {string} keys - The sequence of keys to simulate for the amount entry, in the form of a string where each character represents a key to press. |
| 180 |
* @param {boolean} [isCheckNeeded=false] - Indicates whether additional checks need to be performed after the amount entry. |
| 181 |
* @param {Object} [options={}] - An object containing additional options for checks. The options include: |
| 182 |
* @param {string|null} [options.remaining=null] - The expected remaining amount after the amount is entered on the payment line. If provided and `isCheckNeeded` is `true`, a check will be performed to ensure this remaining amount is correct. |
| 183 |
* @param {string|null} [options.change=null] - The expected change amount after the amount is entered on the payment line. If provided and `isCheckNeeded` is `true`, a check will be performed to confirm this change amount. |
| 184 |
* @param {string|null} [options.amount=null] - The specific amount expected on the payment for this line after the entry. If provided and `isCheckNeeded` is `true`, a check will ensure that the selected amount is correctly displayed. |
| 185 |
* |
| 186 |
* @example |
| 187 |
* // Enter the amount "50" on the "Cash" payment line without additional checks |
| 188 |
* enterPaymentLineAmount("Cash", "50"); |
| 189 |
* |
| 190 |
* @example |
| 191 |
* // Enter the amount "100" on the "Bank" payment line and check that the remaining amount is 50 and the change is 20 |
| 192 |
* enterPaymentLineAmount("Bank", "100", true, { remaining: "50.0", change: "20.0" }); |
| 193 |
*/ |
| 194 |
export function enterPaymentLineAmount(lineName, keys, isCheckNeeded = false, options = {}) { |
| 195 |
const { remaining = null, change = null, amount = null } = options; |
| 196 |
const step = [ |
| 197 |
...clickNumpad(keys.split("").join(" ")), |
| 198 |
...fillPaymentLineAmountMobile(lineName, keys), |
| 199 |
]; |
| 200 |
|
| 201 |
if (isCheckNeeded) { |
| 202 |
if (remaining) { |
| 203 |
step.push(...remainingIs(remaining)); |
| 204 |
} |
| 205 |
if (change) { |
| 206 |
step.push(...changeIs(change)); |
| 207 |
} |
| 208 |
if (amount) { |
| 209 |
step.push(...selectedPaymentlineHas(lineName, amount)); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
return step; |
| 214 |
} |
| 215 |
export function fillPaymentLineAmountMobile(lineName, keys) { |
| 216 |
return [ |
| 217 |
{ |
| 218 |
isActive: ["mobile"], |
| 219 |
content: "click payment line", |
| 220 |
trigger: `.paymentlines .paymentline .payment-infos:contains("${lineName}")`, |
| 221 |
run: "click", |
| 222 |
}, |
| 223 |
...NumberPopup.enterValue(keys).map((step) => ({ |
| 224 |
...step, |
| 225 |
isActive: ["mobile"], |
| 226 |
run: "click", |
| 227 |
})), |
| 228 |
{ |
| 229 |
...Dialog.confirm(), |
| 230 |
isActive: ["mobile"], |
| 231 |
run: "click", |
| 232 |
}, |
| 233 |
]; |
| 234 |
} |
| 235 |
|
| 236 |
export function isShown() { |
| 237 |
return [ |
| 238 |
{ |
| 239 |
content: "payment screen is shown", |
| 240 |
trigger: ".pos .payment-screen", |
| 241 |
}, |
| 242 |
]; |
| 243 |
} |
| 244 |
/** |
| 245 |
* Check if change is the provided amount. |
| 246 |
* @param {String} amount |
| 247 |
*/ |
| 248 |
export function changeIs(amount) { |
| 249 |
return [ |
| 250 |
{ |
| 251 |
content: `change is ${amount}`, |
| 252 |
trigger: `.payment-status-amount .amount:contains("${amount}")`, |
| 253 |
}, |
| 254 |
]; |
| 255 |
} |
| 256 |
export function isInvoiceOptionSelected() { |
| 257 |
return [ |
| 258 |
{ |
| 259 |
content: "Invoice option is selected", |
| 260 |
trigger: ".payment-buttons .js_invoice.highlight", |
| 261 |
}, |
| 262 |
]; |
| 263 |
} |
| 264 |
/** |
| 265 |
* Check if the remaining is the provided amount. |
| 266 |
* @param {String} amount |
| 267 |
*/ |
| 268 |
export function remainingIs(amount) { |
| 269 |
return [ |
| 270 |
{ |
| 271 |
content: `remaining amount is ${amount}`, |
| 272 |
trigger: `.payment-status-amount .amount:contains("${amount}")`, |
| 273 |
}, |
| 274 |
]; |
| 275 |
} |
| 276 |
/** |
| 277 |
* Check if validate button is highlighted. |
| 278 |
* @param {Boolean} isHighlighted |
| 279 |
*/ |
| 280 |
export function validateButtonIsHighlighted(isHighlighted = true) { |
| 281 |
return [ |
| 282 |
{ |
| 283 |
isActive: ["desktop"], |
| 284 |
content: `validate button is ${isHighlighted ? "highlighted" : "not highlighted"}`, |
| 285 |
trigger: isHighlighted |
| 286 |
? `.payment-screen button.validation-button.next.highlight` |
| 287 |
: `.payment-screen button.validation-button.next:not(:has(.highlight))`, |
| 288 |
}, |
| 289 |
]; |
| 290 |
} |
| 291 |
/** |
| 292 |
* Check if the paymentlines are empty. Also provide the amount to pay. |
| 293 |
* @param {String} amountToPay |
| 294 |
*/ |
| 295 |
export function emptyPaymentlines(amountToPay) { |
| 296 |
return [ |
| 297 |
{ |
| 298 |
content: `there are no paymentlines`, |
| 299 |
trigger: `.paymentlines-empty`, |
| 300 |
}, |
| 301 |
{ |
| 302 |
content: `amount to pay is '${amountToPay}'`, |
| 303 |
trigger: `.paymentlines-empty .total:contains("${amountToPay}")`, |
| 304 |
}, |
| 305 |
]; |
| 306 |
} |
| 307 |
/** |
| 308 |
* Check if the selected paymentline has the given payment method and amount. |
| 309 |
* @param {String} paymentMethodName |
| 310 |
* @param {String} amount |
| 311 |
*/ |
| 312 |
export function selectedPaymentlineHas(paymentMethodName, amount) { |
| 313 |
return [ |
| 314 |
{ |
| 315 |
content: `line paid via '${paymentMethodName}' is selected`, |
| 316 |
trigger: `.paymentlines .paymentline.selected .payment-name:contains("${paymentMethodName}")`, |
| 317 |
}, |
| 318 |
{ |
| 319 |
content: `amount tendered in the line is '${amount}'`, |
| 320 |
trigger: `.paymentlines .paymentline.selected .payment-amount:contains("${amount}")`, |
| 321 |
}, |
| 322 |
]; |
| 323 |
} |
| 324 |
export function totalIs(amount) { |
| 325 |
return [ |
| 326 |
{ |
| 327 |
content: `total is ${amount}`, |
| 328 |
trigger: `.total:contains("${amount}")`, |
| 329 |
}, |
| 330 |
]; |
| 331 |
} |
| 332 |
export function pay(method, amount) { |
| 333 |
const steps = []; |
| 334 |
steps.push(...clickPaymentMethod(method)); |
| 335 |
for (const char of amount.split("")) { |
| 336 |
steps.push(...clickNumpad(char)); |
| 337 |
} |
| 338 |
steps.push(...validateButtonIsHighlighted()); |
| 339 |
steps.push(...clickValidate()); |
| 340 |
return steps; |
| 341 |
} |
| 342 |
|
| 343 |
export function isInvoiceButtonChecked() { |
| 344 |
return [ |
| 345 |
{ |
| 346 |
content: "check invoice button is checked", |
| 347 |
trigger: ".js_invoice.highlight", |
| 348 |
}, |
| 349 |
]; |
| 350 |
} |
| 351 |
|
| 352 |
export function clickShipLaterButton() { |
| 353 |
return [ |
| 354 |
{ |
| 355 |
content: "click ship later button", |
| 356 |
trigger: ".button:contains('Ship Later')", |
| 357 |
run: "click", |
| 358 |
}, |
| 359 |
{ |
| 360 |
content: "click confirm button", |
| 361 |
trigger: ".btn:contains('Confirm')", |
| 362 |
run: "click", |
| 363 |
}, |
| 364 |
]; |
| 365 |
} |
| 366 |
|
| 367 |
export function clickPartnerButton() { |
| 368 |
return [ |
| 369 |
{ |
| 370 |
content: "click customer button", |
| 371 |
trigger: "button.partner-button", |
| 372 |
run: "click", |
| 373 |
}, |
| 374 |
{ |
| 375 |
content: "partner screen is shown", |
| 376 |
trigger: `${PartnerList.clickPartner().trigger}`, |
| 377 |
}, |
| 378 |
]; |
| 379 |
} |
| 380 |
|
| 381 |
export function clickCustomer(name, pressEnter = false) { |
| 382 |
return [...PartnerList.searchCustomerValue(name, pressEnter), PartnerList.clickPartner(name)]; |
| 383 |
} |
| 384 |
|
| 385 |
export function shippingLaterHighlighted() { |
| 386 |
return { |
| 387 |
content: "Shipping later button is highlighted", |
| 388 |
trigger: ".button:contains('Ship Later').highlight", |
| 389 |
}; |
| 390 |
} |
| 391 |
|
| 392 |
// This method is used to simulate payment with a payment terminal, before using terminal the order |
| 393 |
// is synced to ensure that the order is up-to-date and ready for payment. |
| 394 |
export function syncCurrentOrder() { |
| 395 |
return [ |
| 396 |
{ |
| 397 |
content: "sync current order", |
| 398 |
trigger: "body", |
| 399 |
run: async () => { |
| 400 |
const currentOrder = posmodel.getOrder(); |
| 401 |
const order = await posmodel.syncAllOrders({ orders: [currentOrder] }); |
| 402 |
|
| 403 |
if (!order[0].isSynced) { |
| 404 |
throw new Error("Order ID is not a number after sync."); |
| 405 |
} |
| 406 |
}, |
| 407 |
}, |
| 408 |
]; |
| 409 |
} |
| 410 |
|
| 411 |
export function isInvoiceButtonUnchecked() { |
| 412 |
return [ |
| 413 |
{ |
| 414 |
content: "check invoice button is not highlighted", |
| 415 |
trigger: ".js_invoice:not(.highlight)", |
| 416 |
}, |
| 417 |
]; |
| 418 |
} |