【发布时间】:2021-08-06 13:00:28
【问题描述】:
Main Block:
public class test3 extends utility {
public WebDriver driver;
@BeforeTest
public void invokeBrowser() throws IOException {
driver = base();
driver.get(resource.getProperty("url"));
}
@Test
public class thirdPage {
public void main() {
actionTest test3 = new actionTest();
test3.executeTest();
}
}
}
执行鼠标操作的代码:
public class actionTest {
public void executeTest() {
Actions execute = new Actions();
execute.moveToElement(driver.findElement(By.id("header-search-input"))).click().keyDown(Keys.SHIFT).sendKeys("Cricket").build().perform();
driver.findElement(By.id("header-desktop-search-button")).click();
}
}
在执行块 test3.executeTest() 时,上面的代码导致“NullPointerException”声明 driver = null;
【问题讨论】:
-
什么返回
base()方法?请添加它的代码。除此之外检查是否没有WebDriver driver的另一个声明,可能在utility类中。 -
Btw 类应该以首字母大写、Test3、Utility、ActionTest 等命名。
-
@pgurbr : base() 方法在实用程序类下将初始化 WebDriver
WebDriver driver = new ChromeDriver();。澄清点是具有鼠标动作活动的 executeTest() 方法是否会被调用到@Test case thirdPage 类。 (或)有没有其他方法可以调用 executeTest() 到 @Test case 类。谢谢! -
base()方法必须返回Webdriver对象(作为 WebDriver 类型)或为声明的WebDriver对象设置值(作为 void)。 -
@pburgr :谢谢,我从您的建议中得到了问题! (抱歉在我之前的回复中提到了错误的名字)
标签: java selenium selenium-webdriver browser-automation