2

I am a beginner in testing and have a question. How can I correctly use ReadOnlyCollection<IWebElement>if I use attribute FindsBy . My collection is always null after started test. Heres is my code in C#:

        [FindsBy(How = How.Name, Using = "role")]
        public ReadOnlyCollection<IWebElement> radPercentage { get; }

and here is testing web: http://testwisely.com/demo/survey

I want to do something like this: radPercentage[2].Click();

Deyeth
  • 59
  • 2
  • 11

2 Answers2

2

You need to call InitElements before using the collection. Pass the driver and an instance of a class containing the FindsBy properties (in my code "this").

IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://testwisely.com/demo/survey");
PageFactory.InitElements(driver, this);
IWebElement radio = this.radPercentage[2];

The method InitElements is expecting the property to be of type IWebElement or IList of IWebElement

[FindsBy(How = How.Name, Using = "role")]
public IList<IWebElement> radPercentage;
derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • Once the page elements are initialized, can you refresh a particular element by using `PageFactory.InitElements(driver, radPercentage)'` ? – marwaha.ks Jun 29 '16 at 09:45
  • For refreshing the elements you not only need to call the method again but also visiting the page. Not sure if there's a shortcut for that. – derloopkat Jun 29 '16 at 10:26
0

Try this.

public void FindStuff()
{
    var stuff = driver.FindElements(By.Name("role"));
    stuff[2].Click();
}
marwaha.ks
  • 540
  • 1
  • 7
  • 19