简体   繁体   中英

running a test script for multiple URLs in multiple browsers (local) in selenium python

I have a test script that I want to be run for multiple URLs on multiple browsers (Chrome and Firefox) locally on my machine. Every browser has to open all the URLs for the test script. I have run the test script for multiple URLs, but I'm confused about how to do it for multiple browsers. I have checked stuff online but all of them doing it remotely. my test script is below:

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

Driver = webdriver.Chrome()

def localitems() :
    local_storage = Driver.execute_script( \
        "var ls = window.localStorage, items = {}; " \
        "for (var i = 0, k; i < ls.length; ++i) " \
        "  items[k = ls.key(i)] = ls.getItem(k);"\
            "return items; ")
    return local_storage;

def sessionitems() :
    session_storage = Driver.execute_script( \
        "var ls = window.sessionStorage, items = {}; " \
        "for (var i = 0, k; i < ls.length; ++i) " \
        "  items[k = ls.key(i)] = ls.getItem(k);"\
            "return items; ")
    return session_storage;

sites = [
    "http://www.github.com",
    "https://tribune.com.pk"
]



 for index, site in enumerate(sites)
        print(index,site)
        Driver.get(site)
        time.sleep(5)
        print('localStorage', localitems())
        print('sessionStorage', sessionitems())
Driver.quit()

If anyone could help me with this, would be thankful.

Create a list with the drivers and then execute your script in the for loop:

drivers = [webdriver.Chrome(), webdriver.Firefox()]

for Driver in drivers:
    def localitems():
        local_storage = Driver.execute_script( \
            "var ls = window.localStorage, items = {}; " \
            "for (var i = 0, k; i < ls.length; ++i) " \
            "  items[k = ls.key(i)] = ls.getItem(k);" \
            "return items; ")
        return local_storage;


    def sessionitems():
        session_storage = Driver.execute_script( \
            "var ls = window.sessionStorage, items = {}; " \
            "for (var i = 0, k; i < ls.length; ++i) " \
            "  items[k = ls.key(i)] = ls.getItem(k);" \
            "return items; ")
        return session_storage;


    sites = [
        "http://www.github.com",
        "https://tribune.com.pk"
    ]

    for index, site in enumerate(sites)
        print(index, site)
        Driver.get(site)
        time.sleep(5)
        print('localStorage', localitems())
        print('sessionStorage', sessionitems())
    Driver.quit()

Also note that there is a convention in Python ( PEP-8 ). According to it, variable name should be lowercase. So it's better to use driver instead of Driver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM