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

时间:2017-12-06 11:23:19

标签: java selenium selenium-webdriver

我每天至少执行15次测试用例。每15次至少出现一次奇怪的弹出窗口并显示错误。我需要在我的测试用例中验证这个弹出窗口,例如

  1. 如果出现弹出窗口,则测试用例失败。
  2. 捕获弹出窗口中的文本并将其作为失败消息传递
  3. 我按照下面的代码进行操作并没有成功。无论是否出现弹出窗口(即在每次执行中),测试用例都失败,说明:IllegalStateException:failed' Offer未创建'。如果出现弹出错误,则会显示相同的消息,但不会显示弹出窗口中的文本。我想显示两个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脚本

    
    
    <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>
    &#13;
    &#13;
    &#13;

    对于这两种情况,目前显示相同的以下输出。我尝试了各种方法,上面的代码就是我在发布之前所做的最终代码。

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

    请帮我纠正此代码。

2 个答案:

答案 0 :(得分:1)

CheckForError()只询问第一个h4元素。你会想要全套。而不是:

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

你想要:

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

然后使用循环从每个调用getText()并合并到返回的Text。 (对不起,我已经写了一年Java了,所以我不能快速拿出实际的代码)

答案 1 :(得分:0)

以下代码将解决第一个问题&#34; 1。如果出现弹出窗口,则测试用例失败。&#34;

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; 
}

上述代码的输出是,

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