Search code examples
javaseleniumwysiwyg

Java - Selenium wait for text to appear in wysiwyg


i have been trying to wait for any text to appear in wysiwyg. here is my code

           WebDriverWait wait = new WebDriverWait(driver, 70);
        wait.until(new ExpectedCondition<Boolean>(){
            @Override
            public Boolean apply(WebDriver f) {
                WebElement iframeMsg = f.findElement(By.className("cke_wysiwyg_frame"));        
                f.switchTo().frame(iframeMsg);
                WebElement body = f.findElement(By.cssSelector("body"));
                return body.getText().length() != 0;

            }            
        });

from this link: Wait Till Text Present In Text Field

but its not working(program does not wait)

<iframe src="" frameborder="0" class="cke_wysiwyg_frame cke_reset" style="width: 100%; height: 100%;" title="Rich Text Editor, editor1" aria-describedby="cke_63" tabindex="0" allowtransparency="true">
	<html dir="ltr" lang="en"><head></head>
	<body contenteditable="true" class="cke_editable cke_editable_themed cke_contents_ltr cke_show_borders" spellcheck="false"><p><br></p>
	</body>
	</html>


</iframe>

this website generate content, which takes about 30 ~ 70 sec with arg of 45 sec. i want to wait for that content which will show up in body of wysiwyg. Currently in using Thread.sleep(70000)


Solution

  • I often have this problem in selenium and nothing I have tried makes the test wait the length of time it should. Rather than shutting the entire process down for that long I implement a loop and check every second:

    long start = System.currentTimeMillis(); 
            long secondsPassed = 0;
    
            while(!condition && secondsPassed < 10)
            {
                secondsPassed = (System.currentTimeMillis()-start)/1000;
                logger.trace("secondsPassed=" + secondsPassed);
    
                //Code here
    
    
                try 
                {
                    Thread.sleep(1000);
                } 
    
                catch (InterruptedException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    

    It's a bit of a hack but the only wait I can get selenium to be both responsive to what is happening and wait reliably. Of course you can tweak the wait to suit your particular needs.