• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Selenium+TestNG+Java Test fails on the TearDown method

 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,

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;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.testng.annotations.*;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;


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);
}

driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
driver.navigate().to("http://myWebAppLogin");

}

@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.
}
}
}



//STACK TRACE

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)
 
Ranch Hand
Posts: 460
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You closed the driver before to do the assert.

First you do the assert and than close the driver.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic