Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import TriggerOptions = Cypress.TriggerOptions;
import ObjectLike = Cypress.ObjectLike;
import ClickOptions = Cypress.ClickOptions;
 
type MouseInteractionOptions = Partial<TriggerOptions & ObjectLike & MouseEvent>;
 
export const ImageView = {
  get image() {
    cy.log("Get main image");
    return cy.get("img[alt=LS]");
  },
  get root() {
    return this.image.closest(".lsf-object");
  },
  get drawingFrame() {
    return this.image.closest('[class^="frame--"]');
  },
  get drawingArea() {
    cy.log("Get Konva.js root");
    return this.drawingFrame.siblings().get('[class^="image-element--"] .konvajs-content');
  },
  get toolBar() {
    cy.log("Get tool bar");
    return this.root.find(".lsf-toolbar");
  },
  get pagination() {
    return this.root.get('[class^="pagination--"]');
  },
  get paginationPrevBtn() {
    return this.pagination.get(".lsf-pagination__btn_arrow-left:not(.lsf-pagination__btn_arrow-left-double)");
  },
  get paginationNextBtn() {
    return this.pagination.get(".lsf-pagination__btn_arrow-right:not(.lsf-pagination__btn_arrow-right-double)");
  },
  waitForImage() {
    cy.log("Make sure that the image is visible and loaded");
    this.image.should("be.visible").and((img) => {
      return expect((img[0] as HTMLImageElement).naturalWidth).to.be.greaterThan(0);
    });
 
    this.drawingArea.get("canvas").should("be.visible");
  },
  /**
   * Clicks at the coordinates on the drawing area
   * @param {number} x
   * @param {number} y
   */
  clickAt(x: number, y: number, options?: Partial<ClickOptions>) {
    cy.log(`Click at the image view at (${x}, ${y})`);
    this.drawingArea.scrollIntoView().click(x, y, options);
  },
  /**
   * Clicks at the relative coordinates on the drawing area
   * @param {number} x
   * @param {number} y
   */
  clickAtRelative(x: number, y: number, options?: Partial<ClickOptions>) {
    this.drawingFrame.then((el) => {
      const bbox: DOMRect = el[0].getBoundingClientRect();
      const realX = x * bbox.width;
      const realY = y * bbox.height;
 
      this.clickAt(realX, realY, options);
    });
  },
  clickAtStageRelative(x: number, y: number, options?: Partial<ClickOptions>) {
    this.drawingArea.then((el) => {
      const bbox: DOMRect = el[0].getBoundingClientRect();
      const realX = x * bbox.width;
      const realY = y * bbox.height;
 
      this.clickAt(realX, realY, options);
    });
  },
  dblClickAt(x, y) {
    this.drawingArea.scrollIntoView().dblclick(x, y);
  },
  dblClickAtRelative(x, y) {
    this.drawingFrame.then((el) => {
      const bbox: DOMRect = el[0].getBoundingClientRect();
      const realX = x * bbox.width;
      const realY = y * bbox.height;
 
      this.dblClickAt(realX, realY);
    });
  },
  /**
   * Draws a rectangle on the drawing area.
   * It also could be used for some drag and drop interactions for example selecting area or moving existing regions.
   * @param {number} x
   * @param {number} y
   * @param {number} width
   * @param {number} height
   */
  drawRect(x: number, y: number, width: number, height: number, options: MouseInteractionOptions = {}) {
    cy.log(`Draw rectangle at (${x}, ${y}) of size ${width}x${height}`);
    this.drawingArea
      .scrollIntoView()
      .trigger("mousedown", x, y, { eventConstructor: "MouseEvent", buttons: 1, ...options })
      .trigger("mousemove", x + width, y + height, { eventConstructor: "MouseEvent", buttons: 1, ...options })
      .trigger("mouseup", x + width, y + height, { eventConstructor: "MouseEvent", buttons: 1, ...options });
  },
  /**
   * Draws the rectangle on the drawing area with coordinates and size relative to the drawing area.
   * It also could be used for some drag and drop interactions for example selecting area or moving existing regions.
   * @param {number} x
   * @param {number} y
   * @param {number} width
   * @param {number} height
   */
  drawRectRelative(x: number, y: number, width: number, height: number, options: MouseInteractionOptions = {}) {
    this.drawingFrame.then((el) => {
      const bbox: DOMRect = el[0].getBoundingClientRect();
      const realX = x * bbox.width;
      const realY = y * bbox.height;
      const realWidth = width * bbox.width;
      const realHeight = height * bbox.height;
 
      this.drawRect(realX, realY, realWidth, realHeight, options);
    });
  },
 
  /**
   * Draws a polygon on the drawing area.
   * @param {Array<[number, number]>} points
   * @param {boolean} autoclose
   * @param {MouseInteractionOptions} options
   */
  drawPolygon(points: [number, number][], autoclose, options: MouseInteractionOptions = {}) {
    const drawingArea = this.drawingArea.scrollIntoView();
    if (autoclose) {
      points = [...points, points[0]];
    }
    points.forEach((point, _index) => {
      drawingArea
        .trigger("mousemove", point[0], point[1], { eventConstructor: "MouseEvent", ...options })
        .trigger("mousedown", point[0], point[1], { eventConstructor: "MouseEvent", buttons: 1, ...options })
        .trigger("mouseup", point[0], point[1], { eventConstructor: "MouseEvent", buttons: 1, ...options });
    });
  },
 
  /**
   * Draws the polygon on the drawing area with coordinates relative to the drawing area.
   * @param {Array<[number, number]>} points
   * @param {boolean} autoclose
   * @param {MouseInteractionOptions} options
   */
  drawPolygonRelative(points: [number, number][], autoclose, options: MouseInteractionOptions = {}) {
    this.drawingFrame.then((el) => {
      const bbox: DOMRect = el[0].getBoundingClientRect();
      const realPoints = points.map(([x, y]) => [x * bbox.width, y * bbox.height]);
 
      this.drawPolygon(realPoints, autoclose, options);
    });
  },
  /**
   * Captures a screenshot of an element to compare later
   * @param {string} name name of the screenshot
   */
  capture(name: string) {
    return this.drawingArea.captureScreenshot(name);
  },
 
  /**
   * Captures a new screenshot and compares it to already taken one
   * Fails if screenshots are identical
   * @param name name of the screenshot
   * @param threshold to compare image. It's a relation between original number of pixels vs changed number of pixels
   */
  canvasShouldChange(name: string, threshold = 0.1) {
    return this.drawingArea.compareScreenshot(name, "shouldChange", { threshold });
  },
 
  /**
   * Captures a new screenshot and compares it to already taken one
   * Fails if screenshots are different
   * @param name name of the screenshot
   * @param threshold to compare image. It's a relation between original number of pixels vs changed number of pixels
   */
  canvasShouldNotChange(name: string, threshold = 0.1) {
    return this.drawingArea.compareScreenshot(name, "shouldNotChange", { threshold });
  },
  selectRect3PointToolByHotkey() {
    cy.get("body").type("{shift}{R}");
  },
  zoomInWithHotkey() {
    cy.get("body").type("{ctrl}{+}");
  },
  zoomOutWithHotkey() {
    cy.get("body").type("{ctrl}{-}");
  },
 
  selectRectangleToolByButton() {
    this.toolBar
      .find('[aria-label="rectangle-tool"]')
      .should("be.visible")
      .click()
      .should("have.class", "lsf-tool_active");
  },
 
  selectEllipseToolByButton() {
    this.toolBar
      .find('[aria-label="ellipse-tool"]')
      .should("be.visible")
      .click()
      .should("have.class", "lsf-tool_active");
  },
 
  selectPolygonToolByButton() {
    this.toolBar.find('[aria-label="polygon-tool"]').should("be.visible").click().should("have.class", "lsf-tool");
  },
 
  selectKeypointToolByButton() {
    this.toolBar
      .find('[aria-label="key-point-tool"]')
      .should("be.visible")
      .click()
      .should("have.class", "lsf-tool_active");
  },
 
  selectBrushToolByButton() {
    this.toolBar.find('[aria-label="brush-tool"]').should("be.visible").click().should("have.class", "lsf-tool_active");
  },
 
  selectMoveToolByButton() {
    this.toolBar.find('[aria-label="move-tool"]').should("be.visible").click().should("have.class", "lsf-tool_active");
  },
 
  rotateLeft() {
    this.toolBar.find('[aria-label="rotate-left"]').should("be.visible").click();
  },
 
  rotateRight() {
    this.toolBar.find('[aria-label="rotate-right"]').should("be.visible").click();
  },
};