如何在基类中包含属性文件,然后调用其他类?

问题描述 投票:0回答:1

在我的应用程序中,我有一个属性文件,我在其中添加了所有输入字段的XPath,通过监听器类调用基类,对失败的情况进行截图。通过监听器类,我调用基类,对失败的情况进行截图。在testng.xml中,我也添加了监听器。每当我运行testng.xml时,它都会显示 "Null Pointer",这使得所有的测试案例都失败了。

注意:在基类中,我有一个初始化函数,其中有驱动、URL、登录的详细信息。在其他的类中,我加入了listener,当我运行时,它显示 "Null Pointer"。当我运行时,它显示 "Null Pointer"。

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 -->

错误:显示 "Null Pointer "异常。

java selenium-webdriver testng testng.xml
1个回答
0
投票

它必然会失败。WebDriver driver 是永远不会初始化的。在使用get方法之前,你需要告诉它是哪个驱动实例。是Chrome还是Firefox还是其他。

步骤如下。

System.setProperty("webdriver.chrome.driver","path//to//chromedriver//");
WebDriver driver = new ChromeDriver();
driver.get(URL);

由于驱动程序从未被初始化,所以 Null 并在失败时给你一个空指针异常。

© www.soinside.com 2019 - 2024. All rights reserved.