简体   繁体   中英

What to do when an exception occurs in Selenium WebDriver?

I know this question must have been asked many times by now but I could not found what I am looking for. Maybe anyone can help me here.

When an selenium test encounters an exception due to any reason be it Timeout, element not found, element not visible, etc., then how should we handle it. I know we should not explicitly Fail it. Should we SKIP that test or we should handle it in some other manner?

Well there are multiple reasons for encountered exceptions. Its all depends on your code. Generally exception it self says some kind of lines by you can identify that where is the problem. As you mentioned about element not found - For that It would be happen that your page not loaded properly and and your selenium command tried to find out the element on the page hence exception encountered. Timeout - Many a times it happens that you are your selenium command wait for action to perform but locators unable to locate that element and after the defined time it displayed as Timeout exception.

Can you please elaborate in which condition you are getting which exception , that would be helpful to get accurate answer.

In my opinion when an exception occurs in one of our tests, we should log it in the best possible way. This means, add full stack trace, time, current URL, screenshot and possibly video of the test.

In my team, we are using attributes for every test class or test to mark if the test should create a picture or save a video on failure. Of course, these are not built in feature in Web Driver.

You can check my logger solution for our automation and its integration with Jenkins- http://automatetheplanet.com/output-mstest-tests-logs-jenkins-console-log/

Another approach that we are using- marking the failed tests and retry them. If they fail again- this means that a new bug has occurred almost for sure.

  1. log your exception, that can be done using proper logger tools,

    log4j2 has become like standard in Java. http://logging.apache.org/log4j/2.x/

    if you are using a good logger, it will print required details along with exception details in log4j2, you can print stacktrace to logs using

      try{ ... } catch(Exception e){ logger.catching(e); } 
  2. To fail your test explicitly or not will depend.
    mostly it will be taken care if you have specific verifications at the end of the each testcase. But without logging each exception, it will be difficult to identify root cause for the failure. You can not handle every exception and some exceptions can be ignored, so it is not advisable to fail test explicitly on each exception catch.

Mohit...This is in response to your comment... "Lets take example for element not found exception in Selenium. In my framework which is based on Page Object pattern, I add try catch statement in my test file which is calls a method from Page Object (method in Page Object is interacting with the Web Element and this Web Element is not found at the time of interaction). How can I handle this in my Test file? I want to mention the name of the class (in which exception occurs which is Page Object in this case) after catching the exception in Test method"

By calling method from a test,If you are trying to find & manipulate a element in a page, handle it in page.

you can use assertion to verify the actual result after the action.

You can log the exception once it occurs in page during a call from the test, it will capture all details.

For Example: LoginPage

public FlightFinderPage doLogin(String uname, String pwd){
          try{
                username.sendKeys(uname);
                password.sendKeys(pwd);
                login.submit();         
            }catch(Exception e){
                TestUtil.takeScreenShot("LoginElement");
                APP_LOGS.warn("Element not found",e);           
            }   
                return PageFactory.initElements(driver, FlightFinderPage.class);    
}

LoginTest

FlightFinderPage ffPage = lp.doLogin("test", "test");
Assert.assertEquals(ffPage.getTitleFFPage(),"Find a Flight: Mercury Tours:");

I agree that there are many ways of doing this. this is my personal opinion...any comments welcome...

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