【发布时间】:2020-10-23 20:22:55
【问题描述】:
我创建了两个测试类。登录测试类依赖于设置测试类。所以,我在登录测试中使用了dependsOnMethods 注释并扩展了设置类。但是在通过TestNG执行两个测试类时,设置测试执行成功,但在第二个测试类执行时出现空指针异常。
测试类单独执行良好。
第 1 类:设置代码
package com.selenium.tests;
import java.io.IOException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.selenium.pageObjects.SignIn;
import com.selenium.pageObjects.DashBoard;
import com.selenium.pageObjects.Setting;
import com.selenium.resources.base;
import junit.framework.Assert;
public class SettingTest extends base {
public WebDriver driver;
SignIn signIn;
String getTitle;
public static Logger log = LogManager.getLogger(SettingTest.class.getName());
@BeforeTest
public void intializeDriver() throws IOException {
System.out.println("before Test");
driver = browserIntilization();
driver.get(prop.getProperty("Application_url"));
log.info("Application is opened");
}
@Test
public void Dealer_to_set_the_app_configuration() throws IOException, InterruptedException {
System.out.println("1st Test");
Setting sourceLogin = new Setting(driver);
Select market = selectFunction(sourceLogin.getMarket());
market.selectByVisibleText("USA");
log.info("market is selected");
Select language = selectFunction(sourceLogin.getlanguage());
language.selectByVisibleText("English");
log.info("Language is Selected");
signIn = sourceLogin.getSignIn();
Thread.sleep(5000);
getTitle = signIn.getPageTitle();
Assert.assertEquals("ICAR-X", getTitle);
log.info("User redirected to Login Page Successfully.");
}
@AfterTest
public void tearDown() {
System.out.println("After Test Executed");
driver.quit();
}
}
第 2 类:登录代码
package com.selenium.tests;
import org.testng.annotations.Test;
import com.selenium.pageObjects.DashBoard;
import junit.framework.Assert;
public class LoginTest extends SettingTest{
@Test (dependsOnMethods = ("Dealer_to_set_the_app_configuration"))
public void Dealer_enters_valid_credential_to_login_inside_the_app() throws InterruptedException {
System.out.println("2nd test");
signIn.getDealerID("70052");
log.info("DealerID is entered successfully");
signIn.getUserID("x579303");
log.info("userID is entered successfully");
signIn.getPassword("Nissan#2020");
log.info("password is entered successfully");
DashBoard dashboard = signIn.getDashBoard();
Thread.sleep(5000);
dashboard.getPageTitle();
Assert.assertEquals("ICAR-X", getTitle);
log.info("User redirected to Dashboard Page Successfully.");
}
}
错误
[RemoteTestNG] detected TestNG version 7.3.0
ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2
before Test
Starting ChromeDriver 85.0.4183.87 (cd6713ebf92fa1cacc0f1a598df280093af0c5d7-refs/branch-heads/4183@{#1689}) on port 40014
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1602609611.367][WARNING]: This version of ChromeDriver has not been tested with Chrome version 86.
Oct 13, 2020 10:50:13 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
1st Test
1st Test
After Test Executed
java.lang.NullPointerException
at com.selenium.resources.base.getScreenShotPath(base.java:78)
at com.selenium.listeners.Listeners.onTestFailure(Listeners.java:53)
at org.testng.internal.TestListenerHelper.runTestListeners(TestListenerHelper.java:96)
at org.testng.internal.TestInvoker.runTestResultListener(TestInvoker.java:220)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:832)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.util.ArrayList.forEach(Unknown Source)
at org.testng.TestRunner.privateRun(TestRunner.java:764)
at org.testng.TestRunner.run(TestRunner.java:585)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.runSuites(TestNG.java:1069)
at org.testng.TestNG.run(TestNG.java:1037)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
基类代码
package com.selenium.resources;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
public class base {
public WebDriver driver;
public Properties prop;
public WebDriver browserIntilization() throws IOException {
prop = new Properties();
FileInputStream fis = new FileInputStream(
System.getProperty("user.dir") + "\\src\\test\\java\\com\\selenium\\resources\\properties.Properties");
prop.load(fis);
String browsername = prop.getProperty("browser");
if (browsername.equals("safari")) {
driver = new SafariDriver();
} else if (browsername.equals("chrome")) {
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir") + "\\browser_Servers\\chromedriver.exe");
driver = new ChromeDriver();
} else if (browsername.equals("firefox")) {
System.setProperty("webdriver.gecko.driver",
System.getProperty("user.dir") + "\\browser_Servers\\geckodriver.exe");
driver = new FirefoxDriver();
} else {
System.setProperty("webdriver.ie.driver",
System.getProperty("user.dir") + "\\browser_Servers\\IEDriverServer.exe");
driver = new FirefoxDriver();
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
return driver;
}
public Select selectFunction(WebElement element) {
Select select = new Select(element);
return select;
}
public String getScreenShotPath(String testCaseName, WebDriver driver) throws IOException
{
TakesScreenshot ts=(TakesScreenshot) driver;
File source =ts.getScreenshotAs(OutputType.FILE);
String destinationFile = System.getProperty("user.dir")+"\\screenShots\\"+testCaseName+".png";
FileUtils.copyFile(source,new File(destinationFile));
return destinationFile;
}
}
【问题讨论】:
-
你能提出你的基类代码吗?看起来空指针位于基类的 getScreenshotPatch 方法上。
-
我已经添加了基类代码。它单独运行良好,但在一起执行时遇到问题。
-
为什么要把驱动对象传给截图方法?驱动程序是从这个基类初始化的,它是公共的。尝试在此方法上向驱动程序对象添加空检查。我认为,浏览器可能已经关闭,但是您正在尝试截屏,在这种情况下驱动程序将为空。
标签: java selenium-webdriver testng