找到你要的答案

Q:What to do when an exception occurs in Selenium WebDriver?

Q:怎样做是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?

我知道这个问题一定已经问了很多次,但现在我找不到我正在寻找。也许有人能帮我。

当硒测试遇到异常,由于任何原因,它超时,元素没有找到,元素不可见,等等,那么我们应该如何处理它。我知道我们不应该明确地失败。我们应该跳过那个测试,还是应该用其他方式处理它?

answer1: 回答1:

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.

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.

你能详细说明你得到的条件是哪一个例外,这将有助于得到准确的答案。

answer2: 回答2:

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.

在我看来,当一个异常发生在我们的测试,我们应该记录在尽可能最好的方式。这意味着,添加完整的堆栈跟踪,时间,当前网址,截图和可能的视频测试。

在我的团队中,我们使用属性为每个测试类或测试标记,如果测试应该创建一个图片或保存视频失败。当然,这些都不是内置在Web驱动程序中的功能。

你可以我的日志解决方案对于我们自动化及其与詹金斯- http://automatetheplanet.com/output-mstest-tests-logs-jenkins-console-log/一体化检查

我们正在使用的另一种方法-标记失败的测试并重试它们。如果他们再次失败-这意味着一个新的错误几乎肯定发生。

answer3: 回答3:
  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.

  1. 记录你的例外,可以做适当的记录工具的使用,

    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.

answer4: 回答4:

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

Mohit…这是针对你的评论…”让以元件没有发现异常的硒。在基于页面对象模式的框架中,我在我的测试文件中添加了catch语句,它是从页面对象调用方法(页面对象中的方法是与Web元素交互的,在交互时没有找到这个Web元素)。我如何处理我的测试文件?在测试方法中捕获异常之后,我想提及类的名称(在这种情况下发生异常的是页对象)

通过从测试中调用方法,如果您试图查找和操作页中的元素,请在页中处理它。

您可以使用断言来验证动作后的实际结果。

您可以记录异常,一旦它发生在页面在测试过程中调用,它会捕捉所有细节。

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:");

我同意做这件事的方法有很多。这是我个人的意见…欢迎任何评论…

java  selenium  selenium-webdriver  webdriver