code

Selenium — 페이지가 완전히로드 될 때까지 기다리는 방법

codestyles 2020. 12. 25. 09:52
반응형

Selenium — 페이지가 완전히로드 될 때까지 기다리는 방법


이 질문에 이미 답변이 있습니다.

Java 및 Selenium WebDriver를 사용하여 일부 테스트 사례를 자동화하려고합니다. 다음과 같은 시나리오가 있습니다.

  • '제품'이라는 페이지가 있습니다. '제품'페이지에서 '상세보기'링크를 클릭하면 해당 상품의 상세 정보가 포함 된 팝업 (모달 대화 상자)이 나타납니다.
  • 팝업에서 '닫기'버튼을 클릭하면 팝업이 닫히고 페이지가 자동으로 새로 고침됩니다 (페이지는 새로 고침 중이며 내용은 변경되지 않은 상태로 유지됩니다).
  • 팝업을 닫은 후 같은 페이지에서 'Add Item'버튼을 클릭해야합니다. 하지만 WebDriver가 '항목 추가'버튼을 찾으려고 할 때 인터넷 속도가 너무 빠르면 WebDriver가 해당 요소를 찾아 클릭 할 수 있습니다.

  • 그러나 인터넷 속도가 느린 경우 WebDriver는 페이지를 새로 고치기 전에 단추를 찾지 만 WebDriver가 단추를 클릭하자마자 페이지가 새로 고쳐지고 StaleElementReferenceException발생합니다.

  • 다른 대기를 사용하더라도 페이지가 다시로드되고 StaleElementReferenceException발생 하기 전에도 모든 대기 조건이 참이됩니다 (페이지의 내용이 다시로드 전후에 동일하므로) .

Thread.sleep(3000);'항목 추가'버튼을 클릭하기 전에를 사용 하면 테스트 케이스가 제대로 작동 합니다. 이 문제에 대한 다른 해결 방법이 있습니까?


결합 할 수있는 3 가지 답변 :

  1. 웹 드라이버 인스턴스를 만든 후 즉시 암시 적 대기를 설정합니다.

    driver.manage().timeouts().implicitlyWait()

    모든 페이지 탐색 또는 페이지 새로 고침시 페이지가 완전히로드 될 때까지 기다리려고합니다.

  2. 페이지 탐색 후 가 반환 return document.readyState될 때까지 JavaScript를 호출 "complete"합니다. 웹 드라이버 인스턴스는 JavaScript 실행기로 사용할 수 있습니다. 샘플 코드 :

    씨#

    new WebDriverWait(driver, MyDefaultTimeout).Until(
    d => ((IJavaScriptExecutor) d).ExecuteScript("return document.readyState").Equals("complete"));
    

    자바

    new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
          webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
    
  3. URL이 예상 한 패턴과 일치하는지 확인하십시오.


"추가"버튼을 클릭하기 전에 페이지가 다시로드 될 때까지 기다려야하는 것 같습니다. 이 경우 새로 고침 된 요소를 클릭하기 전에 "항목 추가"요소가 오래 될 때까지 기다릴 수 있습니다.

WebDriverWait wait = new WebDriverWait(driver, 20);
By addItem = By.xpath("//input[.='Add Item']");

// get the "Add Item" element
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));

//trigger the reaload of the page
driver.findElement(By.id("...")).click();

// wait the element "Add Item" to become stale
wait.until(ExpectedConditions.stalenessOf(element));

// click on "Add Item" once the page is reloaded
wait.until(ExpectedConditions.presenceOfElementLocated(addItem)).click();

항목 추가를 클릭하기 전에 여러 가지 방법으로이를 수행 할 수 있습니다.

WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.elementToBeClickable(By.id("urelementid")));// instead of id u can use cssSelector or xpath of ur element.


or

wait.until(ExpectedConditions.visibilityOfElementLocated("urelement"));

이렇게 기다릴 수도 있습니다. 이전 페이지 요소가 보이지 않을 때까지 기다리려면 :

wait.until(ExpectedConditions.invisibilityOfElementLocated("urelement"));

여기에 대기 및 문서화에 사용할 수있는 모든 셀레늄 웹 드라이버 API를 찾을 수있는 링크가 있습니다.

https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html


yes stale element error is thrown when (taking your scenario) you have defined locator strategy to click on 'Add Item' first and then when you close the pop up the page gets refreshed hence the reference defined for 'Add Item' is lost in the memory so to overcome this you have to redefine the locator strategy for 'Add Item' again

understand it with a dummy code

// clicking on view details 
driver.findElement(By.id("")).click();
// closing the pop up 
driver.findElement(By.id("")).click();


// and when you try to click on Add Item
driver.findElement(By.id("")).click();
// you get stale element exception as reference to add item is lost 
// so to overcome this you have to re identify the locator strategy for add item 
// Please note : this is one of the way to overcome stale element exception 

// Step 1 please add a universal wait in your script like below 
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // just after you have initiated browser

There are two different ways to use delay in selenium one which is most commonly in use. Please try this:

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

second one which you can use that is simply try catch method by using that method you can get your desire result.if you want example code feel free to contact me defiantly I will provide related code

ReferenceURL : https://stackoverflow.com/questions/36590274/selenium-how-to-wait-until-page-is-completely-loaded

반응형