Search code examples
pythonseleniumframeworkspytestpageobjects

How do I handle SSL certificate error in python and selenium POM framework?


Im very new to all this, but I've made a hybrid framework (python, selenium, pytest, page object model) for practicing automation with and kind of hit a tough one with SSL certs and how to ignore the certificate error.I'm using the https://expired.badssl.com site to test it with

The code I've found to use is:

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

and I'm just unsure to where it needs to go, if it even is the correct code to use. This is what is in my conftest.py file:

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) 

I've created a 'sslcert_page.py' page object class and a 'test_sslcert.py'. I've tried including the code in the test file, so it looks like this:


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()

But it doesn't even load the page when I execute the test in the terminal.

Hope someone with more knowledge around this can help me out. The code is fine when it is done without a page object model set up. thanks !


Solution

  • caps = webdriver.DesiredCapabilities.CHROME.copy()
    caps['acceptInsecureCerts'] = True
    driver = webdriver.Chrome('./chromedriver', desired_capabilities=caps)
    driver.get(
        "https://untrusted-root.badssl.com/")
    

    you have to use desired capability