2012-05-02 24 views

回答

0

我不推薦你用任何測試自動化工具自動化這些類型的測試,因爲這個測試不會是可靠的,並且由於以下主要原因不會一致傳遞。

  1. 如何確保特定元素在不同瀏覽器中不可見?

  2. 如何在不同的屏幕分辨率下運行相同的測試?

  3. 同樣在不同尺寸的顯示器上運行相同的測試?

0

@haroonzone說了些什麼。它不能可靠地完成。

這就是說,我想試試用WebElement上的getLocation()getSize()方法。

結合driver.manage().window().getSize()

或者也許有些玩JavaScript。即window,和innerWidth,document,或者可能是document.body DOM元素。如果您不希望滾動條計入視口,請使用clientWidthclientHeight

This link爲IE提供了一些高級建議,因爲IE的工作方式明顯不同。

試試吧,看看它是怎麼回事。

3

我找到了一種方法來檢查元素是否可見。

想法是獲取頁面的1.滾動位置; 2.瀏覽器可見區域的高度,以及3.元素的y位置。

scroll_position_script = """ 
     var pageY; 
     if (typeof(window.pageYOffset) == 'number') { 
      pageY = window.pageYOffset; 
     } else { 
      pageY = document.documentElement.scrollTop; 
     } 
     return pageY; 
    """ 

    yOffset = self.browser.execute_script(scroll_position_script) 

    js_client_height = "return document.documentElement.clientHeight;" 
    browser_height = self.browser.execute_script(js_client_height) 

    elem_yloc = int(element.location['y']) 
    self.assertTrue(elem_yloc>=yOffset and elem_yloc<=yOffset+browser_height) 
+0

這是一個python特定的代碼。 self.browser引用webdriver實例。最後一行確保元素的y位置位於屏幕頂部(yOffset)和底部(yOffset + browser_height)之間, –

相關問題