3
3
"""
4
4
5
5
import json
6
+ import os
6
7
7
8
from selenium import webdriver
8
9
from selenium .webdriver .common .by import By
9
10
from selenium .webdriver .common .desired_capabilities import DesiredCapabilities
10
11
from selenium .webdriver .support import expected_conditions as ec
11
12
from selenium .webdriver .support .wait import WebDriverWait
12
13
13
- from exception import NoSuchDriverException
14
+ from . exception import *
14
15
15
- DEV = False
16
16
17
+ DEV = False
18
+ current_dir = os .path .dirname (os .path .abspath (__file__ ))
17
19
18
20
def url_join (main_url , sub_url ):
19
21
"""
@@ -30,14 +32,14 @@ def url_join(main_url, sub_url):
30
32
31
33
def get_settings ():
32
34
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 :
34
36
return json .load (settings_json )
35
37
else :
36
- with open ('setting.json' , 'r' ) as settings_json :
38
+ with open (os . path . join ( current_dir , 'setting.json' ) , 'r' ) as settings_json :
37
39
return json .load (settings_json )
38
40
39
41
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 :
41
43
return json .load (locators_json )
42
44
43
45
def get_settings_and_locators ():
@@ -68,35 +70,33 @@ def get_driver(mode='phantom'):
68
70
if DEV :
69
71
driver .set_window_size (1920 , 1080 )
70
72
else :
71
- raise NoSuchDriverException ('mode should be either "chrome" or "phantom"(headless)' )
73
+ raise NoSuchDriverException ()
72
74
73
75
return driver
74
76
75
- def wait_until_xpath_load (driver , element_xpath , timeout = 5 ):
77
+ def wait_until_load (driver , by , locator , timeout = 30 ):
76
78
"""
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.
80
83
:return: web element object waiting for
81
84
"""
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 ()
83
93
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 ))
92
94
93
- def _get_element_after_loaded (driver , timeout , locator ):
94
- return WebDriverWait (driver , timeout ).until (ec .presence_of_element_located (locator ))
95
95
96
96
def login_pf_center (driver ):
97
97
settings , locators = get_settings_and_locators ()
98
98
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 ()
100
100
driver .find_element_by_id (locators ['login_email_input_id' ]).send_keys (settings ['admin_info' ]['email' ])
101
101
driver .find_element_by_id (locators ['login_pw_input_id' ]).send_keys (settings ['admin_info' ]['pw' ])
102
102
driver .find_element_by_id (locators ['login_submit_btn_id' ]).click ()
0 commit comments