Selenium, Kotlin and PageFactory - initializing list of webelements

48 Views Asked by At

I'm trying to use PageFactory with Selenium, but for lists of elements I'm running into trouble.

I have this code:

@FindBy(css = "timeline-instances [data-e2e-selector=inst]")
lateinit var timelineInst: List<WebElement>

init { PageFactory.initElements(driver, this) }

When I run this, I get this error:

kotlin.UninitializedPropertyAccessException: lateinit property timelineInst has not been initialized

In the same class, I also have this:

@FindBy(css = "[data-e2e-selector=postponeBtn]")
lateinit var btnWebElement

Which works fine.

Any ideas why it won't initialize a list of WebElements, when a single element works? I've tested by skipping PageFactory and just initializing it directly, and that works fine. So the problem is not with the element itself, at least.

If I replace the lateinit with null initalization, like this:

val timelineInst: List<WebElement>? = null

then it works. But I like using lateinit.

1

There are 1 best solutions below

0
attila-fazekas On

This is because List interface in Kotlin supports only read-only access as explained here.

What you need is MutableList, you can find a working example here.