首页 > 解决方案 > 打印一条消息,说明元素不存在于 catch 块中,硒

问题描述

这是我的功能文件:

@Done @ProdSmoke @KioskSmoke
Scenario Outline: Add Items to the Cart and click Cancel
  Given Open the application on Tablet device
  When Click on the KeyInItem button
  Then Enter Item Number "<upc>"
  And Click Cancel
  And Item "not added" in the cart
  Examples:
    | upctype | upc|
    | upca    | 8 |

这是我的 stepdef 文件:

 @And("^Item \"([^\"]*)\" in the cart$")
 public void itemAddedInTheCart(String action) throws Exception {
    addItem.verify_itemadded(action);
}

这是我的函数 verify_itemadded(action):

public void verify_itemadded(String action) throws Exception {

    try {
        if (action.equals("added") && element("ItemVerify").isDisplayed()) {
            Assert.assertTrue(element("ItemVerify").isDisplayed());
            logger.info("Item Added");
        }
    }
    catch (Exception ex)
    {
        if(action.equals("not added") && !element("ItemVerify").isDisplayed())
            logger.info("Item not added");
    }
}

即使测试通过,我也无法收到“未添加项目”的消息。

有任何想法吗?

标签: javaseleniumcucumber

解决方案


您需要更新代码如下。如果您遇到任何问题,请告诉我。

try
{
    boolean isItemVerified = element("ItemVerify").isDisplayed());
    if (action.equals("added") && isItemVerified ) 
    {
        logger.info("Item Added");
    }
     Assert.assertTrue(isItemVerified);
 }
 catch(AssertionError ae)
 {
     logger.info("Item not added");
 }

推荐阅读