java.lang.NullPointer 异常 (TestNG)

java.lang.NullPointer Exception (TestNG)

我有一个 class“BaseTest”class,其中包含与 Web 驱动程序相关的代码以及所有 BeforeTest 和 AfterTest 方法。我在 'Login' 文件和 'SkillsAcquisition' 文件中扩展了这个方法,但只有登录文件运行正常,我在第二个文件中得到 NullPointer 异常。我不知道我在这里做错了什么。

我是 Selenium 和 testng 的新手,非常感谢任何帮助。

BaseTest.java

package Students;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class BaseTest {

    String driverPath = "my_path_to_chromedriver";
    public String baseUrl = "some_url";

    public WebDriver driver ; 
    public String expected = null;
    public String actual = null;
    private boolean acceptNextAlert;
    
    @BeforeTest
    public void launchBrowser() {
        System.out.println("launching Chrome browser"); 
        System.setProperty("webdriver.chrome.driver", driverPath);
        driver= new ChromeDriver();
        driver.manage().window().maximize();
        driver.get(baseUrl);
    }
    
    @AfterTest
    public void terminateBrowser(){
        driver.close();
    }
    
    private boolean isElementPresent(By by) {
        try {
          driver.findElement(by);
          return true;
        } catch (NoSuchElementException e) {
          return false;
        }
      }

      private boolean isAlertPresent() {
        try {
          driver.switchTo().alert();
          return true;
        } catch (NoAlertPresentException e) {
          return false;
        }
      }

      private String closeAlertAndGetItsText() {
        try {
          Alert alert = driver.switchTo().alert();
          String alertText = alert.getText();
          if (acceptNextAlert) {
            alert.accept();
          } else {
            alert.dismiss();
          }
          return alertText;
        } finally {
          acceptNextAlert = true;
        }
      }

}

Login.java

package Students;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Login extends BaseTest{
    
        @Test(priority = 0)
        public void login() throws InterruptedException{
            driver.findElement(By.id("Username")).clear();
            driver.findElement(By.id("Username")).sendKeys("my_username");
            driver.findElement(By.id("Password")).clear();
            driver.findElement(By.id("Password")).sendKeys("my_password");
            driver.findElement(By.id("Password")).sendKeys(Keys.RETURN);
            expected = "Expected Text";
            actual = driver.getTitle();
            Assert.assertEquals(actual, expected);  
    }
}

SkillsAcquisition.java

package Students;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

@Test
public class SkillsAcquisition extends BaseTest{

        @Test(priority = 1)
        public void addSkills() throws InterruptedException {
            System.out.println("Test reached Second file");
            WebDriverWait d = new WebDriverWait(driver, 20);            
d.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.id("skillAquistionTab")));
            driver.findElement(By.id("skillAquistionTab")).click();
            Thread.sleep(3000);
            d.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//a[@class='add']")));
            Thread.sleep(3000);
            driver.findElement(By.xpath("//a[@class='add']")).click();      
            System.out.println("Added");
            driver.findElement(By.xpath("(//input[@type='text'])[2]")).click();
            driver.findElement(By.xpath("(//input[@type='text'])[2]")).clear();
            driver.findElement(By.xpath("(//input[@type='text'])[2]")).sendKeys("Skill1");
            driver.findElement(By.id("page-wrapper-1")).click();
            Thread.sleep(2000);
            driver.findElement(By.linkText("Skill1")).click();
            Thread.sleep(3000);
            driver.findElement(By.id("addProgramButton")).click();
            Thread.sleep(2000);
            driver.findElement(By.id("ProgramName")).clear();
            driver.findElement(By.id("ProgramName")).sendKeys("Program1");
            driver.findElement(By.id("SaveNewProgram")).click();
            Thread.sleep(2000);
            String Newprogramname = driver.findElement(By.linkText("Program1")).getText();
            String createdprogramName = "Program1";
            Assert.assertEquals(Newprogramname,createdprogramName);
            System.out.println("Program created sucessfully");
        }   
    }

Testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
    <classes>
      <class name="Students.Login"/>
      <class name="Students.SkillsAcquisition"/>
    </classes>
  </test> 
</suite>

错误截图。 click to view the image

您需要将 @BeforeTest 更改为 @BeforeMethod。在对 testng.xml 中的 test 标记执行一次测试之前。这就是为什么您的第一个测试通过但第二个测试失败的原因。

另一种选择(仅供参考,但我确定这不是您需要的)是将您的 testng.xml 更改为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test1">
    <classes>
      <class name="Students.Login"/>
    </classes>
  </test> 
  <test thread-count="5" name="Test2">
    <classes>
      <class name="Students.SkillsAcquisition"/>
    </classes>
  </test> 
</suite>

因为在这里你的每个测试都有自己的 test 标签 类,你的测试将会通过。 因为在这里你有自己的 test 标签给你的每个测试类