【问题标题】:Cross-browser testing with Selenium and Python使用 Selenium 和 Python 进行跨浏览器测试
【发布时间】:2020-02-26 21:46:06
【问题描述】:

我正在尝试运行此代码以在 Chrome 和 Firefox 中执行某些操作,但是当我运行测试运行程序 Chrome 启动并且测试用例在 Chrome 中失败时,Firefox 会打开并且测试用例在 Firefox 中运行良好。

我尝试了 for 循环和一些不起作用的东西。

这是我的代码:

from selenium import webdriver as wd
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")
            time.sleep(3)

        yield
        time.sleep(3)
        self.driver.close()

    def test_Python_website(self,setup1):
        self.driver.find_element_by_id("downloads").click()
        time.sleep(3)

【问题讨论】:

  • 查看您的缩进 - yield 仅在 for 循环之后执行一次。 self.driver 将始终设置为 Firefox。

标签: python selenium testing cross-browser


【解决方案1】:

您应该等待元素,而不是显式的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 进程在内存中运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多