Selenium_python automates cross browser execution of tests (simple multithreading case)

Background:

    Cross-browser testing is a branch of functional testing to verify that web applications work properly on different browsers. Usually, we expect web applications to be usable by our users on any browser. For example, some People like to use IE browser, some people like firefox or some people like Chrome. We expect that the normal use of the system on any browser will attract more users to use it.

Root of the problem:

    The roots that require our cross-browser testing are:

    1. Different web and css styles display different results in different browsers; (ie, which has been a headache for the front-end development, has been with Windows for so many years)

    2. The implementation of javascript in different browsers is different, such as certain post-click actions and post-click return results, etc.;

    3. Some lower version browsers do not support h5;

    4. The problem of picture position and size, and the problem of font size;

    5, div, span... and other label float attribute problems, etc.;

    6. Compatibility issues between browsers and operating systems;

    The above points are slightly different in page style, and in serious cases, some functions will be unavailable;

Solutions:

    How to perform cross-browser testing, manually install multiple browsers to perform manual point-by-point testing, but find that the problem may sometimes be the previous cache, etc., and then close the browser to clear the browsing history and do it again. How to automate this problem? Woolen cloth? We can use Selenium webdriver to run test cases on different browsers (Firefox, IE, Chrome...) to execute tests;

    Next, we try to start multiple browsers for selenium to execute automated tests based on Python's multi-threading technology;

    The specific code is as follows:   

#-*-coding:utf-8-*-
__author__='dong.c'
from selenium import webdriver
import sys
import thread
import threading
from time import sleep
reload(sys)
sys.setdefaultencoding("utf-8")
def test_baidu_search(browser,url):
    driver = None #You
     can customize this to add more browser support 
    if browser == " ie " :
        driver = webdriver.ie()
    elif browser == "firefox":
        driver = webdriver.Firefox()
    elif browser == "chrome":
        driver = webdriver.Chrome()
    if driver == None:
        exit()
    print u " Start [case_001] Baidu search "
    driver.get(url)
    print u " Clear the data in the search, enter the search keyword " 
    driver.find_element_by_id( " kw " ).clear()
    driver.find_element_by_id( " kw " ).send_keys(u " Blog Park " )
     print u " Click the Baidu button to start the test " 
    driver.find_element_by_id( " su " ).click()
    sleep( 3 )
     print u " close the browser, exit webdriver "
    driver.quit()
if __name__ == "__main__":
    #浏览器和首页url
    data = {
        "ie":"http://www.baidu.com",
        "firefox":"http://www.baidu.com",
        "chrome":"http://www.baidu.com"
    }
    #Build and start thread 
    for b, url in data.items():
        t = threading.Thread(target = test_baidu_search,args=(b,url))
        t.start()

 operation result:

    Run the above code, you will find that all three browsers will start to start Baidu search, here is just a demonstration of multi-threading in the simplest core area, you can organize and write in unittest and add some assertions to sort out more suitable for automation As for the test business scenario of selenium, as for how to use selenium to make software compatibility better, there is still more to be explored;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325123518&siteId=291194637