Home  >  Article  >  Backend Development  >  Python multi-threaded Selenium cross-browser testing instructions

Python multi-threaded Selenium cross-browser testing instructions

PHPz
PHPzOriginal
2017-04-02 13:36:131328browse

In web testing, an inevitable test is browser compatibility testing. Before automated testing, we always struggled to install N browsers on one or more machines, and then manually Verify the main business process and key function module functions on different browsers to detect whether our web application can work normally on different browsers or different versions of browsers.

Let’s take a look at how to use python selenium for automated cross-browser testing.

What is cross-browser testing

Cross-browser testing is a branch of functional testing to verify that web applications can work properly on different browsers.

Why cross-browser testing is needed

Normally, we all expect web applications to be used by our users on any browser. For example, some people like to use IE to open the open source Utest web site, but some people prefer Firefox or Chrome.

We hope that our web system can work normally on any browser, so as to attract more users to use it.

The root cause of the need for cross-browser testing is:

The font size does not match in different browsers

The implementation of javascrpit is different

css, html Verification is different

Some browsers or lower versions do not supportHTML5

Page alignment and div size issues

Image position or size issues

Compatibility issues between browsers and operating systems

The above aspects not only affect the layout, but may even cause functions to be unavailable, so we need to conduct cross-browser testing.

How to perform cross-browser testing

If we use selenium webdriver, then we can automatically run test cases on different browsers such as IE, firefox, chrome, etc.

In order to execute test cases simultaneously on different browsers on the same machine, we need multi-threading technology.

Below we try to start multiple browsers at the same time for selenium automated testing based on python's multi-threading technology.

#-*- coding:utf-8 -*-
 
author = u'苦叶子'
 
from selenium import webdriver
import sys
from time import sleep
from threading import Thread
 
reload(sys)
sys.setdefaultencoding("utf-8")
 
def test_baidu_search(browser, url):
  driver = None
  # 你可以自定义这里,添加更多浏览器支持进来
  if browser == "ie":
    driver = webdriver.Ie()
  elif browser == "firefox":
    driver = webdriver.Firefox()
  elif browser == "chrome":
    driver = webdriver.Chrome()
 
  if driver == None:
    exit()
 
  print u"开始[case_0001]百度搜索"
  driver.get(url)
 
  print u"清除搜索中数据,输入搜索关键词"
  driver.find_element_by_id("kw").clear()
  driver.find_element_by_id("kw").send_keys(u"开源优测")
 
  print u"单击 百度一下 按钮 开始搜索"
  driver.find_element_by_id("su").click()
  sleep(3)
 
  print u"关闭浏览器,退出webdriver"
  driver.quit()
 
 
if name == "main":
  # 浏览器和首页url
  data = {
    "ie":"http://www.baidu.com",
    "firefox":"http://www.baidu.com",
    "chrome":"http://www.baidu.com"
    }
 
  # 构建线程
  threads = []
  for b, url in data.items():
    t = Thread(target=test_baidu_search,args=(b,url))
    threads.append(t)
 
  # 启动所有线程
  for thr in threads:
    thr.start()

Run the above code, you will find that all three browsers will start to search on Baidu. Isn’t it interesting? Of course, the above is just a simple demonstration, and more and more practical capabilities need to be explored.

This article initially demonstrates the use of Python multi-threading technology to start multiple browsers for simultaneous Selenium automated testing. Through this example, you should learn more in-depth knowledge and sort out more suitable solutions based on actual business testing. Automated testing business scenarios.

As for how to use selenium more deeply to perform compatibility testing, it remains to be in-depth research and exploration to truly make good use of selenium's features.

The above is the detailed content of Python multi-threaded Selenium cross-browser testing instructions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn