Skip to content

Commit

Permalink
#929 ecommerce goals for tests (#933)
Browse files Browse the repository at this point in the history
* Create SiteDriver.delete_session method

* Create Goals interface for selenium tests

* Use the YandexEcommerceGoals in tests

* Review fixes

* Make rebase to master

* Apply linter rules
  • Loading branch information
ArtemijRodionov authored Jul 5, 2019
1 parent 3aa1b47 commit 63a1de7
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
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

2 comments on commit 63a1de7

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 63a1de7 Jul 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 808-f72e84e8 disappeared from shopelectro/tests/tests_js_analytics.py, that's why I closed #929. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 63a1de7 Jul 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 929-26614c74 discovered in shopelectro/selenium/analytics_goals.py and submitted as #940. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.