ui自动化 webdriver 封装
时间: 2023-11-14 15:59:52 浏览: 53
WebDriver是一种用于浏览器自动化的工具,它提供了许多API和方法,可以用于实现Web UI的自动化测试。您可以使用WebDriver直接调用各种浏览器,并模拟用户在浏览器中的操作,例如点击、输入文本和验证元素等。通过封装WebDriver,您可以将常用的操作组合成更高级别的方法,以便在测试中重复使用。
WebDriver的封装可以根据个人或团队的需求来进行。一种常见的方法是创建一个基类,其中包含一些常用的操作方法,例如打开浏览器、关闭浏览器、定位元素并操作元素等。然后,您可以创建一个测试类,并继承基类,从而可以直接使用基类中的封装方法,而不需要重复编写相同的代码。
除了基本的封装方法外,您还可以根据需要添加其他功能,例如日志记录、报告生成和异常处理等。
相关问题
ui自动化鼠标悬停封装
在UI自动化中,鼠标悬停是一个常见的操作,通常用于选择省市区下拉框或选择框等场景。在Selenium中,可以使用ActionChains类来实现鼠标悬停的动作。首先,需要导入ActionChains类,代码如下:from selenium.webdriver.common.action_chains import ActionChains。然后,可以使用move_to_element方法来实现鼠标悬停操作。具体的封装代码如下:
def move_to_element(self, ele_info):
try:
# 先定位到目标元素
el = self.find_element(ele_info)
# 鼠标悬停在这个元素上面
action = ActionChains(self.driver)
action.move_to_element(el).perform()
self.logger.info(f'鼠标悬浮元素{ele_info}成功')
except Exception as e:
self.logger.error(f'鼠标悬浮元素{ele_info}失败,报错信息为{e}')
# 抛出异常
raise Exception(f'鼠标悬浮元素{ele_info}失败,报错信息为{e}')
这段代码中,首先通过find_element方法定位到目标元素,然后使用ActionChains类创建一个action对象,调用move_to_element方法将鼠标悬停在目标元素上,最后调用perform方法执行操作。这样就实现了鼠标悬停的封装。
#### 引用[.reference_title]
- *1* *2* [webui自动化测试框架(2)——对基本操作进行封装common](https://blog.csdn.net/weixin_42684559/article/details/119870680)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [python+selenium--UI自动化 pom模式封装,长幅慢更](https://blog.csdn.net/weixin_44765296/article/details/123587835)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
python webui自动化基础操作封装代码样式
以下是一个基础的Web UI自动化操作封装代码样式,使用Python语言和Selenium库:
```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
class WebUI:
def __init__(self, driver_path):
self.driver = webdriver.Chrome(executable_path=driver_path)
def open_url(self, url):
self.driver.get(url)
def find_element(self, locator, timeout=10):
return WebDriverWait(self.driver, timeout).until(presence_of_element_located(locator))
def click(self, locator, timeout=10):
element = self.find_element(locator, timeout)
element.click()
def send_keys(self, locator, keys, timeout=10):
element = self.find_element(locator, timeout)
element.send_keys(keys)
def get_text(self, locator, timeout=10):
element = self.find_element(locator, timeout)
return element.text
def close(self):
self.driver.quit()
```
使用示例:
```python
from selenium.webdriver.common.by import By
# 初始化WebUI对象
web = WebUI(driver_path='chromedriver.exe')
# 打开网页
web.open_url('https://www.google.com')
# 输入关键字
search_box_locator = (By.NAME, 'q')
web.send_keys(search_box_locator, 'Python')
# 点击搜索按钮
search_button_locator = (By.NAME, 'btnK')
web.click(search_button_locator)
# 获取搜索结果数量
result_stats_locator = (By.ID, 'result-stats')
result_stats_text = web.get_text(result_stats_locator)
print('搜索结果数量:', result_stats_text)
# 关闭浏览器
web.close()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)