2015-10-19 158 views
0

我要創建與SeleniumWebDriverJSJasmine用於顯示連接到div點擊按鈕只有當div內容的功能測試,並隱藏所有其他元素與id相同。選擇具有相同ID的多個元素 - WebDriverJS

This是需要測試的功能,這是迄今爲止我已經寫了測試的一個片段:

it('should display "BILL2" when the second picture is clicked and hide the others', function() { 
    driver.findElement(webdriver.By.css('#img_bill2')).click() 
     .then(function(){ 
      driver.findElement(webdriver.By.css('#bill2')).isDisplayed() 
       .then(function(displayed) { 
        expect(displayed).toBe(true); 
       }); 
     }) 
     .then(function() { 
      driver.findElement(webdriver.By.xpath('//div[@class="hide", @style="display:none"]')).isDisplayed() 
       .then(function(displayed) { 
        expect(displayed).toBe(false); 
       }) 
     }); 
}); 

我堅持在這裏,因爲我想選擇所有的元素,除了當前顯示的那個。

你對如何解決這個問題有想法嗎?也許使用executeScript()並創建一個腳本來定位所有divsdisplay:none風格可以解決嗎?

在此先感謝您的答覆!

+0

不是找到一個元素,而是找到所有具有相同xpath的元素並聲明元素的個數。由於所有元素都有'@ style =「display:none」'來確保這些元素不被顯示。 –

+0

是的,這是我解決它的方法。我簡單地使用了'xpath('// div [contains(@class,「hide」)並且包含了(@style,「display:none」)]')'和ti。由於你的解決方案有效,你是第一個評論,你可以發佈它作爲答案,並獲得積分:) –

+0

Plz接受我的答案。 :) –

回答

1

我寧願通過計算不可見元素的數量來解決此問題,而不使用.isDisplayed()。由於所有元素都有display:none,這將確保元素不被顯示。

it('should display "BILL2" when the second picture is clicked and hide the others', function() { 
    driver.findElement(webdriver.By.css('#img_bill2')).click() 
     .then(function(){ 
      driver.findElement(webdriver.By.css('#bill2')).isDisplayed() 
       .then(function(displayed) { 
        expect(displayed).toBe(true); 
       }); 
     }) 
     .then(function() { 
      driver.findElements(webdriver.By.xpath('//div[contains(@class, "hide") and contains(@style, "display: none")]')) 
       .then(function(elements) { 
        expect(elements.length).toBe(expectedCount); 
       }) 
     }); 
}); 
相關問題