WebDriver is a tool for testing web applications across different
browsers using different programming languages.
You are now able to make powerful tests because
WebDriver allows you to use a programming language of your
choice in designing your tests.
WebDriver is faster than Selenium RC because of its simpler
architecture.
WebDriver directly talks to the browser while Selenium RC
needs the help of the RC Server in order to do so.
WebDriver’s API is more concise than Selenium RC’s.
WebDriver can support HtmlUnit while Selenium RC cannot.
The only drawbacks of WebDriver are:
It cannot readily support new browsers, but Selenium RC
can.
It does not have a built-in command for automatic
generation of test results.
Both of them allow us to use a programing language in designing test script
Allow us to run test script against different browsers
Example:
public class PG2 {
public static void main(String[] args) {
[Link]("[Link]","C:\\[Link]");
WebDriver driver = new FirefoxDriver();
String baseUrl = "[Link]
String tagName = "";
[Link](baseUrl);
tagName = [Link]([Link]("email")).getTagName();
[Link](tagName);
[Link]();
[Link](0);
}
}
Common Commands
Instantiating Web Elements
Instead of using the long “[Link]([Link]())” syntax every
time you will access a particular element, we can instantiate a
WebElement object for it. The WebElement class is contained in the
“[Link].*” package.
WebElement myElement = [Link]([Link](‘username’));
[Link](‘tutorial’);
Clicking on an Element
Clicking is perhaps the most common way of interacting with web
elements. The click() method is used to simulate the clicking of any
element. The following Selenium Java example shows how click() was
used to click on Mercury Tours’ “Sign-In” button.
[Link]([Link](‘login’)).click();
The method automatically waits for a new page to load if
applicable.
The element to be clicked-on, must be visible (height and width
must not be equal to zero).
Get Commands
Here are some important “get” commands you must be familiar with.
get() It automatically opens a new browser window and fetches the
page that you specify inside its parentheses.
The parameter must be a String object.
getTitle() Fetches the title of the current page, No parameter
getPageSource() Returns the source code of the page as a
String value, No parameter
getCurrentUrl() Fetches the string representing the current
URL that the browser is looking at, No parameter
getText() Fetches the inner text of the element that you specify
Navigate commands
These commands allow you to refresh,go-into and switch back and forth
between different web pages.
navigate().to() It automatically opens a new browser window
and fetches the page that you specify inside its parentheses.
navigate().refresh() It refreshes the current page, no parameter
navigate().back() Takes you back by one page on the browser’s
history, no parameter
navigate().forward() Takes you forward by one page on the
browser’s history, no parameter
Closing and Quitting Browser Windows
close() - It closes only the browser window that WebDriver is
currently controlling, no parameter
quit() - It closes all windows that WebDriver has opened, no
parameter
Switching Between Frames
To access GUI elements in a Frame, we should first direct WebDriver to
focus on the frame or pop-up window first before we can access
elements within them.
This page has 3 frames whose “name” attributes are indicated above. We
wish to access the “Deprecated” link encircled above in yellow. In order
to do that, we must first instruct WebDriver to switch to the “classFrame”
frame using the “switchTo().frame()” method. We will use the name
attribute of the frame as the parameter for the “frame()” part.
[Link]("[Link]","C:\\[Link]");
WebDriver driver = new FirefoxDriver();
[Link]("[Link]
[Link]().frame("classFrame");
[Link]([Link]("Deprecated")).click();
[Link]();
Switching Between Pop-up Windows
WebDriver allows pop-up windows like alerts to be displayed, unlike in
Selenium IDE. To access the elements within the alert (such as the
message it contains), we must use the “switchTo().alert()” method. In
the code below, we will use this method to access the alert box and then
retrieve its message using the “getText()” method, and then
automatically close the alert box using
the “switchTo().alert().accept()” method.
public class myclass {
public static void main(String[] args) {
[Link]("[Link]","C:\\[Link]");
WebDriver driver = new FirefoxDriver();
String alertMessage = "";
[Link]("[Link]
[Link]([Link]("input[value=\"Go!\"]")).click();
alertMessage = [Link]().alert().getText();
[Link]().alert().accept();
[Link](alertMessage);
[Link]();
}
}
Waits: implicitWait, explicitWait, FluentWait,
Static Wait(Java application: [Link]())
Implicit Wait used to set the default waiting time throughout the
program
It is simpler to code than Explicit Waits.
It is usually declared in the instantiation part of the code.
[Link]().timeouts().implicitlyWait(10, [Link])
Explicit Wait used to set the waiting time for a particular instance
only
Explicit waits are done using the WebDriverWait and
ExpectedCondition classes. For the following Selenium WebDriver
example, we shall wait up to 10 seconds for an element whose id is
“username” to become visible before proceeding to the next
command
Declare a WebDriverWait variable. In this example, we will use
“myWaitVar” as the name of the variable
WebDriver driver = new FirefoxDriver();
WebDriverWait myWaitVar = new WebDriverWait(driver, 10);
Use myWaitVar with ExpectedConditions on portions where you need the
explicit wait to occur. In this case, we will use explicit wait on the
“username” (Mercury Tours HomePage) input before we type the text
“tutorial” onto it.
[Link]([Link]([Link](‘username’)));
[Link]([Link](‘username’)).click();
Conditions
isEnabled() is used when you want to verify whether a certain
element is enabled or not before executing a command.
isDisplayed() is used when you want to verify whether a certain
element is displayed or not before executing a command.
isSelected() is used when you want to verify whether a
certain check box, radio button, or option in a drop-down
box is selected. It does not work on other elements.
Using ExpectedConditions The ExpectedConditions class
offers a wider set of conditions that you can use in conjunction with
WebDriverWait’s until() method.
alertIsPresent() – waits until an alert box is displayed.
elementToBeClickable() – Waits until an element is visible and, at
the same time, enabled. The sample Selenium Code below will
wait until the element with id=”username” to become visible and
enabled first before assigning that element as a WebElement
variable named “txtUserName”.
frameToBeAvailableAndSwitchToIt() – Waits until the given
frame is already available, and then automatically switches to it.
Catching Exceptions
When using isEnabled(), isDisplayed(), and isSelected(),
WebDriver assumes that the element already exists on the page.
Otherwise, it will throw a NoSuchElementException. To avoid
this, we should use a try-catch block so that the program will not be
interrupted.
WebElement txtbox_username = [Link]([Link]("username"));
try{
if(txtbox_username.isEnabled()){
txtbox_username.sendKeys("tutorial");
}
}
catch(NoSuchElementException nsee){
[Link]([Link]());
}
If you use explicit waits, the type of exception that you should catch
is the “TimeoutException”.
Summary
To start using the WebDriver API, you must import at least these
two packages.
[Link].*
[Link]
The get() method is the equivalent of Selenium IDE’s “open”
command.
Locating elements in WebDriver is done by using
the findElement() method.
The following are the available options for locating elements in
WebDriver:
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
The [Link]() does not support the “contains” feature.
You can instantiate an element using the WebElement class.
Clicking on an element is done by using the click() method.
WebDriver provides these useful get commands:
get()
getTitle()
getPageSource()
getCurrentUrl()
getText()
WebDriver provides these useful navigation commands
navigate().forward()
navigate().back()
navigate().to()
navigate().refresh()
The close() and quit() methods are used to close browser
windows. Close() is used to close a single window; while quit() is
used to close all windows associated to the parent window that the
WebDriver object was controlling.
The switchTo().frame() and switchTo().alert() methods are used
to direct WebDriver’s focus onto a frame or alert, respectively.
Implicit waits are used to set the waiting time throughout the
program, while explicit waits are used only on specific portions.
You can use the isEnabled(), isDisplayed(),isSelected(), and a
combination
of WebDriverWait and ExpectedConditions methods when
verifying the state of an element. However, they do not verify if the
element does not exists.
When isEnabled(), isDisplayed(),or isSelected() was called while
the element was not existing, WebDriver will throw
a NoSuchElementException.
When WebDriverWait and ExpectedConditions methods were
called while the element was not existing, WebDriver would throw
a TimeoutException.
Note:
[Link]() : It’s used to go to the particular website , But it doesn’t
maintain the browser History and cookies so , we can’t use forward and
backward button , if we click on that , page will not get schedule
[Link]() : it’s used to go to the particular website , but it
maintains the browser history and cookies, so we can use forward and
backward button to navigate between the pages during the coding of
Testcase