首页 > 解决方案 > 如何在 Win App 驱动程序中启动单击一次应用程序?

问题描述

我正在使用 Win App Driver 来自动化 WPF 应用程序,我们的 WPF 应用程序是单击一次应用程序。当我启动应用程序时,我注意到驱动程序为空。

    private string notepadAppid = 
    @"https://saqa.xyz.biz/abcnew/testets.Shell.Applications";       
    protected static OpenQA.Selenium.Appium.Windows.WindowsDriver<WindowsElement> driver;        
    Uri urls = new Uri("http://127.0.0.1:4723");
    
     AppiumOptions opt = new AppiumOptions();
                opt.AddAdditionalCapability("app", notepadAppid);
                opt.AddAdditionalCapability("deviceName", "WindowsPC");
                driver = new WindowsDriver<WindowsElement> (new Uri("http://127.0.0.1:4723"), opt, 
       TimeSpan.FromMinutes(1));

我注意到'驱动程序'为空,我收到异常:“无法使用 appId 找到打开的应用程序窗口:https ://saqa.xyz.biz/abcnew/testets.Shell.Applications和 processId:-1。谢谢你进步。

标签: winappdriver

解决方案


如前所述您需要检查驱动程序是否为空。

//******************************************************************************
//
// Copyright (c) 2017 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//******************************************************************************

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
using System;

namespace CalculatorTest
{
    public class CalculatorSession
    {
        // Note: append /wd/hub to the URL if you're directing the test at Appium
        private const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
        private const string CalculatorAppId = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App";

        protected static WindowsDriver<WindowsElement> session;

        public static void Setup(TestContext context)
        {
            // Launch Calculator application if it is not yet launched
            if (session == null)
            {
                // Create a new session to bring up an instance of the Calculator application
                // Note: Multiple calculator windows (instances) share the same process Id
                DesiredCapabilities appCapabilities = new DesiredCapabilities();
                appCapabilities.SetCapability("app", CalculatorAppId);
                appCapabilities.SetCapability("deviceName", "WindowsPC");
                session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
                Assert.IsNotNull(session);

                // Set implicit timeout to 1.5 seconds to make element search to retry every 500 ms for at most three times
                session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1.5);
            }
        }

        public static void TearDown()
        {
            // Close the application and delete the session
            if (session != null)
            { 
                session.Quit();
                session = null;
            }
        }
    }
}

推荐阅读