Selenium RC DragAndDrop

Selenium RC drag and drop:

作者: Max.Bai

时间: 2014/01


(1) dragAndDrop - Locates the target element and drags the element by x pixels horizontally and y pixels vertically.

In the IDE this should look like;

Command - dragAndDrop
Target - [ locator of the target element ]
Value - [ (x-pixels),(y-pixels) ]

    - where x-pixels/y-pixels can be negative (left/up respectively) or positive (right/down respectively)

            string dragLocator = @"//a[@id=abc]";
            string dropLocator = @"//td[text()='drop to here']";

			decimal dropX = selenium.GetElementPositionLeft(dropLocator);
			decimal dropY = selenium.GetElementPositionTop(dropLocator);
			decimal endX = selenium.GetElementWidth(dropLocator);
			decimal endY = selenium.GetElementHeight(dropLocator);
			endX = Math.Round(dropX + (endX / 2));
			endY = Math.Round(dropY + (endY / 2));
			String movementsString = "" + endX + "," + endY;

			selenium.DragAndDrop(dragLocator, movementsString);

(2) dragAndDropToObject - Locates the target element and drags the element to the centre pixel location of the destination element

In the IDE this should look like;
Command - dragAndDropToObject Target - [ locator of the target element ] Value - [ locator of the destination element you want to drop it on top of ]

            string dragLocator = @"//a[@id=abc]";
            string dropLocator = @"//td[text()='drop to here']";

            Selenium.DragAndDropToObject(dragLocator, dropLocator);

(3) Use MouseDownAt, MouseMoveAt, MouseUpAt to resolve drag and drop.

            string dragLocator = @"//a[@id=abc]";
            string dropLocator = @"//td[text()='drop to here']";


                selenium.MouseMove(dragLocator);
                Thread.Sleep(3000);
                selenium.MouseDownAt(dragLocator, "0,0");
                Thread.Sleep(3000);
                selenium.MouseMoveAt(dropLocator, "0,0");
                Thread.Sleep(3000);
                selenium.MouseUpAt(dropLocator, "0,0");
                Thread.Sleep(3000); 



### 了解 Selenium 的核心概念 Selenium 是一个开源的自动化测试工具,主要用于 Web 应用程序的测试。它可以模拟用户在浏览器中的操作,例如点击按钮、填写表单、导航页面等。Selenium 支持多种编程语言(如 Python、Java、C# 等),并且可以在各种浏览器(如 Chrome、Firefox、Edge)和操作系统(如 Windows、Linux、macOS)上运行[^2]。 Selenium 主要由以下几个组件组成: - **Selenium WebDriver**:这是目前最常用的组件,用于直接与浏览器进行交互。它提供了丰富的 API 来控制浏览器行为。 - **Selenium IDE**:这是一个 Firefox 和 Chrome 插件,适合初学者使用,可以录制和回放测试脚本。 - **Selenium Grid**:用于分布式执行测试,支持并行运行多个测试任务,提高测试效率。 - **Selenium RC(Remote Control)**:早期版本的核心组件,现已逐渐被 WebDriver 取代。 ### 安装 Selenium 并配置开发环境 #### 1. 安装 Selenium 以 Python 为例,可以通过 `pip` 快速安装 Selenium: ```bash pip install selenium ``` 对于 Java 开发者,可以通过 Maven 添加依赖: ```xml <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.12.0</version> </dependency> ``` #### 2. 下载浏览器驱动 Selenium WebDriver 需要配合浏览器驱动来与浏览器通信。常见的驱动包括: - **ChromeDriver**:适用于 Google Chrome 浏览器。 - **GeckoDriver**:适用于 Mozilla Firefox 浏览器。 - **EdgeDriver**:适用于 Microsoft Edge 浏览器。 下载完成后,将驱动文件路径添加到系统环境变量中,或者在代码中指定路径。 #### 3. 第一个 Selenium 脚本 以下是一个简单的 Python 示例,演示如何使用 Selenium 打开网页并查找元素: ```python from selenium import webdriver from selenium.webdriver.common.by import By # 初始化 Chrome 浏览器 driver = webdriver.Chrome() # 打开百度首页 driver.get("https://www.baidu.com") # 查找搜索框并输入关键词 search_box = driver.find_element(By.NAME, "wd") search_box.send_keys("Selenium 自动化测试") # 提交搜索 search_box.submit() # 关闭浏览器 driver.quit() ``` ### 常见的浏览器操作 #### 1. 浏览器窗口操作 - **最大化窗口**: ```python driver.maximize_window() ``` - **设置窗口大小**: ```python driver.set_window_size(1024, 768) ``` - **获取当前窗口大小**: ```python size = driver.get_window_size() print(f"窗口宽度: {size['width']}, 窗口高度: {size['height']}") ``` - **切换窗口或标签页**: ```python # 获取所有窗口句柄 handles = driver.window_handles # 切换到最后一个打开的窗口 driver.switch_to.window(handles[-1]) ``` #### 2. 页面导航 - **前进与后退**: ```python driver.back() # 返回上一页 driver.forward() # 前进到下一页 ``` - **刷新页面**: ```python driver.refresh() ``` #### 3. 元素定位与操作 Selenium 提供了多种方式来定位页面上的元素,包括 ID、NAME、CLASS_NAME、TAG_NAME、LINK_TEXT、PARTIAL_LINK_TEXT、XPATH 和 CSS_SELECTOR。 - **通过 ID 定位元素**: ```python element = driver.find_element(By.ID, "element_id") ``` - **通过 XPATH 定位元素**: ```python element = driver.find_element(By.XPATH, "//input[@name='username']") ``` - **通过 CSS 选择器定位元素**: ```python element = driver.find_element(By.CSS_SELECTOR, ".class_name") ``` #### 4. 鼠标与键盘事件 - **鼠标悬停**: ```python from selenium.webdriver.common.action_chains import ActionChains element = driver.find_element(By.ID, "menu") actions = ActionChains(driver) actions.move_to_element(element).perform() ``` - **右键点击**: ```python actions.context_click(element).perform() ``` - **拖放操作**: ```python source = driver.find_element(By.ID, "drag") target = driver.find_element(By.ID, "drop") actions.drag_and_drop(source, target).perform() ``` - **键盘输入**: ```python from selenium.webdriver.common.keys import Keys element.send_keys(Keys.ENTER) # 模拟按下回车键 element.send_keys(Keys.CONTROL + 'a') # 模拟 Ctrl+A 全选操作 ``` ### 使用 Selenium 进行回归测试 Selenium 可以结合单元测试框架(如 PyTest 或 JUnit)来编写自动化测试脚本,从而实现高效的回归测试。 #### 示例:使用 PyTest 编写测试用例 ```python import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit() def test_baidu_search(browser): browser.get("https://www.baidu.com") search_box = browser.find_element(By.NAME, "wd") search_box.send_keys("Selenium 测试") search_box.submit() assert "Selenium 测试" in browser.title ``` ### 总结 Selenium 是一个功能强大且灵活的自动化测试工具,广泛应用于 Web 应用程序的功能测试、回归测试以及数据抓取场景。掌握其基本操作和常用技巧,不仅可以提升测试效率,还能帮助开发者更好地理解前端交互逻辑。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值