同时运行多个Selenium测试会产生随机故障。我们正在使用Firefox作为浏览器/驱动程序,并使用Selenium Python作为测试套件。
为重现此问题,我们使用以下脚本:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import unittest, time, re
WAIT=10
class UntitledTestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Remote(
desired_capabilities=webdriver.DesiredCapabilities.FIREFOX,
command_executor='http://<IP>:4444/wd/hub'
)
self.accept_next_alert = True
def test_untitled_test_case(self):
driver = self.driver
driver.get("https://www.smashingmagazine.com/")
self.waitForElementClickable(xpath="//ul[@id='js-main-nav__list']/li/a/figure/figcaption/h2/span", msg="1").click()
self.waitForElementClickable(text="Everything You Need To Know About Alignment In Flexbox", msg="2").click()
self.waitForElementClickable(xpath="//ul[@id='js-main-nav__list']/li[4]/a/figure/figcaption/h2/span", msg="3").click()
self.waitForElementClickable(xpath="//div[@id='switches']/div/div[2]/label/span", msg="4").click()
self.waitForElementClickable(xpath="//div[@id='switches']/div/div/label/span", msg="5").click()
self.waitForElementClickable(id_l="js-job-search-input", msg="6").click()
self.waitForElementClickable(id_l="js-job-search-input", msg="7").clear()
self.waitForElementClickable(id_l="js-job-search-input", msg="8").send_keys("deve")
self.waitForElementClickable(id_l="js-job-search-form", msg="9").submit()
def tearDown(self):
self.driver.quit()
def waitForElementClickable(self, text=None, css=None, xpath=None, name=None, id_l=None, msg=None):
if(text!=None):
locator = (By.PARTIAL_LINK_TEXT, text)
elif(css != None):
locator = (By.CSS_SELECTOR, css)
elif(xpath!=None):
locator = (By.XPATH, xpath)
elif(name!=None):
locator = (By.NAME, name)
elif(id_l!=None):
locator = (By.ID, id_l)
error = "Element is not found"
if(msg!=None):
error = msg
link = WebDriverWait(self.driver, WAIT).until(EC.element_to_be_clickable(locator), message=error)
return link
if __name__ == "__main__":
unittest.main()
该脚本在环境中本地执行,如下所示:
并行执行脚本:
$ python magazine.py&> magazine1.log&python magazine.py&> magazine2.log&python magazine.py&> magazine3.log&python magazine.py&> magazine4.log&python magazine.py&> magazine5.log&
请注意,用于初始化驱动程序的IP地址是指本地Docker或服务器上的虚拟机: