1

I need to automate the following scenario. I have a table on a web page, and you can drag and drop the table rows onto each other to switch their position.

Each table row has inside it multiple table datas that can redirect you to difference parts of the web app. So only by clicking and holding on the first element in the table row, is the drag action possible.

Selenium provides in the action builder the click_and_hold method. Problem is this clicks in middle of the table row and triggers another action that i don't want.

I have tried to click and hold on the first table data element in the row and then drop it in the next row, but it doesn't do anything.

I did it like this:

browser.action.click_and_hold(table_data).move_to(second_table_row).release(table_data).perform

My guess is that i would need to click and hold a certain coordinates, but i don't know how to do that.

I'm automating with ruby, but any valid examples in other languages would help me out as well.

CodeGeass
  • 619
  • 2
  • 7
  • 20

2 Answers2

1

You can use this code in Java to do above stuff:-

Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(table_data)
.moveToElement(second_table_row)
.release(table_data).build();

dragAndDrop.perform();

Or you can use dragAndDrop(x,y) function directly :-

Actions actions = new Actions(webdriver);
WebElement srcElement = webdriver.findElement(By.xpath("source Xpath"));
WebElement targetElement = webdriver.findElement(By.xpath("Target Xpath"));
actions.dragAndDrop(srcElement, targetElement); 
actions.build().perform();

Try the above code in Ruby. Hope this helps you out

avidCoder
  • 440
  • 2
  • 10
  • 28
  • The second is not using x and y coordinates, its using the xpath u provided for the element. Neither of these work as the element is not being released. – CodeGeass Feb 15 '18 at 13:46
0

If you ask there is a library available in ruby named Capybara which is a wrapper for Selenium and can help you with the task you are trying to achieve using drag_to method like :

start = page.find('table_data')
end = page.find('second_table_row')
start.drag_to(end)

Use this for further understanding of library : drag_to method description

anurag0510
  • 763
  • 1
  • 8
  • 17