每当我使用driver.findElement(By.anything("")).getText();
时,我都会得到NullPointerException
。在android真实设备上进行测试。
从我的测试库开始的以下代码可以初始化驱动程序,这可能是问题吗?他们缺少的代码部分是用于初始化Driver
。
if (driver == null) {
switch (prop.getProperty("browser")) {
case "chrome":
System.setProperty("webdriver.chrome.driver", prop.getProperty("chrome.driver.path"));
driver = new ChromeDriver();
break;
default:
System.out.println("Wrong driver was chosen! Plsease choose the driver.");
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
return driver;
}
public static AndroidDriver<MobileElement> initializeMobileDriver() {
if (Mdriver == null) {
switch (prop.getProperty("MobileBrowser")) {
case "android":
capabilities = DesiredCapabilities.android();
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, BrowserType.CHROME);
capabilities.setCapability("autoGrantPermissions", "true");
capabilities.setCapability(MobileCapabilityType.SUPPORTS_FINDING_BY_CSS, true);
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, prop.getProperty("DeviceName"));
capabilities.setCapability(MobileCapabilityType.APPIUM_VERSION, prop.getProperty("AppiumVersion"));
capabilities.setCapability(MobileCapabilityType.PLATFORM, Platform.ANDROID);
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, prop.getProperty("PlatformName"));
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, prop.getProperty("PlatformVersion"));
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, prop.getProperty("DeviceName"));
capabilities.setCapability("autoAcceptAlerts", "true");
try {
Mdriver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
driver = Mdriver;
} catch (Exception e) {
System.out.println(" error is " + e.getMessage());
}
break;
default:
System.out.println("Wrong driver was chosen! Plsease choose the driver.");
}
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return Mdriver;
}
任何代码简单代码都可以使用,并且它返回null。
@测试 公共静态无效的SimpleGetTextTest(){
driver.get("https://google.com")
driver.findElement(By.name("q")).sendKeys("Java"+Keys.ENTER);
driver.findElement(By.xpath("//div[@class = 'MUxGbd']")).getText();
这将返回NullPointerException
答案 0 :(得分:0)
我解决了这个问题。
基本上,如您在代码中看到的那样,我使用了Mdriver = new AndroidDriver(new URL(“ http://0.0.0.0:4723/wd/hub”),features);,其中AndroidDriver = Mdriver;
我在两个实例上都将其更改为RemoteDriver,一切开始正常工作。希望这会有所帮助