PAGE 7 : WEB ELEMENT COMMANDS IN SELENIUM WEBDRIVER


All interesting operations to do with interacting with a page will be performed through this WebElement Interface.

Following are the WebElement commands :
  1. Clear Command
  2. SendKeys Command
  3. Click Command
  4. IsDisplayed Command
  5. IsEnabled Command
  6. IsSelected Command
  7. Submit Command
  8. GetText Command
  9. getTagName Command
  10. getAttribute Command
  11. getSize Command


EXAMPLE:

package webElementCommands;

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 WebElementCommands {

WebDriver driver;


@Test
public void testWebElementCommand() {
driver = new FirefoxDriver();
driver.get("http://facebook.com");

// findElement method is useful to locating targeted single element
WebElement element = driver.findElement(By.id("email"));

// To verify whether we can pass values to the text box.
boolean Status = element.isEnabled();
if (Status) {

// this function will clear the value If any element is in the text entry element,
element.clear();

// Send Keys method will send the text to the text box
element.sendKeys("UserName");

}

/*To click on to the "Create Account" button of Facebook . Clicking is perhaps the most common way of interacting with web elements like text elements, links, radio boxes and many more.*/


    WebElement element1=driver.findElement(By.xpath("//div[contains(@class,'mbs _52lq fsl')]/span"));


//isDisplayed method is to verify whether an element is displayed or not
             if(element1.isDisplayed()){
WebElement element2= driver.findElement(By.id("u_0_i"));
element2.click();

}

}

}


Executing test via TestNg.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="WebElement Command Suite">
<test name="WebElement Command Test">
<classes>
<class name="webElementCommands.WebElementCommands" />
</classes>
</test>
</suite>



getAttribute(String Name) : String- This method gets the value of the given attribute of the element.
WebElement element = driver.findElement(By.id("SubmitButton"));
String attValue = element.getAttribute("id"); //This will return "SubmitButton"

getCssValue Command :This method Fetch CSS property value of the give element. This accepts nothing as a parameter and returns a String value.

element.getCssValue(): Color values should be returned as rgba strings, so, for example if the “background-color” property is set as “green” in the HTML source, the returned value will be “rgba(0, 255, 0, 1)”.

getTagName Command :
This method gets the tag name of this element. This accepts nothing as a parameter and returns a String value.
Command - element.getTagName();
This does not return the value of the name attribute but return the tag for e.g. “input“ for the element

getSize Command : Command - element.getSize();
This returns the size of the element on the page.


GetText Command : getText( ) : String- This method will fetch the visible (i.e. not hidden by CSS) innerText of the element.
Command - element.submit();
This returns an innerText of the element, including sub-elements, without any leading or trailing whitespace.


Submit Command : submit( ) : void- This method works well/better than the click() if the current element is a form, or an element within a form. This accepts nothing as a parameter and returns nothing.
Command - element.submit();

IsSelected Command : isSelected( ) : boolean - Determine whether or not this element is selected or not. This accepts nothing as a parameter but returns boolean value(true/false).
Command - element.isSelected();

IsEnabled Command : isEnabled( ) : boolean - This determines if the element currently is Enabled or not? This accepts nothing as a parameter but returns boolean value(true/false).
Command - element.isEnabled();


IsDisplayed Command : isDisplayed( ) : boolean - This method determines if an element is currently being displayed or not. This accepts nothing as a parameter but returns boolean value(true/false).
Command - element.isDisplayed();


Click Command : click( ) : void -Clicking is perhaps the most common way of interacting with web elements like text elements, links, radio boxes and many more.
Command - element.click();


SendKeys Command : sendKeys(CharSequence… keysToSend ) : void - This simulate typing into an element, which may set its value. This method accepts CharSequence as a parameter and returns nothing.
Command - element.sendKeys(“text”);
This method works fine with text entry elements like INPUT and TEXTAREA elements.

Clear Command : clear( ) : void - If this element is a text entry element, this will clear the value. This method accepts nothing as a parameter and returns nothing.
Command - element.clear();
This method has no effect on other elements. Text entry elements are INPUT and TEXTAREA elements.


findElements() :
1. Find all elements within the current page using the given "locating mechanism".
2. Returns List of WebElements.
3. Syntax:  java.util.List<WebElement> findElements(By by)


EXAMPLE:

package webElementCommands;

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;

import java.util.List;

public class FindElementsCommand {

WebDriver driver;

@Test
public void findElementsCommandElample() {
driver = new FirefoxDriver();
driver.get("http://facebook.com");
List<WebElement> textboxes1 = driver.findElements(By.id("email"));
System.out.println("total textboxes " + textboxes1.size());

}

}


Executing test via TestNg.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="WebElement Command Suite">
<test name="WebElement Command Test">
<classes>
<class name="webElementCommands.FindElementsCommand" />
</classes>
</test>
</suite>


Output:
total textboxes 1

===============================================
WebElement Command Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================









No comments:

About Me

My photo
Pune, Maharastra, India
You can reach me out at : jimmiamrit@gmail.com