Python Selenium Chrome模拟手机浏览器

发表于:2018-2-11 10:19  作者:江诗   来源:博客园

字体: | 上一篇 | 下一篇 |我要投稿 | 推荐标签: 软件测试工具 Python Selenium 自动化测试工具

  在做移动端页面测试时可以利用Chrome mobile emulation 辅助完成页面的适配问题,但是目前手机市场上的型号居多我们也没有办法通过人工的模式一一的去适配,所以这里考虑到通过自动化的模式来模拟,下面介绍两种方式通过selenium调用Chrome mobile emulation来完成自动化测试
  1.通过device name模拟的手机型号,示例代码如下
# -*- coding: utf-8 -*-
# @Time    : 2017/11/30 15:42
# @Author  : Hunk
# @File    : ex81.py
# @Software: PyCharm
import time
from selenium import webdriver
mobileEmulation = {'deviceName': 'Apple iPhone 4'}
options = webdriver.ChromeOptions()
options.add_experimental_option('mobileEmulation', mobileEmulation)
driver = webdriver.Chrome(chrome_options=options)
driver.get('http://m.baidu.com')
time.sleep(3)
driver.close()
  附录:
  deviceName列表
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"
}
  2.设置指定分辨率,示例代码如下:
# -*- coding: utf-8 -*-
# @Time    : 2017/11/30 15:42
# @Author  : Hunk
# @File    : ex81.py
# @Software: PyCharm
import time
from selenium import webdriver
WIDTH = 320  # 宽度
HEIGHT = 640  # 高度
PIXEL_RATIO = 3.0  # 分辨率
mobileEmulation = {"deviceMetrics": {"width": WIDTH, "height": HEIGHT, "pixelRatio": PIXEL_RATIO}}
options = webdriver.ChromeOptions()
options.add_experimental_option('mobileEmulation', mobileEmulation)
driver = webdriver.Chrome(chrome_options=options)
driver.get('http://m.baidu.com')
time.sleep(3)
driver.close()

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。

2018年ISTQB高级认证测试经理在线培训全国首播,限免领取考试大纲>>

评 论

论坛新帖

顶部 底部


建议使用IE 6.0以上浏览器,800×600以上分辨率,法律顾问:上海瀛东律师事务所 张楠律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2018, 沪ICP备05003035号
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪公网安备 31010102002173号

51Testing官方微信

51Testing官方微博

扫一扫 测试知识全知道