简体   繁体   English

Selenium WebDriver中发生异常时该怎么办?

[英]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. 正如您提到的找不到元素-为此,您的页面可能无法正确加载,并且您的selenium命令试图找出页面上的元素,因此遇到异常。 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. 超时-很多时候,您是selenium命令,等待执行操作,但是定位器无法找到该元素,并且在定义的时间之后,它显示为超时异常。

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. 这意味着添加完整的堆栈跟踪,时间,当前URL,屏幕截图以及可能的测试视频。

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. 当然,这些不是Web驱动程序中的内置功能。

You can check my logger solution for our automation and its integration with Jenkins- http://automatetheplanet.com/output-mstest-tests-logs-jenkins-console-log/ 您可以检查我的记录仪解决方案以了解我们的自动化及其与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. log4j2已变得像Java中的标准。 http://logging.apache.org/log4j/2.x/ 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 如果您使用的是好的记录器,它将在log4j2中打印所需的详细信息以及异常详细信息,您可以使用以下命令将stacktrace打印到日志中

      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" Mohit ...这是对您的评论的回应... “让我们以Selenium中未发现异常的元素为例。在基于Page Object模式的框架中,我在测试文件中添加try catch语句,这称为Page Object中的方法(Page Object中的方法正在与Web元素交互,并且在交互时找不到此Web元素)。如何在我的Test文件中处理此问题?我想提及类的名称(在在Test方法中捕获异常后发生哪个异常(在这种情况下为Page Object)

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 例如: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... 这是我的个人看法...欢迎任何评论...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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