Skip to content

Commit

Permalink
my_test
Browse files Browse the repository at this point in the history
  • Loading branch information
SiarheiZhachek committed Jul 31, 2023
1 parent 5f543bd commit 7748114
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 18 deletions.
1 change: 0 additions & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ jobs:
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
78 changes: 78 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import pytest
import allure
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as Options_chrome
from selenium.webdriver.firefox.options import Options as Options_ff


@pytest.fixture(scope='function')
def driver(browser_options, host_options):
if browser_options == 'ff' and host_options == 'server':
with allure.step(f'Rune Firefox and {host_options}'):
options = Options_ff()
options.add_argument("--headless")

driver_browser = webdriver.Firefox(options=options)
driver_browser.maximize_window()

elif browser_options == 'ff':
with allure.step('Rune Firefox'):
driver_browser = webdriver.Firefox()
# driver_browser.maximize_window()

elif host_options == 'server':
with allure.step(f'Rune Chrome with {host_options}'):
options = Options_chrome()
options.add_argument("--headless")
options.add_argument("start-maximized")
driver_browser = webdriver.Chrome(options=options)
driver_browser.maximize_window()
else:
with allure.step('Rune Chrome'):
driver_browser = webdriver.Chrome()
driver_browser.maximize_window()
driver_browser.implicitly_wait(10)
yield driver_browser
driver_browser.quit()


# @pytest.fixture(scope='function')
# def driver(browser_options, host_options):
# if host_options == 'server':
# options = Options_chrome()
# options.add_argument("--headless")
# options.add_argument("start-maximized")
# driver_browser = webdriver.Chrome(options=options)
#
# else:
# driver_browser = webdriver.Chrome()
# driver_browser.maximize_window()
#
# driver_browser.implicitly_wait(10)
# yield driver_browser
# driver_browser.quit()


def pytest_addoption(parser):
parser.addoption(
'--browser',
action='store',
default='chrome',
help='Укажите значение браузера, поумолчанию Chrome'
)
parser.addoption(
'--host',
action='store',
default='ui',
help='Укажите варивнт запуска браузера, по умолчанию UI'
)


@pytest.fixture(scope='session')
def browser_options(request):
return request.config.getoption('--browser')


@pytest.fixture(scope='session')
def host_options(request):
return request.config.getoption('--host')
Empty file added pages/__init__.py
Empty file.
76 changes: 76 additions & 0 deletions pages/base_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import math
from selenium.common import NoSuchElementException
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.common.exceptions import NoAlertPresentException, TimeoutException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from pages.locators import base_page_locators as loc


class BasePage:

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

def open_site(self, link):
self.driver.get(link)

def find_element(self, *args):
element, value = args[0]
return self.driver.find_element(element, value)

def find_elements(self, *args):
element, value = args[0]
return self.driver.find_elements(element, value)

def is_element_present(self, *args):
element, value = args[0]
try:
self.driver.find_element(element, value)
except NoSuchElementException:
return False
return True

def solve_quiz_and_get_code(self):
alert = self.driver.switch_to.alert
x_num = alert.text.split(" ")[2]
answer = str(math.log(abs((12 * math.sin(float(x_num))))))
alert.send_keys(answer)
alert.accept()
try:
alert = self.driver.switch_to.alert
alert.accept()
except NoAlertPresentException:
print("No second alert presented")

def is_not_element_present(self, *args, timeout=4):
element, value = args[0]
try:
WebDriverWait(self.driver, timeout).until(
ec.presence_of_element_located((element, value))
)
except TimeoutException:
return True
return False

def is_disappeared(self, *args, timeout=4):
element, value = args[0]
try:
WebDriverWait(self.driver, timeout, 1, TimeoutException).\
until_not(ec.presence_of_element_located((element, value)))
except TimeoutException:
return False
return True

def go_to_login_page(self):
self.find_element(loc.login_link).click()

def should_be_login_link(self):
return self.is_element_present(loc.login_link)

def go_to_cart(self):
self.find_element(loc.cart_button).click()

def should_be_authorized_user(self):
assert self.is_element_present(loc.user_icon), "User icon is not presented," \
" probably unauthorised user"
18 changes: 18 additions & 0 deletions pages/basket_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pages.base_page import BasePage
from pages.locators import basket_page_locators as loc


class BasketPage(BasePage):

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

def message_the_cart_is_empty_is_displayed(self):
return self.find_element(loc.message_the_cart_is_empty).is_displayed()

def the_product_is_not_in_the_cart(self):
return self.is_not_element_present(loc.product_in_the_cart)

def the_product_in_the_cart(self):
return self.find_element(loc.product_in_the_cart).is_displayed()
7 changes: 7 additions & 0 deletions pages/locators/base_page_locators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from selenium.webdriver.common.by import By


cart_button = (By.CSS_SELECTOR, '.btn-group a')
login_link = (By.CSS_SELECTOR, "#login_link")
login_link_invalid = (By.CSS_SELECTOR, "#login_link_inc")
user_icon = (By.CSS_SELECTOR, ".icon-user")
4 changes: 4 additions & 0 deletions pages/locators/basket_page_locators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from selenium.webdriver.common.by import By

message_the_cart_is_empty = (By.CSS_SELECTOR, '#content_inner')
product_in_the_cart = (By.CSS_SELECTOR, '.basket-items')
5 changes: 5 additions & 0 deletions pages/locators/main_page_locators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from selenium.webdriver.common.by import By


login_link = (By.CSS_SELECTOR, "#login_link")
login_link_invalid = (By.CSS_SELECTOR, "#login_link_invalid")
15 changes: 15 additions & 0 deletions pages/main_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pages.base_page import BasePage
from pages.locators import main_page_locators as loc


class MainPage(BasePage):

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

def login_link_is_displayed(self):
return self.find_element(loc.login_link).is_displayed()

def should_be_login_link(self):
return self.find_element(loc.login_link)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ h11==0.14.0
idna==3.4
outcome==1.2.0
PySocks==1.7.1
selenium==4.10.0
selenium==4.7.2
sniffio==1.3.0
sortedcontainers==2.4.0
trio==0.22.2
Expand Down
8 changes: 0 additions & 8 deletions sun_num.py

This file was deleted.

8 changes: 0 additions & 8 deletions test.py

This file was deleted.

Empty file added tests/__init__.py
Empty file.
43 changes: 43 additions & 0 deletions tests/test_main_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import allure
from pages.main_page import MainPage
from pages.basket_page import BasketPage
import pytest
from selenium import webdriver
import json
import os

LINK = 'http://selenium1py.pythonanywhere.com/'

# @pytest.fixture(scope='function')
# def driver():
# driver = webdriver.Chrome()
# # driver.set_window_size(660, 880)
# driver.maximize_window()
# driver.implicitly_wait(10)
# yield driver
# driver.quit()



@allure.feature('Home page')
@allure.story('Login link is displayed')
def test_guest_should_see_login_link(driver):
with allure.step('Open web site'):
main_page = MainPage(driver)
main_page.open_site(LINK)
with allure.step('Login link is displayed'):
assert main_page.should_be_login_link()


@allure.feature('Home page')
@allure.story('No product in the cart')
def test_guest_cant_see_product_in_basket_opened_from_main_pages(driver):
with allure.step('Open web site'):
main_page = MainPage(driver)
main_page.open_site(LINK)
with allure.step('Open the shopping cart'):
main_page.go_to_cart()
cart_page = BasketPage(driver)
with allure.step('Check product is not in the cart'):
assert cart_page.the_product_is_not_in_the_cart()
assert cart_page.message_the_cart_is_empty_is_displayed()

0 comments on commit 7748114

Please sign in to comment.