python自动化--mock、webservice及webdriver模拟手机浏览器

一、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处理webservice

 1 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()
原文地址:https://www.cnblogs.com/hanxiaobei/p/7441551.html