简体   繁体   English

Selenium-Java-无法捕获在12次测试执行中出现一次的Error弹出窗口

[英]Selenium - Java - Unable to capture the Error popup that appears once in 12 times of test execution

I execute my test cases minimum 15 times per day. 我每天至少执行15次测试用例。 There is a strange popup appears at least once per 15times and shows an Error. 每15次至少出现一次奇怪的弹出窗口,并显示一个错误。 I need to validate this popup in my test case like, 我需要在测试用例中验证此弹出窗口,例如,

  1. If the popup appears, then fail the test case. 如果出现弹出窗口,则测试用例失败。
  2. Capture the text present in the popup and pass it as failure message 捕获弹出窗口中存在的文本并将其作为失败消息传递

I followed the below code and didn't work out. 我遵循以下代码,但没有解决。 Whether the popup appears or not(ie in every execution), the test case is failed stating: IllegalStateException: failure 'Offer is not created'. 不管是否出现弹出窗口(即,在每次执行中),测试用例都将失败,说明:IllegalStateException:失败“未创建要约”。 If the popup error comes the same message is shown but the text from the popup is not shown. 如果出现弹出错误,则会显示相同的消息,但不会显示来自弹出窗口的文本。 I would like to display the text from both h4 tags. 我想显示两个h4标签的文本。

public void CreateOffer() throws Exception {
  try
  {
      if ((FindTheElement("xpath", CreateOfferButton)).isDisplayed())
      {
            WaitAndClickOnElement("xpath", CreateOfferButton);
            WaitTillElementToBeDisplayed("xpath", TenderVersion);
            CheckOfferError();
      }
  }
  catch(Exception e) 
  {
      Assert.fail("Offer is not created");
        //CheckOfferError();
        //WaitTillElementToBeDisplayed("xpath", TenderVersion);
  }

}

public void CheckOfferError() throws Exception  
{   
   String Text=null;
    try   
    {


 if(FindTheElement("xpath",OfferErrorPopUp).isDisplayed() ) 
 {    
    WebElement PopUpError = FindTheElement("xpath",PopupError);

    Text=PopUpError.findElement(By.tagName("h4")).getText();
 } 
    }
    catch(Exception e)     
    { 
            Assert.fail("Offer error window appeared with the text: "+ Text);
    } 
}

HTML script for the error popup, 错误弹出的HTML脚本,

 <div class="modal-dialog-error"> <h3 class="uc">Error</h3> <h4>ValidationError</h4> <h4>invalid offer data: ["offer id does not match the existing incomplete offer"]</h4> <button class="button-action uc"> <!-- react-text: 13 -->OK<!-- /react-text --> </button></div> 

For both the situation, the same below output is shown currently. 对于这两种情况,当前显示以下相同的输出。 I tried various ways and the above code is what I followed as final before posting here. 我尝试了各种方式,上面的代码是我在此处发布之前的最终代码。

------------------------------------------------------------------------------
Create Offer For The Opportunity                                      | FAIL |
IllegalStateException: failure 'Offer is not created'
------------------------------------------------------------------------------

Please help me in correcting this code. 请帮助我更正此代码。

CheckForError() is only asking for the first h4 element. CheckForError()仅询问第一个h4元素。 You will want to get the full set. 您将要获得全套。 Instead of: 代替:

        Text=PopUpError.findElement(By.tagName("h4")).getText();

you'll want to: 您将要:

        allH4s = PopUpError.findElements(By.tagName("h4"));

Then use a loop to call getText() from each of those and consolidate to your returned Text. 然后使用循环从每个循环调用getText()并合并到返回的Text中。 (sorry, I haven't written Java for a year, so I can't come up with the actual code for you quickly) (对不起,我已经一年没有写Java了,所以我不能很快为您提供实际的代码)

The below code will resolve 1st concern "1. If the popup appears, then fail the test case." 下面的代码将解决第一个问题“ 1.如果出现弹出窗口,则测试用例失败。”

public void CreateOffer() throws Exception {
  try
  {
      if ((FindTheElement("xpath", OfferPageData.CreateOfferButton)).isDisplayed())
      {
            WaitAndClickOnElement("xpath", OfferPageData.CreateOfferButton);                
            WaitTillElementToBeDisplayed("xpath", OfferPageData.TenderVersion);
            if((FindTheElement("xpath", OfferPageData.OfferPageName)).isEnabled())
            {
                try
                {
                ClickOnElement("xpath",OfferPageData.OfferPageName);
                }
                catch(Exception e)
                {
                    CheckForError();
                    Assert.fail("Offer page is disabled");
                }
            }
      }
      else
      {
          CheckForError();  
      }
  }
  catch(Exception e) 
  {
      String Error = CheckForError();
      Assert.fail("Offer is not created. Error is: " + Error);
  }

}

public String CheckForError() throws Exception  
{   
  String Text=null;
    try   
    {

            WebElement PopUpError = FindTheElement("xpath",OfferPageData.PopupError);
            Text=PopUpError.findElement(By.tagName("h4")).getText();
        }
    catch(Exception e)     
    { 
            Assert.fail("Offer page is interrupted: ");
    }
    return Text; 
}

Output of the above code is, 上面代码的输出是,

------------------------------------------------------------------------------
Create Offer For The Opportunity                                      | FAIL |
IllegalStateException: failure 'Offer is not created. Error is: ValidationError'
------------------------------------------------------------------------------

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

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