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

标签 java selenium-webdriver testng testng.xml

在我的应用程序中,我有属性文件,我在其中添加了输入字段的所有 XPath。通过监听器类,我调用基类,以对失败的情况进行屏幕截图。在 testng.xml 中我还添加了监听器。每当我运行 testng.xml 时,它都会显示“空指针”,这使得所有测试用例都失败。

注意:在基类中,我有一个初始化函数,其中包含驱动程序、URL、登录详细信息。在其他类(class)中,我包括了听众。每当我运行时,它都会显示“空指针”。

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

错误: 显示“空指针”异常。

最佳答案

注定会失败。 WebDriver 驱动程序 从未初始化。在使用 get 方法之前,您需要知道它是哪个驱动程序实例。无论是 Chrome 还是 Firefox 还是其他。

步骤如下:

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

由于驱动程序从未初始化,因此它是Null,并在失败时给您一个空指针异常。

关于java - 如何在基类中包含属性文件然后调用其他类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61543595/

相关文章:

node.js - Node.js 上的 Selenium Webdriver - 如何访问全局窗口变量

java - 找不到记录器的附加程序(selenium webdriver+testng)

java - AccountManager:invalidateAuthToken 不会使 token 失效

java - android 在 Facebook 和其他社交媒体上分享

java - 使用 DTO 的 Post 方法

java - java中自定义异常中的父类(super class)构造函数

python - 键盘选项卡导航的自动化测试

java - Webelement 列表并仅选择一个

java - 如何在 ItestListener 中获取当前的类驱动程序

java - TestNG - 从不同的类导入/运行测试