def test_switch_to_confirm(self): chrome_driver = webdriver.Chrome() # 浏览器打开我们刚才新建的html文件 chrome_driver.get("file:///C:/Users/davieyang/Desktop/test_alert.html") time.sleep(3) # 点击alert按钮 chrome_driver.find_element_by_id("confirm").click() time.sleep(3) # 调用我们封装好的方法 al = Browser_Controller(chrome_driver).switch_to_alert() print(al.text) # 打印弹窗中的文本 # 相当于点击弹窗中的确定按钮,但实际并不是点击只是弹窗对象提供的方法,效果一样 al.dismiss()
def test_cookies(self): cookie_dict = {'name': 'name_yang', 'value': 'cookie_yang'} chrome_driver = webdriver.Chrome() chrome_driver.get("https://www.baidu.com") time.sleep(10) current_cookie = Browser_Controller( chrome_driver).get_current_cookies() print(current_cookie) Browser_Controller(chrome_driver).add_key_value_to_cookie(cookie_dict) key_cookie = Browser_Controller( chrome_driver).get_current_cookie_value('name_yang') print(key_cookie) Browser_Controller(chrome_driver).delete_current_cookie() current_cookie_2 = Browser_Controller( chrome_driver).get_current_cookies() print(str(current_cookie_2) + "只有这几个字没有cookie了")
def test_table_util(self): chrome_driver = webdriver.Chrome() chrome_driver.get("file:///C:/Users/Administrator/Desktop/table.html") chrome_driver.implicitly_wait(20) table_element = chrome_driver.find_element_by_id("table") string = u"用例一失败" Browser_Controller(chrome_driver).click_element_in_table( table_element, string)
def test_select(self): chrome_driver = webdriver.Chrome() chrome_driver.get("http://www.baidu.com") chrome_driver.implicitly_wait(30) mouse = chrome_driver.find_element_by_link_text("设置") ActionChains(chrome_driver).move_to_element(mouse).perform() chrome_driver.find_element_by_link_text("搜索设置").click() time.sleep(5) chrome_driver.find_element_by_name("NR").click() time.sleep(5) select = chrome_driver.find_element_by_name("NR") Browser_Controller(chrome_driver).select_by_value(select, "20") time.sleep(5) Browser_Controller(chrome_driver).select_by_index(select, 1) time.sleep(5) Browser_Controller(chrome_driver).select_by_text(select, "每页显示50条") time.sleep(5)
def test_switch_iframe(self): chrome_driver = webdriver.Chrome() chrome_driver.get("https://mail.163.com") time.sleep(10) frame = chrome_driver.find_element_by_xpath( "//*[@id='loginDiv']/iframe") Browser_Controller(chrome_driver).switch_to_iframe(frame) time.sleep(5) chrome_driver.find_element_by_name("email").send_keys("15901281916") chrome_driver.find_element_by_name("password").send_keys("Ethan005!@#") chrome_driver.find_element_by_id("dologin").click()
def test_control_browser(self): BC = Browser_Controller() # 实例化Browser_Controller类 chrome_driver = BC.driver_browser( "chrome", self.url) # 调用start_browser方法用于启动相应浏览器并打开url Browser_Controller(chrome_driver).set_browser_window(400, 800) chrome_driver.find_element_by_id("kw").send_keys("__davieyang__") chrome_driver.find_element_by_id("su").click() firefox_driver = BC.driver_browser("firefox", self.url) firefox_driver.find_element_by_id("kw").send_keys("__davieyang__") firefox_driver.find_element_by_id("su").click() ie_driver = BC.driver_browser( "ie", self.url) # 调用start_browser方法用于启动相应浏览器并打开url ie_driver.find_element_by_id("kw").send_keys("__davieyang__") ie_driver.find_element_by_id("su").click()