Win10下Winium Driver服务搭建及启动失败问题求助
解决WiniumDriver启动计算器时的NullPointerException问题
首先明确回答你的疑问:Winium.Desktop完全支持Windows 10,只要使用适配Win10的稳定版驱动,就不会存在系统兼容性问题。接下来我们分析你遇到的NullPointerException问题,并给出多种服务启动方案。
异常原因分析
你遇到的NullPointerException出现在WiniumDriverCommandExecutor的构造方法中,本质原因是传入的WiniumDriverService实例为null——也就是说你的服务启动逻辑大概率失败了,但异常被捕获后没有终止流程,导致后续创建WiniumDriver时传入了无效的服务对象。
修正后的服务启动方案(推荐)
下面是调整后的测试类代码,修复了原代码中的初始化逻辑问题,并添加了错误检查:
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 { private WiniumDriver driver = null; private WiniumDriverService service = null; private DesktopOptions options = null; @BeforeClass public void setupEnvironment(){ options = new DesktopOptions(); options.setApplicationPath("C:\\Windows\\System32\\calc.exe"); // 提前检查驱动文件是否存在 File driverPath = new File("C:\\Winium\\Winium.Desktop.Driver.exe"); if (!driverPath.exists()) { throw new RuntimeException("Winium驱动文件未找到,请检查路径!"); } System.setProperty("webdriver.winium.desktop.driver", driverPath.getAbsolutePath()); // 构建Winium服务 service = new WiniumDriverService.Builder() .usingDriverExecutable(driverPath) .usingPort(9999) .withVerbose(true) .withSilent(false) .buildDesktopService(); try { service.start(); System.out.println("Winium服务启动成功"); } catch (IOException e) { System.err.println("启动Winium服务时出现异常"); e.printStackTrace(); // 抛出异常终止测试,避免后续无效执行 throw new RuntimeException("Winium服务启动失败", e); } } @BeforeTest public void startDriver(){ // 确保服务已正常启动 if (service == null || !service.isRunning()) { throw new RuntimeException("Winium服务未处于运行状态!"); } driver = new WiniumDriver(service, options); } @AfterTest public void stopDriver(){ if (driver != null) { driver.close(); driver.quit(); // 彻底释放驱动资源 } } @AfterClass public void tearDown(){ if (service != null && service.isRunning()) { service.stop(); } } // 示例测试方法:验证加法功能 @Test public void testAddition(){ WebElement num1 = driver.findElement(By.name("One")); num1.click(); WebElement plus = driver.findElement(By.name("Plus")); plus.click(); WebElement num2 = driver.findElement(By.name("Two")); num2.click(); WebElement equals = driver.findElement(By.name("Equals")); equals.click(); WebElement result = driver.findElement(By.className("TextBlock")); System.out.println("加法运算结果: " + result.getText()); } }
关键修改点说明
- 移除static修饰符:TestNG的
@BeforeClass方法不需要static,static会导致变量初始化时机不可控,非static方式能确保每个测试实例都正确初始化资源。 - 添加前置检查:提前验证驱动文件是否存在,避免后续隐藏错误。
- 服务状态校验:创建
WiniumDriver前确认服务已启动,从根源避免NullPointerException。 - 增强资源释放:添加
driver.quit()确保彻底关闭驱动进程,避免资源泄漏。 - 异常向上抛出:服务启动失败时直接终止测试,避免无效流程继续执行。
其他服务启动方式(简化版)
如果你不想手动管理WiniumDriverService,可以让WiniumDriver自动启动服务,代码会更简洁:
@BeforeTest public void startDriver() throws MalformedURLException { DesktopOptions options = new DesktopOptions(); options.setApplicationPath("C:\\Windows\\System32\\calc.exe"); System.setProperty("webdriver.winium.desktop.driver", "C:\\Winium\\Winium.Desktop.Driver.exe"); // 直接通过URL连接,Winium会自动启动服务 driver = new WiniumDriver(new URL("http://localhost:9999"), options); } @AfterTest public void stopDriver(){ if (driver != null) { driver.quit(); // 关闭driver时会自动停止服务 } }
额外排查要点
- 权限问题:尝试以管理员身份运行你的IDE或测试程序,访问System32目录下的计算器和启动Winium驱动可能需要管理员权限。
- 端口占用:使用
netstat -ano | findstr :9999命令检查端口9999是否被其他进程占用,若被占用则更换为未使用的端口(如9998)。 - 驱动版本匹配:确保Winium.Desktop.Driver的版本与你的Selenium版本兼容,建议使用最新稳定版驱动适配Windows 10。
内容的提问来源于stack exchange,提问作者Manmohan_singh
相关产品推荐
相关产品推荐

