1

I am not an expert in LINQ. Here is what I am trying to do.

A. I am finding the collection of the elements of a page using the selenium webdriver API

private ReadOnlyCollection<IWebElement> ReturnPageElements()
{
    return Driver.FindElements(PageElementSelector);
}

where PageElementSelector is all the html tags(since it's long list, I did not paste it here). I then instantiate this method inside my BaseClass Constructor.

B. I am using the following to find THE target element

public IWebElement FindButtonById(string Id)
{
    return ReturnPageElements().FirstOrDefault(webElement => webElement.TagName ==     "button" && webElement.GetAttribute("id") == Id);
}

and here is the use of this inside my test

public PlansPage ClickDeletePlanButton()
{
    FindButtonById("btnDelete").Click();
    return new PlansPage(Driver);
}

My issue is some of the pages have a lot of elements and LINQ tremendously slow down the test execution. This is a good concept I want to implement which can save me a lot of time and code duplication. Is there any way to increase the performance of this query? I do not have to use LINQ, any solution is appreciated

Saifur
  • 16,081
  • 6
  • 49
  • 73
  • 1
    This is an incredibly inefficient way to perform this particular search. If the ID attribute is unique on the page, `FindElement(By.Id(elementId))` would suffice. If your HTML is malformed and IDs aren't unique, you could use `FindElement(By.CssSelector("button[id='" + elementId + "']"))` – JimEvans Jul 28 '14 at 01:21
  • @JimEvans I guess I couldn't explain my problem well. The toppest two methods will be in my BaseClass which will be inherited into my pageobjects. And, as soon as I instantiate my pageobjects all the elements of that page will be mapped over automatically and, thus I don't have to map all the element individually. And, Yes I will eventually create some overloading to identify elements with linktext, name..etc. This works as it is just the performance is the issue for me since LINQ do an internal for each loop if I am not mistaking – Saifur Jul 28 '14 at 01:35
  • You've missed the point entirely. For the example you've posted, you're far better off constructing a single locator that gives you what you want rather than looping through some list of elements. You asked for a way to improve performance. The right way to improve it is to use appropriate locators. – JimEvans Jul 28 '14 at 02:10
  • 1
    LINQ has absolutely *zero* to do with "slowness". The "slowness" is because you are giving it a tag name which is going to, potentially, grab a hell of a lot of objects. You'd get the same problem trying to do an XPath of `//a` and looping through them all. Use a single locator that targets your specific element so the browser & Selenium do the work *for you*. – Arran Jul 28 '14 at 16:46

0 Answers0