I am back and this time stuck on something pretty simple. I am getting deep into automation with Selenium+TestNG with the base test code in Java, but I am a newb in Java, and not very good at debugging issues in the code itself. Eclipse Intellisense helps a lot, but I am stuck here.
Below is my test class, and though the "Assertions" in the "try-catch" block pass, the test case fails in the @AfterClass method. I have included the Stack Trace below.
Weird thing is, with the TearDown class I have (exactly as below), the test passes if the Assertions fail. Its behaving in an awkwardly reverse manner.
I have deduced that the issue has to do something with the way I am appending the errors to the "verificationErrorsString" and in the way I have my final "IF" block setup in the TearDown class. But I am not able to put a finger on it and say "Eureka".
Please help a fellow tester out.
//TEST CLASS
package com.test.example;
//Bunch of Imports
public class ExampleTest {
public WebDriver driver;
public StringBuffer verificationErrors = new StringBuffer();
@Parameters({"browser"})
@BeforeTest
public void setup(String browser) throws MalformedURLException, InterruptedException {
DesiredCapabilities capability=null;
if(browser.equalsIgnoreCase("firefox")){
System.out.println("firefox");
capability= DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
capability.setPlatform(org.openqa.selenium.Platform.ANY);
}
}
@Test
public void testExampleTest() throws Exception {
//driver login
try {
Assert.assertEquals("assertion1);
Assert.assertEquals("assertion2);
} catch (Error e) {
verificationErrors.append(e.toString());
}
//driver logout
}
@AfterTest
public void tearDown() throws Exception{
driver.close();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString); //This is the line that my Stack Trace refers to.
}
}
}
FAILED CONFIGURATION: @AfterTest tearDown
java.lang.AssertionError: java.lang.AssertionError: expected [true] but found [false] //This is where I have been pounding my head & racking my brains to no avail.
at org.testng.Assert.fail(Assert.java:89)
at com.cntntmgmt.content.ExampleTest.tearDown(ExampleTest.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.TestRunner.afterRun(TestRunner.java:1021)
at org.testng.TestRunner.run(TestRunner.java:621)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1197)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)
at org.testng.TestNG.run(TestNG.java:1030)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)