Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #641 - Increased Elements Collection performance #653

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/main/java/com/codeborne/selenide/ElementsCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ protected void waitUntil(CollectionCondition condition, long timeoutMs) {
sleep(collectionsPollingInterval);
}
while (System.currentTimeMillis() - startTime < timeoutMs);

condition.fail(collection, actualElements, lastError, timeoutMs);
}

void sleep(long ms) {
Selenide.sleep(ms);
}
Expand Down Expand Up @@ -193,7 +191,7 @@ public static List<String> texts(Collection<WebElement> elements) {
}

/**
* @deprecated Use method com.codeborne.selenide.ElementsCollection#texts(java.util.Collection)
* @deprecated Use method com.codeborne.selenide.ElementsCollection#texts(java.util.Collection)
* that returns List instead of array
*/
@Deprecated
Expand Down Expand Up @@ -242,7 +240,10 @@ public static String elementsToString(Collection<WebElement> elements) {

@Override
public SelenideElement get(int index) {
return CollectionElement.wrap(collection, index);
if (getActualElements().size() <= index) {
actualElements = collection.getActualElements();
}
return CollectionElement.wrap(collection, getActualElements(), index);
}

/**
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/com/codeborne/selenide/impl/CollectionElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,42 @@
import com.codeborne.selenide.Condition;
import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.ex.ElementNotFound;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;

import java.lang.reflect.Proxy;
import java.util.List;

import static com.codeborne.selenide.Condition.visible;

public class CollectionElement extends WebElementSource {
public static SelenideElement wrap(WebElementsCollection collection, int index) {
public static SelenideElement wrap(WebElementsCollection collection, List<WebElement> actualElements, int index) {
return (SelenideElement) Proxy.newProxyInstance(
collection.getClass().getClassLoader(), new Class<?>[]{SelenideElement.class},
new SelenideElementProxy(new CollectionElement(collection, index)));
new SelenideElementProxy(new CollectionElement(collection, actualElements, index)));
}

private final WebElementsCollection collection;
private List<WebElement> actualElements;
private final int index;

CollectionElement(WebElementsCollection collection, int index) {
CollectionElement(WebElementsCollection collection, List<WebElement> actualElements, int index) {
this.collection = collection;
this.actualElements = actualElements;
this.index = index;
}

@Override
public WebElement getWebElement() {
return collection.getActualElements().get(index);
try {
WebElement el = actualElements.get(index);
el.isEnabled(); // check staleness
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes iteration slower. Every call to el.isEnabled() takes some time (because it sends an http request from webdriver to the browser).

But anyway, this solution is better than it was before. I think we can merge this PR and then think how to optimize this method further.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. It makes it a bit slower. But I couldn't find any other ways to check element staleness without sending a request to browser. 😞


return el;
} catch (StaleElementReferenceException | IndexOutOfBoundsException e) {
actualElements = collection.getActualElements();
return actualElements.get(index);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry maybe I get something wrong.
In https://github.com/codeborne/selenide/pull/653/files#diff-7fb24574c58e442aadf59a781455f466R35 checks element for staleness, but here not. why not? is it correct assumption that isn't happening here again?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if element becomes stale in catch block between actualElements re-evaluating and returning the element, the StaleElementRefferenceException will be caught by SelenideElementProxy. So then proxy will handle retry of calling some method on our element. So I mean that absence of staleness validation is quite okay in catch. It just means that collection is still in progress of loading. All timeout and retrying logics is business of SelenideElementProxy.

Just to explain why do we need this try-catch block.
Actual elements in try block may become stale because at this code-point we don't know what has passed this cached list to us and we don't know whether it is really actual or not.
But anyway we hope that list is actual -> and we try to get element that we need. Then we check whether it is stale or not. If not -> just return it.
But if element from cached list became stale or the index of the element is out of bounds -> it means that real collection in browser has been refreshed since this collection has been cached and we reach catch block because of one of two mentioned exceptions.
So staleness validation definitely should be present in try block. Because it is the only place where we can signalize that our collection of actual elements is stale.
So in catch block we just re-evaluate collection and try to return element that we get. We don't have to add any additional checks here because if smth is wrong -> SelenideElementProxy will do the retry for us.

Whew... I hope my explanation is rather clear 😃

Copy link
Member

@BorisOsipov BorisOsipov Dec 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I got you. Thank you for detailed explanation. I am not familiar with this code and such try\catch's looks strange for me😃

}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package com.codeborne.selenide.impl;

import com.codeborne.selenide.SelenideElement;
import org.openqa.selenium.WebElement;

import java.util.Iterator;
import java.util.List;

public class SelenideElementIterator implements Iterator<SelenideElement> {
protected final WebElementsCollection collection;
protected List<WebElement> actualElements;
protected int index;

public SelenideElementIterator(WebElementsCollection collection) {
this.collection = collection;
this.actualElements = collection.getActualElements();
}

@Override
public boolean hasNext() {
return collection.getActualElements().size() > index;
return actualElements.size() > index || (actualElements = collection.getActualElements()).size() > index;
}

@Override
public SelenideElement next() {
return CollectionElement.wrap(collection, index++);
return CollectionElement.wrap(collection, actualElements, index++);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public boolean hasPrevious() {

@Override
public SelenideElement previous() {
return CollectionElement.wrap(collection, --index);
return CollectionElement.wrap(collection, actualElements, --index);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,24 @@ public void testWrap() {
when(mockedWebElement.isDisplayed()).thenReturn(true);
when(mockedWebElement.getText()).thenReturn("selenide");

WebElementsCollection collection =
new WebElementsCollectionWrapper(singletonList(mockedWebElement));
SelenideElement selenideElement = CollectionElement.wrap(
new WebElementsCollectionWrapper(singletonList(
mockedWebElement)), 0);
collection, collection.getActualElements(), 0);
assertEquals("<a>selenide</a>", selenideElement.toString());

}

@Test
public void testGetWebElement() {
WebElementsCollection mockedWebElementCollection = mock(WebElementsCollection.class);
WebElement mockedWebElement1 = mock(WebElement.class);
WebElement mockedWebElement2 = mock(WebElement.class);
List<WebElement> listOfMockedElements = asList(mockedWebElement1, mockedWebElement2);
when(mockedWebElementCollection.getActualElements()).thenReturn(listOfMockedElements);
CollectionElement collectionElement = new CollectionElement(mockedWebElementCollection, 1);
CollectionElement collectionElement = new CollectionElement(mockedWebElementCollection,
mockedWebElementCollection.getActualElements(),
1);

assertEquals(mockedWebElement2, collectionElement.getWebElement());
}
Expand All @@ -48,7 +51,9 @@ public void testGetSearchCriteria() {
int index = 1;
WebElementsCollection mockedWebElementCollection = mock(WebElementsCollection.class);
when(mockedWebElementCollection.description()).thenReturn(collectionDescription);
CollectionElement collectionElement = new CollectionElement(mockedWebElementCollection, index);
CollectionElement collectionElement = new CollectionElement(mockedWebElementCollection,
mockedWebElementCollection.getActualElements(),
1);
assertEquals(String.format("%s[%s]", collectionDescription, index), collectionElement.getSearchCriteria());
}

Expand All @@ -58,7 +63,9 @@ public void testToString() {
String collectionDescription = "Collection description";
when(mockedWebElementCollection.description()).thenReturn(collectionDescription);
int index = 1;
CollectionElement collectionElement = new CollectionElement(mockedWebElementCollection, index);
CollectionElement collectionElement = new CollectionElement(mockedWebElementCollection,
mockedWebElementCollection.getActualElements(),
1);
assertEquals(String.format("%s[%s]", collectionDescription, index), collectionElement.toString());

}
Expand All @@ -69,7 +76,9 @@ public void testCreateElementNotFoundErrorWithEmptyCollection() {
String collectionDescription = "Collection description";
when(mockedWebElementCollection.description()).thenReturn(collectionDescription);
int index = 1;
CollectionElement collectionElement = new CollectionElement(mockedWebElementCollection, index);
CollectionElement collectionElement = new CollectionElement(mockedWebElementCollection,
mockedWebElementCollection.getActualElements(),
1);

Condition mockedCollection = mock(Condition.class);
ElementNotFound elementNotFoundError = collectionElement.createElementNotFoundError(mockedCollection, new Error("Error message"));
Expand All @@ -88,7 +97,9 @@ public void testCreateElementNotFoundErrorWithNonEmptyCollection() {
when(mockedWebElementCollection.description()).thenReturn(collectionDescription);
when(mockedWebElementCollection.getActualElements()).thenReturn(asList(mock(WebElement.class)));
int index = 1;
CollectionElement collectionElement = new CollectionElement(mockedWebElementCollection, index);
CollectionElement collectionElement = new CollectionElement(mockedWebElementCollection,
mockedWebElementCollection.getActualElements(),
1);

Condition mockedCollection = mock(Condition.class);
when(mockedCollection.toString()).thenReturn("Reason description");
Expand Down