在做移动端页面自动化测试时,遇到了一个用Icanlendar实现的地区选择框。不同于PC所用的select,这个需要通过控制手指滑动的幅度来选择不同的地区。通过翻阅文档,我发现可以用selenium的TouchAction模拟用户在移动端的操作。TouchAction的文档地址:https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.touch_actions.html
示例代码:
def testWxLeaderSubmissionCreate():
driver.get("http://10.0.11.129/index.php/mLeaderSubmission/create/leaderTaskId/1755/fUserId/22930")
quotation = driver.find_element_by_name("LeaderSubmission[quotation]")
quotation.send_keys("111")
contact = driver.find_element_by_name("LeaderSubmission[contacts]")
contact.send_keys("fanTest002")
phone = driver.find_element_by_name("LeaderSubmission[phone]")
phone.clear()#
phone.send_keys("13699269673")
description = driver.find_element_by_name("LeaderSubmission[description]")
description.send_keys("页面自动化测试")
#driver.execute_script('$(\"input[name=area_info]\").removeAttr(\"readonly\");')
area_info = driver.find_element_by_name("area_info")
area_info.click()
time.sleep(2)
action = webdriver.TouchActions(driver)
#touch action参见:https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.touch_actions.html
source = driver.find_elements_by_class_name('tooth')[1]
print(source.text)
action.scroll_from_element(source,0,100)#滚动ICalendar插件中的选项
action.perform()
time.sleep(3)
action = webdriver.TouchActions(driver)

本文介绍了如何在移动端自动化测试中,利用Selenium的TouchAction模拟用户操作,特别是针对使用Icalendar实现的地区选择框的滑动和点击。通过提供示例代码和TouchAction的方法说明,如double_tap、flick、scroll等,帮助读者理解如何在Python中执行这些操作。同时强调了在进行移动端测试时开启手机模拟器的重要性。
最低0.47元/天 解锁文章

1140

被折叠的 条评论
为什么被折叠?



