Selenium Web 驱动程序:如何将 html 元素映射到 Java 对象。
Selenium Web Driver : How to map html elements to Java Object.
作为 Selenium Web 驱动程序学习的一部分,我遇到了一个场景。请告诉我专业的处理方法。
我正在测试一个电子商务应用程序,当我点击“移动”时 link 所有手机都得到 displayed.I 想检查它们是否根据名称和价格排序。所以基本上我需要在结果页面中获取所有元素的名称和价格。
所以我的问题是有什么方法可以将 html 元素映射到 java 值对象?任何 API 已经可以为我做这个映射?类似于 gson api 的东西,用于将 java 对象转换为相应的 Json 表示形式 ?
迪普奈尔
//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*/
要捕获断言并在断言失败后继续测试覆盖断言 class 并创建您自己的 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());
}
}
}
使用 CustomAssertions class
而不是使用断言 class 来验证测试
例如:
//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();
希望这对您有所帮助,如果您有任何疑问,请回来...
作为 Selenium Web 驱动程序学习的一部分,我遇到了一个场景。请告诉我专业的处理方法。
我正在测试一个电子商务应用程序,当我点击“移动”时 link 所有手机都得到 displayed.I 想检查它们是否根据名称和价格排序。所以基本上我需要在结果页面中获取所有元素的名称和价格。
所以我的问题是有什么方法可以将 html 元素映射到 java 值对象?任何 API 已经可以为我做这个映射?类似于 gson api 的东西,用于将 java 对象转换为相应的 Json 表示形式 ?
迪普奈尔
//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*/
要捕获断言并在断言失败后继续测试覆盖断言 class 并创建您自己的 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());
}
}
}
使用 CustomAssertions class
而不是使用断言 class 来验证测试例如:
//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();
希望这对您有所帮助,如果您有任何疑问,请回来...