Thursday, 16 May 2013

Print Eclipse IDE console output on a text file after Test execution


Following setting will print your eclipse console output on a separate Text file.

1| Create a text file (.txt) in your project folder
2| In Eclipse, right click Project > Run configurations
3| Click the Tab, "Common"
4| Select the Check boxes, File & Append
5| Now choose the correct directory and select the empty text file you created before
6| Click ok
7| Run the Test
It looks simple and an eclipse configuration :)

Locate and select Auto suggest on search field for a List item (/li) using WebDriver

The below code is for searching a text automatically from the auto suggest; mainly for a list item.

driver.get("http://www.indiabookstore.net");
driver.findElement(By.id("searchBox")).sendKeys("Alche");
Thread.sleep(3000);
List <WebElement> listItems = driver.findElements(By.xpath("/html/body/div[4]/ul/li"));
listItems.get(0).click();
driver.findElement(By.id("searchButton")).click();
    
Note:  We can also repalce the xpath locator,
By.xpath("/html/body/div[4]/ul/li") with By.xpath("//div[4]/ul/li")

It's not a better way to use the above xpath locator; meanwhile these locators can be replaced with one of the following options (use csslocators for better solution).
List <WebElement> listItems = driver.findElements(By.xpath("//div[contains(@class,'acResults')]//li"));    
List <WebElement> listItems = driver.findElements(By.xpath("//div[@class='acResults']//li"));
List <WebElement> listItems = driver.findElements(By.cssSelector(".acResults li"));
List<WebElement> link = driver.findElement(By.id("Element")).findElements(By.tagName("li"));

get(0) is the first option displayed on searching keywords
get(1) is the second option displayed on searching keywords















Anchor Tag :

HTML

<head>
<body id="data-search" class="hassidebar">
<ul id="material-result-list" style="top: 183px; left: 396.5px; width: 270px; display: block;">
<li>
<a>nitrate/0.2</a>
</li>
</ul>

CODE

Here, we need to click on specific anchor tag.

List<WebElement> listItems = driver.findElement(By.id("material-result-list")).findElements(By.tagName("a")); 
listItems.get(2).click();

Thursday, 2 May 2013

Handle iFrames | Selenium

It's easy to work with iFrames.  First, try to find all the iFrames available in web page;  Next, switch into the iframe and then come out of the iframe.


Snippet

try{
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[id='Value']")));
      new WebDriverWait(driver, 5)
      .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("Value")));
System.out.println("Element found");
}catch (TimeoutException e) {
      System.out.println("Element not Found");     
  }
}

How to Terminate iFrame?
To return back from the current iframe to main content, make use of the following script,

driver.switchTo().defaultContent(); 

Handle iFrames without id | name
 
driver.switchTo().frame(0);




Check iFrames availability

Google Chrome
Use Chrome Developer debugging Tool to find all the available iFrames in the web page.




1| Open chrome web Browser
2| Press F12 key
3| Press Esc key
4| In console, you will see a filter icon followed by the dropdown <top frame>
5| Click on the dropdown to see the iFrames availability.

mozilla Firefox
Use the Firefox addon, web-developer to find all the available iframes in a web page. Selecting 'Outline Frames' lets you highlight the web page UI with iframes.