"I wrote this GitHub action, it almost works, but it fails genearte the report in run-on windows-latest. How solve this issue?

483 views Asked by At
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
- generated html file: ///D:/a/AutomatedTests/automatedTests/%3Dreport.html - -
================== 27 passed, 1 warning in 906.01s (0:15:06) ==================

Using Run-on: windows-latest running successfully and generated the report.
INTERNALERROR>   File "/home/runner/work/AutomatedTests/automatedTests/conftest.py", line 56, in pytest_runtest_makereport
INTERNALERROR>     _capture_screenshot(file_name)
INTERNALERROR>   File "/home/runner/work/AutomatedTests/automatedTests/conftest.py", line 65, in _capture_screenshot
INTERNALERROR>     driver.get_screenshot_as_file(name)
INTERNALERROR> AttributeError: 'NoneType' object has no attribute 'get_screenshot_as_file'

While using with run-on:ubuntu-latest its getting error as above
import pytest

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)

global driver

url="https://automatedtestcases"        


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

@pytest.fixture()
def setup(request):
    global driver
    browser = request.config.getoption("browser")

    if browser == "chrome":
        from selenium.webdriver.chrome.service import Service
        from webdriver_manager.chrome import ChromeDriverManager
        driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

    elif browser =="firefox":
        from selenium.webdriver.firefox.service import Service as FirefoxService
        from webdriver_manager.firefox import GeckoDriverManager
        driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))

    print("driver is open 1 ",driver)
    driver.get(url)
    driver.maximize_window()
    request.cls.driver = driver
    yield
    driver.close()

@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)
name: Automate_testcases_py

on:
  push:
    branches: [ "Automate_with_GitActions" ]
  pull_request:
    branches: [ "Automate_with_GitActions" ]

permissions:
  contents: read
  

jobs:
  build:

    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v3
    - name: Set up Python 3.9
      uses: actions/setup-python@v3
      with:
        python-version: "3.9"
    - name: Installing the driver
      run: pip install chromedriver-autoinstaller selenium pyvirtualdisplay      
    
    # Needed to install packages if not installed
    - name: Installing packages
      run: |
        python -m pip install --upgrade pip
        pip install pytest
        pip install selenium
        pip install pytest-html
        pip install python-dotenv
        pip install webdriver-manager
      
   
    - name: Test with pytest for report
      run: |
        py.test -v -s --html =report.html

I have written the workflow file for testcases(selenium with python) and generated the html-report in github actions with RUN-ON : windows-latest (Not in ubuntu-latest). Generated file should open in any branch or should bring out of the git server in order to view that file in same repository (or) different repository.

Need a solution for this problem.Can anyone help me on this

Expecting the solution in run-on:windows-latest

If i need to use Run-on: windows-latest. I need to open the generated file into one branch and later it should interact with another repo
(or)
If i need to use Run-on: Ubuntu-latest. I need to solve issue with "Nonetype attribute error".
0

There are 0 answers