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