2013-04-08 14 views
2

我已經看到約100 touch event examples for the Java webdriver在線,但沒有一個用於python。 有人會在這裏發佈一個,所以它可以節省很多小時的搜索? 這裏是我嘗試在Android模擬器中對元素進行基本的double_tap以放大它。非常感謝使用webdriver python觸摸事件的示例?

編輯:感謝朱利安的幫助,我能夠找出缺失的鏈接:由於某種原因,觸摸操作最後需要額外的.perform()。在下面你會發現一堆觸摸事件正在發生 - 代碼更加清晰。請享用!

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() 

這裏是一個原始的Java例子:

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()); 

回答

4

我做了一些調整,以你的榜樣,至少測試沒有錯誤運行。我不知道您的期望的做網站,當用戶雙擊水龍頭在用戶名字段...

下面是修改後的代碼:

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() 

我留在各種調試打印語句該示例向您展示我如何調查您遇到的問題。我還將網址更改爲您的登錄頁面重定向到的網址。這是針對與我在設備上安裝的Android驅動程序版本無關的問題的解決方法。

僅供參考:我在運行Android 4.0.4的Android手機上使用android-server-2.21.0.apk進行了測試。以下是對示例代碼的重大更改

 touchactions = TouchActions(self.remote) 
     print dir(touchactions) 
     touchactions.double_tap(elems) 
+0

這非常有幫助。請參閱我的原始問題以瞭解Android Webdriver的工作代碼。哇噢! – gorbysbm 2013-04-12 09:22:10

+0

方面的問題:在webdriver __init__文件中,它已經導入:從common.touch_actions導入TouchActions。爲什麼我們需要在測試模塊中再次導入觸摸操作? – gorbysbm 2013-04-12 09:35:06

+0

感謝這個額外的問題:)我從中學到了一些新東西。如果我們導入webdriver,我們*不需要導入TouchActions。它可作爲webdriver對象的一部分,例如'webdriver.TouchActions(...)'然而,因爲你的代碼分別導入了Remote和DesiredCapabilities,看起來TouchActions不在導入的命名空間中。 '從selenium導入webdriver'然後嘗試'dir(webdriver.TouchActions)'來查看方法。 FYI比較'cat py/selenium/webdriver/remote/__ init __。py'與'cat py/selenium/webdriver/__ init __。py' – JulianHarty 2013-04-13 15:42:10