Coder Social home page Coder Social logo

benweese / python-automation Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 3.0 16.97 MB

This is for practicing, learning, and teaching Python, Selenium, and Pytest

License: MIT License

Python 98.53% Dockerfile 1.47%
python-automation selenium circleci scenarios pytest dockerfile docker pipenv chromedriver test-automation testing-tools test-framework pageobjectmodel requests api-testing star-wars-api benweese

python-automation's Introduction

Python Automation

This project is for practicing Python, Selenium, and Pytest. You can find the link for the template above for creating your own automation. To learn more you can check out the Wiki for this repo where I document what I have learned.

Badges

CircleCI Actions Docker Cloud Build Status

GitHub Quality Gate Status GitHub issues StackShare

Motivation

This is to keep my automation skills sharp.

Notification

This automation is happy path only and does not test for failures. It is an example and learning on how it can done. If you wish to test more thoroughly then I would suggest adding Pytest-BDD and use a Scenario Outline to test many different scenarios including failures.

Tools

Built with

Testing Language

Continuous Integration

Dependency Maintenance

Security

Downloads

Features

With testing our Circle-CI runner will use maven to run our automation scripts in Command line.

Page Being Automated

Code Example

conftest.py

import os
import pytest

from selenium.webdriver import Chrome

@pytest.fixture
def browser():
    driver = Chrome(executable_path=os.getcwd()+'/chromedriver')
    driver.implicitly_wait(10)
    yield driver
    driver.quit()

Page Object

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class FormsPOM(object):
    """
    This is the Page Object Model used in test_forms_Page.py for the filling out forms section
    of Ultimate QA's Automation Exercises.
    """
    URL = 'https://www.ultimateqa.com/filling-out-forms/'

    contact_form_1 = (By.ID, 'et_pb_contact_form_0')
    contact_form_2 = (By.ID, 'et_pb_contact_form_1')

    contact_name_1 = (By.ID, 'et_pb_contact_name_0')
    contact_name_2 = (By.ID, 'et_pb_contact_name_1')

    contact_message_1 = (By.ID, 'et_pb_contact_message_0')
    contact_message_2 = (By.ID, 'et_pb_contact_message_1')

    submit_1 = (By.CSS_SELECTOR, "#et_pb_contact_form_0 button.et_pb_contact_submit")
    submit_2 = (By.CSS_SELECTOR, "#et_pb_contact_form_1 button.et_pb_contact_submit")

    captcha = (By.NAME, 'et_pb_contact_captcha_1')

    sh_tweet = (By.CLASS_NAME, 'share-twitter')
    sh_facebook = (By.CLASS_NAME, 'share-facebook')
    sh_pocket = (By.CLASS_NAME, 'share-pocket')
    sh_linkedin = (By.CLASS_NAME, 'share-linkedin')
    sh_tumblr = (By.CLASS_NAME, 'share-tumblr')

    twitter = (By.CLASS_NAME, 'swp_share_link')
    linkedin = (By.CLASS_NAME, 'swp_linkedin')
    email = (By.CLASS_NAME, 'swp_email')
    tumblr = (By.CLASS_NAME, 'swp_tumblr')
    facebook = (By.CLASS_NAME, 'swp_facebook')

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

    def load(self):
        self.browser.get(self.URL)

    def name_1(self, name):
        name_input = self.browser.find_element(*self.contact_name_1)
        name_input.send_keys(name)

    def get_name_1(self):
        return self.browser.find_element(*self.contact_name_1).get_attribute('value')
    ...
    def captcha_calc(self):
        captcha = self.browser.find_element(*self.captcha)
        cap1 = captcha.get_attribute('data-first_digit')
        cap2 = captcha.get_attribute('data-second_digit')

        answer = int(cap1) + int(cap2)

        captcha.send_keys(str(answer))

Test

import time

from POMS.FormsPOM import formsPOM


def test_form1(browser):
    name = 'John Doe'

    forms_page = FormsPOM(browser)
    forms_page.load()
    forms_page.name_1(name)
    assert forms_page.get_name_1() == name

    message = 'I am Batman'
    forms_page.message_1(message)
    assert forms_page.get_message_1() == message

    forms_page.submit1()

    success_message = 'Form filled out successfully'

    try:
        forms_page.wait_form1(success_message)
    finally:
        assert forms_page.get_form_text1() == success_message
        browser.quit()

API Test

def swapi_films(episode):
	"""
	Gets the films listed in the api.
	:param episode:
	:return: response json
	"""
	response = requests.get(SWAPI_API + 'films/' + str(episode))
	return response


def swapi_film_code(episode):
	"""
	Asserts that a 200 was returned
	:param episode:
	"""
	assert swapi_films(episode).status_code == 200


def swapi_films_episode(name, episode):
	"""
	This checks that all the films are in the response.
	:param name:
	:param episode:
	"""
	assert name.lower() == swapi_films(episode).json()['title'].lower()


def test_episode_1():
	"""
	This runs through episode 1.
	"""
	swapi_film_code(1)
	swapi_films_episode('A New Hope', 1)

Documentation

Credits

Ben Weese

python-automation's People

Contributors

benweese avatar dependabot-preview[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

python-automation's Issues

Fix Submit Buttons CSS Selector

Submit1 = (By.CSS_SELECTOR, "#et_pb_contact_form_0 button.et_pb_contact_submit")
Submit2 = (By.CSS_SELECTOR, "#et_pb_contact_form_1 button.et_pb_contact_submit")

is either not working or the wait is not working as the assert gets back:

AssertionError: assert '5 + 15 =\nSubmit' == 'Success'
+ Success
- 5 + 15 =
- Submit

The wait is giving back a:
TypeError: __init__() takes 3 positional arguments but 4 were given

Python Automation Template

Is your feature request related to a problem? Please describe.
Nope

Describe the solution you'd like
Need to create a Python Automation Template similar to my java automation template.

Describe alternatives you've considered
none

Add to Readme

The readme needs updated to include new documentation

CircleCI issues

When running in local Pipenv on multiple computers we get the following Results:
11 passed in 144.62 seconds

When running the same thing in CircleCI we get the following:
11 error in 0.65 seconds

This seems to be due to an issue with the chromedriver:

>       driver = Chrome(executable_path=os.getcwd() + '/chromedriver')

Cleanup/Refactor Code

Now that the code is close to complete it is time to refactor.

  1. Rename all variables from formsPOM from CamelCase to snake_case.

Pytest not running API test

Describe the bug
Pytest is not running the API test

To Reproduce
Steps to reproduce the behavior:

  1. Pipenv run Pytest
  2. Runs UI test only

Expected behavior
I expect it to run both test with name test_

Desktop (please complete the following information):

  • OS: MacOSx

Updating Repo is failing as test run on Docker

Run pipenv install --dev
Warning: the environment variable LANG is not set!
We recommend setting this in ~/.profile (or equivalent) for proper expected behavior.
Warning: Python 3.7 was not found on your system…
You can specify specific versions of Python with:
  $ pipenv --python path/to/python
##[error]Process completed with exit code 1.

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.