python自动化--mock、webservice及webdriver模拟手机浏览器
2017-08-27 21:40
661 查看
一、mock实现
自定义一个类,用来模拟未完成部分的开发代码1 class Say(): 2 3 def say_hello(self): 4 pass
自定义返回值
1 import unittest 2 from unittest import mock 3 from d4231 import Say #自定义类的py文件名,say为类名 4 5 class TestSay(unittest.TestCase): 6 def test_say(self): 7 s = Say() 8 #return_value定义方法的返回值 9 s.say_hello=mock.Mock(return_value="还没有开发完成先然后这个吧") 10 #无论s.say_hello()是否传参,mock返回的都是上面已设定的值 11 result = s.say_hello() 12 13 try: 14 self.assertEqual(result,"还没有开发完成先然后这个吧") 15 print("成功,result=",result) 16 except AssertionError as e: 17 print("失败",str(e)) 18 19 # unittest.main #默认执行以test开头的方法 20 ts = TestSay() 21 ts.test_say()
二、webservice实现
Python处理webservice1 from suds.client import Client 2 3 ws_url = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl" 4 Client = Client(ws_url) 5 # print(Client) 6 #如果有多个参数可以以key=value形式编写,qqOnlineWebServiceSoap为服务名,qqCheckOnline为方法名 7 resulte = Client.service["qqOnlineWebServiceSoap"].qqCheckOnline(qqCode="809773385") 8 print(resulte) 9 10 #如果只有一个参数可以这样编写 11 resulte = Client.service.qqCheckOnline("809773386") 12 print(resulte)
三、webdriver模拟手机浏览器
1 from selenium import webdriver 2 from time import sleep 3 #设置 4 mobileEmulation = {'deviceName': 'iPhone 6'} 5 options = webdriver.ChromeOptions() 6 options.add_experimental_option('mobileEmulation', mobileEmulation) 7 #启动driver 8 # driver=webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options) 9 driver=webdriver.Chrome(chrome_options=options) 10 #访问百度wap页 11 driver.get('http://m.baidu.com') 12 sleep(3) 13 driver.quit()
相关文章推荐
- Python 自动化脚本支持 Firefox 浏览器 官方Webdriver -- Geckodriver
- 轻松自动化---selenium-webdriver(python) (二)---打印URL,设置浏览器固定宽前进后退等
- Selenium-Webdriver(python) (五) --- 电脑模拟手机访问手机版网页
- python模拟浏览器webdriver登陆网站后抓取页面并输出
- selenium+python学习--webdriver API学习之浏览器
- Python+Selenium WebDriver API:浏览器及元素的常用函数及变量整理总结
- 轻松自动化---selenium-webdriver(python) (六)
- python+chrome+Selenium模拟手机浏览器
- python selenium-webdriver 常用浏览器的操作 (十)
- webdriver基础python版(一)简单的浏览器操作
- 轻松自动化---selenium-webdriver(python) (十一)
- 轻松自动化---selenium-webdriver(python) (九)
- python 与 webdriver结合打开不同浏览器
- Python操作浏览器 WebDriver
- 轻松自动化---selenium-webdriver(python) (十一)
- webdriver+python 对三大浏览器的支持
- Python Webdriver 重新使用已经打开的浏览器实例
- webdriver for python模拟键盘操作
- 轻松自动化---selenium-webdriver(python) (一)
- 轻松自动化---selenium-webdriver(python) (十二)