1. Locators:
Finding WebElement by using various
locators in WebDriver
A.
Using ID :
We are finding the element using Id
then the sample code for it is:
Example:
WebElement
element = driver.findElement(By.id("Id from webpage"));
B.
Using Name :
We are finding the element using Name then
sample code for it is:
Example:
WebElement
element = driver.findElement(By.name("Name of WebElement"));
C.
Using Tag
Name :
We are finding the element using tag
name then the sample code for it is:
Example:
WebElement element =
driver.findElement(By.tagName("tag name"));
D.
Using
Xpath :
We are finding the using xpath then the sample
code for it is:
Example:
WebElement
element = driver.findElement(By.xpath("xpath of webElement"));
E.
Using CSS :
We are finding the element using css
selector then the sample code for it is :
Example:
WebElement element =
driver.findElement(By.CSS("CSS locator path"));
F.
Using
LinkText :
We are finding the element using the
linktext then the sample code for it is:
Example:
WebElement welement =
driver.findElement(By.LinkText("LinkText"));
2. Fetching pop-up message in
Selenium-WebDriver:
Suppose we require a function for fetching the pop-up message in the web page then the sample code for it is:Example:
public static String getPopupMessage(final WebDriver driver) {
String message = null;
try {
Alert alert = driver.switchTo().alert();
message = alert.getText();
alert.accept();
}
public static String getPopupMessage(final WebDriver driver) {
String message = null;
try {
Alert alert = driver.switchTo().alert();
message = alert.getText();
alert.accept();
}
catch (Exception e) {
message = null;
}
System.out.println("message"+message);
return message;
}
message = null;
}
System.out.println("message"+message);
return message;
}
3. Canceling pop-up in
Selenium-WebDriver:
In the above example we have seen the function for fetching the
pop-message now we are going to write sample code for cancelling the pop-up
message.
Example:
public static String cancelPopupMessageBox(final WebDriver driver) {
String message = null;
try {
Alert alert = driver.switchTo().alert();
message = alert.getText();
alert.dismiss();
} catch (Exception e) {
message = null;
}
return message;
}
public static String cancelPopupMessageBox(final WebDriver driver) {
String message = null;
try {
Alert alert = driver.switchTo().alert();
message = alert.getText();
alert.dismiss();
} catch (Exception e) {
message = null;
}
return message;
}
4. Inserting string in Text
Field in Selenium-WebDriver:
In a web page we find several text boxes ,we need to enter a string in
the text field then we require some sample code to attain the functionality:
Example:
public static void
insertText(WebDriver driver, By locator, String value) {
WebElement field = driver.findElement(locator);
field.clear();
field.sendKeys(value);
}
WebElement field = driver.findElement(locator);
field.clear();
field.sendKeys(value);
}
5. Reading Title text in in
Selenium-WebDriver:
Suppose we are in we need to know title of the web-page then we require
sample code to represent this:
Example:
public static String tooltipText(WebDriver driver, By locator){
String tooltip = driver.findElement(locator).getAttribute("title");
return tooltip;
}
public static String tooltipText(WebDriver driver, By locator){
String tooltip = driver.findElement(locator).getAttribute("title");
return tooltip;
}
6. Selecting Radio Button in
Selenium-WebDriver
We are having radio-button selection then the sample code requires to
perform this:
Example:
public static void selectRadioButton(WebDriver driver, By locator, String value){
public static void selectRadioButton(WebDriver driver, By locator, String value){
List select =
driver.findElements(locator);
for (WebElement element : select)
{
if (element.getAttribute("value").equalsIgnoreCase(value)){
element.click();
}
}
for (WebElement element : select)
{
if (element.getAttribute("value").equalsIgnoreCase(value)){
element.click();
}
}
7. Selecting
CheckBox in Selenium-WebDriver
We are having the checkbox in page to perform this sample code for it is:
We are having the checkbox in page to perform this sample code for it is:
Example:
public static void selectCheckboxes(WebDriver driver, By locator,String value)
{
List abc = driver.findElements(locator);
List list = new ArrayListArrays.asList(value.split(",")));
for (String check : list){
for (WebElement chk : abc){
if(chk.getAttribute("value").equalsIgnoreCase(check)){
chk.click();
}
public static void selectCheckboxes(WebDriver driver, By locator,String value)
{
List abc = driver.findElements(locator);
List list = new ArrayListArrays.asList(value.split(",")));
for (String check : list){
for (WebElement chk : abc){
if(chk.getAttribute("value").equalsIgnoreCase(check)){
chk.click();
}
}
}
}
8. Selecting Dropdown in
Selenium-WebDriver
We are having the dropdown option for selecting an options we can do this
by having sample code for it is:
Example:
public static void
selectDropdown(WebDriver driver, By locator, String value){
Select select =new Select (driver.findElement(locator));
Select select =new Select (driver.findElement(locator));
select.selectByVisibleText(value);
}
9. Selecting searched dropdown in
Selenium-WebDriver
We have to search for an product in search box ,we enter some string in
search box then we have to select that specific string then the sample code for
it is:
Example:
public static void
selectSearchDropdown(WebDriver driver, By locator, String value){
driver.findElement(locator).click();
driver.findElement(locator).sendKeys(value);
driver.findElement(locator).sendKeys(Keys.TAB);
}
driver.findElement(locator).click();
driver.findElement(locator).sendKeys(value);
driver.findElement(locator).sendKeys(Keys.TAB);
}
10. Uploading file
using Selenium-WebDriver
We will be uploading some files in the application to obtain this sample
code for this is :
Example:
public static void
uploadFile(WebDriver driver, By locator, String path){
driver.findElement(locator).sendKeys(path);
}
driver.findElement(locator).sendKeys(path);
}
11. Wait() in Selenium-WebDriver
A.
Implicit Wait :
Example:
driver.manage.timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.manage.timeouts().implicitlyWait(10,TimeUnit.SECONDS);
B.
Explicit Wait:
Example:
WebDriverWait wait = new
WebDriverWait(driver,10);
wait.until(ExpectedConditons.elementToBeClickable(By.id/xpath/name("locator"));
wait.until(ExpectedConditons.elementToBeClickable(By.id/xpath/name("locator"));
C.
Using Sleep method of java:
Thread.sleep(time in milisecond);
Thread.sleep(time in milisecond);
12. Navigation method of WebDriver
Interface
Different kinds navigations were present.
A.get()- used to get
the Url
driver.get(URL);
B.to() method (its a alternative of get() method)
driver.navigate().to(Url);
This will open the URL that you have inserted as argument
driver.navigate().to(Url);
This will open the URL that you have inserted as argument
C.back() – use to navigate one step back from current position in recent
history syntax driver.navigate().back();
D.forward() – use to navigate one step forward in browser
driver.navigate().forward();
E.refresh() – This will refresh you current open url
driver.navigate().refresh();
13. Deleting all Cookies before doing any
kind of action
We have to delete the cookies in a web page
driver.manage().deleteAllCookies();
This will delete all cookies
This will delete all cookies
14. Pressing any Keyboard key using
Action builder class of WebDriver
We are
having one class Action to handle all keyboard and Mouse action.
While creating a action builder its constructor takes WebDriver as argument.
Here I am taking example of pressing Control key:
Example:
Actions builder = new Actions(driver);
builder.keyDown(Keys.CONTROL).click(someElement).click(someOtherElement).keyUp(Keys.CONTROL).build().perform();
Actions builder = new Actions(driver);
builder.keyDown(Keys.CONTROL).click(someElement).click(someOtherElement).keyUp(Keys.CONTROL).build().perform();
When we press multiple keys or action
together then we need to bind all in a single command by using build() method
and perform() method intend us to perform the action.
In the same way you can handle other key actions.
In the same way you can handle other key actions.
15. Drag and Drop action in Webdriver
In this we need to specify both WebElement like Source and target and for draganddrop Action class has a method with two argument so let see how it normally look like
In this we need to specify both WebElement like Source and target and for draganddrop Action class has a method with two argument so let see how it normally look like
Example:
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();
No comments:
Post a Comment