如何在web页面下做自动化测试?

【30K上岸京东测试岗】全靠了这套python自动化测试全栈测试开发技术入门到精通教程。

一、线程的基础

运行多个线程同时运行几个不同的程序类似,但具有以下优点:
进程内共享多线程与主线程相同的数据空间,如果他们是独立的进程,可以共享信息或互相沟通更容易.
线程有时称为轻量级进程,他们并不需要多大的内存开销,他们关心的不是过程便宜.
一个线程都有一个开始,执行顺序,并得出结论。它有一个指令指针,保持它的上下文内正在运行的跟踪.
(1)、它可以是抢占(中断)
(2)、它可以暂时搁置(又称睡眠),而其他线程正在运行
看一下以下的小案例:

  1. import thread

  2. from time import sleep, ctime

  3. def loop0():

  4. print "loop 0开始时间:",ctime() #第一个函数loop0开始时间

  5. sleep(4) # 休眠4秒

  6. print "loop 0 结束时间:_’,ctime()

  7. def loopl():

  8. print "loop 1 开始时间:",ctime()

  9. sleep(2)

  10. print "loop 1 结束时间:_’,ctime()

  11. def main():

  12. print "程序开始时间:",ctime()

  13. thread.start_new_thread(loop0,()) # 第二个参数是必不可少的,即使loope没有传递参数,仍然要写一个空元组

  14. thread.stant_new_thnead(loopl,())

  15. sleep(6) #这里休眠6秒的原因是确保两个线程己经执行完毕,主线程才接着执行下面的语句

  16. print "程序结束时间:",ctime()

  17. if __name__ == '__main__':

  18. main()

web测试中,不可避免的一个测试就是浏览器兼容性测试,在没有自动化测试前,我们总是苦逼的在一台或多台机器上安装N种浏览器,然后手工在不同的浏览器上验证主业务流程和关键功能模块功能,以检测不同浏览器或不同版本浏览器上,我们的web应用是否可以正常工作。如果我们使用selenium webdriver,那我们就能够自动的在IE、firefox、chrome、等不同浏览器上运行测试用例。为了能在同一台机器上不同浏览器上同时执行测试用例,我们需要多线程技术。下面我们基于python的多线程技术来尝试同时启动多个浏览器进行selenium自动化测试。

  1. #-*- coding:utf-8

  2. from selenium import webdriver

  3. import sys

  4. from time import sleep

  5. from threading import Thread

  6. reload(sys)

  7. sys.setdefaultencoding("utf-8")

  8. def test_baidu_seanch(browsen, url):

  9. driver = None

  10. #可添加更多浏览器支持进来

  11. if browser == "ie":

  12. driver = webdriver.Ie()

  13. elif browser == "finefox":

  14. driver = webdriver.Firefox()

  15. elif browser == "chrome":

  16. driver = webdriver.Chnome()

  17. if driver == None:

  18. exit()

  19. driver.get(url)

  20. sleep(3)

  21. driver.find_element_by_id("xxx").send_keys(u"xxxx")

  22. sleep(3)

  23. driver.find_element_by_id("xxx").click()

  24. sleep(3)

  25. driver.quit()

  26. if __name__ == "__main__":

  27. #浏览器和首页url

  28. data = {

  29. "ie":"http://www.xxx.com",

  30. "firefox": "http: //www.xxx.com",

  31. "chrome":"http://www.xxxx.com"

  32. }

  33. #构建线程

  34. threads =[]

  35. for b, url in data.items():

  36. t = Thread(target=test_baidu_search,angs=(b, url))

  37. threads.append(t)

  38. #启动所有线程

  39. for thr in threads:

  40. thr.start()

二、 多线程进阶学习

threading 高级线程接口

  1. import threading

  2. class MyThnead(threading.Thread):

  3.    def __init__(self, name=None):

  4.    threading.Thread.__init__(self)

  5.   self.name = name

  6.    def run(self):

  7.   print self.name

  8.    def test():

  9.    for i in range(0, 100):

  10. t = MyThread("thread_" + str(i))

  11. t.start()

  12. if __name__ == '__main__':

  13. test()

Lock 线程锁
这里创建实现了一个计数器 count 这个全局变量会被多个线程同时操作,使其能够被顺序相加,需要靠线程锁的帮助。

  1. #-*- encoding: utf-8

  2. import threading

  3. import time

  4. class Test(threading.Thread):

  5. def __init__(self, num):

  6. threading.Thread.—init—(self)

  7. self._run_num = num

  8. def run(self):

  9. global count, mutex

  10. threadname = threading.currentThnead().getName()

  11. for x in nange(int(self._run_num)):

  12. mutex.acquire()

  13. count = count + 1

  14. mutex.release()

  15. print (thneadname, x, count)

  16. time.sleep(l)

  17. if __name__ == '__main__':

  18. global count^ mutex

  19. threads =[]

  20. num = 5

  21. count =0

  22. #创建锁

  23. mutex = threading.Lock()

  24. #创建线程对象

  25. for x in nange(num):

  26. threads.append(Test(10))

  27. #启动线程

  28. for t in threads:

  29. t. start()

  30. #等待子线程结束

  31. for t in threads:

  32. t.join()

Queue队列

  1. #!/usr/bin/env python

  2. import Queue

  3. import threading

  4. import urllib2

  5. import time

  6. hosts = ["http://xxxx.com", "http://xxxxx.com","http://xxxxxx.com","http://xxxxx.com", "http://xxxxx.com"]

  7. queue = Queue.Queue()

  8. class ThreadUrl(thneading.Thread):

  9. ""”Threaded Uni Grab

  10. def __init__(self, queue):

  11. threading.Thread.__init__(self)

  12. self.queue = queue

  13. def run(self):

  14. while True:

  15. #gnabs host from queue

  16. host = self.queue.get()

  17. url = urllib2.urlopen(host)

  18. #gnabs urls of hosts and prints first 1024 bytes of page

  19. uni = urllib2.urlopen(host)

  20. #signals to queue job is done

  21. self.queue.task_done()

  22. start = time.time()

  23. def main():

  24. #spawn a pool of threads, and pass them queue instance

  25. for i in nange(5):

  26. t = ThreadUrl(queue)

  27. t.setDaemon(True)

  28. t.start()

  29. #populate queue with data

  30. for host in hosts:

  31. queue.put(host)

  32. #wait on the queue until everything has been processed

  33. queue.join()

  34. main()

  35. print "Elapsed Time: %s" % (time.time() - start)

三、使用队列与线程

当线程需要共享数据或资源时,线程可能会变得复杂。线程模块提供许多同步原语,包括信号量条件变量,事件和锁。虽然存在这些选项,但它被认为是最佳做法,而是专注于使用队列。队列更容易处理,并且使线程编程更安全,因为它们有效地将资源访问单个线程,并允许更清晰和更可读的设计模式
首先创建一个程序,该程序将按顺序或一个接一个地获取网站的URL,并打印页面的前1024个字节。这是一个经典的例子,可以使用线程更快地完成任务。首先,让我们一起使用这个urllib2 模块来抓住这些页面,然后再使用代码:

  1. import urllib2

  2. import time

  3. hosts = ["http://xxxx.com", "http://xxxxx.com","http://xxxxxx.com","http://xxxxx.com", "http://xxxxx.com"]

  4. start = time.time()

  5. for host in hosts:

  6. url = urllib2.urlopen(host)

  7. print url.read(1024)

  8. print "Elapsed Time: %s" % (time.time() - start)

导入两个模块首先, urllib2模块是什么是繁重的抓住网页。其次,通过调用创建开始时间值 time.time(),然后再次调用它,并减去初始值以确定程序执行多长时间。最后,在查看程序的速度时,“两个半秒”的结果是不可怕的,但是如果您有数百个网页来检索,则考虑到目前的平均值,大概需要50秒。看看如何创建一个线程版本加快速度:

  1. import Queue

  2. import threading

  3. import urllib2

  4. import time

  5. hosts = ["http://xxxx.com", "http://xxxxx.com","http://xxxxxx.com","http://xxxxx.com", "http://xxxxx.com"]

  6. queue = Queue.Queue()

  7. class ThreadUrl(thneading.Thread):

  8. ""”Threaded Uni Grab

  9. def __init__(self, queue):

  10. threading.Thread.__init__(self)

  11. self.queue = queue

  12. def run(self):

  13. while True:

  14. #gnabs host from queue

  15. host = self.queue.get()

  16. url = urllib2.urlopen(host)

  17. #gnabs urls of hosts and prints first 1024 bytes of page

  18. print url.read(1024)

  19. #signals to queue job is done

  20. self.queue.task_done()

  21. def main():

  22. #spawn a pool of threads, and pass them queue instance

  23. for i in nange(5):

  24. t = ThreadUrl(queue)

  25. t.setDaemon(True)

  26. t.start()

  27. for host in hosts:

  28. queue.put(host)

  29. queue.join()

  30. main()

  31. print "Elapsed Time: %s" % (time.time() - start)

上面的案例并不比第一个线程示例复杂得多,这要归功于使用排队模块。这种模式是使用Python的线程的一种非常常见的推荐方式。步骤描述如下:
1. 创建一个实例,Queue.Queue()然后用数据填充它。

2.将填充数据的实例传递到您从继承中创建的线程类threading.Thread。

3.产生一个守护进程池线程。

4. 一次将一个项目拉出队列,并使用线程内的数据,运行方法来完成工作。

5.完成工作后,向queue.task_done()任务完成发送一个信号到队列。

6. 加入队列,这意味着等到队列为空,然后退出主程序。

只是一个关于这种模式的注释:通过将守护进程线程设置为true,它允许主线程或程序退出,如果只有守护进程线程存活。这将创建一种控制程序流程的简单方法,因为您可以在退出之前连接队列,或等到队列为空。具体过程最好在队列模块的文档中描述,如相关主题所示:join()“块直到队列中的所有项目已经被处理完毕,每当一个项目被添加到队列中时,未完成任务的计数就会上升,当消费者线程调用task_done()来指示项目被检索时,所有的工作都是完成的,当未完成任务的计数下降到零时,join()解除阻塞。

总结:

感谢每一个认真阅读我文章的人!!!

作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助。

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

 

          视频文档获取方式:
这份文档和视频资料,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!以上均可以分享,点下方小卡片即可自行领取。

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值