2012-03-14 39 views
1

我正在评估Selenium2在Internet Explorer 9上的一个项目,我选择了奔驰网站,因为它使用AJAX和一些内联弹出窗口,所以我写了一个小测试。具有动态可见性的无法点击的元素/动作

  1. 转到mercedes-benz.ch
  2. 点击到 “A”,然后在显示覆盖选择 “Konfigurator”
  3. 点击到 “Weiter>”
  4. 点击到 “>率ändern”弹出窗口会显示出来。
  5. 单击,在弹出的“i”按钮

我在我的示例代码2个问题:

  • 1:如何建立一个动作链与动态内容尚未可用于构建链 (即moveToElement(A).moveToElement(B):A使B可见,因此在构建时,B不存在)

  • 第2个:这是img元素包含在a -element,我想点击:

<a onclick="var cf = function(){ openInfo('InsurancePpi'); return false;}; 
var oamSF = function(){ return oamSubmitForm('calcForm','calcForm:j_id31');}; 
return (cf()==false) ? false : oamSF();" href="#"> 
<img src="images/mb/btn_info_rb.gif"> 
</a> 

但。点击()不会做任何事情。点击似乎被执行,但在弹出窗口应该显示的地方没有任何反应。如果我使用.sendKeys(Keys.Enter)而不是.click(),它工作得很好。

我的示例代码:

package org.test.demo; 
import org.openqa.selenium.*; 
import org.openqa.selenium.interactions.Actions; 
import org.testng.annotations.Test; 

@Test 
public class TestStep_DebugChangeRate { 
    public void test() { 
     driver.manage().deleteAllCookies(); 
     driver.get("http://www.mercedes-benz.ch"); 

     WebElement btnA = driver.findElement(By.xpath("//a[.='A']")); 
     // first problem, i cannot combine moveToElement(btnA) and 
     // moveToElement(btnKonfigurator), because btnKonfigurator is not 
     // visible at the moment of building the Action which will then fail 
     // in NoSuchElement, that's why I cheat with sendKeys(), any tips? 
     (new Actions(driver)) 
      .moveToElement(btnA) 
      .click(btnA) 
      .sendKeys(Keys.TAB) 
      .sendKeys(Keys.TAB) 
      .sendKeys(Keys.TAB) 
      .sendKeys(Keys.ENTER) 
      .build() 
      .perform();   

     performWaitBool(driver, PerformWaitExpectedConditions.isPageLoadFinished(), "timeout waitForPageLoad"); 
     WebElement btnContinue = driver.findElement(By.id("vsAppLnkContinue1")); 
     btnContinue.click(); 

     performWaitBool(driver, PerformWaitExpectedConditions.isPageLoadFinished(), "timeout waitForPageLoad"); 
     WebElement btnChangeRate = driver.findElement(By.id("vsAppLnkPcnChangeRate")); 
     btnChangeRate.click(); 

     performWaitBool(driver, PerformWaitExpectedConditions.isPageLoadFinished(), "timeout waitForPageLoad"); 
     WebElement frameInline = driver.findElement(By.xpath("//div[@class='contentsC']/iframe")); 
     WebDriver frame = driver.switchTo().frame(frameInline); 
     WebElement btnInfoPpi = frame.findElement(By.xpath("//a[contains(@onclick, 'InsurancePpi')]/img")); 
     // does not throw an error, though the info popup is not opened 
     btnInfoPpi.click(); 

     Boolean isDisplayed = btnInfoPpi.isDisplayed(); // true 
     int elementWidth = btnInfoPpi.getSize().getWidth(); // 23 
     btnInfoPpi.sendKeys(Keys.Enter); // this will open the popup however 
    } 
} 

回答

0

关于与IMG你的问题我会尝试通过

((JavascriptExecutor) driver).executeScript("script;"); 

从一个标签内执行相关JS而且我不能帮助的Cuz页面不适合我。 尝试使用Selenium IDE来获取您的操作核心。导出为Java Junit4 webdriver测试用例。这可能会帮助您找到解决方案。

+0

我主要关心的是Selenium Webdriver在这里有问题。由于我正在测试一个网站,我无法执行脚本,因为我需要模拟一个用户,所以它必须是点击图像。如果图片周围没有,测试会通过,但不正确。 Selenium IDE做了同样的事情,只需标识元素并发送.click()即可。由于某些神秘原因,它不适用于硒:( – MushyPeas 2012-03-16 17:43:20

+0

执行用户按下按钮时执行的脚本与用户按下按钮时相同,这是一种解决方法。当然,我不是那种坚信IE因为对我来说Firefox的测试效果更好;-)使用IDE你真的不知道它在幕后做了什么。因为它生成的代码通常与IDE Test不一样。 – Tarken 2012-03-17 04:02:47