Tuesday, 24 January 2017

Difference between sendKeys() Present in WebElelement & Action classs


Q.Difference between sendKeys() Present in WebElelement & Action classs ?
-sendKeys() persent in WebElelement interface is used to type given input in the specified element.

where as SendKeys() method present in Actions class is used to type the given input in the given cursor position or on the specific element

There are 2 send Keys() method in Action class

one is used to type the sendKeys in current element cursor location.
Another is used to type the send keys in specified location.

Example

1. WebElement un=driver.findElement(By.id("username));
     un.sendkeys("Admin").click();

2. Actions actions=new Actions(driver);
    actions.sendKeys("abc").perform();

3. actions.sendKeys(un,"xyz").perform(); //Method overloading

Handling Context Menu in Selenium

Q.How to handle Context Menu in Selenium ?
- using  ContextClick(webelement) of Actions method

Code:

import java.awt.event.ActionEvent;

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 ContextMenu 
{
public static void main(String[] args) throws InterruptedException 
{
WebDriver driver=new FirefoxDriver();
driver.get("http://localhost/login.do");

WebElement link=driver.findElement(By.linkText("Actimind Inc."));
Actions actions=new Actions(driver);
actions.contextClick(link).perform();
Thread.sleep(2000);
actions.sendKeys("t").perform();
}
}

Output:





Monday, 23 January 2017

Handling DropDownMenu using java Selenium code


Q.How to Handle DropDownMenu in Selenium?
- using ' moveToElement() ' method of Actions class.


Selenium Code

import javax.swing.Action;

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 DropDownMenu 
{
public static void main(String[] args) 
{
WebDriver driver=new FirefoxDriver();
driver.get("http://www.actimind.com");

WebElement menu=driver.findElement(By.xpath("//span[text()='About Company']"));

Actions actions=new Actions(driver);
actions.moveToElement(menu).perform();

driver.findElement(By.linkText("Basic Facts")).click();
}


}



Browser OUTPUT: