Skip to content

Commit 01868c2

Browse files
committed
Shrink wait_util methods to one
1 parent 7ee7efe commit 01868c2

File tree

7 files changed

+77
-50
lines changed

7 files changed

+77
-50
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@
2727

2828

2929
## 2. Usage
30-
`$ git clone https://github.com/goofcode/lion`
30+
```
31+
$ git clone https://github.com/goofcode/lion
32+
$ cd lion
33+
$ pip3 install -r requirements.txt
34+
```
35+
36+
3137
```python
3238
from lion import message
3339
from lion import info

exception.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55

66
class NoSuchDriverException(Exception):
7-
def __init__(self, message):
7+
def __init__(self, message='mode should be either "chrome" or "phantom"'):
88
self.message = message
99

10+
11+
class NoSuchLocateMethodException(Exception):
12+
def __init__(self, message='by should be "id" or "xpath" or "class"'):
13+
self.message = message
14+
15+

info.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1+
"""
2+
author: Mookeun Ji, [email protected]
3+
"""
4+
15
import re
26
import time
37

4-
import util
5-
8+
from .util import *
69

710
def get_friend_number(mode='phantom'):
811
"""
912
:param mode: 'chrome' or 'phantom'(headless)
1013
:return: (int) number of friend of plus friend
1114
"""
12-
driver = util.get_driver(mode)
13-
util.login_pf_center(driver)
14-
util.to_pf_home(driver)
15+
driver = get_driver(mode)
16+
login_pf_center(driver)
17+
to_pf_home(driver)
1518

16-
friend_element = util.wait_until_xpath_load(driver, util.get_locators()['friend_number_xpath'])
19+
friend_element = wait_until_load(driver, 'xpath', get_locators()['friend_number_xpath'])
1720
# friend number is loaded dynamically
1821
time.sleep(2)
1922
friend_number = int(re.findall(r'\d+', friend_element.text)[0])
@@ -25,11 +28,11 @@ def get_left_free_message_number(mode='phantom'):
2528
:param mode: 'chrome' or 'phantom'(headless)
2629
:return: (int) number of left free message
2730
"""
28-
driver = util.get_driver(mode)
29-
util.login_pf_center(driver)
30-
util.to_pf_home(driver)
31+
driver = get_driver(mode)
32+
login_pf_center(driver)
33+
to_pf_home(driver)
3134

32-
free_message_element = util.wait_until_xpath_load(driver, util.get_locators()['free_message_xpath'])
35+
free_message_element = wait_until_load(driver, 'xpath', get_locators()['free_message_xpath'])
3336
# left_free_message_number is loaded dynamically
3437
time.sleep(2)
3538
free_message = int(free_message_element.text.replace(',', ''))

locator.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"link_input_id": "linkUpload",
1717
"no_share_radio_xpath": "//input[@id='fieldRadio0Forshareable']/../label[1]/span[1]",
1818
"next_btn_xpath": "//button[contains(text(),'다음')]",
19+
"show_calendar_class": "lab_date",
1920
"submit_btn_xpath": "//button[contains(text(),'등록')]",
2021
"confirm_btn_xpath": "//button[contains(text(),'확인')]"
2122
}

message.py

+26-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
author: Mookeun Ji, [email protected]
33
4-
# Functions defined here may raise some exceptions from driver in case of
4+
Functions defined here may raise some exceptions from driver in case of
55
66
1. setting or locator fault
77
2. loss of internet connection
@@ -13,17 +13,17 @@
1313
modifying this set of codes appropriately are recommended for further use
1414
"""
1515

16-
from util import *
16+
from .util import *
1717

18-
def send_basic_text_message(content, link=None, share=True, mode='phantom'):
18+
def send_basic_text_message(content, time=None, link=None, share=True, mode='phantom'):
1919
"""
2020
send basic text type message to all friends
2121
2222
:param content: message content to be sent
23-
:param link: link to be sent w/ message if none, only content will be send
24-
:param share: whether enable 'share to friend'
25-
:param mode: 'chrome' or 'phantom'(headless)
26-
23+
:param [opt] time: time message to be sent(formatted as yyyymmdd hh:mm, if none, time set to be now
24+
:param [opt] link: link to be sent w/ message if none, only content will be send
25+
:param [opt] share: whether enable 'share to friend'
26+
:param [opt] mode: 'chrome' or 'phantom'(headless)
2727
"""
2828

2929
driver = get_driver(mode)
@@ -33,19 +33,29 @@ def send_basic_text_message(content, link=None, share=True, mode='phantom'):
3333
driver.get(url_join(settings['pf_home_url'], locators['basic_message_url']))
3434

3535
# fill inputs for new message
36-
wait_until_id_load(driver, locators['message_input_id']).send_keys(content)
36+
wait_until_load(driver, 'id', locators['message_input_id']).send_keys(content)
3737

3838
if link is not None:
39-
wait_until_xpath_load(driver, locators['add_link_radio_xpath']).click()
40-
wait_until_id_load(driver, locators['link_name_id']).send_keys("바로가기")
41-
wait_until_id_load(driver, locators['link_input_id']).clear()
42-
wait_until_id_load(driver, locators['link_input_id']).send_keys(link)
39+
wait_until_load(driver, 'xpath', locators['add_link_radio_xpath']).click()
40+
wait_until_load(driver, 'id', locators['link_name_id']).send_keys("바로가기")
41+
wait_until_load(driver, 'id', locators['link_input_id']).clear()
42+
wait_until_load(driver, 'id', locators['link_input_id']).send_keys(link)
4343

4444
if share is False:
45-
wait_until_xpath_load(driver, locators['no_share_radio_xpath']).click()
45+
wait_until_load(driver, 'xpath', locators['no_share_radio_xpath']).click()
46+
47+
wait_until_load(driver, 'xpath', locators['next_btn_xpath']).click()
4648

47-
wait_until_xpath_load(driver, locators['next_btn_xpath']).click()
48-
wait_until_xpath_load(driver, locators['submit_btn_xpath']).click()
49-
wait_until_xpath_load(driver, locators['confirm_btn_xpath']).click()
49+
'''
50+
TODO : add time feature
51+
52+
if time is not None:
53+
d_time = datetime.datetime.strptime(time, '%Y%m%d %H:%M')
54+
wait_until_load(driver, 'class', locators['show_calendar_class']).click()
55+
'''
56+
57+
wait_until_load(driver, 'xpath', locators['submit_btn_xpath']).click()
58+
wait_until_load(driver, 'xpath', locators['confirm_btn_xpath']).click()
5059

5160
driver.close()
61+

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
selenium==3.5.0

util.py

+22-22
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
"""
44

55
import json
6+
import os
67

78
from selenium import webdriver
89
from selenium.webdriver.common.by import By
910
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
1011
from selenium.webdriver.support import expected_conditions as ec
1112
from selenium.webdriver.support.wait import WebDriverWait
1213

13-
from exception import NoSuchDriverException
14+
from .exception import *
1415

15-
DEV = False
1616

17+
DEV = False
18+
current_dir = os.path.dirname(os.path.abspath(__file__))
1719

1820
def url_join(main_url, sub_url):
1921
"""
@@ -30,14 +32,14 @@ def url_join(main_url, sub_url):
3032

3133
def get_settings():
3234
if DEV:
33-
with open('dev_setting.json', 'r') as settings_json:
35+
with open(os.path.join(current_dir, 'dev_setting.json'), 'r') as settings_json:
3436
return json.load(settings_json)
3537
else:
36-
with open('setting.json', 'r') as settings_json:
38+
with open(os.path.join(current_dir, 'setting.json'), 'r') as settings_json:
3739
return json.load(settings_json)
3840

3941
def get_locators():
40-
with open('locator.json', 'r', encoding='utf-8') as locators_json:
42+
with open(os.path.join(current_dir, 'locator.json'), 'r', encoding='utf-8') as locators_json:
4143
return json.load(locators_json)
4244

4345
def get_settings_and_locators():
@@ -68,35 +70,33 @@ def get_driver(mode='phantom'):
6870
if DEV:
6971
driver.set_window_size(1920, 1080)
7072
else:
71-
raise NoSuchDriverException('mode should be either "chrome" or "phantom"(headless)')
73+
raise NoSuchDriverException()
7274

7375
return driver
7476

75-
def wait_until_xpath_load(driver, element_xpath, timeout=5):
77+
def wait_until_load(driver, by, locator, timeout=30):
7678
"""
77-
:param driver: selenium driver trying to load page
78-
:param element_xpath: xpath of element waiting for
79-
:param timeout: maximum time to wait. after this time elapsed, TimeoutException will be raised.
79+
:param driver: selenium driver loading page
80+
:param by: locating method 'id', 'xpath', 'class'
81+
:param locator: proper locator according to param by
82+
:param timeout: maximum time to wait. After this time elapsed, TimeoutException will be raised.
8083
:return: web element object waiting for
8184
"""
82-
return _get_element_after_loaded(driver, timeout, (By.XPATH, element_xpath))
85+
if by is 'id':
86+
return WebDriverWait(driver, timeout).until(ec.presence_of_element_located((By.ID, locator)))
87+
elif by is 'xpath':
88+
return WebDriverWait(driver, timeout).until(ec.presence_of_element_located((By.XPATH, locator)))
89+
elif by is 'class':
90+
return WebDriverWait(driver, timeout).until(ec.presence_of_element_located((By.CLASS_NAME, locator)))
91+
else:
92+
raise NoSuchLocateMethodException()
8393

84-
def wait_until_id_load(driver, element_id, timeout=5):
85-
"""
86-
:param driver: selenium driver trying to load page
87-
:param element_id: id of element waiting for
88-
:param timeout: maximum time to wait. after this time elapsed, TimeoutException will be raised.
89-
:return: web element object waiting for
90-
"""
91-
return _get_element_after_loaded(driver, timeout, (By.ID, element_id))
9294

93-
def _get_element_after_loaded(driver, timeout, locator):
94-
return WebDriverWait(driver, timeout).until(ec.presence_of_element_located(locator))
9595

9696
def login_pf_center(driver):
9797
settings, locators = get_settings_and_locators()
9898
to_pf_center(driver)
99-
wait_until_xpath_load(driver, locators['to_login_page_btn_xpath']).click()
99+
wait_until_load(driver, 'xpath', locators['to_login_page_btn_xpath']).click()
100100
driver.find_element_by_id(locators['login_email_input_id']).send_keys(settings['admin_info']['email'])
101101
driver.find_element_by_id(locators['login_pw_input_id']).send_keys(settings['admin_info']['pw'])
102102
driver.find_element_by_id(locators['login_submit_btn_id']).click()

0 commit comments

Comments
 (0)