コード例 #1
0
ファイル: test_keyboard.py プロジェクト: More2Chi/ahk
class TestKeyboard(TestCase):
    def setUp(self):
        """
        Record all open windows
        :return:
        """
        self.ahk = AHK()
        self.before_windows = self.ahk.windows()
        self.p = subprocess.Popen("notepad")
        time.sleep(1)
        self.notepad = self.ahk.find_window(title=b"Untitled - Notepad")

    def tearDown(self):
        self.p.terminate()
        time.sleep(0.2)

    def test_window_send(self):
        self.notepad.send("hello world")
        time.sleep(1)
        self.assertIn(b"hello world", self.notepad.text)

    def test_send(self):
        self.notepad.activate()
        self.ahk.send("hello world")
        assert b"hello world" in self.notepad.text

    def test_send_key_mult(self):
        self.notepad.send(KEYS.TAB * 4)
        time.sleep(0.5)
        self.assertEqual(self.notepad.text.count(b"\t"), 4, self.notepad.text)

    def test_send_input(self):
        self.notepad.activate()
        self.ahk.send_input("Hello World")
        assert b"Hello World" in self.notepad.text

    def test_type(self):
        self.notepad.activate()
        self.ahk.type("Hello, World!")
        assert b"Hello, World!" in self.notepad.text

    def test_type_escapes_equals(self):
        """
        https://github.com/spyoungtech/ahk/issues/96
        """
        self.notepad.activate()
        self.ahk.type("=foo")
        assert b"=foo" in self.notepad.text

    def test_sendraw_equals(self):
        """
        https://github.com/spyoungtech/ahk/issues/96
        """
        self.notepad.activate()
        self.ahk.send_raw("=foo")
        assert b"=foo" in self.notepad.text

    def test_set_capslock_state(self):
        self.ahk.set_capslock_state("on")
        assert self.ahk.key_state("CapsLock", "T")
コード例 #2
0
ファイル: test_keyboard.py プロジェクト: wanqiuchansheng/ahk
class TestKeyboard(TestCase):
    def setUp(self):
        """
        Record all open windows
        :return:
        """
        self.ahk = AHK()
        self.before_windows = self.ahk.windows()
        self.p = subprocess.Popen('notepad')
        time.sleep(1)
        self.notepad = self.ahk.find_window(title=b'Untitled - Notepad')

    def tearDown(self):
        self.p.terminate()
        time.sleep(0.2)

    def test_window_send(self):
        self.notepad.send('hello world')
        time.sleep(1)
        self.assertIn(b'hello world', self.notepad.text)

    def test_send(self):
        self.notepad.activate()
        self.ahk.send('hello world')
        assert b'hello world' in self.notepad.text

    def test_send_key_mult(self):
        self.notepad.send(KEYS.TAB * 4)
        time.sleep(0.5)
        self.assertEqual(self.notepad.text.count(b'\t'), 4, self.notepad.text)

    def test_send_input(self):
        self.notepad.activate()
        self.ahk.send_input('Hello World')
        assert b'Hello World' in self.notepad.text

    def test_type(self):
        self.notepad.activate()
        self.ahk.type('Hello, World!')
        assert b'Hello, World!' in self.notepad.text

    def test_type_escapes_equals(self):
        '''
        https://github.com/spyoungtech/ahk/issues/96
        '''
        self.notepad.activate()
        self.ahk.type('=foo')
        assert b'=foo' in self.notepad.text

    def test_sendraw_equals(self):
        '''
        https://github.com/spyoungtech/ahk/issues/96
        '''
        self.notepad.activate()
        self.ahk.send_raw('=foo')
        assert b'=foo' in self.notepad.text
コード例 #3
0
 post = soup.find("h1", {"class": "entry-title"})
 if post.text != "This is somewhat embarrassing, isn’t it?":
     post_text = post.text.split(' ')[0]
     chapter = post.text.split(':')[1]
     chapter = chapter.strip()
     print(chapter)
     if post_text == "Protected:":
         print("Chapter is posted")
         x = False
         y = True
         link_url = post.find('a')['href']
         clipboard.copy(link_url + " Chapter created")
         ahk.run_script("WinActivate, ahk_exe discord.exe", blocking=False)
         time.sleep(0.2)
         win = ahk.active_window
         ahk.send_input("^t")
         time.sleep(0.2)
         ahk.send_input("patreon-spoilers")
         time.sleep(0.2)
         ahk.send_input("{enter}")
         time.sleep(0.2)
         ahk.send_input("{Esc}{Esc}")
         time.sleep(0.2)
         if win.title == b"#patreon-spoilers - Discord":
             ahk.send_input("^a^v")
             time.sleep(0.2)
             ahk.send_input("{enter}")
         else:
             print("Error, channel not found/Discord not open")
             exit()
         while y:
コード例 #4
0
ファイル: test_keyboard.py プロジェクト: spyoungtech/ahk
class TestKeyboard(TestCase):
    def setUp(self):
        """
        Record all open windows
        :return:
        """
        self.ahk = AHK()
        self.before_windows = self.ahk.windows()
        self.p = subprocess.Popen('notepad')
        time.sleep(1)
        self.notepad = self.ahk.find_window(title=b'Untitled - Notepad')

    def tearDown(self):
        self.p.terminate()
        time.sleep(0.2)

    def test_window_send(self):
        self.notepad.send('hello world')
        time.sleep(1)
        self.assertIn(b'hello world', self.notepad.text)

    @pytest.mark.flaky(reruns=5)
    def test_window_send_raw(self):
        self.notepad.send('{Tab 4}', raw=True, delay=10, press_duration=10)
        time.sleep(0.5)
        assert b'{Tab 4}' in self.notepad.text

    def test_send(self):
        self.notepad.activate()
        self.ahk.send('hello world')
        assert b'hello world' in self.notepad.text

    def test_send_key_mult(self):
        self.notepad.send(KEYS.TAB * 4)
        time.sleep(0.5)
        self.assertEqual(self.notepad.text.count(b'\t'), 4, self.notepad.text)

    def test_send_input(self):
        self.notepad.activate()
        self.ahk.send_input('Hello World')
        time.sleep(0.5)
        assert b'Hello World' in self.notepad.text

    def test_type(self):
        self.notepad.activate()
        self.ahk.type('Hello, World!')
        assert b'Hello, World!' in self.notepad.text

    def test_type_escapes_equals(self):
        """
        https://github.com/spyoungtech/ahk/issues/96
        """
        self.notepad.activate()
        self.ahk.type('=foo')
        assert b'=foo' in self.notepad.text

    def test_sendraw_equals(self):
        """
        https://github.com/spyoungtech/ahk/issues/96
        """
        self.notepad.activate()
        self.ahk.send_raw('=foo')
        assert b'=foo' in self.notepad.text

    def test_set_capslock_state(self):
        self.ahk.set_capslock_state('on')
        assert self.ahk.key_state('CapsLock', 'T')
 startPage = requests.get(url)
 soup = BeautifulSoup(startPage.content, "lxml")
 post = soup.find("header", {"class": "entry-header"})
 post_text = post.text.partition(' ')[0]
 if post_text == "\nProtected:":
     time_now = datetime.today().strftime('%X')
     print("[" + time_now + "] Chapter is Protected")
     time.sleep(10)
 else:
     print("Chapter is public")
     print(url)
     clipboard.copy(url + " Chapter public")
     ahk.run_script("WinActivate, ahk_exe discord.exe", blocking=False)
     time.sleep(0.2)
     win = ahk.active_window
     ahk.send_input("^t")
     time.sleep(0.2)
     ahk.send_input("public-spoilers")
     time.sleep(0.2)
     ahk.send_input("{enter}")
     time.sleep(0.2)
     ahk.send_input("{Esc}{Esc}")
     time.sleep(0.2)
     if win.title == b"#public-spoilers - Discord":
         ahk.send_input("^a^v")
         time.sleep(0.2)
         ahk.send_input("{enter}")
     else:
         print("Error, channel not found/Discord not open")
         exit()
     x = False
コード例 #6
0
class InputAutomator(webdriver.Ie):

    options = webdriver.IeOptions()
    user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'
    options.add_argument(f'user-agent={user_agent}')
    options.add_argument("IgnoreZoomLevel=true")

    def __init__(self, *args, **kwargs):
        kwargs['executable_path'] = 'IEDriverServer.exe'
        kwargs['ie_options'] = self.options
        super().__init__(*args, **kwargs)
        self.maximize_window()
        self.ahk = AHK(executable_path="AutoHotkeyU64.exe")

    def adjust_coordinates(self, x, y):
        """
        Adjusts document coordinates given by selenium to screen coordinates used by AHK.
        :param x: x coordinate (pixels) in document
        :param y: y coordinate (pixels) in document
        :return: (x_adj, y_adj)
        """
        window_pos = self.get_window_position()
        browser_navigation_panel_height = self.execute_script(
            'return window.outerHeight - window.innerHeight;')
        return x + window_pos['x'], y + window_pos[
            'y'] + browser_navigation_panel_height

    def move_rand(self, elem_type, elem_text, x_offset=0, y_offset=0):
        """
        Moves mouse to a given element using a random walk path. Does not seem to make a difference.
        :param elem_type: one of "class", "css" or "id"
        :param elem_text: value of "class", "css" or "id"
        :param x_offset: x offset from element coordinates for final mouse position
        :param y_offset: y offset from element coordinates for final mouse position
        """
        try:
            if elem_type == "class":
                out = self.find_element_by_class_name(elem_text).location
            elif elem_type == "css":
                out = self.find_element_by_css_selector(elem_text).location
            elif elem_type == "id":
                out = self.find_element_by_class_name(elem_text).location
            else:
                raise ValueError("Unknown elem_type: must be class, css or id")
        except (NoSuchElementException, TimeoutException, JavascriptException):
            return False
        x_final, y_final = self.adjust_coordinates(out['x'] + x_offset,
                                                   out['y'] + y_offset)
        x0, y0 = self.ahk.mouse_position
        x_path, y_path = constrained_walk_2d((x0, y0), (x_final, y_final))
        # Reduce points
        x_path = x_path[:1] + x_path[::max([len(x_path) //
                                            50, 1])] + x_path[-1:]
        y_path = y_path[:1] + y_path[::max([len(y_path) //
                                            50, 1])] + y_path[-1:]
        for x, y in zip(x_path, y_path):
            self.ahk.run_script(f"SetMouseDelay, -1\nMouseMove, {x}, {y} , 0")
            # self.ahk.mouse_move(x=x, y=y, blocking=True, speed=0)

    def move_to(self, elem_type, elem_text, x_offset=0, y_offset=0):
        self.maximize_window()
        """
        Moves mouse to a given element directly. Passes reCAPTCHA.
        :param elem_type: one of "class", "css" or "id"
        :param elem_text: value of "class", "css" or "id"
        :param x_offset: x offset from element coordinates for final mouse position
        :param y_offset: y offset from element coordinates for final mouse position
        """
        try:
            if elem_type == "class":
                out = self.find_element_by_class_name(elem_text).location
            elif elem_type == "css":
                out = self.find_element_by_css_selector(elem_text).location
            elif elem_type == "id":
                out = self.find_element_by_class_name(elem_text).location
            else:
                raise ValueError("Unknown elem_type: must be class, css or id")
            self.ahk.mouse_move(*self.adjust_coordinates(
                out['x'] + x_offset, out['y'] + y_offset),
                                speed=10,
                                blocking=True)
            return True
        except (NoSuchElementException, TimeoutException, JavascriptException):
            return False

    def click(self):
        self.ahk.click()

    def type(self, text):
        self.ahk.send_input(text)

    def scroll(self, direction, times):
        [self.ahk.mouse_wheel(direction) for i in range(times)]

    def wait_for(self, elem_type, elem_text, timeout=15):
        result = False
        start = time()
        while not result and time() - start < timeout:
            try:
                if elem_type == "class":
                    result = self.find_element_by_class_name(
                        elem_text).is_displayed()
                elif elem_type == "css":
                    result = self.find_element_by_css_selector(
                        elem_text).is_displayed()
                elif elem_type == "id":
                    result = self.find_element_by_class_name(
                        elem_text).is_displayed()
                return result
            except (NoSuchElementException, TimeoutException,
                    JavascriptException):
                result = False
            sleep(0.1)
        if not result:
            print(f"Warn: Element {elem_text} not found after {timeout} sec")
            return result