-
Notifications
You must be signed in to change notification settings - Fork 575
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
return el; | ||
} catch (StaleElementReferenceException | IndexOutOfBoundsException e) { | ||
actualElements = collection.getActualElements(); | ||
return actualElements.get(index); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry maybe I get something wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if element becomes stale in catch block between Just to explain why do we need this try-catch block. Whew... I hope my explanation is rather clear 😃 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. 😞