Launch a browser using respective driver. WebDriver supports browsers like FireFox, IE, Safari and Chrome. For Firefox, it has native inbuilt support. For other browsers WebDriver needs to know the executable path of the browser.
Listing 1. Code sample
//For Firefox Browser
WebDriver driver = new FireFoxDriver();
For IE:Download and install Internet Explorer driver from http://docs.seleniumhq.org/download/
System.setProperty(“webdriver.ie.driver”, “<parent directory>/IEDriverServer.exe”);
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_
DOMAINS, true);
ieCapabilities.setCapability(“ignoreProtectedModeSettings”, true);
WebDriver iedriver = new InternetExplorerDriver(ieCapabilities);
Launch the AUT (Application Under Test). The get method of the driver object will access a valid url as a parameter and opens in current active browser window. (NOT SURE, I THINK IT OPENS A NEW BROWSER).
driver.get(<url>)
We can use navigate method of driver object
driver.navigate.to(<url>)
WebDriver provides lot of Locator Strategies, “By” Class having list of static methods to handle web elements
• By.className
• By.cssSelector
• By.id
• By.linkText
• By.name
• By.partialLinkText
• By.tagName
• By.xpath
These methods will return an object of WebElement.
Listing 2. Code sample
For Identifying WebElement Using By.className:
<td class=name> </td>
WebElement td=driver.findElement(By.className(“name”));
Using By.cssSelector:
<input id=create>
driver.findElement(By.cssSelector(“#create”)) Using By.id:
<td id=newRecord> </td>
WebElement td=driver.findElement(By.id(“newRecord”));
Using By.id:
<a onclick=gotonext()>Setup </a>
WebElement link=driver.findElement(By.linkText(“Setup”));
Handling Complicated Elements: when in a web Application other than normal html tags are used, there may be complicated elements such as Dropdown, iframes and tables.
Handle Dropdown: WebDriver API provides “Select” class to handle Dropdown list and Its options, WebElement can converted in to Select Object to fetch options.
Listing 3. Code sample
HTML CODE
<select id=”city”>
<option value=”Op1”>Chennai</option>
<option value=”Op2”>Hyderabad</option>
<option value=”Op3”>Bangalore</option>
</select>
WebElement selectElement= driver.findElement(By.id(“city”));
Select selectObject=new Select(selectElement)
Handle iframe: An inline frame is used to embed another document within the current HTML document. It means the iframe is actually a webpage within the webpage which have its own DOM for every iframe on the page.
Listing 4. Code sample
<frame name=”frame1” id=”Frame1”>
……
</iframe>
To access DOM elements inside the driver control needs to change to this frame driver.switchTo().frame(“frame1”);
Switching to frame can be handled in different ways frame(index)
frame(Name of Frame [or] Id of the frame frame(WebElement frameElement)
driver.switchto.defaultcontent();
//This will change the control of driver in to parent window
WebDriver Interactions with WebElement. You need different types of interactions with different types of
• webElement.sendKeys() – Type a sequence of characters to the text box fields
• webElement.click – click the button /link element
• webElement.clear // clearing the value in given Text area
• webElement.submit // submit the Form Element
• selectObject.getOptions() // Will fetch all available Options for that dropdown
• selectCatogory.selectByValue(value) // Select the provided value from dropdown
• selectCatogory.deselectByValue(value) // deselect the Option with given value
Verifying WebElement state: WebElements should visible to interact from driver, or it must be enable to click/type. To get those state of element.
Listing 5. Code sample
WebElement element = driver.findElement(by. cssSelector(“#Name”) //this is username Text Box element.isEnabled() / element.isDisabled()
Return Boolean based on the current state of element.
If the element is type of radio button or check box, to verify the state whether its selected or not
userName.isSelected() <id=userName>SDJ </a>
username.getText() – return the Text part of element
Identifying Attributes and properties for WebElements. For a chosen element, we can verify other properties in DOM by providing the attribute name
userName.getAttribute(“name”)//name= name of the attribute
Navigating between the browser Windows. In a web application, a functionality can be opened in a new window, or can navigate to next page, driver object have a facility to navigate between back and forth between windows and also to switch to a new window.
//To Open a URL
driver.navigate().to(“https://www.google.co.in/”);
//Refresh the Current Page driver.navigate().refresh();
//move back from current window driver.navigate().back();
//step forward from current window driver.navigate().forward();
There may be delays in web pages load times due to many factors, this can be due to network speed, more Ajax Calls, more images, etc. Until an element is loaded, WebDriver cannot interact with that element.
Webdriver API has “wait” commands built in.
//Implicit wait hold the driver before each element interaction before throw an error driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//Wait for the page to load completely before throwing error driver.manage().timeouts().pageLoadTimeout(30, SECONDS);
//Tell driver to wait until the element load/gets visibility-Explicit wait new WebDriverWait(driver, 60)
.until(new ExpectedCondition<Boolean>(){
@Override
public Boolean apply(WebDriver d) { return theElement.isDisplayed();
Action Builder. mIf you want to perform complicated actions like DoubleClick or drag a element from one place to other place, you need to use the “Action” interface
Actions action = new Actions(ffDriver) ; //To Double Click
action.doubleClick(element);
action.build().perform();
//To drag from one position to another Actions dragAction = new Actions(ffDriver) ; dragAction.dragAndDrop(dragthis, dropHere);
dragAction.build().perform();
Now, a user can extend the framework using the TestNG framework for good Reporting structure as well as for Test Execution Control like number of Test cases executed by configuring Test Groups, Data driver test case using Data Provider, and also use a Configuration and Integration Tools like Maven/Ant will gives the effective maintenance for Test Framework.
The Combination of WebDriver+TestNg+Maven supports a Effective, easy maintenance Test Framework in a Hybrid Way.
Happy Testing