Web application UI automation using Selenium Web driver
Currently many of the applications are web based and many
companies are moving their existing applications to web, so it is very
important to test quality of web application build. If some functionality of
web application is developed completely and new features/ improvements are being
implemented in the web application it is very important to be careful about
your existing functionality should not break. Automation helps in these
situations. You can automate the test cases/stories for your existing feature
and run them against the new released build so that manual efforts can be
reduced and accuracy can be achieved.
To start with automation of web application UI automation
there are many tools that can be used e.g. Selenium RC, Selenium Web Driver,
Watir, Watin, QTP etc.
Selenium is the good tool to get start with because of
following reasons:
- -
Open source
- - Good community support
- - Good library support and incremental updates
- - Support for multiple browsers etc.
Selenium web driver interacts natively with browser
application in order to find elements/ performing actions etc.
To get started with automating the web application you have
to create a web driver
object of the browser you are going to use.
WebDriver driver = new FirefoxDriver();
Please note that here
I am using Firefox browser.
As discussed in post ‘http://mayureshkulkarni-49.blogspot.in/2014/07/automation-of-windows-native.html’,
for UI automate of any application we are using mnemonic:
WFVA
WFVA
-
W (Wait)
for element to load, visible, enable etc.
When you navigate any web page you wait for the element to
get visible/ enable (Of course for that you need to find the element and keep
on validating it)
For this you can have your own logic, following is sample logic for the same:
For this you can have your own logic, following is sample logic for the same:
int tryCount = 0;
WebElement element;
do
{
try
{
// find element
element = <find_element_logic>
// wait
thread.sleep(500);
}
catch {}
}
while((element == null && !element.Enabled) && tryCount < MAX_RETRY_CNT);
WebElement element;
do
{
try
{
// find element
element = <find_element_logic>
// wait
thread.sleep(500);
}
catch {}
}
while((element == null && !element.Enabled) && tryCount < MAX_RETRY_CNT);
In above code snippet you can set any value for MX_RETRY_CNT
depending on your web application page load time. I am setting up as 120 here,
i.e if page is not loaded in ~60 seconds it will break from the element finding
logic.
Waiting logic:
1.
Implicit wait: (C#.Net)
protected IWebElement WaitTillElementFound(int timeOut, OpenQA.Selenium.By
findBy)
{
IWebElement result = null;
int wait = 0;
// Find
out first level
do
{
try
{
++wait;
WebDriverWait webwait = new WebDriverWait(
webDriver, TimeSpan.FromSeconds(2));
result =
webwait.Until<IWebElement>(
(d) =>
{
return
(IWebElement)webDriver.FindElement(findBy);
});
if (result != null)
{
if (result.Displayed)
{
break;
}
}
}
catch (NoSuchElementException)
{
continue;
}
catch (StaleElementReferenceException)
{
continue;
}
catch (WebDriverException)
{
continue;
}
}
while (result == null && wait < timeOut);
return result;
}
2. Explicit wait:
public bool WaitForElement(string elementType, string elementValue)
{
int count = 0;
do
{
try
{
IWebElement element = null;
switch (elementType)
{
case "XPATH":
element = this.FindControlByXPath(elementValue);
break;
case "ID":
element = this.FindControlById(elementValue);
break;
case "NAME":
element = this.FindControlByName(elementValue);
break;
case "CLASS":
element = this.FindControlByClass(elementValue);
break;
}
// Verify if element present or not
if (null != element)
{
if (element.Displayed)
{
return true;
}
}
}
catch (Exception) { }
count++;
ImplicitlyWait(500);
}
while (count < timeoutWait);
return false;
}
-
F (Find)
the web element:
Once the page load gets completed you can get the element
object by finding it using below mechanism: Id, XPath, name, classname, text,
link text, partial link text, tag etc.
You can follow the same precedence in which above mechanisms
re arranged. Try finding the element by Id, if the Id attribute is not present
for the web element then go for the XPath then name etc.
Web element can be found by using following statements:
//
get the driver object.
WebDriver
driver = new FirefoxDriver();
WebElement
element=driver.findElement(By.id("id_of_element"));
WebElement
element=driver.findElement(By.xpath("\\xpath_of_element"));
Finding xpath of elements covered in blog ‘Finding Xpaths of
a web element’.
-
V
(Validate) the element:
Validating is important to verify if the page is loaded
successfully or if you want to perform any actions on the element without
getting into the unnecessary exceptions. E.g. NullRefExceptions, ElementNotEnabledException
etc.
protected void ValidateElement(
IWebElement element)
{
//
verify that web element should not be null and disabled before performing any
action.
If (element != null && element.Enabled)
{
// Perform action
}
If (element != null && element.Enabled)
{
// Perform action
}
}
-
A
(Action) that you can perform on the web element after validating it
Once the element is found and
validated successfully you are good to go for performing action on that element.
//
click on button
webElement.Click();
Some
of the event that was handled using Javascript code can be triggered using
Javascript actions that were provided by Selenium libraries.
IJavaScriptExecutor
js = (IJavaScriptExecutor)WebDriver;
js.ExecuteScript("$(arguments[0]).click()", this.webElement);
Valuable Information in easy words!
ReplyDelete