gpt4 book ai didi

java - 当我使用组时无法运行 testng xml

转载 作者:行者123 更新时间:2023-12-02 02:23:09 34 4
gpt4 key购买 nike

我创建了一个 testng.xml 文件来运行一些标记为冒烟测试(使用组)的测试。现在我尝试将此 xml 文件作为 testng 套件运行,但它无法通过所有测试用例。浏览器甚至打不开。请参阅下面的 xml 文件。

<suite name="Smoke Test" verbose="1">
<parameter name="browser" value="chrome"></parameter>
<test name="Smoke Test">
<groups>
<run>
<include name="smoketest" />
</run>
</groups>

<packages>
<package name="com.banner.tests" />
</packages>

</test>
</suite>

错误消息是:

java.lang.ExceptionInInitializerError
at com.cub.tests.Coupons.addCouponAsSignedInUser(Coupons.java:125)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: java.lang.NullPointerException
at java.io.File.<init>(File.java:277)
at com.relevantcodes.extentreports.Report.setFilePath(Report.java:527)
at com.relevantcodes.extentreports.ExtentReports.<init>(ExtentReports.java:80)
at com.relevantcodes.extentreports.ExtentReports.<init>(ExtentReports.java:375)
at com.common.utils.ExtentManager.getReporter(ExtentManager.java:13)
at com.common.utils.ExtentTestManager.<clinit>(ExtentTestManager.java:11)
... 26 more

下面是我用来设置驱动程序的 SuiteBase 代码:

@BeforeSuite
public void beforeSuiteActivties() {
logger.info("Performing BeforeSuite activities..");
setApplicationProperties();
testURL = getTestURL();
setTestData();
hubURL = getHubAddress();
if(banner.equalsIgnoreCase("MACKENTHUNS")){
updateXML = new UpdateXML();
updateXML.updateTestData(testDataFileName);
}
extentReportPath = System.getProperty("user.dir") + "\\test-output\\extent\\" + banner + "\\extentreport.html";
extent = ExtentManager.getReporter();
}

@AfterSuite
public void afterSuiteActivities() {
extent.close();
}

@Parameters({ "browser" })
@BeforeTest
public void beforeTestActivities(String browserName) {
logger.info("Performing Before Test Activities");
setUpDriver(browserName);
setObjects();
}

@BeforeMethod
public void beforeMethodActivities(Method method) {
logger.info("Performing Before Method Activities");
ExtentTestManager.startTest(method.getName());
/*if (!linkExists(testURL)) {
throw new SkipException("The test link --> " + testURL + " is not working");
}*/
commonPage = new CommonPage(getDriver());
boolean isSignInVisible = false;
isSignInVisible = commonPage.isSignInPresent();
logger.info("Is User Logged Out? " + isSignInVisible);
if (!isSignInVisible) {
try {
commonPage.clickFooterLink(CommonPage.FOOTERLINK_LOGOUT);
CommonPage.waitForPageToBeReady(getDriver());
} catch (Exception exception) {
logger.error("Error occurred while trying to click on Logout link");
}
} else
logger.info("Sign In link is available for login so proceeding with next test method execution");

}

public void setUpDriver(String browserName) {
try {
if (testURL != "" && hubURL != "") {
logger.info("Test Environment -- > " + environment);
logger.info("Validation on URL -- > " + testURL);
logger.info("Hub URL -- > " + hubURL);
driver = new RemoteWebDriver(new URL(hubURL), getBrowserCapabilities(browserName));
setWebDriver(driver);
getDriver().get(testURL);
getDriver().manage().window().maximize();
getDriver().manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
} else {
logger.error("Either test URL or Hub URL is empty... So cannot procced with the test execution");
}
} catch (Exception exception) {
logger.error("Something went wrong while initializing the driver");
exception.printStackTrace();
}
}

@AfterMethod
public void afterMethodActivities(ITestResult result) {
logger.info("Performing After Method Activities");
commonPage = new CommonPage(getDriver());
try {
commonPage.closePopup();
} catch (Exception exception) {
logger.error("Something went wrong while closing the popup --> After Method");
//exception.printStackTrace();
}

if (result.getStatus() == ITestResult.FAILURE) {
ExtentTestManager.getTest().log(LogStatus.FAIL, result.getThrowable());
} else if (result.getStatus() == ITestResult.SKIP) {
ExtentTestManager.getTest().log(LogStatus.SKIP, "Test skipped " + result.getThrowable());
} else {
ExtentTestManager.getTest().log(LogStatus.PASS, "Test passed");
}

if(commonPage.isAlertPresent()){
commonPage.closeAlert();
}

ExtentManager.getReporter().endTest(ExtentTestManager.getTest());
ExtentManager.getReporter().flush();
}

public void setObjects() {
logger.info("Setting the object repository file");
objectFileName = resourceFolder + "\\" + banner + "\\webobjects.properties";
logger.info("Banner - " + banner + "/" + "Repository file - " + objectFileName);
try {
webObjectProperties = new Properties();
fip = new FileInputStream(new File(objectFileName));
webObjectProperties.load(fip);
} catch (Exception exception) {
logger.error("Error occurred while reading the webobjects properties file");
exception.printStackTrace();
}
}

public void setTestData() {
banner = getBanner();
logger.info("Setting the test data file");
if (environment.contains("prod")) {
testDataFileName = resourceFolder + "\\" + banner + "\\prodtestdata.xml";
} else {
testDataFileName = resourceFolder + "\\" + banner + "\\testdata.xml";
}
logger.info("Banner - " + banner + "/" + "TestData file - " + testDataFileName);
}

public static String getBanner() {
if (System.getProperty("banner") == null) {
try {
return properties.getProperty("banner");
} catch (Exception exception) {
logger.error("Error occurred while reading the banner value");
exception.printStackTrace();
}
} else {
return System.getProperty("banner");
}
return "";
}

private static DesiredCapabilities getBrowserCapabilities(String browserType) {
DesiredCapabilities capability;
switch (browserType) {
case "firefox":
System.out.println("Opening firefox driver");
capability = DesiredCapabilities.firefox();
// capability.setCapability("marionette", true);
capability.setBrowserName("firefox");
capability.setVersion("ANY");
capability.setPlatform(Platform.ANY);
return capability;
case "chrome":
System.out.println("Opening chrome driver");
return DesiredCapabilities.chrome();
case "internet explorer":
System.out.println("Opening IE driver");
capability = DesiredCapabilities.internetExplorer();
System.out.println("IE PlatForm : " + capability.getPlatform());
System.out.println("IE Browser Name :" + capability.getBrowserName());
capability.setPlatform(Platform.WINDOWS);
capability.setBrowserName("internet explorer");
capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capability.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
capability.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
return capability;
case "safari":
System.out.println("Opening Safari driver");
capability = DesiredCapabilities.safari();
SafariOptions safariOptions = new SafariOptions();
safariOptions.setUseCleanSession(true);
safariOptions.setUseTechnologyPreview(true);
capability.setCapability(SafariOptions.CAPABILITY, safariOptions);
capability.setPlatform(Platform.MAC);
return capability;
default:
System.out.println("browser : " + browserType + " is invalid, Launching Firefox as browser of choice..");
return DesiredCapabilities.firefox();
}
}

最佳答案

alwaysRun = true 添加到您的 @BeforeSuite/@AfterSuite 中,然后重试。你应该很好。

问题是您的前后套件配置方法不属于任何组,这就是您似乎正在初始化范围报告的地方。

当您按组运行时,TestNG 将仅执行属于该组的那些测试/配置。

为了确保无论选择运行哪个组,如果 TestNG 应始终运行您的配置方法,您应该向它们添加 alwaysRun=true

关于java - 当我使用组时无法运行 testng xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48193957/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com