Man*_*ngh 6 windows desktop selenium automation ui-automation
我使用以下类代码通过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();
}
Run Code Online (Sandbox Code Playgroud)
将测试类作为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)
Run Code Online (Sandbox Code Playgroud)
我仔细检查了calc.exe和Winium.Desktop.Driver.exe的路径,但仍然无法启动WiniumDriverService.有没有其他方法来启动此服务?winium支持windows 10吗?
这里的问题是,'startDriver'在setupEnvironment方法和变量之前执行service并且options没有初始化.因此它抛出空指针异常.
因为testNG下面给出了执行顺序.
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite
Run Code Online (Sandbox Code Playgroud)
有三种解决方案.
更改以下方法的注释,如下所示.这样,启动驱动程序将在安装环境方法之后运行.
@BeforeMethod
public void startDriver(){
driver = new WiniumDriver(service,options);
}
@AfterMethod
public void stopDriver(){
driver.close();
}
Run Code Online (Sandbox Code Playgroud)更改下面给出的注释.
@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();
}
Run Code Online (Sandbox Code Playgroud)更改下面给出的注释.我相信,这个将是最佳解决方案.
@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();
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
5004 次 |
| 最近记录: |