cheezy / page-object

Gem to implement PageObject pattern in watir-webdriver and selenium-webdriver
MIT License
653 stars 220 forks source link

How to make page-object use Selenium driver instead of Watir #481

Open mahula opened 5 years ago

mahula commented 5 years ago

How exactly do I make page-object use a Selenium webdriver when initializing it?

kapoorlakshya commented 5 years ago

Hi @mahula,

That is documented here - https://github.com/cheezy/page-object#creating-your-page-object

If you're using the PageFactory module with Cucumber, you need to use a @browser variable for your webdriver in your Hooks. More info on that is here - https://github.com/cheezy/page-object/wiki/Creating-and-using-page-objects

Hope this helps.

mahula commented 5 years ago

Hi @kapoorlakshya,

thank you for the quick response. My setup does not include PageFActory with Cucumber. I am utilizing Rspec, Selenium Webdriver and Page-Object.

I initialize my page objects the way gprovided in the doc you mention https://github.com/cheezy/page-object#creating-your-page-object:

browser = Selenium::WebDriver.for :firefox my_page_object = MyPageObject.new(browser)

But still, when running a test where the driver cannot locate an element of the declared page objects, the failure information is about Watir timie out caused by Watir trying to find the element:

Failures: Failure/Error: login_page.login_button Watir::Exception::UnknownObjectException: timed out after 10 seconds, waiting for #<Watir::Button: located: false; {:class=>"submit_button", :tag_name=>"button"}> to be located # ./bundle/ruby/2.5.0/gems/page-object-2.2.5/lib/page-object/platforms/watir/page_object.rb:1066:in 'instance_eval' # ./bundle/ruby/2.5.0/gems/watir-6.16.5/lib/watir/elements/element.rb:787:in 'element_call' # ./bundle/ruby/2.5.0/gems/watir-6.16.5/lib/watir/elements/element.rb:145:in 'click' # (eval):1:in 'process_watir_call' # ./bundle/ruby/2.5.0/gems/page-object-2.2.5/lib/page-object/platforms/watir/page_object.rb:1066:in 'instance_eval' # ./bundle/ruby/2.5.0/gems/page-object-2.2.5/lib/page-object/platforms/watir/page_object.rb:1066:in 'process_watir_call' # ./bundle/ruby/2.5.0/gems/page-object-2.2.5/lib/page-object/platforms/watir/page_object.rb:472:in 'click_button_for' # ./bundle/ruby/2.5.0/gems/page-object-2.2.5/lib/page-object/accessors.rb:435:in 'block in button' # ./spec/smoke/01_Smoke_tests_spec.rb:46:in 'block (3 levels) in <top (required)>' # ------------------ # --- Caused by: --- # Watir::Wait::TimeoutError: # timed out after 10 seconds, waiting for true condition on #<Watir::Button: located: false; {:class=>"submit_button", :tag_name=>"button"}> # ./bundle/ruby/2.5.0/gems/watir-6.16.5/lib/watir/wait.rb:46:inuntil'`

And at initialization the module PageObject seems to use Watir anyway: https://github.com/cheezy/page-object/blob/master/lib/page-object.rb (see line 71 in initialize(root, visit=false) and initialize_browser(root) line 76), if I did not misunderstood ...

Is there a way to use page-object only with a Selenium Webdriver?

jkotests commented 5 years ago

@mahula , is there a reason you want to purely use Selenium?

As of version 2 of Page-Object, Watir is used under the covers - regardless of whether you initialize the page-object with a Selenium::WebDriver or a Watir::Browser. This was done to remove a lot of code duplication in the framework.

There was an attempt to make it transparent from the user's perspective - ie that the user could still write their page object methods in Selenium API. However, I think we need some improvements there.

mahula commented 5 years ago

Hi @jkotests,

thank you for the response. Yes, there are reasons wanting to apply the page object modelling approach exclusively with Selenium.

The tool combination of Selenium and RSpec works fine for my web application testing purposes. Given the know how, it provides the level of control over the tool chain needed.

Selenium (in Java) implements the page object modelling approach. Since using RSpec, in my testing setup I apply Selenium's Ruby bindings, which do not include the page object modelling - yet.

So - thanks to @cheezy and everyone else contributing to this project - the page-object library can be utilized in that context, which is great. But, I want to avoid depending on another tool or layer, which might come with pitfalls or bugs, complicating the mentioned working setup.

titusfortner commented 5 years ago

The reason 2.0 uses watir is because it was essentially already implementing a lot of the same functionality, but in a way that wasn't as comprehensive and wasn't able to take advantage of many key features found in watir 6.

Watir uses selenium, while attempting to add additional test logic to optimize testing reliability. If you use the page object API the way cheezy intended it should handle all of the wrapping for you without the implementation itself mattering to you.

One of the disadvantages of using an opinionated DSL is that you don't always have the flexibility to work around it. If there's something in particular you are trying to accomplish, we can show you the right way to do it in the page object gem.