[英]How to build a winium Driver service on Windows 10?
我正在使用以下 class 代碼通過 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();
}
在運行測試 class 作為 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?
這里的問題是, 'startDriver'
在setupEnvironment
方法之前執行,並且變量service
和options
未初始化。 因此它拋出空指針異常。
因為testNG
的執行順序如下。
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite
有三種解決方案。
更改以下方法的注釋,如下所示。 這樣,啟動驅動程序將在安裝環境方法之后運行。
@BeforeMethod public void startDriver(){ driver = new WiniumDriver(service,options); } @AfterMethod public void stopDriver(){ driver.close(); }
更改下面給出的注釋。
@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(); }
更改下面給出的注釋。 我相信,這個將是最佳解決方案。
@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(); }
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.