Skip to content

Commit 393e469

Browse files
First cut of amazon.in automation
1 parent b809242 commit 393e469

File tree

9 files changed

+240
-7
lines changed

9 files changed

+240
-7
lines changed

Data/TestData/amazon.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
url: https://www.amazon.in/
22
category: Books
3-
search_text: artificial intelligence
3+
search_text: artificial intelligence
4+
search_department: Computing, Internet & Digital Media
5+
search_sub_department: Programming & Software Development
6+
avg_customer_review: 4 Stars & Up
7+
sort_by: Publication Date
8+
book_format:
9+
- Paperback
10+
- Kindle eBooks
11+
- Hardcover
12+
min_price: 1000
13+
max_price: 1500
14+
select_product: 1
15+
delivery_pincode: 600001

Library/driver.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
class Driver:
1111
driver = None
12+
current_window_handle = None
1213

1314
def __init__(self) -> None:
1415
if not Var.env("browser") == "None":
@@ -55,11 +56,26 @@ def find_elements(self, by, value):
5556
def refresh(self) -> None:
5657
self.driver.refresh()
5758

58-
def execute_script(self, script, locator) -> None:
59-
self.driver.execute_script(self, script, locator)
59+
def execute_script(self, script, *args) -> None:
60+
self.driver.execute_script(self, script, *args)
6061

6162
def current_url(self) -> str:
6263
return self.driver.current_url
6364

6465
def quit(self):
6566
self.driver.quit()
67+
68+
def switch_to_new_tab(self):
69+
self.current_window_handle = self.driver.current_window_handle
70+
list_of_window_handles = self.driver.window_handles
71+
list_of_window_handles.remove(self.current_window_handle)
72+
self.driver.switch_to.window(list_of_window_handles.pop())
73+
74+
def window_handles_count(self):
75+
return self.driver.window_handles
76+
77+
def switch_to_parent_tab(self):
78+
self.driver.switch_to.window(self.current_window_handle)
79+
80+
def close_current_tab(self):
81+
self.driver.close()

Locators/amazon_home_page.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ class AmazonHomePageLocator:
66
amazon_search_categories = Locator("css selector", "div.nav-search-scope select.nav-search-dropdown")
77
amazon_search_categories_text = Locator("css selector", "div.nav-search-facade span")
88
amazon_search_textbox = Locator("css selector", "div.nav-search-field input.nav-input")
9+
amazon_search_button = Locator("css selector", "div.nav-search-submit")
910

1011
def __init__(self):
11-
print("Locators for google page")
12+
print("Locators for Amazon home page")
1213

1314
@staticmethod
1415
def amazon_search_category_list(string):

Locators/amazon_product_page.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from Library.locator import Locator
2+
3+
4+
class AmazonProductPageLocator:
5+
amazon_deliver_to_link = Locator("css selector", "div#contextualIngressPt")
6+
amazon_deliver_to_pincode = Locator("css selector", "div.a-column input[aria-label='or enter a pincode']")
7+
amazon_deliver_to_pincode_apply = Locator("css selector", "div.a-column input.a-button-input")
8+
amazon_product_page_identifier = Locator("css selector", "div#dp")
9+
# Product details locator
10+
amazon_product_title = Locator("css selector", "span#productTitle")
11+
amazon_product_byline_info = Locator("css selector", "div#bylineInfo")
12+
amazon_product_formats = Locator("css selector", "div#formats")
13+
amazon_product_detail_description = Locator("css selector", "div#iframeContent")
14+
amazon_product_offers = Locator("css selector", "div#sopp_feature_div")
15+
amazon_product_description = Locator("css selector", "div#productDescription_feature_div.a-row")
16+
amazon_product_details = Locator("css selector", "div#detail_bullets_id")
17+
18+
def __init__(self):
19+
print("Amazon Product page locator")

Locators/amazon_search_result.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from Library.locator import Locator
2+
3+
4+
class AmazonSearchResultLocator:
5+
amazon_search_result_top_header = Locator("css selector", "div.sg-col-inner div.a-section")
6+
amazon_search_result_department_see_more = Locator("css selector", "div#departments span.a-expander-prompt")
7+
amazon_search_sort_by = Locator("css selector", "span.a-dropdown-container")
8+
amazon_search_price_min = Locator("css selector", "div#priceRefinements input[placeholder='Min']")
9+
amazon_search_price_max = Locator("css selector", "div#priceRefinements input[placeholder='Max']")
10+
amazon_search_price_go = Locator("css selector", "div#priceRefinements span.a-button")
11+
12+
def __init__(self):
13+
print("Locators for Amazon search result page page")
14+
15+
@classmethod
16+
def amazon_search_result_department(cls, string):
17+
return Locator("xpath", "//div[@id='departments']//li[contains(@class,'a-spacing-micro')]"
18+
"//span[text()='%s']" % string)
19+
20+
# For average customer review use the following strings for respective avg cust review
21+
# 4 Stars & Up
22+
# 3 Stars & Up
23+
# 2 Stars & Up
24+
# 1 Star & Up
25+
@classmethod
26+
def amazon_search_avg_customer_review(cls, string):
27+
return Locator("css selector", "div#reviewsRefinements section[aria-label='%s']" % string)
28+
29+
@classmethod
30+
def amazon_search_sort_by_selector(cls, string):
31+
return Locator("xpath", "//div[contains(@class,'a-popover-inner')]//a[text()='%s']" % string)
32+
33+
@classmethod
34+
def amazon_search_checkbox_filters(cls, string):
35+
return Locator("css selector", "ul.a-unordered-list li[aria-label='%s']" % string)
36+
37+
@classmethod
38+
def amazon_search_result_list_header(cls, number):
39+
return Locator("xpath", "((//div[contains(@class,'s-result-list')]//div"
40+
"[contains(@class,'s-include-content-margin')])[%s]//div"
41+
"[contains(@class,'sg-col-4-of-12')]//div[@class='sg-row']//span)[1]" % number)

Pages/amazon_home_page.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ def get_selected_category(cls):
2020
return AmazonHomePageLocator.amazon_search_categories_text.text()
2121

2222
@classmethod
23-
def search_in_the_search_box(cls,string):
23+
def search_in_the_search_box(cls, string):
2424
AmazonHomePageLocator.amazon_search_textbox.send_keys(string)
25+
AmazonHomePageLocator.amazon_search_button.click()

Pages/amazon_product_page.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from Locators.amazon_product_page import AmazonProductPageLocator
2+
3+
4+
class AmazonProductPage(AmazonProductPageLocator):
5+
6+
def __init__(self):
7+
super().__init__()
8+
9+
@classmethod
10+
def is_product_page_displayed(cls):
11+
return AmazonProductPageLocator.amazon_product_page_identifier.wait_till_displayed()
12+
13+
@classmethod
14+
def set_delivery_pincode(cls, string):
15+
AmazonProductPageLocator.amazon_deliver_to_link.click()
16+
AmazonProductPageLocator.amazon_deliver_to_pincode.wait_till_displayed()
17+
AmazonProductPageLocator.amazon_deliver_to_pincode.send_keys(string)
18+
AmazonProductPageLocator.amazon_deliver_to_pincode_apply.click()
19+
return AmazonProductPageLocator.amazon_product_page_identifier.wait_till_displayed()
20+
21+
@classmethod
22+
def get_delivery_pincode(cls):
23+
return AmazonProductPageLocator.amazon_deliver_to_link.text()

Pages/amazon_search_result.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from Locators.amazon_search_result import AmazonSearchResultLocator
2+
3+
4+
class AmazonSearchResultPage(AmazonSearchResultLocator):
5+
6+
def __init__(self):
7+
super().__init__()
8+
9+
@classmethod
10+
def is_search_result_page_displayed(cls):
11+
return AmazonSearchResultLocator.amazon_search_result_top_header.wait_till_displayed()
12+
13+
@classmethod
14+
def get_search_result_head_liner_text(cls):
15+
return AmazonSearchResultLocator.amazon_search_result_top_header.text()
16+
17+
@classmethod
18+
def is_filter_set_in_head_liner(cls, string):
19+
head_liner = AmazonSearchResultLocator.amazon_search_result_top_header.text()
20+
result = head_liner.find(string)
21+
if result == 0:
22+
return True
23+
else:
24+
return False
25+
26+
@classmethod
27+
def select_department(cls, string):
28+
AmazonSearchResultLocator.amazon_search_result_department_see_more.click()
29+
AmazonSearchResultLocator.amazon_search_result_department(string).click()
30+
31+
@classmethod
32+
def select_sub_department(cls, string):
33+
AmazonSearchResultLocator.amazon_search_result_department_see_more.click()
34+
AmazonSearchResultLocator.amazon_search_result_department(string).click()
35+
36+
@classmethod
37+
def select_average_customer_review(cls, string):
38+
AmazonSearchResultLocator.amazon_search_avg_customer_review(string).click()
39+
40+
@classmethod
41+
def set_sort_by(cls, string):
42+
AmazonSearchResultLocator.amazon_search_sort_by.click()
43+
AmazonSearchResultLocator.amazon_search_sort_by_selector(string).wait_till_displayed()
44+
AmazonSearchResultLocator.amazon_search_sort_by_selector(string).click()
45+
46+
@classmethod
47+
def set_min_max_price(cls, min_price, max_price):
48+
AmazonSearchResultLocator.amazon_search_price_min.send_keys(min_price)
49+
AmazonSearchResultLocator.amazon_search_price_max.send_keys(max_price)
50+
AmazonSearchResultLocator.amazon_search_price_go.click()
51+
52+
@classmethod
53+
def select_checkbox_filter(cls, string):
54+
AmazonSearchResultLocator.amazon_search_checkbox_filters(string).move_and_click()
55+
56+
@classmethod
57+
def select_a_product_from_search_result(cls, number):
58+
AmazonSearchResultLocator.amazon_search_result_list_header(number).click()

Tests/amazon.py

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import pytest
33
from Library.driver import Driver
44
from Pages.amazon_home_page import AmazonHomePage
5+
from Pages.amazon_search_result import AmazonSearchResultPage
6+
from Pages.amazon_product_page import AmazonProductPage
57
from Library.variable import Var
68

79

@@ -29,5 +31,65 @@ def test_amazon_book_search_001():
2931
assert (AmazonHomePage.get_selected_category() ==
3032
static_variable.static_value_for("category")), "Category is not selected properly"
3133

32-
with allure.step("Search for the text which is needed in this case: " + static_variable.static_value_for("search_text")):
33-
AmazonHomePage.search_in_the_search_box(static_variable.static_value_for("search_text"))
34+
with allure.step("Search for the text which is needed in this"):
35+
search_text = static_variable.static_value_for("search_text")
36+
AmazonHomePage.search_in_the_search_box(search_text)
37+
assert (AmazonSearchResultPage.is_search_result_page_displayed() is True), "Search result page is not displayed"
38+
assert (AmazonSearchResultPage.is_filter_set_in_head_liner(search_text) is True), "Searched text is not " \
39+
"displayed in the result page"
40+
41+
with allure.step("Apply the filter categories in the search results page"):
42+
AmazonSearchResultPage.select_department(static_variable.static_value_for("search_department"))
43+
AmazonSearchResultPage.select_sub_department(static_variable.static_value_for("search_sub_department"))
44+
AmazonSearchResultPage.select_average_customer_review(static_variable.static_value_for("avg_customer_review"))
45+
AmazonSearchResultPage.set_sort_by(static_variable.static_value_for("sort_by"))
46+
assert (AmazonSearchResultPage.is_filter_set_in_head_liner(
47+
static_variable.static_value_for("search_department")) is True), "Searched department is not displayed " \
48+
"in result page head liner"
49+
assert (AmazonSearchResultPage.is_filter_set_in_head_liner(
50+
static_variable.static_value_for("search_sub_department")) is True), "Searched sub department is not " \
51+
"displayed in result page head liner"
52+
assert (AmazonSearchResultPage.is_filter_set_in_head_liner(
53+
static_variable.static_value_for("avg_customer_review")) is True), "Avg customer review is not displayed " \
54+
"in result page head liner"
55+
assert (AmazonSearchResultPage.is_filter_set_in_head_liner(
56+
static_variable.static_value_for("sort_by")) is True), "sort by is not displayed in result page head liner"
57+
AmazonSearchResultPage.set_min_max_price(static_variable.static_value_for("min_price"),
58+
static_variable.static_value_for("max_price"))
59+
assert (AmazonSearchResultPage.is_filter_set_in_head_liner(
60+
static_variable.static_value_for("min_price")) is True), "min_price is not displayed in result page head " \
61+
"liner "
62+
assert (AmazonSearchResultPage.is_filter_set_in_head_liner(
63+
static_variable.static_value_for("max_price")) is True), "max_price is not displayed in result page head " \
64+
"liner "
65+
66+
with allure.step("Apply check box filters in the search result page"):
67+
list_of_checkbox = static_variable.static_value_for("book_format")
68+
size_of_list = len(list_of_checkbox)
69+
for book_format in list_of_checkbox:
70+
AmazonSearchResultPage.select_checkbox_filter(book_format)
71+
assert (AmazonSearchResultPage.is_filter_set_in_head_liner(
72+
"Format: %s selected" % size_of_list)), "Applied checkbox filter is not listed in the result page headliner"
73+
74+
with allure.step("Select a product from the search result page"):
75+
AmazonSearchResultPage.select_a_product_from_search_result(static_variable.static_value_for("select_product"))
76+
assert (driver.window_handles_count == 2), "New tab is not opened after clicking a product"
77+
driver.switch_to_new_tab()
78+
assert (AmazonProductPage.is_product_page_displayed() is True), "Product page is not displayed after selecting"
79+
80+
with allure.step("Set the delivery pincode in the product page"):
81+
AmazonProductPage.set_delivery_pincode(static_variable.static_value_for("delivery_pincode"))
82+
result = AmazonProductPage.get_delivery_pincode().find(static_variable.static_value_for("delivery_pincode"))
83+
assert (result is True), "Delivery Pin code is not set properly"
84+
85+
with allure.step("Store the UI details in the dynamic dictionary"):
86+
ui_dynamic_data["amazon_product_title"] = AmazonProductPage.amazon_product_title.texts()
87+
ui_dynamic_data["amazon_product_byline_info"] = AmazonProductPage.amazon_product_byline_info.texts()
88+
ui_dynamic_data["amazon_product_formats"] = AmazonProductPage.amazon_product_formats.texts()
89+
ui_dynamic_data["amazon_product_detail_description"] = AmazonProductPage.amazon_product_detail_description.texts()
90+
ui_dynamic_data["amazon_product_offers"] = AmazonProductPage.amazon_product_offers.texts()
91+
ui_dynamic_data["amazon_product_description"] = AmazonProductPage.amazon_product_description.texts()
92+
ui_dynamic_data["amazon_product_details"] = AmazonProductPage.amazon_product_details.texts()
93+
94+
with allure.step("Compare the dynamic value from UI with the stored file"):
95+
dynamic_variable.compare(ui_dynamic_data)

0 commit comments

Comments
 (0)