Selenium Web驱动程序:如何将html元素映射到Java对象。

时间:2015-06-16 03:21:12

标签: selenium webdriver mapping

作为Selenium网络驱动程序学习的一部分,我遇到了一个场景。请让我知道继续进行的专业方法。

我正在测试一个电子商务应用程序,当我点击移动链接时,所有手机都会显示。我想检查它们是否根据名称和价格进行分类。所以基本上我需要得到名字&结果页面中所有元素的价格。

所以我的问题是否有任何方法可以将html元素映射到java值对象?是否有任何API可用于为我执行此映射?类似于gson api的东西,用于将java对象转换为相应的Json表示形式吗?

Deepu Nair

1 个答案:

答案 0 :(得分:0)

//Get all the mobile phones links into a list before sorting

 List<WebElement> mobilelinks=driver.findElements(("locator"));

 Map maps = new LinkedHashMap();//use linked hash map as it preserves the insertion order

 for(int i=0;i<mobilelinks.size();i++){

 //store the name and price as key value pair in map

 maps.put("mobilelinks.get(i).getAttribute('name')","mobilelinks.get(i).getAttribute('price')" );

    }

    /*sort the map based on keys(names) store it in a separate list
      sort the map based on values(prices) store it in a separate list
     */

    /* Using webdriver click the sort by name and compare it with the list which we got after sorting

     and also click sort by prices and compare it with the list*/

捕获断言并在断言失败后继续测试覆盖Assertion类并创建自己的CustomAssertion或使用SoftAssertions

<强> CustomAssertion.java

public class CustomAssertions extends Assertion {

  private Map<AssertionError, IAssert> m_errors = Maps.newLinkedHashMap();

  @Override
  public void executeAssert(IAssert a) {
    try {
      a.doAssert();
    } catch(AssertionError ex) {
      onAssertFailure(a, ex);
      System.out.println(a.getActual());
      System.out.println(ex.getMessage());
      m_errors.put(ex, a);
    }
  }

  public void assertAll() {
    if (! m_errors.isEmpty()) {
      StringBuilder sb = new StringBuilder("The following asserts failed:\n");
      boolean first = true;
      for (Map.Entry<AssertionError, IAssert> ae : m_errors.entrySet()) {
        if (first) {
          first = false;
        } else {
          sb.append(", ");
        }
        sb.append(ae.getKey().getMessage());
      }
      throw new AssertionError(sb.toString());
    }
  }
}

而不是使用Assertions类来验证测试使用CustomAssertions类

例如:

//create an object of CustomAssertions class

CustomAssertions custom_assert=new CustomAssertions();

cust_assert.assertTrue(2<1);

cust_assert.assertEquals("test", "testing");

//and finally after finishing the test in aftersuite method call

cust_assert.assertAll();

希望如果您有任何疑问,请帮助您... ...