0
0
Selenium Pythontesting~10 mins

Drag and drop in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test automates dragging an element from one place and dropping it onto another. It verifies that the drop action was successful by checking the target element's text.

Test Code - Selenium
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

class DragAndDropTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://jqueryui.com/droppable/')
        self.wait = WebDriverWait(self.driver, 10)

    def test_drag_and_drop(self):
        self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, '.demo-frame')))
        source = self.wait.until(EC.presence_of_element_located((By.ID, 'draggable')))
        target = self.wait.until(EC.presence_of_element_located((By.ID, 'droppable')))

        actions = ActionChains(self.driver)
        actions.drag_and_drop(source, target).perform()

        self.assertEqual(target.text, 'Dropped!')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to load the test page-PASS
2Navigates to 'https://jqueryui.com/droppable/'Page with drag and drop demo loaded-PASS
3Waits for iframe with class 'demo-frame' and switches to itInside iframe containing draggable and droppable elementsiframe is available and switchedPASS
4Finds draggable element by ID 'draggable'Draggable element located on the pageElement with ID 'draggable' is presentPASS
5Finds droppable element by ID 'droppable'Droppable element located on the pageElement with ID 'droppable' is presentPASS
6Performs drag and drop from draggable to droppableDraggable element moved and dropped onto droppable element-PASS
7Checks that droppable element text changed to 'Dropped!'Droppable element text is 'Dropped!' confirming drop successtarget.text == 'Dropped!'PASS
8Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Draggable or droppable element not found or drag and drop action fails
Execution Trace Quiz - 3 Questions
Test your understanding
What is the purpose of switching to the iframe in this test?
ATo refresh the page
BTo access elements inside the iframe for drag and drop
CTo close the iframe before testing
DTo open a new browser tab
Key Result
Always switch to the correct iframe before interacting with elements inside it to avoid element not found errors.