【发布时间】:2020-09-01 14:56:37
【问题描述】:
我有一个类“BaseTest”类,其中包含与 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
【问题讨论】:
-
您的驱动程序似乎为空。从 SkillsAcquisition 的行数(第 33 行)来看,这两行中的一行似乎有异常 ` WebDriverWait d = new WebDriverWait(driver, 20); d.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.id("skillAquistionTab"))); `
-
是的,
driver为空,堆栈跟踪指向WebDriverWait的构造函数。 -
不应该从 BaseTest.java 类中提取驱动程序属性吗?如果没有,请协助我如何解决此错误。我将驱动程序属性添加到此文件中,但它会出现相同的错误消息。 @Smutje
-
快速解决方案 - 在您的基础中为
getDriver()创建一个方法:returns driver;- 然后,不要直接调用驱动程序,而是调用 getDriver。可能还有其他方法,但是有这么多行代码,这很困难。为了获得定制答案的最佳机会,我建议您遵循本指南:stackoverflow.com/help/minimal-reproducible-example -
感谢@RichEdwards 的建议。我添加了返回驱动程序的返回类型方法,但似乎没有完成这项工作。
标签: java selenium selenium-webdriver testng