def toCustom(self): driver = self.base.login() fd = FindElement(driver, '../config/LocalElement.ini') ActionChains(driver).move_to_element(fd.get_element( 'custom', 'custom')).perform() fd.get_element('custom', 'myCustom').click() return driver
class RegisterPage(object): def __init__(self, driver): self.search = FindElement(driver) def get_email_element(self): return self.search.get_element('user_email') def get_name_element(self): return self.search.get_element('user_name') def get_password_element(self): return self.search.get_element('password') def get_code_element(self): return self.search.get_element('captcha_code') def get_register_button(self): return self.search.get_element('register_btn') def get_email_error_element(self): return self.search.get_element('user_email_error') def get_name_error_element(self): return self.search.get_element('user_name_error') def get_password_error_element(self): return self.search.get_element('password-error') def get_code_error_element(self): return self.search.get_element('captcha_code-error')
def login(self,broswer='Chrome'): ''' 公用登录方法 ''' if broswer == 'Chrome': driver = webdriver.Chrome() driver.get('http://www.18crm.com') driver.maximize_window() fd = FindElement(driver,'../config/LocalElement.ini') rd = ReadIni('../config/TestData.ini') fd.get_element('login','username').send_keys(rd.get_value('public_login','username')) fd.get_element('login','password').send_keys(rd.get_value('public_login','password')) fd.get_element('login','loginButton').click() self.acceptAlert(driver) return driver
class RegisterPage(object): def __init__(self, driver): self.fd = FindElement(driver) def get_email_element(self): return self.fd.get_element('user_email') def get_username_element(self): return self.fd.get_element('user_name') def get_password_element(self): return self.fd.get_element('user_password') def get_code_element(self): return self.fd.get_element('code_text') def get_button_element(self): return self.fd.get_element('user_button') def get_email_error_element(self): return self.fd.get_element('user_email_error') def get_username_error_element(self): return self.fd.get_element('user_name_error') def get_password_error_element(self): return self.fd.get_element('user_password_error') def get_code_error_element(self): return self.fd.get_element('user_code__error') # # if __name__=='__main__': # driver=webdriver.Chrome() # driver.get('https://sso.kedacom.com') # r=RegisterPage(driver) # r.get_username_element().send_keys('*****@*****.**') # # driver.quit()
def addCustom(self): driver = self.toCustom() oldHandle = driver.current_window_handle fd = FindElement(driver, '../config/LocalElement.ini') ActionChains(driver).move_to_element(fd.get_element( 'custom', 'custom')).perform() fd.get_element('custom', 'myCustom').click() self.util.switchFrame(driver, 'frame', 'mainframe') fd.get_element('custom', 'add').click() allHandle = driver.window_handles self.util.switchWindow(driver, oldHandle, allHandle) fd.get_element('custom', 'customName').send_keys('hjkl') sleep(2)
class LoginHandle: def __init__(self, driver, filename): self.fd = FindElement(driver, filename) def usernameElement(self): return self.fd.get_element('login', 'username') def passwordElement(self): return self.fd.get_element('login', 'password') def loginBtElement(self): return self.fd.get_element('login', 'loginButton') def toRegisterElement(self): return self.fd.get_element('login', 'toRegister') def forgetPwdElement(self): return self.fd.get_element('login', 'forgetPwd') def homePageMyName(self): return self.fd.get_element('home', 'myname') def sendUsername(self, username): self.usernameElement().clear() self.usernameElement().send_keys(username) def sendPassword(self, password): self.passwordElement().clear() self.passwordElement().send_keys(password) def clickLoginBt(self): self.loginBtElement().click() def clickToRegister(self): self.toRegisterElement().click() def clickForgetPwd(self): self.forgetPwdElement().click()
def get_element(self, key): find_element = FindElement(self.driver) element = find_element.get_element(key) return element
def switchFrame(self, driver, section, key): fd = FindElement(driver, '../config/LocalElement.ini') driver.switch_to.frame(fd.get_element(section, key)) return driver
class CustomHandle: def __init__(self, driver): self.fd = FindElement(driver, '../config/LocalElement.ini') self.util = WebdriverUtil() self.base = CommonUtil() self.driver = driver def toCustom(self): #点击"我的客户" ActionChains(self.driver).move_to_element( self.fd.get_element('custom', 'custom')).perform() self.fd.get_element('custom', 'myCustom').click() #切换到主Frame self.util.switchFrame(self.driver, 'frame', 'mainframe') return self.driver def clickAdd(self): self.driver = self.toCustom() #点击新增按钮 oldHandle = self.driver.current_window_handle self.fd.get_element('custom', 'add').click() #切换到新弹出的"新增客户"页面 allHandle = self.driver.window_handles self.util.switchWindow(self.driver, oldHandle, allHandle) return self.driver def addCustom(self, username, notnull): self.driver = self.clickAdd() #输入姓名、必填项,点击确定 ,username,notnull self.fd.get_element('custom', 'customName').send_keys(username) self.fd.get_element('custom', 'notNullText').send_keys(notnull) self.fd.get_element('custom', 'save').click() def getFirstCustom(self): return self.fd.get_element('custom', 'fistCustom').text def getSecondCustom(self): return self.fd.get_element('custom', 'secondCustom').text def getCountCustom(self): element = self.fd.get_element('custom', 'totalCustom') return element.get_attribute('value') def deleteCustom(self): print(self.getCountCustom()) self.fd.get_element('custom', 'checkCustom').click() self.fd.get_element('custom', 'deleteCustom').click() self.base.acceptAlert(self.driver) sleep(5) self.base.acceptAlert(self.driver) sleep(5) print(self.getCountCustom())
class TestCase(unittest.TestCase): # @classmethod # def setUpClass(cls): def setUp(self): self.driver = webdriver.Chrome() # self.driver.get('http://sahitest.com/demo/promptTest.htm') # self.driver.get('http://sahitest.com/demo/dragDropMooTools.htm') # self.driver.get('http://sahitest.com/demo/selectTest.htm') # self.driver.get('http://sahitest.com/demo/keypress.htm') # self.driver.get('http://sahitest.com/demo/reactpage/react.html' self.driver.get('http://sahitest.com/demo/alertTest.htm') self.fd = FindElement(self.driver) def tearDown(self): self.driver.quit() # @classmethod # def tearDownClass(cls): # cls.driver.quit() # 提示框操作 def test_01(self): if WD(self.driver, 10).until( EC.text_to_be_present_in_element( self.fd.get_located('check', 'Node1', 'practice_element.ini'), 'Prompt Test')): print('True') AC(self.driver).click( self.fd.get_element('click', 'Node1', 'practice_element.ini')).perform() time.sleep(2) if WD(self.driver, 5).until(EC.alert_is_present()): AL(self.driver).send_keys('sahai test') time.sleep(1) AL(self.driver).accept() if WD(self.driver, 5).until( EC.element_to_be_clickable( self.fd.get_located('prompt_by', 'Node1', 'practice_element.ini'))): AC(self.driver).click( self.fd.get_element('back', 'Node1', 'practice_element.ini')).perform() wu = WD(self.driver, 5).until(EC.title_is('Sahi Tests')) self.assertTrue(wu) # 拖拽 def test_02(self): find_dragme = self.fd.get_element('dragme', 'Node2', 'practice_element.ini') find_ltem1 = self.fd.get_element('ltem1', 'Node2', 'practice_element.ini') find_ltem4 = self.fd.get_element('ltem4', 'Node2', 'practice_element.ini') locator_ltem1 = self.fd.get_located('ltem1', 'Node2', 'practice_element.ini') if WD(self.driver, 10).until(EC.title_is('Mootools Drag and Drop Example')): if WD(self.driver, 10).until(EC.visibility_of_element_located(locator_ltem1)): AC(self.driver).drag_and_drop(find_dragme, find_ltem1).perform() time.sleep(2) AC(self.driver).drag_and_drop(find_dragme, find_ltem4).perform() time.sleep(2) text = WD(self.driver, 5).until( EC.text_to_be_present_in_element(locator_ltem1, 'dropped')) self.assertTrue(text, '用例失败') # select_practice def test_03(self): select1_element = self.fd.get_element('select1', 'Node3', 'practice_element.ini') select2_element = self.fd.get_element('select2', 'Node3', 'practice_element.ini') select3_element = self.fd.get_element('select3', 'Node3', 'practice_element.ini') select_element = self.fd.get_element('select', 'Node3', 'practice_element.ini') # 假如下拉类型不是select则需要二次定位法来选择各个选项 # element_s = self.fd.get_element('select', 'Node3', 'practice_element.ini') # element_s.find_element_by_xpath('//*[@id="s2Id"]/option[3]').click() if WD(self.driver, 10).until(EC.visibility_of(select1_element)): Select(select1_element).select_by_index(1) time.sleep(1) Select(select2_element).select_by_value('o2') time.sleep(1) Select(select3_element).select_by_visible_text('o3') time.sleep(1) # Select(select_element).first_selected_option() # Select(select1_element).deselect_by_index(1) # Select(select_element).all_selected_options() # a=EC.element_selection_state_to_be(select1_element,WE.is_selected()) time.sleep(3) # self.assertTrue(result) # key press def test_04(self): see_located = self.fd.get_located('SeeResult', 'Node4', 'practice_element.ini') enter_here = self.fd.get_element('EnterHere', 'Node4', 'practice_element.ini') check_submit = self.fd.get_element('CheckSubmit', 'Node4', 'practice_element.ini') submit = self.fd.get_element('Submit', 'Node4', 'practice_element.ini') keyup = self.fd.get_element('KeyUp', 'Node4', 'practice_element.ini') keypress = self.fd.get_element('KeyPress', 'Node4', 'practice_element.ini') if WD(self.driver, 10).until(EC.visibility_of_element_located(see_located), '有效时间内没有进入页面'): AC(self.driver).send_keys_to_element( enter_here, '大家好').send_keys_to_element(check_submit, 'qiehuan').click(submit).perform() time.sleep(2) AC(self.driver).key_down( Keys.CONTROL, enter_here).send_keys('a').key_up(Keys.CONTROL).send_keys( Keys.BACKSPACE).send_keys('new').perform() if WD(self.driver, 10).until(EC.visibility_of(keyup)): AC(self.driver).click(keyup).send_keys(Keys.DOWN, Keys.DOWN, Keys.ENTER).perform() time.sleep(2) result = keypress.is_selected() self.assertTrue(result) time.sleep(2)