2015-10-19 105 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); 
       }) 
     }); 
}); 
相关问题