使用Selenium和Python进行跨浏览器测试

时间:2019-10-31 13:57:49

标签: python selenium testing cross-browser

我正在尝试运行此代码以在Chrome和Firefox中执行某些操作,但是当我运行测试运行器时,Chrome启动,并且测试用例在Chrome中失败,然后Firefox打开,并且测试用例在Firefox中工作正常。

我已经尝试过循环和一些不起作用的事情。

这是我的代码:

AAAAAEAA =

1 个答案:

答案 0 :(得分:0)

您应该等待元素,而不要使用显式的sleep

from selenium import webdriver as wd
from selenium.webdriver.support import expected_conditions as EC
import pytest
import time

Chrome=wd.Chrome(executable_path=r"C:\Chrome\chromedriver.exe")
Firefox=wd.Firefox(executable_path=r"C:\geckodriver\geckodriver.exe")

class TestLogin():
    @pytest.fixture()
    def setup1(self):
        browsers = [Chrome, Firefox]
        for i in browsers:
            self.driver = i
            i.get("https://www.python.org")

        yield
        self.driver.quit()

    def test_Python_website(self, setup1):
        wait = WebDriverWait(self.driver, 10)
        downloads = wait.until(EC.element_to_be_clickable(By.ID, "downloads"))
        downloads.click()

注意:您可能需要self.driver.quite(),因为这将关闭窗口并导致浏览器进程也关闭。测试完成后,对self.driver.close()的调用只会关闭该窗口,但将使firefox.exe或chrome.exe进程在内存中运行。