write a testcase to verify the login with Sliding verification code,using python.
使用python编写一个testcase来验证滑动校验码的登录。
This test case uses the Selenium WebDriver to automate the login process and slide the verification code. It then verifies that the user is logged in by checking the welcome message on the page. The setUp method is used to set up the test environment, while the tearDown method is used to clean up after the test is complete. The ActionChains class is used to perform the sliding action on the verification code.
这个测试用例使用Selenium WebDriver来自动化登录过程和滑动校验码。然后,它通过检查页面上的欢迎消息来验证用户是否已登录。setUp方法用于设置测试环境,而tearDown方法用于在测试完成后进行清理。ActionChains类用于在校验码上执行滑动操作。
import unittest
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
class LoginTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get("https://example.com/login")
def test_login_with_sliding_verification_code(self):
# Enter username and password
username = self.driver.find_element_by_id("username")
password = self.driver.find_element_by_id("password")
username.send_keys("testuser")
password.send_keys("testpass")
# Click on login button
login_button = self.driver.find_element_by_id("login-button")
login_button.click()
# Slide the verification code
slider = self.driver.find_element_by_id("slider")
ActionChains(self.driver).drag_and_drop_by_offset(slider, 200, 0).perform()
# Click on verify button
verify_button = self.driver.find_element_by_id("verify-button")
verify_button.click()
# Verify that user is logged in
welcome_message = self.driver.find_element_by_id("welcome-message")
self.assertEqual(welcome_message.text, "Welcome, testuser!")
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
Selenium之动作链(ActionChains)
用selenium做自动化,有时候会遇到需要模拟鼠标操作才能进行的情况,比如单击、双击、点击鼠标右键、拖拽等等。而selenium给我们提供了一个类来处理这类事件——ActionChains。
selenium.webdriver.common.action_chains.ActionChains(driver)
ActionChains的执行原理:
当你调用ActionChains的方法时,并不会立即执行,而是会把所有的操作按顺序存放在一个队列里,当你调用perform()方法时,队列中的时间才会依次执行。
ActionChains类方法列表:
click(on_element=None) ——单击鼠标左键
click_and_hold(on_element=None) ——点击鼠标左键,不松开
context_click(on_element=None) ——点击鼠标右键
double_click(on_element=None) ——双击鼠标左键
drag_and_drop(source, target) ——拖拽到某个元素然后松开
drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开
key_down(value, element=None) ——按下某个键盘上的键
key_up(value, element=None) ——松开某个键
move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标
move_to_element(to_element) ——鼠标移动到某个元素
move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置
perform() ——执行链中的所有动作
release(on_element=None) ——在某个元素位置松开鼠标左键
send_keys(*keys_to_send) ——发送某个键到当前焦点的元素
send_keys_to_element(element, *keys_to_send) ——发送某个键到指定元素