【问题标题】:How to build a winium Driver service on Windows 10?如何在 Windows 10 上构建 winium Driver 服务?
【发布时间】:2018-06-18 07:15:07
【问题描述】:

我正在使用以下类代码通过 WiniumDriver 启动计算器。在创建 WiniumDriver 实例之前,我正在启动一个 winium 驱动程序服务。

import java.io.File;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.winium.DesktopOptions;
import org.openqa.selenium.winium.WiniumDriver;
import org.openqa.selenium.winium.WiniumDriverService;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class CalculatorTest {

    static WiniumDriver driver = null;
    static WiniumDriverService service = null;
    static DesktopOptions options = null;

    @BeforeClass
    public static void setupEnvironment(){
        options = new DesktopOptions(); //Instantiate Winium Desktop Options
        options.setApplicationPath("C:\\Windows\\System32\\calc.exe");
        File driverPath = new File("C:\\Winium\\Winium.Desktop.Driver.exe");
        System.setProperty("webdriver.winium.desktop.driver","C:\\Winium\\Winium.Desktop.Driver.exe");
        service = new WiniumDriverService.Builder().usingDriverExecutable(driverPath).usingPort(9999).withVerbose(true)
                .withSilent(false).buildDesktopService();
        try {
            service.start();
        } catch (IOException e) {
            System.out.println("Exception while starting WINIUM service");
            e.printStackTrace();
        }
    }

    @BeforeTest
    public void startDriver(){
        driver = new WiniumDriver(service,options);
    }

    @AfterTest
    public void stopDriver(){
        driver.close();
    }

    @AfterClass
    public void tearDown(){
        service.stop();
    }

将测试类作为 TestNG 项目运行后,观察到以下异常。

FAILED CONFIGURATION: @BeforeTest startDriver
java.lang.NullPointerException
    at org.openqa.selenium.winium.WiniumDriverCommandExecutor.<init>(WiniumDriverCommandExecutor.java:59)
    at org.openqa.selenium.winium.WiniumDriver.<init>(WiniumDriver.java:75)
    at com.bravura.automation.CalculatorTest.startDriver(CalculatorTest.java:41)

我仔细检查了 calc.exe 和 Winium.Desktop.Driver.exe 的路径,仍然无法启动 WiniumDriverService。还有其他方法可以启动此服务吗? winium 支持 windows 10 吗?

【问题讨论】:

    标签: windows selenium automation ui-automation desktop


    【解决方案1】:

    这里的问题是,'startDriver'setupEnvironment 方法之前执行,变量serviceoptions 没有初始化。因此它抛出空指针异常。

    因为testNG中的执行顺序如下所示。

    @BeforeSuite
    @BeforeTest
    @BeforeClass
    @BeforeMethod
    @Test
    @AfterMethod
    @AfterClass
    @AfterTest
    @AfterSuite
    

    有三种解决方案。

    1. 更改以下方法的注释,如下所示。这样,启动驱动程序将在设置环境方法之后运行。

       @BeforeMethod
       public void startDriver(){
          driver = new WiniumDriver(service,options);
       }
      
      @AfterMethod
      public void stopDriver(){
         driver.close();
      }
      
    2. 如下所示更改注释。

      @BeforeTest
      public static void setupEnvironment(){
          options = new DesktopOptions(); //Instantiate Winium Desktop Options
          options.setApplicationPath("C:\\Windows\\System32\\calc.exe");
          File driverPath = new File("C:\\Winium\\Winium.Desktop.Driver.exe");
          System.setProperty("webdriver.winium.desktop.driver","C:\\Winium\\Winium.Desktop.Driver.exe");
          service = new WiniumDriverService.Builder().usingDriverExecutable(driverPath).usingPort(9999).withVerbose(true)
                  .withSilent(false).buildDesktopService();
          try {
              service.start();
          } catch (IOException e) {
              System.out.println("Exception while starting WINIUM service");
              e.printStackTrace();
          }
      }
      
      @BeforeClass
      public void startDriver(){
          driver = new WiniumDriver(service,options);
      }
      
      @AfterClass
      public void stopDriver(){
          driver.close();
      }
      
      @AfterTest
      public void tearDown(){
          service.stop();
      }
      
    3. 如下所示更改注释。我相信,这将是最佳解决方案。

      @BeforeTest
      public static void setupEnvironment(){
          options = new DesktopOptions(); //Instantiate Winium Desktop Options
          options.setApplicationPath("C:\\Windows\\System32\\calc.exe");
          File driverPath = new File("C:\\Winium\\Winium.Desktop.Driver.exe");
          System.setProperty("webdriver.winium.desktop.driver","C:\\Winium\\Winium.Desktop.Driver.exe");
          service = new WiniumDriverService.Builder().usingDriverExecutable(driverPath).usingPort(9999).withVerbose(true)
                  .withSilent(false).buildDesktopService();
          try {
              service.start();
          } catch (IOException e) {
              System.out.println("Exception while starting WINIUM service");
              e.printStackTrace();
          }
      }
      
      @BeforeMethod
      public void startDriver(){
          driver = new WiniumDriver(service,options);
      }
      
      @AfterMethod
      public void stopDriver(){
          driver.close();
      }
      
      @AfterTest
      public void tearDown(){
          service.stop();
      }
      

    【讨论】:

      【解决方案2】:
      1. 这是正确的使用方法
      public class SimpleTest
      {
          DesktopOptions options;
          WiniumDriverService service;
          WiniumDriver driver;
      
          @BeforeTest
          public void bt()
          {
              //Instantiate Winium Desktop Options
              options = new DesktopOptions();
              
              // Path of application you want to run and test
              options.setApplicationPath("C:\\Windows\\System32\\calc.exe");
              
              //Path for Winium Desktop Driver
              File driverPath = new File(System.getProperty("user.dir")+File.separator+"drivers"+File.separator+"Winium.Desktop.Driver.exe");
              
              //Port is 9999, you can change it
              service = new WiniumDriverService.Builder().usingDriverExecutable(driverPath).usingPort(9999).withVerbose(true).withSilent(false).buildDesktopService();
              
              try
              {
                   service.start();
              }
              catch (IOException e)
              {
                  System.out.println("Exception while starting WINIUM service");
                  e.printStackTrace();
              }
              driver = new WiniumDriver(service,options);
          }
      
          @AfterTest
          public void at()
          {
              service.stop();
          }
      }
      
      1. 如果需要其他配置可以参考this link。但是,我不建议您将 Winium 用于 Windows 自动化,即使对于基本的 Calculator 程序它也无法正常工作,Winium 的最后一个版本是在 2016 年,我认为它现在没有定期维护。取而代之的是使用 WinAppDriver,它是一个更好的工具并且有很好的文档。首先参考this link

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多