在word文档之间切换互操作

时间:2017-04-19 10:58:33

标签: c# unit-testing interop appium

我创建了许多单元测试。在每个测试开始时,我创建一个单词实例。对于某个单元测试,我需要打开第二个单词实例,但它会在第一个实例后面打开,我不知道为什么会这样做。我已经尝试过很多东西,例如将它作为活动应用程序并将其设置为焦点,但我没有运气。我使用互操作来尝试这样做,而Appium尝试运行单元测试。我如何让Word的第二个实例出现在前面?

我的代码如下

[TestClass]
public class Word
{

    private const string wordId = @"C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.exe";
    private const string AppDriverUrl = "http://127.0.0.1:4723";
    private static WindowsDriver<WindowsElement> wordSession;

    private static string wordTitleTemp = @"N:\Testing\V8\TestMaster\V8.6\Testing\V8.6.0.11 Testing\wordTitleTemp.docx";

    [TestInitialize]

    public void SetUp()
    {

        var appCapabilities = new DesiredCapabilities();
        appCapabilities.SetCapability("app", wordId);

        var initialSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), appCapabilities);

        var capabilities = new DesiredCapabilities();
        capabilities.SetCapability("app", "Root");
        wordSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), capabilities);
        wordSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

    }

    [TestMethod]
    public void wordTitle()
    {

        Thread.Sleep(4000);
        var wordApplication = new word.Application();
        var myDocument = wordApplication.Documents.Open(wordTitleTemp);
        wordSession.CloseApp();
        wordApplication.Visible = true;
        var paragraphs = myDocument.Paragraphs;
        word.Paragraph paragraph = paragraphs.First;
        word.Range paragraphRange = paragraph.Range;
        word.Sentences sentences = paragraphRange.Sentences;
        word.Range firstSentence = sentences.First;
        firstSentence.Select();
        wordApplication.Activate();
        wordApplication.WindowState = word.WdWindowState.wdWindowStateMaximize;
        word.Window window = myDocument.ActiveWindow;
        window.SetFocus();
        window.Activate();
        myDocument.ActiveWindow.SetFocus();
    //wordSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        //wordSession.FindElementByName("Close").Click();//Close out of opened Word as it gets in the way


        wordSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        wordSession.FindElementByName("Create").Click();

        wordSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        wordSession.FindElementByName("Heading 1").Click();



    }

0 个答案:

没有答案