data-transfer.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { Locator, Page, expect } from "@playwright/test";
  2. import path from "path";
  3. import { fileURLToPath } from "url";
  4. // Create ESM-safe __dirname
  5. const __filename = fileURLToPath(import.meta.url);
  6. const __dirname = path.dirname(__filename);
  7. export class AdminDataTransfer {
  8. readonly page: Page;
  9. constructor(page: Page) {
  10. this.page = page;
  11. }
  12. async AdminDataTransferSectionGoto() {
  13. await this.page.goto("admin/settings/data-transfer/imports");
  14. }
  15. async DataTransfer(
  16. type: string,
  17. action: string,
  18. validation_strategy: string,
  19. allowed_errors: string,
  20. field_separator: string,
  21. file_name: string
  22. // image_file_name: string,
  23. ) {
  24. await this.AdminDataTransferSectionGoto();
  25. await this.page.waitForTimeout(2000);
  26. await this.page.click("a.primary-button");
  27. await this.page.waitForTimeout(2000);
  28. await this.page.selectOption('select[name="type"]', type);
  29. /*
  30. * Here you can change the Product file path as per your requirement
  31. */
  32. const filePath = path.resolve(
  33. __dirname,
  34. `../data/data-transfer/${file_name}`
  35. );
  36. const [fileChooser] = await Promise.all([
  37. this.page.waitForEvent("filechooser"),
  38. this.page.click('input[name="file"]'),
  39. ]);
  40. // Upload CSV
  41. await fileChooser.setFiles(filePath);
  42. console.log("File selected:", filePath);
  43. /*
  44. // * Here you can change the Product image zip path as per your requirement
  45. // */
  46. // const imageFilePath = path.resolve(__dirname, `../data/data-transfer/${image_file_name}`);
  47. // const [imageFileChooser] = await Promise.all([
  48. // this.page.waitForEvent("filechooser"),
  49. // this.page.click('input[name="upload_images"]'),
  50. // ]);
  51. // // Upload CSV
  52. // await imageFileChooser.setFiles(imageFilePath);
  53. // console.log("Image File selected:", imageFilePath);
  54. await this.page.waitForTimeout(2000);
  55. await this.page.selectOption('select[name="action"]', action);
  56. await this.page.selectOption(
  57. 'select[name="validation_strategy"]',
  58. validation_strategy
  59. );
  60. await this.page.fill('input[name="allowed_errors"]', allowed_errors);
  61. await this.page.fill('input[name="field_separator"]', field_separator);
  62. await this.page.click('label[for="process_in_queue"]');
  63. await this.page.click('//button[contains(.," Save Import ")]');
  64. await this.page.waitForTimeout(2000);
  65. await expect(
  66. this.page.locator("#app").getByText("Import created successfully.")
  67. ).toBeVisible;
  68. await this.page.click('//button[contains(.," Validate ")]');
  69. await this.page.waitForTimeout(2000);
  70. await this.page.click('//button[contains(.," Import ")]');
  71. await this.page.waitForSelector(
  72. '//p[contains(.," Congratulations! Your import was successful. ")]',
  73. { state: "visible" }
  74. );
  75. await expect(
  76. this.page.locator(
  77. '//p[contains(.," Congratulations! Your import was successful. ")]'
  78. )
  79. ).toBeVisible();
  80. }
  81. }