Giter VIP home page Giter VIP logo

Comments (2)

RonnyPfannschmidt avatar RonnyPfannschmidt commented on May 26, 2024

This looks like a user error, driver is none in the local conftest

Please copy and paste text, screenshots of code and tracebacks are very rude

from pytest.

gonzamol17 avatar gonzamol17 commented on May 26, 2024

Hey Ronny, yes sure:
This is the code of one from my test:

`import time
import pytest
import unittest
import sys
import os
sys.path.append(os.path.join(os.path.dirname(file),"..",".."))
import HtmlTestRunner
from Utils import Utils as Utils
from Utils.BaseClass import BaseClass
from POM.HomePage import HomePage
from POM.AccordionsPage import AccordionsPage

@pytest.mark.usefixtures("test_setup")
class TestAccordions(BaseClass):

def test_Accordions(self):
    log = self.get_Logger()
    driver = self.driver
    hp = HomePage(driver)
    time.sleep(1)
    hp.closeCookiesWindows()
    time.sleep(1)
    driver.execute_script("window.scrollTo(0, 300)")
    time.sleep(1)
    hp.clickBtnAccordions()
    ap = AccordionsPage(driver)
    time.sleep(1)
    ap.selectAccordion()
    time.sleep(1)
    assert ap.getTitleFromAccordion() == "This is an accordion item."
    time.sleep(2)`

This is the code of conftest

`import time
import warnings
import json
import pytest
from Utils import Utils
from selenium.common.exceptions import NoSuchElementException
driver = None

def pytest_addoption(parser):
parser.addoption("--browser", action="store", default="chrome")

@pytest.fixture()
def test_setup(request):
global driver
from selenium import webdriver
browser = request.config.getoption("--browser")
if browser == 'chrome':
driver = webdriver.Chrome("C:\Users\admin\PycharmProjects\Practice-Automation\Drivers\chromedriver.exe")
elif browser == 'firefox':
driver = webdriver.Firefox("C:\Users\admin\PycharmProjects\Practice-Automation\Drivers\geckodriver.exe")
warnings.simplefilter('ignore', ResourceWarning)
#driver = webdriver.Chrome("C:\Users\admin\PycharmProjects\SegundoProyecto\Drivers\chromedriver.exe")
driver.implicitly_wait(10)
driver.maximize_window()
request.cls.driver = driver
driver.get(Utils.URL)
#time.sleep(3)
#driver.switch_to.frame("framelive")
yield
driver.close()
driver.quit()

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""
Extends the PyTest Plugin to take and embed screenshot in html report, whenever test fails.
:param item:
"""
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])

if report.when == 'call' or report.when == "setup":
    xfail = hasattr(report, 'wasxfail')
    if (report.skipped and xfail) or (report.failed and not xfail):
        file_name = report.nodeid.replace("::", "_") + ".png"
        _capture_screenshot(file_name)
        if file_name:
            html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \
                   'onclick="window.open(this.src)" align="right"/></div>' % file_name
            extra.append(pytest_html.extras.html(html))
    report.extra = extra

def _capture_screenshot(name):
#driver.get_screenshot_as_file(name)
driver.get_screenshot_as_file("C:\Users\admin\PycharmProjects\Practice-Automation\Screenshots\"+name)`


this is the content from the BaseClass:

`import inspect
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import logging

#ver si en esta clase base esta anotación de pytest.mark va también, porque ya la estamos usando en la clase

que contiene el test. Por lo tanto dentro de la clase del Test, lo único que hay que hacer es llamar

a esta clase base, para que pueda usar estos métodos heredados

#@pytest.mark.usefixtures("test_setup")
class BaseClass:

def get_Logger(self):
    loggername = inspect.stack()[1][3]
    logger = logging.getLogger(loggername)
    fileHandler = logging.FileHandler("C:\\Users\\admin\\PycharmProjects\\Practice-Automation\\Data\\logfile.log")
    formatter = logging.Formatter("%(asctime)s :%(levelname)s : %(name)s :%(message)s")
    fileHandler.setFormatter(formatter)
    logger.addHandler(fileHandler)
    #logger = logging.FileHandler(filemode='w')
    logger.setLevel(logging.DEBUG)
    return logger`

and the file from the utils file, where i alocate the url:

URL = "https://practice-automation.com/"


And one of the files that i am used on the Page Object Model for this object:

`import time

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

class AccordionsLocators:
arrowAccordion = (By.CSS_SELECTOR, "#post-1261 > div summary")
titleFromAccordion = (By.CSS_SELECTOR, "#post-1261 details > div > p")

class AccordionsPage:

def __init__(self, driver):
    self.driver = driver

def selectAccordion(self):
    return self.driver.find_element(*AccordionsLocators.arrowAccordion).click()

def getTitleFromAccordion(self):
    return self.driver.find_element(*AccordionsLocators.titleFromAccordion).text`

from pytest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.