[英]Cross-browser testing with Selenium and Python
I'm trying to run this code to perform some action in Chrome and Firefox, but when I run the test runner Chrome starts and the test cases are failing in Chrome, then Firefox opens and test cases work just fine in Firefox.我正在尝试运行此代码以在 Chrome 和 Firefox 中执行一些操作,但是当我运行测试运行程序 Chrome 启动并且测试用例在 Chrome 中失败时,Firefox 打开并且测试用例在 Z763F7F1AEC350CD1A29C2 中运行良好。
I've tried for loop and a couple of things that didn't work.我已经尝试过 for 循环和一些不起作用的事情。
Here's my code:这是我的代码:
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)
Instead of explicit sleep
's, you should wait for the element:而不是显式
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()
Note: You probably want self.driver.quite()
, as this will close the window and cause the browser process to close down as well.注意:您可能需要
self.driver.quite()
,因为这将关闭 window 并导致浏览器进程也关闭。 The call to self.driver.close()
will only close the window, but will leave the firefox.exe or chrome.exe process running in memory after the test finishes.对
self.driver.close()
的调用只会关闭 window,但会在测试完成后让 firefox.exe 或 chrome.exe 进程在 memory 中运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.