Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#929 ecommerce goals for tests #933

Merged
merged 6 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions shopelectro/selenium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

from .driver import SiteDriver
from .pages import *
from .analytics_goals import Goals, YandexEcommerceGoals
46 changes: 46 additions & 0 deletions shopelectro/selenium/analytics_goals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import abc

from shopelectro.selenium import SiteDriver

# @todo #929:30m Implement GoogleEcommerceGoals and YandexMetrikaGoals and use them in tests.


class Goals(abc.ABC):
"""Front-end reached goals."""

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

@abc.abstractmethod
def fetch(self):
raise NotImplementedError

@abc.abstractmethod
def __iter__(self):
raise NotImplementedError

@abc.abstractmethod
def __getitem__(self, index: int):
raise NotImplementedError


class YandexEcommerceGoals(Goals): # Ignore PyDocStyleBear
"""
Unwrap an excess nesting level common for every goal:
[{"ecommerce": ...}] -> ...
Structure docs: https://yandex.com/support/metrica/data/e-commerce.html
"""

def __init__(self, driver: SiteDriver):
super().__init__(driver)
self.goals = []

def fetch(self):
self.goals = self.driver.execute_script('return window.dataLayer.results;')

def __getitem__(self, index: int):
return self.goals[index][0]['ecommerce']

def __iter__(self):
for g in self.goals:
yield g[0]['ecommerce']
4 changes: 4 additions & 0 deletions shopelectro/selenium/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ def __exit__(self, *args, **kwargs):

def get(self, url):
super().get(urljoin(self.site_url, url))

def delete_session(self):
self.delete_cookie('sessionid')
self.execute_script('localStorage.clear();')
27 changes: 11 additions & 16 deletions shopelectro/tests/tests_js_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,13 @@ class YandexEcommerce(Ecommerce):

def tearDown(self):
# delete the session to clear the cart
self.browser.delete_cookie('sessionid')
self.browser.execute_script('localStorage.clear();')
self.browser.delete_session()
super().tearDown()

# @todo #808:30m Create Goals class for tests.
# Remove these utility functions in favor of the class.
def get_goals(self):
return self.browser.execute_script('return window.dataLayer.results;')

def get_goal(self, reached, index=0):
"""Get a goal with the given index and unfold it."""
return reached[index][0]['ecommerce']
def get_goals(self) -> selenium.Goals:
goals = selenium.YandexEcommerceGoals(self.browser)
goals.fetch()
return goals

def test_purchase(self):
self.buy()
Expand All @@ -113,7 +108,7 @@ def test_purchase(self):
reached_goals = self.get_goals()
self.assertTrue(reached_goals)

reached = self.get_goal(reached_goals)
reached = reached_goals[0]
self.assertIn('purchase', reached)
self.assertEqual(reached['currencyCode'], 'RUB')

Expand All @@ -140,7 +135,7 @@ def test_product_detail(self):
reached_goals = self.get_goals()
self.assertTrue(reached_goals)

reached = self.get_goal(reached_goals)
reached = reached_goals[0]
self.assertIn('detail', reached)
self.assertEqual(reached['currencyCode'], 'RUB')

Expand Down Expand Up @@ -171,7 +166,7 @@ def test_clear_cart(self): # Ignore CPDBear
reached_goals = self.get_goals()
self.assertTrue(reached_goals)

reached = self.get_goal(reached_goals, 2)
reached = reached_goals[2]
self.assertIn('remove', reached)
self.assertEqual(reached['currencyCode'], 'RUB')

Expand Down Expand Up @@ -200,7 +195,7 @@ def test_remove_from_cart(self):
reached_goals = self.get_goals()
self.assertTrue(reached_goals)

reached = self.get_goal(reached_goals, 2)
reached = reached_goals[2]
self.assertIn('remove', reached)
self.assertEqual(reached['currencyCode'], 'RUB')

Expand All @@ -227,7 +222,7 @@ def test_add_from_product_page(self):
reached_goals = self.get_goals()
self.assertTrue(reached_goals)

reached = self.get_goal(reached_goals, 1) # Ignore CPDBear
reached = reached_goals[1] # Ignore CPDBear
self.assertIn('add', reached)
self.assertEqual(reached['currencyCode'], 'RUB')

Expand Down Expand Up @@ -258,7 +253,7 @@ def test_add_from_category_page(self):
reached_goals = self.get_goals()
self.assertTrue(reached_goals)

reached = self.get_goal(reached_goals)
reached = reached_goals[0]
self.assertIn('add', reached)
self.assertEqual(reached['currencyCode'], 'RUB')

Expand Down
4 changes: 1 addition & 3 deletions shopelectro/tests/tests_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,10 +636,8 @@ def setUp(self):
self.wait_page_loading()

def tearDown(self):
# Delete only a session cookie to flush a cart.
# If we will flush all cookies it will probably rise csrf-protection errors.
self.browser.delete_cookie('sessionid')
self.browser.execute_script('localStorage.clear();')
self.browser.delete_session()

def buy_products(self):
self.browser.get(self.category)
Expand Down