在我的应用程序中,我有属性文件,我在其中添加了输入字段的所有 XPath。通过侦听器类我调用基类,为失败的情况截屏。在 testng.xml 中,我还添加了监听器。每当我运行 testng.xml 时,它都会显示“空指针”,这会使所有测试用例都失败。
注意:在基类中,我有一个初始化函数,其中包含驱动程序、URL、登录详细信息。在其他课程中,我包括了听众。每当我运行时,它都会显示“空指针”。
Base Class:
public class base {
public static WebDriver driver;
public void initialization() throws FileNotFoundException{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("./src/obj.properties");
try {
prop.load(fis);
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
driver.get(prop.getProperty("url"));
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement email = driver.findElement(By.xpath(prop.getProperty("email_path")));
email.sendKeys(prop.getProperty("email"));
email.sendKeys(Keys.TAB);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement pass = driver.findElement(By.xpath(prop.getProperty("pwd_path")));
pass.sendKeys(prop.getProperty("pwd"));
WebElement btn = driver.findElement(By.xpath(prop.getProperty("login_btn")));
btn.click();
driver.manage().window().maximize();
}
public void failed(String testmethodname) {
File scrfile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrfile,new File("./screenshots/"+testmethodname+".jpg"));
}catch (IOException e) {
e.printStackTrace();
}
}
Listener Class:
public class listener extends base implements ITestListener{
// private WebDriver driver;
public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("TestCase started and details are "+result.getName());
}
public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub
Reporter.log("TestCase Pass");
}
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("Failed Test cases are "+result.getName());
failed(result.getMethod().getMethodName());
Reporter.log("TestCase Fail");
}
Example Class:
@Listeners(listener.class)
public class example extends base{
@BeforeSuite
public void setUp() throws FileNotFoundException {
initialization();
}
@Test(groups={"IV"},enabled = true, priority = 1)
public void exam() throws FileNotFoundException, InterruptedException {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("./src/obj.properties");
try {
prop.load(fis);
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
JavascriptExecutor jse22 = (JavascriptExecutor) driver;
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Thread.sleep(500);
new WebDriverWait(driver, 20)
.until(ExpectedConditions.elementToBeClickable(By.xpath(prop.getProperty("path"))))
.click();
WebElement s = driver.findElement(By.xpath(prop.getProperty("a_path")));
s.click();
Thread.sleep(1000);
}
@AfterMethod
public void tearDown() {
driver.quit();
}
Testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
<listener class-name="pkg.listener"/>
</listeners>
<test name="Test">
<groups>
<run>
<include name="IV"/>
</run>
</groups>
<classes>
<class name="pkg.example"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
错误:显示“空指针”异常。