from seleniumbase import BaseCase


class CanvasTests(BaseCase):
    def get_pixel_colors(self):
        # Return the RGB colors of the canvas element's top left pixel
        x = 0
        y = 0
        if self.browser == "safari":
            x = 1
            y = 1
        color = self.execute_script(
            "return document.querySelector('canvas').getContext('2d')"
            ".getImageData(%s,%s,1,1).data;" % (x, y)
        )
        if self.is_chromium():
            return [color[0], color[1], color[2]]
        else:
            return [color["0"], color["1"], color["2"]]

    def test_canvas_actions(self):
        self.open("https://seleniumbase.io/canvas/")
        self.highlight("canvas")
        rgb = self.get_pixel_colors()
        self.assert_equal(rgb, [221, 242, 231])  # Looks greenish
        self.click_with_offset("canvas", 500, 350)
        self.highlight("canvas", loops=5)
        rgb = self.get_pixel_colors()
        self.assert_equal(rgb, [39, 42, 56])  # Blue by hamburger

    def test_canvas_click(self):
        self.open("https://seleniumbase.io/other/canvas")
        self.click_with_offset("canvas", 300, 200)
        self.sleep(1)  # Not needed (Lets you see the alert pop up)
        alert = self.switch_to_alert()
        self.assert_equal(alert.text, "You clicked on the square!")
        self.accept_alert()
        self.sleep(1)  # Not needed (Lets you see the alert go away)

@@ -630,15 +630,25 @@
 vas_
-actions
+click_from_center
 (sel
@@ -699,15 +699,20 @@
 .io/
+other/
 canvas
-/
 %22)%0D%0A
@@ -728,334 +728,331 @@
 elf.
-highlight(%22canvas%22)%0D%0A        rgb = self.get_pixel_colors()%0D%0A        self.assert_equal(rgb, %5B221, 242, 231%5D)  # Looks greenish%0D%0A        self.click_
+click_with_offset(%22canvas%22, 0, 0, center=True)%0D%0A        self.sleep(1)  # Not needed (Lets you see the alert pop up)%0D%0A        alert = self.s
 wit
+c
 h_
-offset(%22canvas%22, 500, 350)%0D%0A        self.highlight(%22canvas%22, loops=5)%0D%0A        rgb = self.get_pixel_colors()%0D%0A        self.assert_equal(rgb, %5B39, 42, 56%5D)  # Blue by hamburger
+to_alert()%0D%0A        self.assert_equal(alert.text, %22You clicked on the square!%22)%0D%0A        self.accept_alert()%0D%0A        self.sleep(1)  # Not needed (Lets you see the alert go away)
 %0D%0A%0D%0A
@@ -1065,27 +1065,32 @@
 f test_c
-anvas_click
+lick_with_offset
 (self):%0D
@@ -1129,36 +1129,31 @@
 niumbase.io/
-other/
 canvas
+/
 %22)%0D%0A        
@@ -1161,320 +1161,332 @@
 elf.
-click_with_offset(%22canvas%22, 300, 200)%0D%0A        self.sleep(1)  # Not needed (Lets you see the alert pop up)%0D%0A        alert = self.s
+highlight(%22canvas%22)%0D%0A        rgb = self.get_pixel_colors()%0D%0A        self.assert_equal(rgb, %5B221, 242, 231%5D)  # Looks greenish%0D%0A        self.click_
 wit
-c
 h_
-to_alert()%0D%0A        self.assert_equal(alert.text, %22You clicked on the square!%22)%0D%0A        self.accept_alert()%0D%0A        self.sleep(1)  # Not needed (Lets you see the alert go away)
+offset(%22canvas%22, 500, 350)%0D%0A        self.highlight(%22canvas%22, loops=5)%0D%0A        rgb = self.get_pixel_colors()%0D%0A        self.assert_equal(rgb, %5B39, 42, 56%5D)  # Blue by hamburger
 %0D%0A

Update example tests