与蟒蛇的webdriver触摸事件的例子吗? [英] Example of touch event with webdriver python?
问题描述
我看到约100 触摸事件的例子为Java的webdriver在线的,但没有一个为Python。
会有人这么好心,张贴在这里的,所以它可以节省很多的人搜索时间的?
这是我尝试做一个基本的DOUBLE_TAP一个元素在Android模拟器,以放大它。不胜感谢
编辑:多亏了朱利安的帮助下,我能找出缺少的环节:由于某些原因,触摸操作需要一个额外的 .perform()的结尾。下面你会发现一堆动作触摸事件的 - 和code是清洁的。尽情享受吧!
导入单元测试,时间
硒进口的webdriver打印这是我们可以触摸操作(忽略看起来像__xx__的那些):DIR(webdriver.TouchActions)
#PRINT目录(的webdriver)类测试(unittest.TestCase的):
高清设置(个体经营):
self.driver = webdriver.Remote(command_executor =的http://本地主机:8080 / WD /中心,desired_capabilities = webdriver.DesiredCapabilities.ANDROID)
self.touch = webdriver.TouchActions(self.driver) #self.driver = TouchActions(self.driver)
#self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
高清testHotmail(个体经营):
self.driver.get(http://www.hotmail.com) ELEM = self.driver.find_element_by_css_selector(输入[名称='登录'])
#tap命令
self.touch.tap(ELEM).perform()
time.sleep(2)
elem.send_keys(世界你好)
time.sleep(2)
#双击
self.touch.double_tap(ELEM).perform()
time.sleep(2) #testing,经常webdriver的命令仍然有效
打印self.driver.find_element_by_partial_link_text(无法访问)。文本 ELEM = self.driver.find_element_by_css_selector(输入[类型=提交])
self.touch.tap(ELEM).perform()
time.sleep(3)
高清拆解(个体经营): time.sleep(3) 尝试:
self.driver.quit()
除例外情况:
打印(TearDown中的方法:浏览器似乎已经关闭。) 通过
如果__name__ ==__main__:
unittest.main()
下面是一个原始的Java示例:
WebElement toFlick = driver.findElement(By.id(图像));
// 400像素离开以正常速度
动作片= getBuilder(司机).flick(toFlick,0,-400,FlickAction.SPEED_NORMAL)
。建立();
flick.perform();
WebElement secondImage = driver.findElement(secondImage);
assertTrue(secondImage.isDisplayed());
我做了一些调整,以你的榜样,至少在测试运行没有错误。我不知道你期望的网站做什么,当用户双击水龙头在用户名字段...
下面是修改code:
导入单元测试,时间从selenium.webdriver导入远程
从selenium.webdriver进口DesiredCapabilities
从selenium.webdriver.remote进口webelement,命令
从selenium.webdriver.common.action_chains进口ActionChains
从selenium.webdriver.common.touch_actions进口TouchActions
类测试(unittest.TestCase的):
高清设置(个体经营):
远程=远程(command_executor =的http://本地主机:8080 / WD /中心,desired_capabilities = DesiredCapabilities.ANDROID)
self.remote =远程
remote.implicitly_wait(30) 高清拆解(个体经营):
通过
高清测试名(个体经营):
#self.remote.get(http://icd.intraxinc.com/pxr)
self.remote.get(https://icd.intraxinc.com/pxr/ext/login.action)
elems = self.remote.find_element_by_css_selector(#为j_username)
打印目录(个体经营)
打印目录(self.remote)
touchactions = TouchActions(self.remote)
打印目录(touchactions)
touchactions.double_tap(elems)
如果__name__ ==__main__:
#进口SYS; sys.argv中= ['','Test.testName']
unittest.main()
我留下的各种调试打印语句中的例子来告诉你我是如何考察你所面临的问题。我也改变了URL到您的登录页面重定向到一个。这是一个不相关的问题,我曾与一个版本的Android驱动程序,安装在设备上的解决方法。
FYI:我与Android的服务器2.21.0.apk测试运行Android 4.0.4的Android手机上。以下是你的榜样,code材质的变化
touchactions = TouchActions(self.remote)
打印目录(touchactions)
touchactions.double_tap(elems)
I have seen about 100 touch event examples for the Java webdriver online, but not a single one for python. Would someone be so kind as to post one here, so it saves people many hours of search? Here is my attempt to do a basic double_tap on an element in an android simulator in order to zoom in on it. Much thanks
EDIT: Thanks to Julian's help I was able to figure out the missing link: for some reason, the touch actions require an extra .perform() at the end. Below you will find a bunch of touch events in action--and the code is cleaner. Enjoy!
import unittest, time
from selenium import webdriver
print "Here are our available touch actions (ignore the ones that look like __xx__): ", dir(webdriver.TouchActions)
#print dir(webdriver)
class Test(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Remote(command_executor='http://localhost:8080/wd/hub', desired_capabilities=webdriver.DesiredCapabilities.ANDROID)
self.touch =webdriver.TouchActions(self.driver)
#self.driver = TouchActions(self.driver)
#self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
def testHotmail(self):
self.driver.get("http://www.hotmail.com")
elem=self.driver.find_element_by_css_selector("input[name='login']")
#tap command
self.touch.tap(elem).perform()
time.sleep(2)
elem.send_keys("hello world")
time.sleep(2)
#double tap
self.touch.double_tap(elem).perform()
time.sleep(2)
#testing that regular webdriver commands still work
print self.driver.find_element_by_partial_link_text("Can't access").text
elem= self.driver.find_element_by_css_selector("input[type='submit']")
self.touch.tap(elem).perform()
time.sleep(3)
def tearDown(self):
time.sleep(3)
try:
self.driver.quit()
except Exception:
print(" TearDown Method: Browser seems already closed.")
pass
if __name__ == "__main__":
unittest.main()
Here is an original Java Example:
WebElement toFlick = driver.findElement(By.id("image"));
// 400 pixels left at normal speed
Action flick = getBuilder(driver).flick(toFlick, 0, -400, FlickAction.SPEED_NORMAL)
.build();
flick.perform();
WebElement secondImage = driver.findElement("secondImage");
assertTrue(secondImage.isDisplayed());
I've made some tweaks to your example, at least the test runs without error. I don't know what you expect the web site to do when a user double-taps in the username field...
Here is the revised code:
import unittest, time
from selenium.webdriver import Remote
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.remote import webelement , command
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.touch_actions import TouchActions
class Test(unittest.TestCase):
def setUp(self):
remote = Remote(command_executor='http://localhost:8080/wd/hub', desired_capabilities=DesiredCapabilities.ANDROID)
self.remote=remote
remote.implicitly_wait(30)
def tearDown(self):
pass
def testName(self):
# self.remote.get("http://icd.intraxinc.com/pxr")
self.remote.get("https://icd.intraxinc.com/pxr/ext/login.action")
elems= self.remote.find_element_by_css_selector("#j_username")
print dir(self)
print dir(self.remote)
touchactions = TouchActions(self.remote)
print dir(touchactions)
touchactions.double_tap(elems)
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
I left various debug print statements in the example to show you how I investigated the problem you were facing. I also changed the URL to the one your login page redirects to. This was a workaround for an unrelated problem I had with a version of the Android driver, installed on the device.
FYI: I tested with android-server-2.21.0.apk on an Android phone running 4.0.4 of Android. Here are the material changes to your example code
touchactions = TouchActions(self.remote)
print dir(touchactions)
touchactions.double_tap(elems)
这篇关于与蟒蛇的webdriver触摸事件的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!