[英]How to build a winium Driver service on Windows 10?
I am using the following class code for launching calculator via WiniumDriver.我正在使用以下 class 代码通过 WiniumDriver 启动计算器。 Before creating an instance of WiniumDriver, I am starting a winium driver service.
在创建 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();
}
After running the test class as TestNG item, following exception is observed.在运行测试 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)
I double checked the path to calc.exe and Winium.Desktop.Driver.exe, still I am not able to launch the WiniumDriverService.我仔细检查了 calc.exe 和 Winium.Desktop.Driver.exe 的路径,但仍然无法启动 WiniumDriverService。 Is there any other way to start this service?
还有其他方法可以启动此服务吗? Does winium support windows 10?
winium是否支持windows 10?
The problem here is that, 'startDriver'
is executing before setupEnvironment
method and the variables service
and options
are not initialized. 这里的问题是,
'startDriver'
在setupEnvironment
方法之前执行,并且变量service
和options
未初始化。 Hence it is throwing null pointer exception. 因此它抛出空指针异常。
because the order of execution in testNG
is given below. 因为
testNG
的执行顺序如下。
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite
There are three solutions. 有三种解决方案。
change the annotation for the following methods as given below. 更改以下方法的注释,如下所示。 so that, the start driver will run after the setup environment method.
这样,启动驱动程序将在安装环境方法之后运行。
@BeforeMethod public void startDriver(){ driver = new WiniumDriver(service,options); } @AfterMethod public void stopDriver(){ driver.close(); }
change the annotations as given below. 更改下面给出的注释。
@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(); }
change the annotations as given below. 更改下面给出的注释。 I believe, this one will be optimal solution.
我相信,这个将是最佳解决方案。
@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.