简体   繁体   中英

Display errors in JUnit failure trace

I use Selenium Web Driver in Eclipse with JUnit. I need create simple checking if some text exists on the page - if it is, than I need generate error. I want this error to be displayed in JUnit failure trace. This is my code:

public class Check  {
@Rule
public ErrorCollector errCollector = new ErrorCollector();

@FindBy(tagName="body")
@CacheLookup
private WebElement titl;
@FindBy(partialLinkText="Exception")
@CacheLookup
private WebElement excep;


public void check_false(String t, String r) {
        try {
             String bodyText = titl.getText();
             Assert.assertFalse("Exception found", bodyText.contains(t)); }
         catch (AssertionError e) {
             System.err.println(r + ": " + e.getMessage());
             System.out.println(excep.getText());
             errCollector.addError(e);   
            }    
          }

If I get error, it is displayed in Eclipse consol, but test if shown as without error in JUnit and no exception message is displayed. How can I make checking properly?

I use

AssertJUnit.assertFalse("Exception found", bodyText.contains(t));

It's from http://testng.org/doc/index.html see http://testng.org/javadoc/org/testng/AssertJUnit.html

Within eclipse when my test fails, in the junit window I get the stackstrace. Have never tried collecting the errors. It would throw an

AssertionFailedError

if the test fails but if you catch it, I don't know if the stacktrace will be in the JUnit window.

This might not be quite what you're after, in which case I apologise, but you can simply fail it and supply an optional message as to why.

public void checkText(String actual, String expected){
  try{
    if(!expected.equals(actuals){
      fail("Expected : [ " expected + " ] , actual [" + actual + "]"
  }catch(SomeOtherException soe){
      fail(soe.getMessage());
  }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM