如何处理 python 和 selenium POM 框架中的 SSL 证书错误?

问题描述

我对这一切都很陌生,但我已经制作了一个混合框架(python、selenium、pytest、页面对象模型),用于练习自动化,并使用 SSL 证书以及如何忽略证书错误来解决问题.我正在使用 https://expired.badssl.com 网站对其进行测试

我发现使用的代码是:

    options = self.driver.ChromeOptions()
    options.add_argument('--ignore-certificate-errors')
    self.driver.Chrome(chrome_options=options)

我只是不确定它需要去哪里,如果它甚至是要使用的正确代码。 这是我的 conftest.py 文件中的内容:

from selenium import webdriver
import pytest
from selenium.webdriver.chrome import options


@pytest.fixture()
def setup(browser):
    if browser == 'chrome':
        driver = webdriver.Chrome()
    elif browser == 'firefox':
        driver = webdriver.Firefox()
    else:
        driver = webdriver.Chrome()
    return driver


# have to include --browser (opyion) otherwise will return error
# added else,if no browser is specified,default to chrome


def pytest_addoption(parser):  # this will get the value from the cli/hooks
    parser.addoption("--browser")


@pytest.fixture()
def browser(request):  # this will return the browser to the setup method
    return request.config.getoption("--browser")


####### Pytest HTML reports #######

# hook for adding environment info to html report
def pytest_configure(config):
    config._metadata['Project Name'] = 'Hybrid Framework'
    config._metadata['Module Name'] = 'Customers'
    config._metadata['Tester'] = 'Amar'

# hook for delete/modify environment info to html report
@pytest.mark.optionalhook
def pytest_metadata(metadata):
    metadata.pop("JAVA_HOME",None)
    metadata.pop("Plugins",None) 

我创建了一个“sslcert_page.py”页面对象类和一个“test_sslcert.py”。我试过在测试文件中包含代码,所以它看起来像这样:


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

from pageObjects.SSLCertificate.sslcert_page import SSLCertHandling
from utilities.readProperties import ReadConfig


class Test_sslcertf:
    baseURL = ReadConfig.getApplicationURL()

    # logger = LogGen.loggen()

    def test_1(self,setup):
        # self.logger.info("************* Test_001_Login *************")
        self.driver = setup
        options = self.driver.ChromeOptions()
        options.add_argument('--ignore-certificate-errors')
        self.driver.Chrome(chrome_options=options)
        self.driver.get(self.baseURL)
        self.driver.maximize_window()
        sleep(5)
        self.sslcert = SSLCertHandling
        #self.sslcert.ignoreCert()

但是当我在终端中执行测试时它甚至没有加载页面。

希望对此有更多了解的人可以帮助我。代码在没有设置页面对象模型的情况下完成时很好。谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)