Tuesday, 7 January 2014

Difference between find element () and findelements ()

Difference between find element () and find elements ()?
findElement() :
Find the first element within the current page using the given "locating mechanism".
Returns a single WebElement.
Syntax: WebElement findElement(By by)
Ex:
driver.get("http://ruchi-myseleniumblog.blogspot.in/"); 
WebElement widget = driver.findElement(By .xpath(".//*[@id='BlogArchive1_ArchiveList']"));
widget.click();
findElements() :
Find all elements within the current page using the given "locating mechanism".
Returns List of WebElements.
Syntax: 
        WebElement ullist = driver.findElement(By.className("posts"));
List<WebElement> posts = ullist.findElements(By.tagName("li"));
System.out.println("List of Posts are Below");
for (int i = 0; i < posts.size(); i++) {
String post = posts.get(i).findElement(By.tagName("a")).getText();

System.out.println(post);
}

Sample Program:

package programs;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class Sample {
WebDriver driver;

@Test
public void test() {

driver = new FirefoxDriver();
driver.get("http://ruchi-myseleniumblog.blogspot.in/");
WebElement widget = driver.findElement(By
.xpath(".//*[@id='BlogArchive1_ArchiveList']"));
WebElement hierarchy = widget.findElement(By.className("hierarchy"));
System.out.println("Year is "
+ hierarchy.findElement(By.className("post-count-link"))
.getText());
System.out.println("Year-Posts size  is "
+ hierarchy.findElement(By.className("post-count")).getText());
WebElement sublist = hierarchy.findElement(By.className("hierarchy"));
System.out.println("Month is "
+ sublist.findElement(By.className("post-count-link"))
.getText());
System.out.println("Month-Posts size  is "
+ sublist.findElement(By.className("post-count")).getText());
WebElement ullist = driver.findElement(By.className("posts"));
List<WebElement> posts = ullist.findElements(By.tagName("li"));
System.out.println("List of Posts are Below");
for (int i = 0; i < posts.size(); i++) {
String post = posts.get(i).findElement(By.tagName("a")).getText();

System.out.println(post);
}
driver.close();
}


}

What is actions class in web driver

71. What is actions class in web driver?
Actions class with web Driver help is Sliding element, Resizing an Element, Drag & Drop
Hovering a mouse, especially in a case when dealing with mouse over menus.

Dragging & Dropping an Element:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class testDragandDrop {
  public static void main(String[] args) throws InterruptedException {
   WebDriver driver = new FirefoxDriver();
  driver.get("http://jqueryui.com/resources/demos/droppable/default.html"); 
  WebElement draggable = driver.findElement(By.xpath("//*[@id='draggable']"));
  WebElement droppable = driver.findElement(By.xpath("//*[@id='droppable']"));   
  Actions action = new Actions(driver); 
action.dragAndDrop(draggable, droppable).perform();
  }
}
Sliding an Element:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class testSlider {
  /**
  * @param args
  * @throws InterruptedException
  */
 public static void main(String[] args) throws InterruptedException {
   WebDriver driver = new FirefoxDriver();
  driver.get("http://jqueryui.com/resources/demos/slider/default.html"); 
  WebElement slider = driver.findElement(By.xpath("//*[@id='slider']/a")); 
  Actions action = new Actions(driver);
  Thread.sleep(3000);
  action.dragAndDropBy(slider, 90, 0).perform();
  }
}
Re-sizing an Element:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class testResizable {
  public static void main(String[] args) throws InterruptedException {
   WebDriver driver = new FirefoxDriver();
  driver.get("http://jqueryui.com/resources/demos/resizable/default.html");
    WebElement resize = driver.findElement(By.xpath("//*[@id='resizable']/div[3]")); 
  Actions action = new Actions(driver);
  action.dragAndDropBy(resize, 400, 200).perform();
    }
}



How to switch between frames

70. How to switch between frames?

WebDriver's driver.switchTo().frame() method takes one of the three possible arguments:
·         A number.
Select a frame by its (zero-based) index. That is, if a page has three frames, the first frame would be at index "0", the second at index "1" and the third at index "2". Once the frame has been selected, all subsequent calls on the WebDriver interface are made to that frame.
·         A name or ID.
Select a frame by its name or ID. Frames located by matching name attributes are always given precedence over those matched by ID.
Select a frame using its previously located WebElement.
Get the frame by it's id/name or locate it by driver.findElement() and you'll be good.



What are the different assertions in SIDE

What are the different assertions in SIDE?

Assertions are like Accessors, but they verify that the state of the application conforms to what is expected. Examples include "make sure the page title is X" and "verify that this checkbox is checked".
All Selenium Assertions can be used in 3 modes: "assert", "verify", and "waitFor".

 For example, you can "assertText", "verifyText" and "waitForText". When an "assert" fails, the test is aborted. When a "verify" fails, the test will continue execution, logging the failure. This allows a single "assert" to ensure that the application is on the correct page, followed by a bunch of "verify" assertions to test form field values, labels, etc.

"waitFor" commands wait for some condition to become true (which can be useful for testing Ajax applications). They will succeed immediately if the condition is already true. However, they will fail and halt the test if the condition does not become true within the current timeout setting (see the setTimeout action below).



How to highlight an object with selenium and java

How to highlight an object like qtp/uft does through selenium and java?
public void highlightElement(WebDriver driver, WebElement element) {
for (int i = 0; i < 2; i++)
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: yellow; border: 2px solid yellow;");
js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "");
}}
Call the highlightElement method and pass webdriver and WebElement which you want to highlight as arguments.


TestNG vs. Junit

TestNG Vs.Junit?
Advantages of TestNG over Junit
·         In Junit we have to declare @BeforeClass and @AfterClass which is a constraint where as in TestNG there is no constraint like this.
·         Additional Levels of setUp/tearDown level are available in TestNG like @Before/AfterSuite,@Before/AfterTest and @Before/AfterGroup
·         No Need to extend any class in TestNG.
·         There is no method name constraint in TestNG as in Junit. You can give any name to the test methods in TestNG
·         In TestNG we can tell the test that one method is dependent on another method where as in Junit this is not possible. In Junit each test is independent of another test.
·         Grouping of testcases is available in TestNG where as the same is not available in Junit.
·         Execution can be done based on Groups. For ex. If you have defined many cases and segregated them by defining 2 groups as Sanity and Regression. Then if you only want to execute the “Sanity” cases then just tell TestNG to execute the “Sanity” and TestNG will automatically execute the cases belonging to the “Sanity” group.
·         Also using TestNG your selenium test case execution can be done in parallel.




How to work with radio button in web driver

How to work with radio button in web driver?

 We can select the value from the drop down by using 3 methods.

selectByVisibleText - select by the text displayed in drop down
selectByIndex  - select by index of option in drop down
selectByValue  - select by value of option in drop down

<select id="44"> <option value="1">xyz</option>
 <option value="2">abc</option>
 <option value="3">pqr</option>
</select>

WebElement e = driver.findElement(By.id("44"));
Select selectElement=new Select(e);
// both of the below statements will select first option in the weblist
selectElement.selectByVisibleText("xyz"); 
selectElement.selectByValue("1");