def __init__(self, base_url): options = webdriver.ChromeOptions() options.add_argument("headless") options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") self.inner = webdriver.Chrome(options=options) self.base_url = base_url self.wait_timeout = 5 self.touch_actions = webdriver.TouchActions(self.inner)
def huadongdate(): driver.get("http://www.jq22.com/yanshi4976") sleep(2) driver.switch_to.frame("iframe") driver.find_element_by_css_selector("#appDate").click() #定位要滑动的年月日 element = driver.find_elements_by_css_selector(".dwwo") year = element[0] mouth = element[1] day = element[2] action = webdriver.TouchActions(driver) action.scroll_from_element(year, 0, 5).perform() action.scroll_from_element(mouth, 0, 4).perform() action.scroll_from_element(day, 0, 10).perform() driver.find_elements_by_css_selector(".dwb")[0].click()
def selectTime(): driver = webdriver.Chrome() driver.get("http://www.jq222.com/yanshi14976") sleep(2) driver.switch_to.frame("iframe") driver.find_element_by_id("appDate").click() #定位要滑动的年、月、日 dwwos = driver.find_elements_by_class_name("dwwo") year = dwwos[0] month = dwwos[1] day = dwwos[2] action = webdriver.TouchActions(driver) action.scroll_from_element(year, 0, 5).perfomr() action.scroll_from_element(month, 0, 30).perfomr() action.scroll_from_element(day, 0, 30).perfomr()
def setUp(self): #unittest self.id prints out the full test name and file. self.name_of_tst = self.id().split('.')[-1] self.party_info_dict = {} self.run_config = "" #self.driver = webdriver.Remote({"browserName":"iexplore", "platform":"WINDOWS"}) print("*" * 88) if len(sys.argv) > 1: args = sys.argv port = args[1] platform = args[2] browser = args[3] self.run_config = args[4] #Enable Touch Actions for mobile tests if platform == 'ANDROID' and browser == 'android': self.driver = webdriver.Remote( command_executor='http://localhost:8080/wd/hub', desired_capabilities=webdriver.DesiredCapabilities.ANDROID) self.touch = webdriver.TouchActions(self.driver) else: self.driver = webdriver.Remote("http://localhost:4444/wd/hub", desired_capabilities={ "browserName": browser, "platform": platform, "node": port }) else: print "No command line args passed in. Running regular Webdriver" self.driver = webdriver.Firefox() #self.driver = webdriver.Ie() self.driver.implicitly_wait(7) self.now = datetime.datetime.now() print("RUNNING TEST: ", self.name_of_tst, " time is: ", self.now.strftime("%Y-%m-%d %H:%M:%S")) self.test_number = 1 # for having different tests continue past fail for data driven tests driver = self.driver print
from NSDAEvent.cases.login import Login from NSDAEvent.cases.rules import Rules from NSDAEvent.cases.judge import Judge from NSDAEvent.cases.room import Room from NSDAEvent.cases.check import Check if __name__ == '__main__': wait_time = Data.wait_time driver = webdriver.Chrome() driver.maximize_window() driver.get(Data.url) WebDriverWait(driver, wait_time).until( lambda driver: driver.find_element(LoginLocator.EMAIL_INPUT_BOX[0], LoginLocator.EMAIL_INPUT_BOX[1])) driver.implicitly_wait(Data.implicitly_wait_time) action = webdriver.TouchActions(driver) InitDriver.set_driver(InitDriver, driver=driver) InitDriver.set_action(InitDriver, action=action) #-----------------------------执行测试用例---------------------------------------------------- suite_check = unittest.TestLoader().loadTestsFromTestCase(Check) #检查赛事配置 suite_login = unittest.TestLoader().loadTestsFromTestCase(Login) #登录赛事管理员 suite_rules = unittest.TestLoader().loadTestsFromTestCase(Rules) #设置比赛规则 suite_judge = unittest.TestLoader().loadTestsFromTestCase(Judge) #添加裁判测试用例 suite_room = unittest.TestLoader().loadTestsFromTestCase(Room) #添加教室测试用例 suite = unittest.TestSuite([suite_check, suite_login, suite_rules]) unittest.TextTestRunner(verbosity=2).run(suite)
def action(self): action = webdriver.TouchActions(self.driver) return action
desired_caps['app-package'] = 'com.example.android.apis' desired_caps['app-activity'] = '.ApiDemos' driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) el = driver.find_element_by_partial_link_text("Animat") assert el.text == "Animation" el = driver.find_element_by_class_name("android.widget.TextView") assert el.text == "Accessibility" el = driver.find_element_by_link_text("App") el.click() time.sleep(THINK_TIME) els = driver.find_elements_by_class_name("android.widget.TextView") assert els[1].text == "Activity" driver.back() time.sleep(THINK_TIME) el = driver.find_element_by_link_text("Animation") flick = webdriver.TouchActions(driver).flick_element(el, 0, -100, 0) flick.perform() el = driver.find_element_by_link_text("Views") el.click() time.sleep(THINK_TIME) driver.quit()
def is_element_present(driver, by, locator): try: driver.find_element(by, locator) except NoSuchElementException: return False return True dr = webdriver.Chrome() wait = WebDriverWait(dr, 2, poll_frequency=0.1) wait.until() wait.until_not() actions = webdriver.ActionChains(dr) ta = webdriver.TouchActions(dr) # dr.implicitly_wait(5) dr.get("https://google.com") assert is_element_present(dr, By.CLASS_NAME, "gNO89b") dr.find_element() # link = dr.find_element_by_css_selector("div#SIvCob a") # link.click() el = dr.find_element_by_css_selector('.gLFyf.gsfi[type="text"]') el.clear() el.send_keys("python") el.send_keys(Keys.HOME) # el.send_keys(Keys.ARROW_LEFT)
def find_element_by_suffix(driver, key, val, buffer=None): try: function = key.split('_')[0] option = key.split('_')[-1] isSyn = lambda option: True if option == 'syn' else False if function == 'url': driver.get(val) #old_page = driver.find_element_by_tag_name('html') #ui.WebDriverWait(driver, time_out).until(EC.staleness_of(old_page)) elif function == 'click': if isSyn(option): element = find_element_by_syn(driver, val.split(',')) else: locator = find_locator_by_option(option, val) ui.WebDriverWait(driver, time_out).until( EC.visibility_of_element_located(locator)) element = EC.element_to_be_clickable(locator)(driver) element.click() elif function == 'sendkeys': if isSyn(option): element = find_element_by_syn(driver, val.split(',')[1:]) else: locator = find_locator_by_option(option, val.split(',')[-1]) ui.WebDriverWait(driver, time_out).until( EC.visibility_of_element_located(locator)) element = EC.visibility_of_element_located(locator)(driver) element.send_keys(val.split(',')[0]) elif function == 'none': if isSyn(option): find_element_by_syn(driver, val.split(',')) else: locator = find_locator_by_option(option, val) ui.WebDriverWait(driver, time_out).until( EC.visibility_of_element_located(locator)) EC.visibility_of_element_located(locator)(driver) elif function == 'get': if isSyn(option): element = find_element_by_syn(driver, val.split(',')) else: locator = find_locator_by_option(option, val) ui.WebDriverWait(driver, time_out).until( EC.visibility_of_element_located(locator)) element = EC.visibility_of_element_located(locator)(driver) buffer[key] = element.text elif function == 'getAttribute': if isSyn(option): element = find_element_by_syn(driver, val.split(',')) else: locator = find_locator_by_option(option, val.split(',')[-1]) ui.WebDriverWait(driver, time_out).until( EC.visibility_of_element_located(locator)) element = EC.visibility_of_element_located(locator)(driver) buffer[key] = element.get_attribute(val.split(',')[0]) elif function == 'getByfilter': if isSyn(option): element = find_element_by_syn(driver, val.split(',')) else: locator = find_locator_by_option(option, val.split(',')[-1]) ui.WebDriverWait(driver, time_out).until( EC.visibility_of_element_located(locator)) element = EC.visibility_of_element_located(locator)(driver) myfilter = eval(val.split(',')[0]) buffer[key] = myfilter(element.text) elif function == 'verify': if isSyn(option): element = find_element_by_syn(driver, val.split(',')) else: verify_context = "" if val.split(',')[0][0] == '$': verify_context = buffer[val.split(',')[0][1:]] else: verify_context = val.split(',')[0] locator = find_locator_by_option(option, val.split(',')[-1]) ui.WebDriverWait(driver, time_out).until( EC.visibility_of_element_located(locator)) element = EC.visibility_of_element_located(locator)(driver) if verify_context not in element.text: raise VarFail("Verify failed:" + verify_context) elif function == 'touch': if isSyn(option): element = find_element_by_syn(driver, val.split(',')) else: touch = webdriver.TouchActions(driver) locator = find_locator_by_option(option, val) ui.WebDriverWait(driver, time_out).until( EC.visibility_of_element_located(locator)) element = EC.visibility_of_element_located(locator)(driver) touch.tap(element).perform() elif function == 'script': js = val.split(',')[0] if len(val.split(',')) == 1: driver.execute_script(js) else: parms = map(lambda pa: buffer[pa[1:]] if pa[0] == '$' else pa, val.split(',')[1:]) driver.execute_script(js, parms) elif function == 'post': account, password, method = val.split(',')[:3] parms = map(lambda pa: buffer[pa[1:]] if pa[0] == '$' else pa, val.split(',')[3:]) eval("WebsiteHelper()()." + method)(*parms) elif function == 'alert': alert = driver.switch_to.alert eval("alert." + val + "()") except Exception, ex: raise VarAbort(ex.__str__())