0

What is the best way to initialize a variable with the Selenium's FindBy annotation in Kotlin?

Something like

@FindBy(id = "example")
private lateinit var button: WebElement

or

@FindBy(id = "example")
private val button: WebElement? = null

or

@FindBy(id = "example")
private var button: WebElement? = null

or something else?

Note that all the previous methods works perfectly.

ᴜsᴇʀ
  • 1,109
  • 2
  • 9
  • 23

2 Answers2

0

You want late init because if the annotation fails to find it you will have a more understandable exception rather than null pointer exception

cutiko
  • 9,887
  • 3
  • 45
  • 59
-1

The second option likely will not work, because the val is already initialized to null and cannot be changed.

I believe using the lateinit is the way to go in this case. It's meant for this purpose mainly.

Joffrey
  • 32,348
  • 6
  • 68
  • 100
  • It works, I tested it: when using the annotation `FindBy` it simply doesn't matter. Anyway, if you prefer, you can consider the same question but with `var` instead of `val` (I just added it in the main question). – ᴜsᴇʀ Nov 17 '21 at 23:58