java.lang.NullPointer异常(TestNG)
我有一个“BaseTest”类,其中包含与web驱动程序相关的代码以及所有BeforeTest和AfterTest方法。我在'Login'文件和'skillsaquisition'文件中扩展了这个方法,但是只有登录文件运行良好,我在第二个文件中得到了NullPointer异常。我不知道我做错了什么 我对Selenium和testng是新手,因此非常感谢您的帮助java.lang.NullPointer异常(TestNG),java,selenium,selenium-webdriver,testng,Java,Selenium,Selenium Webdriver,Testng,我有一个“BaseTest”类,其中包含与web驱动程序相关的代码以及所有BeforeTest和AfterTest方法。我在'Login'文件和'skillsaquisition'文件中扩展了这个方法,但是只有登录文件运行良好,我在第二个文件中得到了NullPointer异常。我不知道我做错了什么 我对Selenium和testng是新手,因此非常感谢您的帮助 BaseTest.java package Students; import org.openqa.selenium.Alert; i
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;
}
}
}
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);
}
}
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");
}
}
Login.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;
}
}
}
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);
}
}
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");
}
}
skillsaquisition.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;
}
}
}
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);
}
}
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>
错误的屏幕截图。
您需要将
@BeforeTest
更改为@beforethod
。在对testng.xml
中的test
标记执行一次测试之前。这就是为什么您的第一个测试通过了,但第二个测试失败了
另一个选项(仅供参考,但我相信这不是您需要的)是将testng.xml
更改为:
因为在这里,每个测试类都有自己的test
标记,所以测试将通过。
因为这里您有自己的
test
标记用于每个测试类别,所以您的驱动程序看起来是空的。从计算技能请求的行数(第33行)来看,这两行中的一行似乎有一个例外`WebDriverWait d=new WebDriverWait(driver,20);d、 直到(被(By.id(“skillaquisitiontab”))拒绝的所有人员的预期条件、可视性)`是的,driver
为空,堆栈跟踪指向WebDriverWait
的构造函数。它不应该从BaseTest.java类中提取驱动程序属性吗?如果没有,请协助我如何解决此错误。我将驱动程序属性添加到此文件中,但它仍然会出错并显示相同的错误消息@SmutjeQuick解决方案-在您的基础上为getDriver()
创建一个方法,该方法的作用是:返回驱动程序代码>-然后,不要直接调用驱动程序,而是调用getDriver。可能还有其他的方法,但由于代码行太多,这很困难。为了获得定制答案的最佳机会,我建议您遵循以下指南:谢谢@RichEdwards的建议。我添加了返回驱动程序的返回类型方法,但它似乎没有起到作用。谢谢@Alexey R.的建议。我尝试了这两种方法,我确实看到web驱动程序按预期工作,但它会为两种方法中的每个测试启动一个新的浏览器。例如,它启动一个浏览器,运行登录测试,然后再次启动另一个浏览器以运行第二个文件。由于第二个文件与登录无关,所以测试只会出错。当然,您需要应用一些设计技巧使代码保持一致。TestNg本身没有给出答案,因为它现在没有任何关于测试逻辑的内容。您可以将一些特定的@beforethod
添加到负责日志的非登录类中。