Tuesday, January 29, 2013

WebDriver waits


Web application automation is depends on so many factors. Browser, network speed ...etc. We should write unquie code for running in all environments. For achieveing that we need to wait for WebElements before performing any operation on that.

Here we will see some built-in waits in WebDriver.

implicitlyWait

1WebDriver driver = new FirefoxDriver();
2driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
3driver.get("http://www.google.co.in");
4WebElement myElement = driver.findElement(By.id("someId"));
Implicit waits are basically your way of telling WebDriver the latency that you want to see if specified web element is not present that WebDriver looking for. (Click here for more information). This will be useful when certain elements on the webpage will not be available immediately and needs some time to load.

pageLoadTimeout

1driver.manage().timeouts().pageLoadTimeout(30, SECONDS);
Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

setScriptTimeout

1driver.manage().timeouts().setScriptTimeout(30,SECONDS);

Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error.


Regards,
SantoshSarma

Monday, January 28, 2013

isTextPresent() ? In WebDriver

While automation web applications in some places we want/need to check particular text is present or not.

In WebDriver (Selenium 2), there is no predefined method for checking this. (In Selenium-RC isTextPresent("text") built-in method exist ) So we need to implement our own method to achieve this.


Let us consider you are searching for text "WebDriver"



boolean isTextPrest=false;

Method-1
 isTextPrest=driver.findElement(By.tagName("body")).getText().contains("WebDriver");  

Method-2
 isTextPrest=driver.findElement(By.xpath("//*[contains(.,'WebDriver')]")).isDisplayed();  

Method-3
 Selenium sel=new WebDriverBackedSelenium(driver, "");  
 isTextPresent=sel.isTextPresent("WebDriver");  


FYI : Method-1 will take more time than remaining (comparatively)

Happy Coding :)


Regards,
SantoshSarma

XPath indexing

Q : What does //td[2] mean?
A : All td elements in that page (document), that are second child of their parent.


Q : Will //td[2] return only one element?
A :  No, there may be many such elements. (second child td of their parent)


 if you've any doubts regarding this xpath indexing post it in comments section.



Related Topics
How to write xpath?
Locating elements using xpath




Tuesday, January 22, 2013

Finding elements within iframe using firebug


Firebug Tip

Firebug command line allows executing JavaScript expressions within context of the current page. Firebug also provides a set of built in APIs and one of them is cd().

Lets consider below iframe

                <iframe id="testID" name="testName" src="iframe.html" />


  • cd(document.getElementById("testID").contentWindow); switch to a frame by ID
  • cd(window.frames[0]); switch to the first frame in the list of frames
  • cd(window.frames["testName"]); switch to a frame using by name
  • cd(window.top); switching back to the top level window


Happy Coding .. :)


Regards,
SantoshSarma

Thursday, January 3, 2013

isElementPresent? Method another way





“we can use the findElements() call, and then we just need to check that the size of the list returned is 0.

You can write logic like this.

List elements = driver.findElements(By.Id("element id"));
if(elements.size()==0)
        No element found
else
        Element found



Monday, November 19, 2012

How to take screen shot ?

Do you want to take screenshots while automation running? TakesScreenshot interface for capturing a screenshot
of a web page. If take Screenshot when en exception or failure occurred then it will be easy to pin point issue. We can also take screenshot for verifying values, text in webPage.

Using below method you can take screenshots while running the automation.

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.TakesScreenshot;

 public static void takeScreenShot(WebDriver driver,String fileName)  
      {  
           try {  
                File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
                FileUtils.copyFile(scrFile, new File("C:\\"+fileName+".png"));  
           }   
           catch (Exception e) {  
                e.printStackTrace();  
           }  
      }  

Saturday, October 13, 2012

Implicit wait Vs. Explicit wait

WebDriver Waits
Implicit wait

WebDriver driver = new FirefoxDriver();  
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Implicit waits are basically your way of telling WebDriver the latency that you want to see if specified web element is not present that WebDriver looking for. So in this case, you are telling WebDriver that it should wait 10 seconds in cases of specified element not available on the UI (DOM).


  • Implicit wait time is applied to all elements in your script



Explicit wait (By Using FluentWait)

public static WebElement explicitWait(WebDriver driver,By by)  
{  
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)  
             .withTimeout(20, TimeUnit.SECONDS)  
             .pollingEvery(2, TimeUnit.SECONDS)  
             .ignoring(NoSuchElementException.class); 

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


Explicit waits are intelligent waits that are confined to a particular web element. Using explicit waits you are basically telling  WebDriver at the max it is to wait for X units of time before it gives up.

  •  In Explicit you can configure, how frequently (instead of 2 seconds) you want to check condition
  • Explicit wait time is applied only for particular specified element.
Explicit wait

public static WebElement explicitWait(WebDriver driver,By by)  
 {  
      WebDriverWait wait = new WebDriverWait(driver, 30);  
      wait.until(ExpectedConditions.presenceOfElementLocated(by));  
 }       

In above method it will wait until either ExpectedConditions become true or Timeout (30 sec).