Skip to content

Commit 3684c7a

Browse files
committed
add selenium test patterns
1 parent 8e023e5 commit 3684c7a

9 files changed

+153
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,4 @@ ENV/
8989
.ropeproject
9090
.idea/
9191

92+
.DS_Store

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ PhantomJS: http://phantomjs.org/</br>
77
# Doc reference
88
Selenium with Python: http://selenium-python.readthedocs.io/</br>
99
PhantomJS: http://phantomjs.org/api/</br>
10+
pyquery: http://pyquery.readthedocs.io/en/latest/</br>
1011
PyMySQL: https://github.com/PyMySQL/PyMySQL</br>
1112

element.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from selenium.webdriver.support.ui import WebDriverWait
2+
3+
class BasePageElement(object):
4+
"""Base page class that is initialized on every page object class."""
5+
def __set__(self, obj, value):
6+
"""Sets the text to the value supplied"""
7+
driver = obj.driver
8+
WebDriverWait(driver, 100).until(lambda driver: driver.find_element_by_name(self.locator))
9+
driver.find_element_by_name(self.locator).send_keys(value)
10+
11+
def __get__(self, obj, owner):
12+
"""Gets the text of the specified object"""
13+
driver = obj.driver
14+
WebDriverWait(driver, 100).until(lambda driver: driver.find_element_by_name(self.locator))
15+
element = driver.find_element_by_name(self.locator)
16+
return element.get_attribute("value")

locators.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from selenium.webdriver.common.by import By
2+
3+
class MainPageLocators(object):
4+
"""A class for main page locators. All main page locators should come here"""
5+
GO_BUTTON = (By.CLASS_NAME, 'btn-search')
6+
7+
8+
class SearchResultsPageLocators(object):
9+
"""A class for search results locators. All search results locators should come here"""
10+
pass
11+

page.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from element import BasePageElement
2+
from locators import MainPageLocators
3+
4+
class SeachTextElement(BasePageElement):
5+
"""This class gets the search text from the specified locator"""
6+
7+
# The locator for search box where search string is entered
8+
locator = 'q'
9+
10+
class BasePage(object):
11+
"""Base class to initialize the base page that will be called from all pages"""
12+
def __init__(self, driver):
13+
self.driver = driver
14+
15+
16+
class MainPage(BasePage):
17+
"""Home page action methods come here. i.e. taobao.com"""
18+
search_text_element = SeachTextElement()
19+
20+
def is_title_matches(self):
21+
"""Verifies that the hardcoded text "淘宝" appears in page title"""
22+
return "淘宝" in self.driver.title
23+
24+
def click_go_button(self):
25+
"""Triggers the search"""
26+
elem = self.driver.find_element(*MainPageLocators.GO_BUTTON)
27+
elem.click()
28+
29+
class SearchResultsPage(BasePage):
30+
"""Search results page action methods come here"""
31+
def is_results_found(self):
32+
# Probably should search for this text in the specific page
33+
# element, but as for now it works fine
34+
return "No results found" not in self.driver.page_source

setup.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name = "spider",
5+
version = "0.1",
6+
packages = find_packages(), install_requires=['page']
7+
)

spider_jd.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def get_products():
6060
'shop': item.find('.p-shop').text(),
6161

6262
}
63-
# print(product)
64-
save_to_mysql(product)
63+
print(product)
64+
# save_to_mysql(product)
6565

6666
def search():
6767
print("---搜索关键字: " + KEY_WORD_JD)

taobao.sql

Whitespace-only changes.

test_taobao.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import unittest
2+
from selenium import webdriver
3+
from selenium.webdriver.common.keys import Keys
4+
from selenium.webdriver.support.select import Select
5+
import page
6+
7+
8+
class TestTaobao(unittest.TestCase):
9+
def setUp(self):
10+
self.driver = webdriver.Chrome()
11+
12+
def test_search(self):
13+
driver = self.driver
14+
driver.get("http://www.taobao.com")
15+
self.assertIn("淘宝", driver.title)
16+
elem = driver.find_element_by_css_selector('#q')
17+
elem.send_keys("手机")
18+
elem.send_keys(Keys.RETURN)
19+
assert "No results found" not in driver.page_source
20+
21+
def test_dropdown(self):
22+
driver = self.driver
23+
driver.get("https://www.w3.org/")
24+
# element = driver.find_element_by_css_selector('#region_form > div > select')
25+
# all_options = element.find_elements_by_tag_name("option")
26+
# for option in all_options:
27+
# print("Value is: %s" % option.get_attribute("value"))
28+
# option.click()
29+
select = Select(driver.find_element_by_name('region'))
30+
options_number = len(select.options)
31+
print(str(options_number))
32+
for i in range(1, options_number):
33+
print(select.options[i].get_attribute('value'))
34+
select.select_by_index(i)
35+
36+
def test_switch_window(self):
37+
driver = self.driver
38+
driver.get("http://www.qq.com/")
39+
# driver.switch_to_window("_self")
40+
for handle in driver.window_handles:
41+
driver.switch_to.window(handle)
42+
43+
def test_popup(self):
44+
driver = self.driver
45+
driver.get("http://www.qq.com/")
46+
driver.find_element_by_class_name("login").click()
47+
driver.switch_to_alert()
48+
driver.find_element_by_id('switcher_plogin').click()
49+
50+
def test_forward_back(self):
51+
driver = self.driver
52+
driver.get("http://www.baidu.com")
53+
input = driver.find_element_by_css_selector("#kw")
54+
input.clear()
55+
input.send_keys("电影")
56+
input.send_keys(Keys.RETURN)
57+
driver.back()
58+
driver.forward()
59+
60+
# A sample test class to show how page object works
61+
def test_search_in_taobao(self):
62+
driver = self.driver
63+
driver.get("http://www.taobao.com")
64+
65+
# Load the main page. In this case the home page of taobao.com.
66+
main_page = page.MainPage(driver)
67+
# Checks if the word "淘宝" is in title
68+
assert main_page.is_title_matches(), "taobao.com title not match"
69+
# Sets the text of search textbox to "手机"
70+
main_page.search_text_element = "手机"
71+
main_page.click_go_button()
72+
search_results_page = page.SearchResultsPage(driver)
73+
assert search_results_page.is_results_found(), "No results found"
74+
75+
76+
def tearDown(self):
77+
self.driver.close()
78+
79+
if __name__ == '__main__':
80+
test_dropdown()
81+

0 commit comments

Comments
 (0)