我需要为场景执行拖放操作,我怎样才能使用Page-Object实现此目的。
我需要点击一个元素(如选项中的按钮)并将其放在文本区域。我已经找到了解决方案,但我无法找到。请帮助解决此问题。感谢
答案 0 :(得分:2)
Watir有更简单的案例:
drag_and_drop_by - 按给定的偏移量拖放此元素。
browser.div(:id => "draggable").drag_and_drop_by 100, -200
drag_and_drop_on - 将此元素拖放到另一个元素实例上。
a = browser.div(:id => "draggable")
b = browser.div(:id => "droppable")
a.drag_and_drop_on b
来源:http://rubydoc.info/gems/watir-webdriver/Watir/Element#drag_and_drop_by-instance_method
答案 1 :(得分:1)
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();
browser.div(:text=>"from_div").wd.drag_and_drop_on(browser.div(:text=>"to_div").wd)
1)drag_and_drop_helper.js(https://gist.github.com/2362544)到你的测试/助手目录
2)创建一个新方法:
def drag_and_drop(source,target)
js_filepath=File.dirname(__FILE__)+"/drag_and_drop_helper.js"
js_file= File.new(js_filepath,"r")
java_script=""
while (line=js_file.gets)
java_script+=line
end
js_file.close
@driver.execute_script(java_script+"$('#{source}').simulateDragDrop({ dropTarget: '#{target}'});")
rescue Exception => e
puts "ERROR :" + e.to_s
end
希望有所帮助
答案 2 :(得分:0)
从Justin提出的尚未实现的评论开始,您可以在以下内容中修补Page-object gem:
module PageObject
module Elements
class Element
def drag_and_drop_on(other)
element.drag_and_drop_on other.element
end
end
end
end
这至少会在watir-webdriver gem中执行drag_and_drop_on方法而不会有弃用警告。
答案 3 :(得分:0)
以下是在jqueryui.com/droppable/页面上使用带有Cucumber,Ruby,PageObject Selenium WebDriver框架的PageObject的“拖放”示例:
#
# Feature file with Gherkin scenario
#
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