Selenide 结合了 2 个 ElementsCollections
Selenide combines 2 ElementsCollections
我有 2 个 ElementsCollections
即 oddTableRowItems
和 evenTableRowItems
:
private static ElementsCollection oddTableRowItems() {
return $$(By.className("odd"));
}
private static ElementsCollection evenTableRowItems() {
return $$(By.className("even"));
}
我想合并 2 以便只执行一次 for 循环。它是行项目,只有类名因样式目的而不同,我只能通过类名来识别它们。
这就是我尝试组合它的方式 - 但它不起作用:
ElementsCollection rowElements = evenTableRowItems();
rowElements.addAll(oddTableRowItems());
我得到一个:
java.lang.UnsupportedOperationException
有人知道我怎样才能将 2 ElementsCollections
组合起来吗?
根据 API:
Note that this implementation throws an UnsupportedOperationException unless add(int, E) is overridden.
API 在这里可能会更友好一些。但是通过这种方式,您可以组合两个 ElementsCollection 实例。这里的关键是 WebElementsCollectionWrapper
class.
ElementsCollection evenElements = $$(By.className("even"));
ElementsCollection oddElements = $$(By.className("odd"));
List<SelenideElement> elementsCombined = new ArrayList<>(evenElement);
elementsCombined.addAll(oddElements);
WebElementsCollectionWrapper wrapper = new WebElementsCollectionWrapper(elementsCombined);
ElementsCollection selenideCollectionCombined = new ElementsCollection(wrapper);
所有 add*
方法都按设计抛出 UnsupportedOperationException
。这是因为 ElementsCollections
表示网页上现有网页元素的集合;并且页面元素不能被测试修改。这就是您不能在页面上添加或删除元素的原因。
最简单的方法是一次 select 所有匹配的元素:
$$(".odd,.even").shouldHave(size(10));
稍微长一点的方法是组成一个包含两个集合的新列表:
List<String> newList = new ArrayList<String>();
newList.addAll($$(".odd"));
newList.addAll($$(".even"));
但你的目标对我来说似乎很可疑。您将获得包含 无效订单 的列表。为什么它能有用?为什么需要迭代所有元素?我无法想象一个用例。
您可以试试这个代码。这很好用!
ArrayList<SelenideElement> newList = new ArrayList<SelenideElement>();
newList.addAll(Selenide.$$(By.className("odd"));
newList.addAll(Selenide.$$(By.className("even"));
我有 2 个 ElementsCollections
即 oddTableRowItems
和 evenTableRowItems
:
private static ElementsCollection oddTableRowItems() {
return $$(By.className("odd"));
}
private static ElementsCollection evenTableRowItems() {
return $$(By.className("even"));
}
我想合并 2 以便只执行一次 for 循环。它是行项目,只有类名因样式目的而不同,我只能通过类名来识别它们。
这就是我尝试组合它的方式 - 但它不起作用:
ElementsCollection rowElements = evenTableRowItems();
rowElements.addAll(oddTableRowItems());
我得到一个:
java.lang.UnsupportedOperationException
有人知道我怎样才能将 2 ElementsCollections
组合起来吗?
根据 API:
Note that this implementation throws an UnsupportedOperationException unless add(int, E) is overridden.
API 在这里可能会更友好一些。但是通过这种方式,您可以组合两个 ElementsCollection 实例。这里的关键是 WebElementsCollectionWrapper
class.
ElementsCollection evenElements = $$(By.className("even"));
ElementsCollection oddElements = $$(By.className("odd"));
List<SelenideElement> elementsCombined = new ArrayList<>(evenElement);
elementsCombined.addAll(oddElements);
WebElementsCollectionWrapper wrapper = new WebElementsCollectionWrapper(elementsCombined);
ElementsCollection selenideCollectionCombined = new ElementsCollection(wrapper);
所有 add*
方法都按设计抛出 UnsupportedOperationException
。这是因为 ElementsCollections
表示网页上现有网页元素的集合;并且页面元素不能被测试修改。这就是您不能在页面上添加或删除元素的原因。
最简单的方法是一次 select 所有匹配的元素:
$$(".odd,.even").shouldHave(size(10));
稍微长一点的方法是组成一个包含两个集合的新列表:
List<String> newList = new ArrayList<String>();
newList.addAll($$(".odd"));
newList.addAll($$(".even"));
但你的目标对我来说似乎很可疑。您将获得包含 无效订单 的列表。为什么它能有用?为什么需要迭代所有元素?我无法想象一个用例。
您可以试试这个代码。这很好用!
ArrayList<SelenideElement> newList = new ArrayList<SelenideElement>();
newList.addAll(Selenide.$$(By.className("odd"));
newList.addAll(Selenide.$$(By.className("even"));