Saturday, March 3, 2018

Selenium WebDriver and WebElement Commands

WebDriver is a web automation framework that allows you to execute your tests against different browsers using their corresponding driver functions. Also it allows us to use a programming language in creating test scripts.

programming languages are supported by WebDriver as follows:

Java

.Net

PHP

Python

Perl

Ruby

WebDriver is implementing classes one should use are listed as below:

ChromeDriver

EventFiringWebDriver

FirefoxDriver

HtmlUnitDriver

InternetExplorerDriver

PhantomJSDriver

RemoteWebDriver

SafariDriver

NOTE : Depending upon the browser, we are selecting the above WebDriver method.

While automating the test cases we are selecting the desired WebElement from the web pages and performing the desire operation as per the need. It is a essential part because it helps us to uniquely identify the HTML element or web objects from the web browser page.

What is WebElement ?

WebElement is an HTML element that helps the users to drive automation tests. Selenium WebDriver provides well-organized web page interactions through WebElements, such as locating elements, getting attribute properties, asserting text present in WebElement, and more. However, to interact with hidden elements in a web page, it is necessary to unhide the hidden elements first and then find the element in web page. Also you can refer below link to build more understanding on WebElement :

WebElement represents an HTML element and HTML elements are written with a start tag, with an end tag, with the content in between: <tagname> content </tagname>. Lets see the below HTML tags it gives you a complete idea about HTML document structure :

Click Command

click( ) : void – This simulates the clicking of any element. Accepts nothing as a parameter and returns nothing.

Command – element.click();

NOTE : it helps to interact with web elements like text elements, links, radio boxes and many more.

Most of the time we click on the links and it causes a new page to load, this method will attempt to wait until the page has loaded properly before handing over the execution to next statement. But If click() causes a new page to be loaded via an event or is done by sending a native event for example through javascript, then the method will not wait for it to be loaded.

There are some preconditions for an element to be clicked. The element must be Visible and it must have a Height and Width greater than 0.

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();

Note: Do not confuse this method with element present on the page or not. This will return true if the element is present on the page and throw a NoSuchElementFound exception if the element is not present on the page. This refers the property of the element, sometimes the element is present on the page but the property of the element is set to hidden, in that case this will return false, as the element is present in the DOM but not visible to us.

getTagName Command

getTagName( ) : String– This method gets the tag name of this element. This accepts nothing as a parameter and returns a String value.

Command – element.getTagName();

NOTE : When we call the getTagName Method It will return Tag name only. Lets say this is a complete HTML tag name e.g. <input name="username" id="username" /> and when we call getTagName Method, it will show "input" as a result.