Thursday 11 December 2014

How to wait until the element is present or load a banner in selenium java cucumber

Here the way how to wait until the element is present .

there are many ways to do this..

Thread.sleep(1000) is old static way of doing...

If you are waiting to load any image or banner or progress bar then you have to use javascriptexecutor technique

Another trick is to realise that the WebDriver instances implement JavascriptExecutor. This interface can be used to execute arbitrary Javascript within the context of the browser (a trick selenium finds much easier). This allows us to use the state of javascript variables to control the test. For example – we can have a variable populated once some asynchronous action has completed; this can be the trigger for the test to continue.

First, we add some Javascript to our example page above. Much as before, it simply loads a new page and updates the <div> with this content. This time, rather than show the div, it simply sets a variable – “working” – so the div can be visible already.

   

 /**
 * Call this method before an event that will change the page.
 */
private void beforePageLoad() {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("document.mpPageReloaded='notYet';");
}

/**
 * Call this method after an event that will change the page.
 *
 * @see #beforePageLoad
 *
 *      Waits for the previous page to disappear.
 */
private void afterPageLoad() throws Exception {
    (new WebDriverWait(driver, 10)).until(new Predicate<WebDriver>() {

        @Override
        public boolean apply(WebDriver driver) {
            JavascriptExecutor js = (JavascriptExecutor) driver;
            Object obj = js.executeScript("return document.mpPageReloaded;");
            if (obj == null) {
                return true;
            }
            String str = (String) obj;
            if (!str.equals("notYet")) {
                return true;
            }
            return false;
        }
    });
}

 http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/JavascriptExecutor.html

http://stackoverflow.com/questions/5868439/wait-for-page-load-in-selenium

Way 1:

WebDriverWait wait = new WebDriverWait(driver, 15);
  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath here")));
 
  System.out.print("Text box text3 is now visible");

Way 2:

In webdriver:
driver.findElement(By.id("")).isSelected or Enabled or Displayed)
{
//actions
Do some thing...
}

Way 3:

 //To verify element is present on page or not.
   String XPath = "//input[@id='text"+i+"']";
   Boolean iselementpresent = driver.findElements(By.xpath(XPath)).size()!= 0;
   if (iselementpresent == true)
   {
    System.out.print("element present..");
   }
   else
   {
    System.out.print("Element is not present");
   }


Way 4:

  if(selenium.isElementPresent(objectId)) 
//** it will check for the ObjectID in the Input sheet
                    {
                        driver.findElement(By.id(objectId)).clear();
 //** it will execute ,once it got the respective ObjectID

 }

Ref

http://stackoverflow.com/questions/20903231/selenium-wait-until-element-is-present

http://www.selenium-automation.co.uk/blog/selenium-iselementpresent-in-webdriver/

http://stackoverflow.com/questions/19851518/selenium-webdriver-wait-for-element-to-be-present-when-locating-with-webdriver

No comments:

Post a Comment