在做移动端页面自动化测试时,遇到了一个用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)