Skip to content

Commit

Permalink
0.2 - fix testing
Browse files Browse the repository at this point in the history
  • Loading branch information
wwakabobik committed Oct 16, 2023
1 parent 661c5a4 commit 651f4d8
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 168 deletions.
3 changes: 2 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Copyright (c) 2023. All rights reserved.
Created: 25.08.2023
Last Modified: 12.09.2023
Last Modified: 16.09.2023
Description:
This file is init point for project-wide structure.
Expand All @@ -14,6 +14,7 @@
# Engines
from .openai_api.src.openai_api.chatgpt import ChatGPT # pylint: disable=unused-import
from .openai_api.src.openai_api.dalle import DALLE # pylint: disable=unused-import
from .leonardo_api import Leonardo, LeonardoAsync # pylint: disable=unused-import

# Utils
from .utils.tts import CustomTTS # pylint: disable=unused-import
Expand Down
19 changes: 10 additions & 9 deletions __main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
Copyright (c) 2023. All rights reserved.
Created: 25.08.2023
Last Modified: 12.09.2023
Last Modified: 16.09.2023
Description:
This file is entry point for project-wide structure.
"""

# Engines
from openai_api.src.openai_api.chatgpt import ChatGPT # pylint: disable=unused-import
from openai_api.src.openai_api.dalle import DALLE # pylint: disable=unused-import
from .openai_api.src.openai_api.chatgpt import ChatGPT # pylint: disable=unused-import
from .openai_api.src.openai_api.dalle import DALLE # pylint: disable=unused-import
from .leonardo_api import Leonardo, LeonardoAsync # pylint: disable=unused-import

# Utils
from utils.tts import CustomTTS # pylint: disable=unused-import
from utils.transcriptors import CustomTranscriptor # pylint: disable=unused-import
from utils.translators import CustomTranslator # pylint: disable=unused-import
from utils.audio_recorder import AudioRecorder, record_and_convert_audio # pylint: disable=unused-import
from utils.logger_config import setup_logger # pylint: disable=unused-import
from utils.other import is_heroku_environment # pylint: disable=unused-import
from .utils.tts import CustomTTS # pylint: disable=unused-import
from .utils.transcriptors import CustomTranscriptor # pylint: disable=unused-import
from .utils.translators import CustomTranslator # pylint: disable=unused-import
from .utils.audio_recorder import AudioRecorder, record_and_convert_audio # pylint: disable=unused-import
from .utils.logger_config import setup_logger # pylint: disable=unused-import
from .utils.other import is_heroku_environment # pylint: disable=unused-import
131 changes: 131 additions & 0 deletions examples/image_generation/gpt_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import requests
from PIL import Image
from io import BytesIO

import json

from creds import oai_token, oai_organization
from openai_api.src.openai_api.dalle import DALLE
from leonardo_api.leonardo_sync import Leonardo
from page_retriever import PageRetriever
from pytest_runner import run_tests


doc_engine = PageRetriever('https://wwakabobik.github.io/')


def get_weather(city, units):
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
"q": city,
"appid": "93171b03384f92ee3c55873452a49c7c",
"units": units
}
response = requests.get(base_url, params=params)
data = response.json()
return data


def get_current_weather(location, unit="metric"):
"""Get the current weather in a given location"""
owm_info = get_weather(location, units=unit)
weather_info = {
"location": location,
"temperature": owm_info["main"]["temp"],
"unit": unit,
"forecast": owm_info["weather"][0]["description"],
"wind": owm_info["wind"]["speed"]
}
return json.dumps(weather_info)


def draw_image_using_dalle(prompt):
dalle = DALLE(auth_token=oai_token, organization=oai_organization)
image = dalle.create_image_url(prompt)
url_dict = {'image_url': image[0]}
response = requests.get(image[0])
img = Image.open(BytesIO(response.content))
img.show()
return json.dumps(url_dict)


def draw_image(prompt):
leonardo = Leonardo(auth_token='a0178171-c67f-4922-afb3-458f24ecef1a')
leonardo.get_user_info()
response = leonardo.post_generations(prompt=prompt, num_images=1, guidance_scale=5,
model_id='e316348f-7773-490e-adcd-46757c738eb7', width=1024, height=768)
response = leonardo.wait_for_image_generation(generation_id=response['sdGenerationJob']['generationId'])
url_dict = {'image_url': response[0]['url']}
response = requests.get(url_dict['image_url'])
img = Image.open(BytesIO(response.content))
img.show()
return json.dumps(url_dict)


gpt_functions = [
{
"name": "draw_image",
"description": "Draws image using user prompt. Returns url of image.",
"parameters": {
"type": "object",
"properties": {
"prompt": {
"type": "string",
"description": "Prompt, the description, what should be drawn and how",
},
},
"required": ["prompt"],
},
},
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
{
"name": "get_page_code",
"description": "Get page code to generate locators and tests",
"parameters": {
"type": "object",
"properties": {
"url": {
"type": "string",
"description": "The URL of the page to get the code from"
}
},
"required": []
}
},
{
"name": "get_tests_results",
"description": "Get the results of the tests",
"parameters": {
"type": "object",
"properties": {
"test_files": {
"type": "array",
"items": {
"type": "string"
},
"description": "The list of test files to run"
}
},
"required": []
}
}
]

gpt_functions_dict = {'get_current_weather': get_current_weather,
'draw_image': draw_image,
'get_page_code': doc_engine.get_body_without_scripts,
'get_tests_results': run_tests('tests/test_example.py')}
4 changes: 2 additions & 2 deletions examples/speak_and_hear/test_gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from utils.transcriptors import CustomTranscriptor
from utils.tts import CustomTTS

from creds import oai_token, oai_organization
from openai_api.src.openai_api.chatgpt import ChatGPT
from ..creds import oai_token, oai_organization
from ...openai_api import ChatGPT


gpt = ChatGPT(auth_token=oai_token, organization=oai_organization, model="gpt-3.5-turbo")
Expand Down
95 changes: 95 additions & 0 deletions examples/test_generator/generator_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# -*- coding: utf-8 -*-
"""
Filename: __generator_test__.py
Author: Iliya Vereshchagin
Copyright (c) 2023. All rights reserved.
Created: 16.10.2023
Last Modified: 16.10.2023
Description:
This file contains testing procedures for ChatGPt experiments
"""

import json
import logging

import asyncio

from openai_api.src.openai_api import ChatGPT
from openai_api.src.openai_api.logger_config import setup_logger
from examples.creds import oai_token, oai_organization
from examples.test_generator.pom_case_generator import PomTestCaseGenerator
from examples.test_generator.gpt_functions import gpt_functions, gpt_functions_dict

generator = PomTestCaseGenerator(url='https://www.saucedemo.com/')
#generator = PomTestCaseGenerator(url='https://automationintesting.com/selenium/testpage/')


system_instructions = """
You're bot responsible for QA automation testing. You tech stack is selenium + pytest. I will provide you url for testing.
1) You may obtain page code by calling "get_page_code" function. It will return you:
raw HTML document, what needs to be tested (guarded by ```). And you need to respond with json in following format:
{
"page_objects": [
"@property\\n
def calculate_button(self):\\n
return WebDriverWait(self.driver, 10).until(\\n
EC.presence_of_element_located((By.XPATH, '//button[.='''Calculate''']'))\\n
)", <...>
],
"tests": ["def test_division_by_zero(page):\\n
page.numbers_input.send_keys(1024)\\n
page.divide_button.click()\\n
page.calculator_input.send_keys('0')\\n
page/calculate_button.click()\\n
assert page.error.text() == 'Error: divide by zero'", <...>],
}
This means you need to create page objects for each object on the page using laconic and stable XPATH locators (as short and stables as you can, use only By.XPATH locators, not By.ID, not By.CSS_SELECTOR or By.CLASS name), and then create all possible test cases for them. It might be some filed filling tests (errors, border checks, positive and negative cases), clicking, content changing, etc. Please respect to use 'page' fixture for every test, it's predefined in code and opens page under test before it.
2) Then I may ask you to execute some tests. You can run demanded test via "get_tests_results" function, based on gathered content, you need to respond with json in following format:
results = {
"passed": [],
"failed": [],
"error": [],
"failure details": {}
}
where "failure details" - is dict with keys equal to test names (which you generated) and possible failures details. If you got an failures and errors, you need to respond as in 1 with fixed code (page objects and/or tests).
Answer only with JSON in format I mentioned in 1. Never add anything more than that (no explanations, no extra text, only json).
3) In addition to 1 and 2 i may pass you extra info what kind of test data might be used (i.e. for form filling), but in general you need to generate all possible scenarios (valid/invalid/border cases, always add what's not listed by user, but should be for best quality of testing coverage).
"""


def setup_gpt():
"""Setup GPT bot with appropriate functions and settings"""
gpt = ChatGPT(auth_token=oai_token, organization=oai_organization, model="gpt-4-0613")
gpt.logger = setup_logger("gpt", "gpt.log", logging.INFO)
gpt.system_settings = ""
gpt.function_dict = gpt_functions_dict
gpt.function_call = 'auto'
gpt.functions = gpt_functions
gpt.system_settings = system_instructions
return gpt


async def main():
print("===Setup GPT bot===")
gpt = setup_gpt()
print("===Get page code of https://www.saucedemo.com/ and generate POM and tests===")
response = await anext(gpt.str_chat("Get page code of https://www.saucedemo.com/ and generate POM and tests"))
print(response)
response = response.replace('\n', '')
generator.create_files_from_json(json.loads(response),
pom_folder='examples/test_generator/pom',
tests_folder='examples/test_generator/tests')
print("===Get tests results for examples/test_generator/tests/test_index.py==")
response = await anext(gpt.str_chat("Get tests results for examples/test_generator/tests/test_index.py"))
print(response)
print("===If there are failures in code, please fix it by fixing POM and tests===")
response = await anext(gpt.str_chat("If there are failures in code, please fix it by fixing POM and tests"))
print(response)
generator.create_files_from_json(json.loads(response),
pom_folder='..pom',
tests_folder='examples/test_generator/tests')

asyncio.run(main())
Loading

0 comments on commit 651f4d8

Please sign in to comment.