从Windows资源管理器拖放到Windows资源管理器

最后编辑了

概述

可能希望将从Windows资源管理器自动拖放到AUT中,或者其他方式。

不幸的是,由于这些要点,Windows Explorer非常不友好地对自动化:

要克服这些问题,可以将Windows资源管理器嵌入自定义应用程序中,例如 embedded_explorer. ,然后自动化此自定义应用程序。

此类自定义应用程序的重点是它保证它在一个单独的新进程中运行(允许将Windows屏蔽挂钩,并且正确配置右窗格视图,从而简化了识别此窗格中显示的对象。

例子

此测试脚本演示拖放拖放 embedded_explorer. into notepad.exe.

# -*- coding: utf-8 -*-
try:
    import __builtin__
except:
    import builtins as __builtin__
import os
import os.path
import subprocess
import sys


def main():
    # Must use 32-bit notepad, 64-bit variant may
    # not support drag & drop from a 32-bit application
    aut = "notepad"
    path = r"%s\SysWOW64" % os.environ["windir"]
    if not os.path.exists(path):
        path = os.environ["windir"]
    registerAUT(aut, path)
    startApplication('"' + aut + '"')

    # Ensure window activation:
    mouseClick(waitForObject({"type": "Window"}), 50, 20, MouseButton.PrimaryButton)

    drag_drop_file_on_object(squishinfo.testCase, "test.py", {"type": "Edit"})

    o = waitForObject({"type": "Edit"})
    test.verify(waitForObject({"type": "Edit"}).text.startswith("# -*- coding: utf-8 -*-"))

    return

def drag_drop_file_on_object(file_path, file_name, object_or_name):
    app_ctx_backup = currentApplicationContext()

    trg = waitForObject(object_or_name)
    trg_x = trg.x
    trg_y = trg.y

    aut = "embedded_explorer"
    path = squishinfo.testCase + "/.."
    registerAUT(aut, path)
    startApplication('"%s" "%s"' % (aut, file_path))
    waitForObject({"type": "ListView"})

    src = waitForObject({"text": file_name, "type": "ListViewItem", "container": {"type": "ListView"}})
    src_x = src.x
    src_y = src.y

    drag_drop_start_win(src_x + 5, src_y + 5)

    # Switch to previous application via operating
    # system - should be our notepad instance:
    nativeType("<Alt+Tab>")
    snooze(0.5)

    drag_drop_end_win(trg_x + 15, trg_y + 15)

    currentApplicationContext().detach()
    snooze(1)

    setApplicationContext(app_ctx_backup)


def drag_drop_start_win(source_x, source_y):
    # Start dragging:
    mouseMove(source_x, source_y)
    mousePress(source_x, source_y, MouseButton.PrimaryButton)

    snooze(0.5)
    for offset in range(20):
        mouseMove(source_x + offset, source_y)
        snooze(0.1)


def drag_drop_end_win(target_x, target_y):
    # Start dropping:
    snooze(0.5)
    for offset in range(-20, 1, 1):
        mouseMove(target_x + offset, target_y)
        snooze(0.1)

    snooze(0.5)
    mouseRelease(target_x, target_y, MouseButton.PrimaryButton)

    snooze(1)


def registerAUT(aut, path, quoted_args=[], squishserver_host=None, squishserver_port=None):
    binary_path = os.path.join(path, aut)
    binary_path_lower = binary_path.lower()
    if sys.platform == "win32" \
            and not binary_path_lower.endswith(".bat") \
            and not binary_path_lower.endswith(".cmd") \
            and not binary_path_lower.endswith(".jar") \
            and not os.path.exists(binary_path):
        binary_path += ".exe"
    if not os.path.exists(binary_path):
        test.fixateResultContext(1)
        try:
            test.fatal("File does not exist: %s" % binary_path)
            sys.exit(1)
        finally:
            test.restoreResultContext()
        return

    cmd = [os.path.join(os.environ["SQUISH_PREFIX"], "bin", "squishrunner")]
    if squishserver_host is not None:
        cmd.append("--host")
        cmd.append(squishserver_host)
    if squishserver_port is not None:
        cmd.append("--port")
        cmd.append("%s" % squishserver_port)
    cmd.append("--config")
    cmd.append("addAUT")
    cmd.append(aut) 
    cmd.append(path)
    test.log("Executing: %s" % repr(cmd))
    subprocess.Popen(cmd).communicate()

    args_string = quoted_args
    if __builtin__.type(args_string) != str:
        args_string = " ".join(args_string)
    aut_args = '"%s" %s' % (aut, args_string)
    return aut_args, aut, args_string, path
test.py.

来自其他鳞片包

例如,如果要将文件从Explorer移到Qt应用程序,例如,您需要略微修改DRAD_DROP-Method。 除了另外工具包的许可外,这还需要访问Windows许可组件的Quish。您必须从中启动服务器 嘲笑Windows包并在此服务器上启动嵌入的资源管理器。

那样,这看起来是这样的:

def drag_drop_file_on_object(file_path, file_name, object_or_name,server_port):
    app_ctx_backup = currentApplicationContext()

    trg = waitForObject(object_or_name)
    trg_x = trg.x
    trg_y = trg.y

    aut = "embedded_explorer_64"
    path = squishinfo.testCase + "\\.."
    registerAUT(aut, path,squishserver_host="localhost",squishserver_port="{}".format(server_port))
    testSettings.setWrappersForApplication(aut, ("Windows"));
    startApplication('"%s" "%s"' % (aut, file_path),"localhost",server_port)
    waitForObject({"type": "ListView"})

    src = waitForObject({"text": file_name, "type": "ListViewItem", "container": {"type": "ListView"}})
    src_x = src.x
    src_y = src.y

    drag_drop_start_win(src_x + 5, src_y + 5)

    # Switch to previous application via operating
    # system - should be our notepad instance:
    nativeType("<Alt+Tab>")
    snooze(0.5)

    drag_drop_end_win(trg_x + 15, trg_y + 15)

    currentApplicationContext().detach()
    snooze(1)

    setApplicationContext(app_ctx_backup)
test.py.