2012-12-16

How to run selenium webdriver code in IE

In this article we are going to see necessary steps for running selenium web driver code in Internet Explorer. I am using c# code with VS2010.

After adding dll in properties of the project , we have to add the references in the upper section of the class.
using OpenQA.Selenium.IE;

Then we have to initiate the driver object as Internet Explorer Driver. 
private IWebDriver driver  = new InternetExplorerDriver();

Now this driver can be used Just Like as other Firefox or chrome drivers.

We can initiate InternetExplorerDriver with 6 ways(overridden).

1.With no parameter
-InternetExplorerDriver();
2.With a path [need when running remotely]
-InternetExplorerDriver("string")
3. With an Option
-InternetExplorerDriver(options)
4. With a path and an Option
-InternetExplorerDriver("string", options)
5. With a path, an option and Timeout(wait each command)
-InternetExplorerDriver("string",options, cmdTout)
6. With an option, timeout and a specific driver Service.
- InternetExplorerDriver(service, options,cmdTout)

Note : In every case ,
-"string" is the path to the directory containing IEDriverServer.exe
- option  is an object of OpenQA.Selenium.IE.InternetExplorerOptions
- cmdTout is an object of System.TimeSpan
- service is an object of OpenQA.Selenium.DriverService

The Internet Explorer Driver Server link here.
We know , for the safely running without errors 
From IE menu goto tools>Internet Options>Security tab, make sure all your zones are set to the same value



Setting Authentication  via http URL(htaccess) : 

In some cases we may find authentication URL in the starting of the site in JavaScript(like network authentication). We need to pass username and password using URL. By default it is disabled for windows(i use win 7 64). We have add registry key for that. to do that
1. Run Registry edit(in run, regedit.exe)
2. Go to HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE]
3. Set iexplore.exe and explorer.exe as 0. if they are not present, add them
"iexplore.exe"=dword:00000000
"explorer.exe"=dword:00000000
 
 So, now running tests in IE will be easier.

.... Thanks...:)... 

2012-12-10

How to run JavaScript in selenium WebDriver

In this following article we will see how can we run custom JavaScript in selenium web driver. I am using c#(VS2010) here. As there are many uses of JS ,this will be a long document. I am providing the basic ideas. I will update incrementally.

To run java script , we need a Java script executor
private IJavaScriptExecutor jsExecuter;

In the method we will initialize that
jsExecuter = (IJavaScriptExecutor)driver;
Note : Here we are casting the driver.I use Firefox driver. Firefox driver implements remote driver which implements java script executor. By this we can specify the JavaScript executor functions.

-To Run JavaScript(asynchronously) in the currently selected frame or window.
jsExecuter.ExecuteAsyncScript("my JavaScript as text");

-To Run JavaScript in the currently selected frame or window.
jsExecuter.ExecuteScript("my JavaScript as text");
Note : both takes script as string, arguments as parameter and provides output as object (object type based on how we use that or what we use as the script). Both of these functions also execute java script with parameters. For that just add another parameter as value[ExecuteScript("script", "Value")]. For return of the executers
-For an HTML element, this method returns a IWebElement
-For a number, a Int64 is returned
-For a boolean, a Boolean is returned
-For all other cases a String is returned.
-For an array, we check the first element, and attempt to return a List(T) of that type, following the rules above. Nested lists are not supported.
-If the value is null or there is no return value, a null reference is returned.

For JavaScript, we always have to remind that the text that we will use as java script, is fully a java script code . If we need a function output from that java script, we have to use " return"  before the script. For example, if we want title
jsExecuter.ExecuteScript("return document.title");



How to work with JavaScript Alert in Selenium WebDriver

In this article we are going to see the functions needed to control JavaScript pop-up alert messages. I am using c#(CV2010).

After declaring web driver declare the Alart type veritable.
IAlert myAlert;
In the function , initiate myAlert object as
myAlart = driver.SwitchTo().Alert();
Now, this myAlart object can do some work.
-To  Accept the alert
myAlert.Accept();
-To Dismiss the alert
myAlart.Dismiss();
-To Write text in the alert(when needed)
myAlart.SendKeys("string");
-To get the text of the alert
myAlart.Text;
Note : This will provide a string text as output

...Thanks...:)... To be continued...

2012-12-08

How to work with a web element in Selenium WebDriver

In this article we will see how can we work with all the property of an web element found by the driver. This is necessary when we need to check the values of the properties of a web element to test.(i.e. If we want to check data, font, style,arrangement, indexing etc of a combo box selector) and to perform any action on the element. I am using c#(VS2010).

In the previous post, we see an element(IWebElement type) can be found using driver
So, the declaration code will be 
private IWebDriver driver = new FirefoxDriver();
private IWebElement element;
//In the method we will use like following
element = driver.FindElement(By.XPath("String"));// we can use any of eight way to find element.
So, we have an element, if we compare it's different properties with known values, we can prove that the element is tested.

- To Clear the content of the element
element.Clear();
 - To Click the element.
element.Click();
 -To Submit the element to the web server.(i.e- log In button) 
element.Submit();

-To write text into the element(i.e- text box)
element.SendKeys("String");

-To Get a value(true/false) indicating whether or not the element is displayed.
element.Displayed;

-To Get a value(true/false) indicating whether or not the element is enabled.
element.Enabled;
-To Get a value(true/false) indicating whether or not the element is selected
element.Selected;

-To Get the value(as string) of the specified attribute for this element.
element.GetAttribute("attribute Name as string");
-To Get the value(as string) of a CSS property of this element
element.GetCssValue("CSS Name as string");

-To Get a value(true/false) indicating whether the element is empty(use this to find an element's existence) 
element.Location.IsEmpty;
 -To Get the x-coordinate of Selected element(in Integer)
element.Location.X;
 -To Get the y-coordinate of Selected element(in Integer)
element.Location.Y;

- To Get integer(in pixel) value of the Vertical length of the element
element.Size.Height;
- To Get integer(in pixel) value of the Horizontal length of the element
element.Size.Width;
-To Get a value(true/false) indicating whether the element has width and height as Zero(0)
element.Size.IsEmpty;

-To Get the tag name of the element.
element.TagName;

-To Get the Inner Text of the element(No beginning or ending whitespace, collapsed other whitespaces)
element.Text;

Note : The basic difference to use "element" and "selectElement" is, element can be found from a page and work directly with a web component, where as selectElement ca work with page's web components as well as with there's inner items.

..Thanks... :)

2012-12-07

Working with Selected item in Selenium Webdriver

In this article we are going to see the functions to work with a selected item in selenium web driver. I am using c#(VS 2010) here.

From my previous post we can see how to select an element.In this way we can work with the item as well as inner item elements. So, for selection we use the code
private IWebElement element;
private SelectElement myElement ;
//In the test function we will use following code
element = driver.FindElement(By.XPath("String"));        
myElement = new SelectElement(element);
myElement.DeselectAll();//This is optional, for safe operation
myElement.SelectByIndex(12);

After selection, we can performs following actions

- To Get a value(true/false) indicating whether or not selected element has multiple selectivity ( like multiple check box, or multiple combo item selection)
myElement.IsMultiple// provides true/false output

- To Clear the content of selected element.
  myElement.SelectedOption.Clear();

- To Click selected element.
 myElement.SelectedOption.Click();

-To Get a value(true/false) indicating whether or not selected element is selected
myElement.SelectedOption.Selected;

-To Get a value(true/false) indicating whether or not selected element is displayed.
myElement.SelectedOption.Displayed;

-To Get a value(true/false) indicating whether or not selected element is enabled.
myElement.SelectedOption.Enabled;

- To Get integer(in pixel) value of the Vertical length of  selected element
 myElement.SelectedOption.Size.Height;

- To Get integer(in pixel) value of the Horizontal length of  selected element
myElement.SelectedOption.Size.Width;

-To Get the tag name of selected element.
myElement.SelectedOption.TagName;

-To write text into the selected element(i.e- text box)
myElement.SelectedOption.SendKeys("String");

-To Submit selected element to the web server.(i.e- log In button)
myElement.SelectedOption.Submit();

-To Get the Inner Text of selected element(with no leading or trailing whitespace, collapsed other whitespaces)
 myElement.SelectedOption.Text;


-To Get a value(true/false) indicating whether selected element is empty(use this to find an element's existence)
myElement.SelectedOption.Location.IsEmpty;

-To Get the x-coordinate of Selected element(in Integer)
myElement.SelectedOption.Location.X;

-To Get the y-coordinate of Selected element(in Integer)
myElement.SelectedOption.Location.Y;

Note : The basic difference to use "element" and "selectElement" is, element can be found from a page and work directly with a web component, where as selectElement ca work with page's web components as well as with there's inner items.

Thanks....:) I will try to provide an example using those in separate post.

How to select and deselect web element in selenium webdriver.

In this article we are going to see the what are the functions needed to select and dis select any web element found by web driver. I am using c# code(vs2010) here.
From my previous post, we know how to find a web element. To select an web element, we need to declare select element object and then assign the found element as selected. So the code will be
private SelectElement myElement ;
element = driver.FindElement(By.XPath("String"));//finding element

We can select an element by 3 ways.

1. To Select the element option by the index(as determined by the "index" attribute of the element)
myElement.SelectByIndex(12); //Here 12 is an integer

2. To Select all options by the text displayed
myElement.SelectByText("String");

3. To Select an option by the value.
myElement.SelectByValue("string");

Like as selected ,deselect can be done in 3 ways also.
myElement.DeselectByIndex(12);
myElement.DeselectByText("string");
myElement.DeselectByValue("String");

In addition, we can deselect all
myElement.DeselectAll();

...Thanks...:)

How to Find a web element in Selenium Webdriver

In this article, we are going to see the functions needed to find a web element from a webpage from code using selenium web driver. I am using c# code to do that.

First, every element of an webpage are following IWebElement contract(similar to web element class) . So we need to declare that. The code will be -
private IWebElement element;

Now, we need to assign the element by finding using driver. We can find a web element in eight ways.
1. To find elements by their CSS class(web driver has different class for different type css)
element = driver.FindElement(By.ClassName("String"));

2. To find elements by their cascading stylesheet (CSS) selector
element = driver.FindElement(By.CssSelector("string"));

3. To find elements by their ID(maintained id in full page)
element = driver.FindElement(By.Id("String"));

4. To find elements by their link text(link showing text)
element = driver.FindElement(By.LinkText("String"));

5. To find elements by their name(maintained name in full page)
element = driver.FindElement(By.Name("String"));

6. To find elements by a partial match on their link text(not exac match with link text)
element = driver.FindElement(By.PartialLinkText("String"));

7. To find elements by their tag name
element = driver.FindElement(By.TagName("String"));

8. To find elements by an XPath query
element = driver.FindElement(By.XPath("String"));

Note : Xpath query is the most convenient way to use in dynamic web page. we should learn about xpath also.

...thanks.:) .. if selenium versions updated, I will update the finding ways.

What is Bug Cycle

2012-12-04

How to insert data in Selenium WebDriver

In this article , we will see the functions needed for inserting data in a text box.
Usually the input text fields takes data and send request to server(i.e.- Log In page).
For this , we have to declear an element outside of method.
private IWebElement element;
Now inside any method , (for user name input text box )
-To find the element for taking username as input (in here txtUserName).
element = driver.FindElement(By.Id("txtUserName"));
-To clear existing or inputted text
element.Clear();
-To write user name "Shantonu"
element.SendKeys("Shantonu");

In the same way we can insert a password
And then, find the button and then
-To click the button
element.Click();

So, finally the full code will be
element = driver.FindElement(By.Id("txtUserName"));
element.Clear();
element.SendKeys("Shantonu");
element = driver.FindElement(By.Id("txtPassword"));
element.Clear();
element.SendKeys("123456");
element = driver.FindElement(By.Id("btnSignIn"));
element.Click();

Note - I use C# code.

How to work with Window and Frames in Selenium WebDriver

In this article we will see the methods to work with Window and Frame(IFRAME elements) in selenium web driver. I use c#(VS2010).

-To switch focus to another window by its Name.
 driver.SwitchTo().Window("The name of the window to select");
Note : If we want to switch the focus, then we use this. Mostly used when we need to change one window to another. The name is in string format for the title of the window.

-To switch focus to another frame by Zero based Index
driver.SwitchTo().Frame(0);
 -To switch focus to another frame by its name or ID
driver.SwitchTo().Frame("ID");
-To switch focus to An OpenQA.Selenium.IWebDriver frame.( an object of IWebDrive)
driver.SwitchTo().Frame(element);
Note : In hear, element is a IWebElement type object. Actually its a real web element. For the element object finding, I use
private IWebElement element; // declaring element, outside of method

And for switch frame parameter, I use this
element = driver.FindElement(By.XPath("Xpath Location"));
driver.SwitchTo().Frame(element);
Note: Xpath location is one of the popular way to identify web elements accurately.

-To switch focus to currently focused element OR the body element [if no element focused]
driver.SwitchTo().ActiveElement();

-To Select either the first frame on the page or the main document when a page contains iFrames
driver.SwitchTo().DefaultContent();

-To switch to the currently active modal dialog
driver.SwitchTo().Alert();

...to be continued... thanks..:)

How to manage Browser Timeouts in Selenium WebDriver

In this article we will see the functions to control browser timeouts by selenium web driver.

For this, we need to create a TimeSpan object(period of time).
Public TimeSpan myTime = new TimeSpan (Parameter);
We can parametrize it by 4 ways
1. ticks//This is Long Type, 1 ticks =100 nanosecond
2. hours, minutes, seconds//All are Integer type
3. days, hours, minutes, seconds//All are Integer type,
4. days,hours, minutes, seconds,milliseconds//All are Integer type
 I use 3rd, so my code
private TimeSpan myTime = new TimeSpan(2,1,1,5);

-To Specify the amount of time the web driver will wait when searching for an element if it is not immediately present
driver.Manage().Timeouts().ImplicitlyWait(myTime);
Note :  We need to use it carefully as this will increase testing time(spatially with XPath).

-To Specify the amount of time the web driver will wait when executing JavaScript asynchronously.
driver.Manage().Timeouts().SetScriptTimeout(myTime);
           

2012-12-01

How to Manage Cookies in Selenium WebDriver

In this article we will see the functions to manage cookie of a browser.
In here I am using full c# code, I will provide JAVA code later on.

First step should be declaring the driver
private IWebDriver driver = new FirefoxDriver();

Please follow the your necessary functions from my past posts. Let's start our topic on cookie.

-To create a cookie object in c#
private Cookie myCookie = new Cookie();
There are 4 ways to create a cookie object (to give parameter)
1. (name, value)
2. (name, value, path)
3. (name, value, path,expiry)
4. (name, value, domain, path,expiry)
Parameters:
name: The name of the cookie.(string type)
value: The value of the cookie.(string type)
path:The path of the cookie.(string type)
domain:The domain of the cookie. (string type)
expiry:The expiration date of the cookie.(DateTime structure type)
Note : A cookie is Serializable(can be stored)
I use following way to declear a cookie

private Cookie myCookie = new Cookie("name", "value", "Domain", "Path", expDate);
Note : for expDate, I use static to use it all around 
private static DateTime expDate = new DateTime(2012,12,25);

-To add our own cookie(in here myCookie) 
driver.Manage().Cookies.AddCookie(myCookie);

-To Delete all cookie
driver.Manage().Cookies.DeleteAllCookies();

-To Delete one Specific cookie(in here myCookie)
driver.Manage().Cookies.DeleteCookie(myCookie);

-To Delete one Specific cookie whose name(string) is "shantonu"            driver.Manage().Cookies.DeleteCookieNamed("shantonu");

-To Get the cookie whose name(string) is "shantonu"             driver.Manage().Cookies.GetCookieNamed("Shantonu");
Note : This will give us an Cookie type object as output

-To know how many cookie is present
driver.Manage().Cookies.AllCookies.Count;
Note : This will give us an integer(the number of cookie) as output

-To get the Index value of one Specific cookie(in here myCookie)            driver.Manage().Cookies.AllCookies.IndexOf(myCookie);
Note : This will give us an integer(Zero Based Index Value) as output.

-To Check one Specific cookie(in here myCookie) is present or not            driver.Manage().Cookies.AllCookies.Contains(myCookie);
Note : This will give bool result ( true or false)

-To Get/set all cookies defined for the current page.
driver.Manage().Cookies.AllCookies;
Note : This will give you(or you have to provide) a collection of cookie objects from current page.

From myCookie object we can get some information through its properties.
-To Get the domain(as string) of the cookie.
myCookie.Domain;

-To Get the expiration date(as DateTime) of the cookie.
myCookie.Expiry.Value;

-To Get the Name(as string) of the cookie.
myCookie.Name;

-To Get the Path(as string) of the cookie
myCookie.Path;

-To Get a value(bool) to know whether the cookie is secure.
myCookie.Secure;

-To Get the Value(as string) of the cookie.
st = myCookie.Value;


...Thanks...:)