PageObject drag and drop for Ruby
The following is a sample of 'drag and drop' using PageObject with Cucumber, Ruby, PageObject Selenium WebDriver framework, using jqueryui.com/droppable/ page:
#
# Feature file with Gherkin scenario
#
Feature: Drag and Drop test
Scenario: Perform drag-and-drop test
Given I am on drag and drop test page
When I switch to frame with demo-frame class
And I drag left element and drop it on right element
#
# Step definitions file
#
Given("I am on drag and drop test page") do
visit(HomePage)
end
When("I switch to frame with demo-frame class") do
on(HomePage) do |page|
# call switch_to.frame only in one step definition,
# as it will give an error when try to call it in the next step definition
page.browser.switch_to.frame page.browser.find_element(:class, 'demo-frame')
end
end
And("I drag left element and drop it on right element") do
on(HomePage) do |page|
from_el = page.get_from_element
to_el = page.get_to_element
# elements has to be assigned directly from Selenium WebDriver,
# if assigned from the PageObject next line will give an error
page.browser.action.click_and_hold(from_el).perform
page.browser.action.move_to(to_el).perform
page.browser.action.release.perform
sleep 10 # just to see the drag and drop result
end
end
#
# Page file
#
class HomePage
include PageObject
page_url "https://jqueryui.com/droppable/"
def get_from_element
self.browser.find_element(:id, "draggable")
end
def get_to_element
self.browser.find_element(:id, "droppable")
end
end