您的位置:首页 > 编程语言 > Python开发

使用python selenium TouchAction模拟实现lCalendar滚动和点击效果

2017-11-13 15:37 232 查看
在做移动端页面自动化测试时,遇到了一个用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)#这个一定要重新再绑定一次,因为perform方法执行的时候就把action 给fire了
action.tap(driver.find_element_by_class_name('larea_finish')).perform()
time.sleep(2)

#area_info.send_keys("辽宁省,大连市,中山区")
#driver.execute_script('$("input[name=\'LeaderSubmission[area_id]\']") .attr("type","text");')
#area_id = driver.find_element_by_name("LeaderSubmission[area_id]")
#area_id.send_keys("6,108,1532")
area_info.submit();
print('微信端测试成功')
print(driver.current_url)
testWxLeaderSubmissionCreate()


Icalendar示例图片:



最后提醒大家一点,对于移动端进行页面测试的时候,一定要开网页的手机模拟器,否则一些代码是没办法起作用的哦。

开网页的手机模拟器的代码如下:

import time
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options

mobile_emulation = {
#"deviceName": "Apple iPhone 3GS"
#"deviceName": "Apple iPhone 4"
#"deviceName": "Apple iPhone 5"
#"deviceName": "Apple iPhone 6"
#"deviceName": "Apple iPhone 6 Plus"
#"deviceName": "BlackBerry Z10"
#"deviceName": "BlackBerry Z30"
#"deviceName": "Google Nexus 4"
#"deviceName": "Google Nexus 5"
#"deviceName": "Google Nexus S"
#"deviceName": "HTC Evo, Touch HD, Desire HD, Desire"
#"deviceName": "HTC One X, EVO LTE"
#"deviceName": "HTC Sensation, Evo 3D"
#"deviceName": "LG Optimus 2X, Optimus 3D, Optimus Black"
#"deviceName": "LG Optimus G"
#"deviceName": "LG Optimus LTE, Optimus 4X HD"
#"deviceName": "LG Optimus One"
#"deviceName": "Motorola Defy, Droid, Droid X, Milestone"
#"deviceName": "Motorola Droid 3, Droid 4, Droid Razr, Atrix 4G, Atrix 2"
#"deviceName": "Motorola Droid Razr HD"
#"deviceName": "Nokia C5, C6, C7, N97, N8, X7"
#"deviceName": "Nokia Lumia 7X0, Lumia 8XX, Lumia 900, N800, N810, N900"
#"deviceName": "Samsung Galaxy Note 3"
#"deviceName": "Samsung Galaxy Note II"
#"deviceName": "Samsung Galaxy Note"
#"deviceName": "Samsung Galaxy S III, Galaxy Nexus"
#"deviceName": "Samsung Galaxy S, S II, W"
#"deviceName": "Samsung Galaxy S4"
#"deviceName": "Sony Xperia S, Ion"
#"deviceName": "Sony Xperia Sola, U"
#"deviceName": "Sony Xperia Z, Z1"
#"deviceName": "Amazon Kindle Fire HDX 7″"
#"deviceName": "Amazon Kindle Fire HDX 8.9″"
#"deviceName": "Amazon Kindle Fire (First Generation)"
#"deviceName": "Apple iPad 1 / 2 / iPad Mini"
#"deviceName": "Apple iPad 3 / 4"
#"deviceName": "BlackBerry PlayBook"
#"deviceName": "Google Nexus 10"
#"deviceName": "Google Nexus 7 2"
#"deviceName": "Google Nexus 7"
#"deviceName": "Motorola Xoom, Xyboard"
#"deviceName": "Samsung Galaxy Tab 7.7, 8.9, 10.1"
#"deviceName": "Samsung Galaxy Tab"
#"deviceName": "Notebook with touch"
"deviceName": "iPhone 6"
# Or specify a specific build using the following two arguments
#"deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
#"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
}

# Define a variable to hold all the configurations we want
chrome_options = webdriver.ChromeOptions()

# Add the mobile emulation to the chrome options variable
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

# Create driver, pass it the path to the chromedriver file and the s
c6b2
pecial configurations you want to run
driver = webdriver.Chrome(executable_path="你存放chromedriver.exe的目录绝对路径", chrome_options=chrome_options)


配置好模拟器就开始写测试代码了。这次的页面自动化测试代码编写就到这里完成了。希望对各位小伙伴们能有所帮助。

最后附上TouchAction的英文文档(转载自https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.touch_actions.html,方便看不了的小伙伴):

selenium.webdriver.common.touch_actions

The Touch Actions implementation

class selenium.webdriver.common.touch_actions.TouchActions(driver)

Generate touch actions. Works like ActionChains; actions are stored in the TouchActions object and are fired with perform().

Creates a new TouchActions object.

Args :

driver: The WebDriver instance which performs user actions. It should be with touchscreen enabled.

double_tap(on_element)

Double taps on a given element.

Args : on_element: The element to tap.

flick(xspeed, yspeed)

Flicks, starting anywhere on the screen.

Args :

xspeed: The X speed in pixels per second.

yspeed: The Y speed in pixels per second.

flick_element(on_element, xoffset, yoffset, speed)

Flick starting at on_element, and moving by the xoffset and yoffset with specified speed.

Args :

on_element: Flick will start at center of element.

xoffset: X offset to flick to.

yoffset: Y offset to flick to.

speed: Pixels per second to flick.

long_press(on_element)

Long press on an element.

Args :

on_element: The element to long press.

move(xcoord, ycoord)

Move held tap to specified location.

Args :

xcoord: X Coordinate to move.

ycoord: Y Coordinate to move.

perform()

Performs all stored actions.

release(xcoord, ycoord)

Release previously issued tap ‘and hold’ command at specified location.

Args :

xcoord: X Coordinate to release.

ycoord: Y Coordinate to release.

scroll(xoffset, yoffset)

Touch and scroll, moving by xoffset and yoffset.

Args :

xoffset: X offset to scroll to.

yoffset: Y offset to scroll to.

scroll_from_element(on_element, xoffset, yoffset)

Touch and scroll starting at on_element, moving by xoffset and yoffset.

Args :

on_element: The element where scroll starts.

xoffset: X offset to scroll to.

yoffset: Y offset to scroll to.

tap(on_element)

Taps on a given element.

Args :

on_element: The element to tap.

tap_and_hold(xcoord, ycoord)

Touch down at given coordinates.

Args :

xcoord: X Coordinate to touch down.

ycoord: Y Coordinate to touch down.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息