1

I hope you are having a great day!

I'm wandering something, when using the POM (Page object model) we always create new instances of our classes, like the following simple example:

class LoginPage
   
  def initialize(driver)
    @driver = driver
  end

  def click_button
    @driver.find_element(xpath: "//button[@title='Login']").click
  end
end

# We create a new instance and we click the button

login_page = LoginPage.new(driver)
login_page.click_button 

This makes sense for me, we can create multiple pages if we need to, we can update the state of whatever we need.

However, if we only would have one page open at the time, and we know nothing changes, doesn't it make more sense to take a class method based approach like?:

class LoginPage
  class << self
    def click_button
      @driver.find_element(xpath: "//button[@title='Login']").click
    end
  end
end

# We create a new instance and we click the button

LoginPage.click_button

Please let me know your ideas, and if you have tried this approach before

  • What does this gain you? How are you setting the class-level driver variable (or using a global)? – Dave Newton May 18 '22 at 23:24
  • Well the particular case where this would help me is when I'm using cucumber and I have long steps files, usually I do work arounds to not have to instantiate everything and have a lot of instance variables, but I'm wondering if that is even needed. Regarding the driver, I can set it on a global variable in the before hook of cucumber for example or I can also use a helper module to make the driver available by extending it on the class – Agustin Pequeno May 18 '22 at 23:47
  • 1
    @AgustinPequeno consider your own outcomes if you use a class method: you need to create a helper method or before hook or global variable to set a driver. I wouldn't want to run around trying to figure out where the "@driver" is coming from in your code. When you have an object, hey, "@driver" is right there. "we know nothing changes" - at the moment; in reality requirements always change and it is easier to refactor instance methods than class methods. I'd rather have a "login_page" helper methods that instantiates and sets up a LoginPage object. imho. – Alex May 19 '22 at 04:17

0 Answers0