2012-10-16 2 views
0

Selenium Webdriver 테스트를 실행할 때 정말 이상한 문제가 있습니다.때때로 오류를 일으키는 테스트

내 코드

driver.findElement(By.id("id")).click(); 
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS); 
driver.findElement(By.xpath("//a[starts-with(@href,'/problematic_url')]")).click(); 
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS); 
driver.findElement(By.className("green_true")).click(); 

요소는 실제로 존재한다. 나는 문제가있는 URL이 webdriver에 의해 클릭된다는 것을 알 수 있지만 아무 일도 일어나지 않습니다. 브라우저가 페이지를 이동하지 않고 green_true 요소를 찾지 않습니다. 오류가 발생했습니다. 그러나 때때로. 때로는 테스트가 정상적으로 실행됩니다.

아무도이를 알 수 있습니까?

정확한 URL은 선택한 언어에 따라 다르기 때문에 사용할 수 없습니다.

답변

0

우물. 다음과 같은 방법으로 수정 제안 : 대신

driver.findElement(By.id("id")).click(); 
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS); 
driver.findElement(By.xpath("//a[starts-with(@href,'/problematic_url')]")).click(); 
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS); 
driver.findElement(By.className("green_true")).click(); 

의 시도 사용 다음

public WebElement fluentWait(final By locator){ 
     Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) 
       .withTimeout(30, TimeUnit.SECONDS) 
       .pollingEvery(5, TimeUnit.SECONDS) 
       .ignoring(NoSuchElementException.class); 

     WebElement foo = wait.until(
new Function<WebDriver, WebElement>() { 
      public WebElement apply(WebDriver driver) { 
         return driver.findElement(locator); 
       } 
       } 
); 
          return foo;    }  ; 

fluentWait(By.id("id")).click(); 
fluentWait(By.xpath("//a[starts-with(@href,'/problematic_url')]")).click(); 
fluentWait(By.className("green_true")).click(); 

문제는 당신이 prolly (요소와 상호 작용 한 후 페이지에서 클릭을 몇 가지 AJAX를 얻을 수 있습니다, 기타). IMHO 우리는 좀 더 견고한 대기 메커니즘을 사용할 필요가 있습니다.

조언 : webelement 또는 css 선택기의 xpath를 얻었을 때 fireBug, ffox 확장자에서 발견 된 locator를 확인하는 것을 잊지 마십시오. locators verify 감사합니다.

+0

같은 문제가 여전히 발생합니다. 테스트는 일반적으로 처음 실행될 때 수행되므로 드라이버 초기화와 관련이있을 수 있습니까? – mjgirl

+0

예외가 발생했을 때 소스를 제공 할 수 있습니까? –

+0

오류가 발생하면 요소를 찾을 수 없습니다. { "method": "class name", "selector": "green_true"} – mjgirl

0

동적 요소를 클릭 할 때 명시 적 대기를 사용하십시오. 요소가 웹 브라우저에 표시되거나 조치가 적용될 때까지 기다리십시오. 이 패턴을 사용할 수 있습니다 :

final FluentWait<WebDriver> wait = 
      new FluentWait<WebDriver>(getDriver()) 
        .withTimeout(MASK_PRESENCE_TIMEOUT, TimeUnit.SECONDS) 
        .pollingEvery(100, TimeUnit.MILLISECONDS) 
        .ignoring(NoSuchElementException.class) 
        .ignoring(StaleElementReferenceException.class) 
        .withMessage("Time out while waiting the element is loaded"); 

    wait.until(new Predicate<WebDriver>() { 

     @Override 
     public boolean apply(final WebDriver driver) { 
      return ! driver.findElements(By.id("id")).isEmpty(); 
     } 

    }); 
관련 문제