1

I am trying to print messages in Selenium when a step is failing. I am not sure which method should I use. My script is in Java and all it does is to open a website and click some stuff. The error messages are not helpful and I want to be able to translate these unhelpful messages to some customized ones by myself.

Which method or code should be used for that?

Termininja
  • 6,620
  • 12
  • 48
  • 49
Zomzomzom
  • 47
  • 10
  • 1
    use jUnit/TestNG Assertions, have a look at http://stackoverflow.com/questions/15926005/how-make-junit-to-print-asserts-and-results – Andrejs Aug 16 '16 at 11:36

2 Answers2

2

Use TestNG/Junit as your test framework and then you can use assertions. I use a lot of soft asserts. Example:

softAssert.assertEquals('Expected','Actual',"Failed because Actual did not match expected");

The third value there is the error message. You can set it to whatever you want. It will report only if failure occurs.

Short of that, you can use some try/catch methods but that's not ideal when there are already tools for the job.

Wes Young
  • 76
  • 7
1

you can simply use if else statement, if you don't want to complex things.

WebElement a = driver.findElement(By.id("android:id/button1"));

                    if(a.isDisplayed())
                    {
                        System.out.println("Element is displayed");

                        a.click();

                        System.out.println("Element is Clicked");
                    }
                   else {
                        System.out.println("Element not present");
                    } 

Hope this will solve your problem

rajan
  • 77
  • 1
  • 7
  • it works for the successful ones .. for the else condition it returns something like "unable to locate element" – Zomzomzom Aug 18 '16 at 07:40
  • 1
    let's say how to change this sentence to make it more meaningful for a normal user :D .. I don't want him to see "unable to locate Element". – Zomzomzom Aug 18 '16 at 07:42