【问题标题】:Connecting to a running process in Winappdriver using Javascript使用 Javascript 连接到 Winappdriver 中正在运行的进程
【发布时间】:2020-06-29 05:46:23
【问题描述】:

我对 JS/Winappdriver 还很陌生。

我要测试的应用程序是来自.Net 的基于 Windows 的“单击一次”应用程序,因此我必须从 IE 访问网站并单击“安装”。这将打开应用程序。

应用程序运行后,我无法在使用 JavaScript 时连接应用程序以执行我的 UI 交互。

使用 C#,我在进程中循环查找进程名称,获取窗口句柄,将其转换为十六进制,将其添加为功能并创建驱动程序 - 它有效。示例代码如下,

public Setup_TearDown()
        {
            string TopLevelWindowHandleHex = null;
            IntPtr TopLevelWindowHandle = new IntPtr();
            foreach (Process clsProcess in Process.GetProcesses())
            {
                if (clsProcess.ProcessName.StartsWith($"SomeName-{exec_pob}-{exec_env}"))
                {
                    TopLevelWindowHandle = clsProcess.Handle;
                    TopLevelWindowHandleHex = clsProcess.MainWindowHandle.ToString("x");
                }
            }
            var appOptions = new AppiumOptions();
            appOptions.AddAdditionalCapability("appTopLevelWindow", TopLevelWindowHandleHex);
            appOptions.AddAdditionalCapability("ms:experimental-webdriver", true);
            appOptions.AddAdditionalCapability("ms:waitForAppLaunch", "25");
            AppDriver = new WindowsDriver<WindowsElement>(new Uri(WinAppDriverUrl), appOptions);
            AppDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);
        }

如何在 Javascript 中执行此操作?我似乎找不到任何代码示例。 基于来自repo 的示例,我在 JS 中尝试了以下操作以找到要锁定的进程,但没有运气。

import {By2} from "selenium-appium";
// this.appWindow = this.driver.element(By2.nativeAccessibilityId('xxx'));
        // this.appWindow = this.driver.element(By2.nativeXpath("//Window[starts-with(@Name,\"xxxx\")]"));
        // this.appWindow = this.driver.elementByName('WindowsForms10.Window.8.app.0.13965fa_r11_ad1');
        // thisappWindow = this.driver.elementByName('xxxxxxx');

async connectAppDriver(){
        await this.waitForAppWindow();
        var appWindow = await this.appWindow.getAttribute("NativeWindowHandle");
        let hex = (Number(ewarpWindow)).toString(16);
        var currentAppCapabilities =
            {
                "appTopLevelWindow": hex,
                "platformName": "Windows",
                "deviceName": "WindowsPC",
                "newCommandTimeout": "120000"
            }
        let driverBuilder = new DriverBuilder();
        await driverBuilder.stopDriver();
        this.driver = await driverBuilder.createDriver(currentEwarpCapabilities);
        return this.driver;
    }

我在 Winappdriver 中不断收到此错误

{"status":13,"value":{"error":"unknown error","message":"An unknown error occurred in the remote end while processing the command."}}

我也打开了这张票here

这似乎是一件容易的事,但我无法弄清楚这一点。

我可以使用任何节点包轻松获得顶级窗口句柄吗?

我愿意接受有关如何在将 JavaScript 用于 Winappdriver 时解决此问题的建议。

【问题讨论】:

    标签: javascript windows selenium-webdriver appium winappdriver


    【解决方案1】:

    希望对大家有所帮助,

    通过使用 C# 创建一个 exe 来解决这个问题,该 exe 生成应用程序的十六进制以根据进程名称进行连接,它看起来像这样。

     public string GetTopLevelWindowHandleHex()
        {
            string TopLevelWindowHandleHex = null;
            IntPtr TopLevelWindowHandle = new IntPtr();
            foreach (Process clsProcess in Process.GetProcesses())
            {
                if (clsProcess.ProcessName.StartsWith(_processName))
                {
                    TopLevelWindowHandle = clsProcess.Handle;
                    TopLevelWindowHandleHex = clsProcess.MainWindowHandle.ToString("x");
                }
            }
            if (!String.IsNullOrEmpty(TopLevelWindowHandleHex))
                return TopLevelWindowHandleHex;
            else
                throw new Exception($"Process: {_processName} cannot be found");
        }
    

    从JS调用得到顶层窗口句柄的十六进制,像这样,

    async getHex () {
        var pathToExe =await path.join(process.cwd(), "features\\support\\ProcessUtility\\GetWindowHandleHexByProcessName.exe");
        var pathToDir =await path.join(process.cwd(), "features\\support\\ProcessUtility");
        const result = await execFileSync(pathToExe, [this.processName]
                , {cwd: pathToDir, encoding: 'utf-8'}
                , async function (err, data) {
                    console.log("Error: "+ err);
                    console.log("Data(hex): "+ data);
                    return JSON.stringify(data.toString());
                });
        return result.toString().trim();
    }
    

    像这样使用十六进制连接到应用程序,

    async connectAppDriver(hex) {
        console.log(`Hex received to connect to app using hex: ${hex}`);
        const currentAppCapabilities=
            {
                "browserName": '',
                "appTopLevelWindow": hex.trim(),
                "platformName": "Windows",
                "deviceName": "WindowsPC",
                "newCommandTimeout": "120000"
            };
        const appDriver = await new Builder()
            .usingServer("http://localhost:4723/wd/hub")
            .withCapabilities(currentAppCapabilities)
            .build();
        await driver.startWithWebDriver(appDriver);
        return driver;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-26
      • 2022-08-14
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2020-02-10
      • 2021-01-12
      • 2015-12-26
      相关资源
      最近更新 更多