【问题标题】:How do I import the white project into an IronPython program?如何将白色项目导入 IronPython 程序?
【发布时间】:2013-06-22 00:56:57
【问题描述】:

我正在开发一些 UI 自动化软件,最近将项目从 Python 转移到 IronPython,因为该项目的要求表明它只能在 Windows 环境中使用。但是,我需要自动化使用 Windows Presentation Foundation (WPF) 的程序的 UI。我发现了一个看起来很有用的库,叫做 White。

http://white.codeplex.com/

所以我想在我的 IronPython 程序中使用它,但是到目前为止我看到的所有用于导入用 C# 编写或使用 C# 接口编写的模块的示例代码都是用于 Microsoft/Windows 内置的。我想我应该可以参考它,因为您可以根据这篇文章使用 IronRuby 来完成它。

http://www.natontesting.com/2010/02/17/how-to-test-a-wpf-app-using-ironruby-and-white/

但是,我不得不想象 IronRuby 导入/引用 White 的方式/语法与 IronPython 的方式非常不同。我还发现其他开发人员的帖子说他们正在使用 IronPython 和 White,但找不到包含实际引用 White 的代码的帖子。我该怎么办?

【问题讨论】:

    标签: wpf windows ironpython ui-automation white-framework


    【解决方案1】:
    import clr
    clr.AddReference("White.Core")
    clr.AddReference("White.NUnit")
    from White.NUnit import *
    from White import *
    from White.Core import *
    from White.Core.Configuration import *
    from White.Core.UIItems import *
    from White.Core.UIItems.WindowItems import *
    from White.Core.UIItems.ListBoxItems import *
    from White.Core.UIItems.Container import *
    from White.Core.UIItems.Finders import *
    from White.Core.Factory import *
    from White.Core.Finder import *
    from White.Core.AutomationElementSearch import *
    from White.Core.WindowsAPI import *
    

    然后照常使用白色的api。

    app = Application.Attach(proc)
    win = app.GetWindow('Window Caption')
    print win.Name
    box = win.Get[MultilineTextBox]('textBoxId')
    print box.Text
    

    【讨论】:

    • 这看起来很棒但是当你调用 clr.AddReference("White.Core") clr.AddReference("White.NUnit") White 需要放在哪里所以它在搜索路径上AddReference() 使用?
    • ironpython.net/documentation/dotnet/dotnet.html。程序集需要位于应用程序的 bin 目录或 GAC 中。或者,如果您在其他位置有程序集,则可以使用带有完全限定路径的 clr.AddReferenceToFileAndPath
    • 啊,现在我明白了 robowahoo 的第一个答案在说什么。谢谢你们!
    【解决方案2】:

    IronPython 能够使用以下方法处理任何 CLR 程序集:

    import clr
    
    clr.AddReference("AssemblyName")
    

    因为白色项目是基于 .NET 的,所以这将起作用。要使用程序集中的对象:

    from AssemblyName import *
    

    (当然你可以在这里使用一个子集)

    然后简单地实例化并使用您的对象:

    from System.Collections import BitArray
    ba = BitArray(5)
    ba.Set(0, True) # call the Set method
    ba[0]
    

    这个documentation 应该会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多