我正在学习appium并尝试将一个对象从一个类调用到另一个类并面向空指针异常。 以下是我的代码:
public class TestCommons {
public AndroidDriver driver;
public void setUp() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "MotoE");
File file = new File("D:/APK1/com.vector.guru99.apk");
capabilities.setCapability("app", file);
try {
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void tearDown() {
driver.closeApp();
}
}
我想使用上面的类,即其他类中的“TestCommons”。我想使用驱动程序对象。
第二课如下:
public class Day03 extends TestCommons {
TestCommons commons = new TestCommons();
@BeforeClass
public void beforeClass() {
commons.setUp();
}
@Test(enabled = true)
public void f() {
if (driver.findElement(By.id("com.vector.guru99:id/action_quiz")).isDisplayed()) {
System.out.println("Quiz is displayed");
driver.findElement(By.id("com.vector.guru99:id/action_quiz")).click();
System.out.println("quiz is click");
}
}
@AfterClass(enabled = true)
public void afterClass() {
commons.tearDown();
}
}
在第二个程序@:
中获取空指针if(driver.findElement(By.id("com.vector.guru99:id/action_quiz")).isDisplayed();
请有人澄清我。
答案 0 :(得分:0)
你有两个问题之一。
1)在setUp()中未正确设置驱动程序。如果是这种情况,您可能会遇到异常。检查您的日志以确保那里没有异常。
2)driver.findElement(By.id(" com.vector.guru99:id / action_quiz"))返回null。您可以通过设置调试点并在该调用上运行evaluate表达式来检查这一点。
答案 1 :(得分:0)
尝试这种方式:
public class TestCommons {
public static AndroidDriver driver;
@BeforeClass
public void setUp() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "MotoE");
File file = new File("D:/APK1/com.vector.guru99.apk");
capabilities.setCapability("app", file);
try {
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@AfterClass
public void tearDown() {
driver.closeApp();
}
}
public class Day03 extends TestCommons {
@Test(enabled = true)
public void f() {
if (driver.findElement(By.id("com.vector.guru99:id/action_quiz")).isDisplayed()) {
System.out.println("Quiz is displayed");
driver.findElement(By.id("com.vector.guru99:id/action_quiz")).click();
System.out.println("quiz is click");
}
}
}