Sunday, 26 July 2015

An Introduction to Selenium with Java



Introduction to Selenium:

What is Selenium?
Selenium is a functional Automation tool for Web applications.
Selenium is a Browser automation tool.

What we can do with Selenium?
Regression testing.
Retesting.
Browser compatibility testing.
Operating system compatibility testing.
GUI testing.

History of the Selenium
1) Selenium 1.0 (2004) released by Thaught works.
2) Founder of selenium 1.0 -- Jason Huggins.
3) Selenium 2.0 (2010) released by Google.
4) Lead engineer for selenium 2.0 -- Simon stewart.
5) Current version of the selenium -- 2.46.0

Selenium 1.0 components:
1) Selenium IDE
2) Selenium RC
3) Selenium Grid

 Selenium 2.0 Components:
1) Selenium Webdriver ( Webdriver)
2) WebdriverBackedSelenium
3) Remote Webdriver
 

Selenium vs QTP:

 1) Selenium is a open source tool , QTP is not.
 2) Selenium supports  browsers like IE, Firefox,Chrome, Opera and Safari , Qtp Only IE.
 3) Selenium support all Os's like Windows,Linux and Mac , Qtp supports only Windows.
 4) Selenium supports languages like Java,C#,Phython,Ruby and Perl , Qtp only VBscript.
 5) Selenium is Extendable and flexible , Qtp is flexible.
 6) Selenium will not support desktop applications, Qtp support both apps.

Frameworks:

 1) Junit frame work.
 2) TestNG Frame work.

Editor’s can be Used

I). Eclipse
II).NetBeans
III).IntelliIdea
  

An Introduction to Selenium IDE


Selenium Integrated Development Envrionment (Selenium IDE)  used to  record and play back actions on a browser.
It is Firefox  add on can easily added/installed to Firefox web browser.

Steps to Install Selenium IDE:

1. Go to seleniumhq.org/download
2. Choose latest version under Selenium IDE section and click on
3. Click on allow
4. Click on install
5. Restart Firefox.
6. Now you can see the Selenium IDE icon on right corner of your Firefox browser.

 * To convert selenium ide recorded script in to your language --> go to options -->  Select your language ex: java --> it will give your language code.

To change formats:
Click on Options --> under options click on options --> select toggle button as 'Enable experimental features'.

To save recorded script:
Click on file --> click on save test case --> save test case as '.html' in your local disk.
 
To import script:
Click on file --> click on open --> go to your folder --> select your script --> Execute against your application.

To maintain timeouts:
1. Select drag and drop available in tool bar and move to fast to slow.
2. To execute single script :
3. Click on play current test case.
4. To Execute single line of code :
5. Double Click on line.

Creating Test suite in SIDE:
Click on file --> create multiple test cases --> click on save test suite --> save all test case with names --> save test suite as '.html' .

To import Test suite into SIDE :

 Click on file --> click on open test suite --> goto saved folder and select test suite.

To run test suite:
Click on play entire test suite , which is available in tool bar.

Clip board format:
Click on options--> Click on Clip board format --> choose your language and then copy single or multiple line under test case -->  paste those lines in to your Script.

 Ex :

1. html
<tr>
     <td>type</td>
       <td>id=gbqfq</td>
            <td>video</td>
</tr>

 2.Java
driver.findBy(by.id("gbqfq"));

3. python
driver.find_element_by_id("gbqfq").clear()
driver.find_element_by_id("gbqfq").send_keys("video")
 
selenium ide vs Testing
S.No Testing Task Yes or No
1 Record and playback Yes
2 Regression Testing  No
3 Retesting           No
4 Validation          No
5 Reporting           No
6 Database Validation No

Saturday, 18 July 2015

Diffrence between FindElement and FindElements

 Difference between FindElement and FindElements

 

findElement() :   

  • Syntax: WebElement findElement(By by)    
  • Find the first element within the current page using the given "locating mechanism"
  • Returns a single WebElement  
  • Throws a runtime exception NOSuchElementException when element is not found
  • Input parameter is instance of By class
  • We use findElement method frequently in our webdriver to find a Web element

findElements() :   

  • Syntax: java.util.List<WebElement> findElements(By by)
  • Find all elements within the current page using the given "locating mechanism"
  • Returns Empty list when no matching elements found or list of web elements when match found
  • NO exception throws when element is not found
  • Input parameter is instance of By class
  • We use findElements method occasionally to find out list of web elements, E.g: List of Links,buttons,..,

Examples of findElement() and findElments() methods:

 

1.    findElement()

Let's see how the code looks like to find out the first web element tag name on a Google Search page because It find’s the first element within the current page using the given "locating mechanism"

public class GoogleSearchPageByTagName{
 public static void main(String[] args){
        IWebDriver driver = null;  
           driver  = new ChromeDriver(@"C:\ChromDriverSelenium");
           driver.Navigate().GoToUrl("http://www.google.com");
           IWebElement webelement= driver.FindElement(By.TagName("input"));
           Console.WriteLine("Tag name is" + webelement.TagName);
           Console.ReadLine();
        driver.Quit();
   }
}

2.    findElments()

 

Let's see how the code looks like to find out the number of input tags present on a Google Search page.
 public class GoogleSearchPageByTagName{
        static void Main(string[] args)
      
        {
           
           IWebDriver driver = null;  
           driver  = new ChromeDriver(@"C:\ChromDriverSelenium");
           driver.Navigate().GoToUrl("http://www.google.com");
           IList<IWebElement> Tags= driver.FindElements(By.TagName("input"));
           Console.WriteLine("Total input Tags" + Tags.Count());
           Console.ReadLine();
           driver.Quit();

        }
       
    }

Wednesday, 8 July 2015

Locating WebElements Continuation.


5.    By.Tagname method:

Tag name method used when trying to find out list of web Elements  in a page, For example all the links in a web page or list of buttons or list of text boxes in a page.
 By.Tagname results more then one web element so it is correct  to use findElements() method instead of findElement() method.
For now we remember FindElements method to find out list of webelements in a page but  later we go in details difference between findElement() and findElements() as it is very import to know.


Let's see how the code looks like to find out the number of buttons present on a Google Search page.


 public class GoogleSearchPageByTagName{
 public static void main(String[] args){
 IWebDriver driver = null;      
driver = new FirefoxDriver();      
driver.Navigate().GoToUrl("http://www.google.com");     
IList<IWebElement> buttons = driver.FindElements(By.TagName("button"));     
 Console.WriteLine("the numbeers of buttons in google page"+buttons.Count());      
driver.Quit();
}
}



The above code prints a list of all the buttons available on the page that is 3 in this case.

6.    By.Class name method:
 
The className() method is used to identify the web element when html code contains an class name like below.



In the above code we can identify the WebElement by using Name or Id or Class Name
Here take Class name " gsfi".

let's try this on our Google search page. We will try to make WebDriver identify the Google Search box using its class name and search for 123selenium.blogspot.in  . To see the html code of that Search text box right click on that google search text box and select Inspect Element option and you can see the html code.



Below is the Sample code to search 123selenium.blogspot.in in Google.

public class GoogleSearchPageByClassName{
public static void main(String[] args){
IWebDriver driver = null;  
           driver = new FirefoxDriver();
           driver.Navigate().GoToUrl("https://www.google.com");
           IWebElement searchTextbox = driver.FindElement(By.ClassName("gsfi"));
           searchTextbox.SendKeys("123selenium.blogspot.in");
           driver.Quit();
}
}



7.    By. CSS Selectors method:

We can also use CSS selectors to identify a web element in web page below are the different CSS Selectors.

  •  Tag and ID
  •  Tag and class
  •  Tag and attribute
  •  Tag, class, and attribute
  •  Inner text

  1. Tag and ID :

In this method we  use tag name and Id of the web element in html code , For example when we have blow html code we can see the tag name and ID of Google search box.


The Syntax is: css=tag#id
In Above html code below are the Tag and Id values.

Tag =input
Id =lst-ib

So the CSS selector to use is input#Email

let's try this on our Google search page. We will try to make WebDriver identify the Google Search box using its CSS Selector Tag and Id and search for 123selenium.blogspot.in

public class GoogleSearchPageCssSelectorName{
public static void main(String[] args){
IWebDriver driver = null; 
           driver = new FirefoxDriver();
           driver.Navigate().GoToUrl("https://www.google.com");
           IWebElement SearchTextbox = driver.FindElement(By.CssSelector("input#lst-ib"));
           SearchTextbox.SendKeys("123selenium.blogspot.in");
           driver.Quit();
}
}



  1. Tag and class
In this method use tag name and class name of the web element to identify a web element, For example when we have below html code you can see Tag and Class name for bing search text box .



The Syntax is : css=tag.class

In Above html code below are the Tag and Class names values.
Tag name =a
Class name =b_searchbox

So the CSS selector to use is a.need-help

let's try this on our Bling search page. We will try to make WebDriver identify the Bling Search box using its CSS Selector Tag and Class name and search for 123selenium.blogspot.in


public class GoogleSearchPageByCssSelector1{
IWebDriver driver = null; 
 driver  = new ChromeDriver(@"C:\ChromDriverSelenium");
 driver.Navigate().GoToUrl("http://www.bing.com");
 IWebElement SearchTextbox = driver.FindElement(By.CssSelector("input.b_searchbox"));
 SearchTextbox.SendKeys("123selenium.blogspot.in");
 IWebElement SearchButton = driver.FindElement(By.CssSelector("input.b_searchboxSubmit"));
 SearchButton.Click();
 driver.Quit();
}
}



  1. Tag and attribute

In this method use tag name and selected attribute value of the web element to identify a web element for example when we have below html code.

        The Syntax is css=tag[attribute=value]

In Above html code below are the Tag and Class names values.

Tag name =input
Attribute name = name
Attribute value =q

So the CSS selector to use is input[name=”q”]

let's try this on our Bling search page. We will try to make WebDriver identify the Bling Search box using its CSS Selector Tag and Attribute name and search for 123selenium.blogspot.in


 public class GoogleSearchPageByCssSelector2{
 public static void main(String[] args){
 IWebDriver driver = null; 
 driver = new FirefoxDriver();
 driver.Navigate().GoToUrl("http://www.bing.com");
  IWebElement SearchTextbox = driver.FindElement(By.CssSelector("input[name=q]"));
  SearchTextbox.SendKeys("123selenium.blogspot.in");
  driver.Quit();
}
}


  1.  Tag, class, and attribute

In this method use tag name,Class Name and selected attribute value of the web element to identify a web element for example when we have below html code.

The Syntax is css=tag.class[attribute=value]

In Above html code below are the Tag and Class names values.

Tag =input
Class =gsfi
Attribute name = name
Attribute value =q

So the CSS selector to use input.gsfi[name='q']

The above is html code for google search text  box after identifying the web element we enter some text to search as shown in below.




 public class GoogleSearchPageByCssSelector3{
 public static void main(String[] args){
 IWebDriver driver = null; 
 driver = new FirefoxDriver();
 driver.Navigate().GoToUrl("https://www.google.com");
 IWebElement SearchTextbox = driver.FindElement(By.CssSelector("input.gsfi[name='q']"));
 SearchTextbox.SendKeys("123selenium.blogspot.in");
 driver.Quit();
}
}



  1.  Inner text
In this method use tag name and inner text of web element. 

 The Syntax is css=tag:contains("inner text")


8.    By. Xpath method:

 Before going to see the Xpath of an web element we install the firefox add –on Firebug and it’s extension Firepath.

Search google for Firebug and install it
Once installed restart the browser
When open the firefox browser after restart you can see the icon on the top right corner

Now click on that icon    to launch the Firebug which will open like below.


Now you can right click on any web element and select the option Inspect Element with Firebug will shows the html code of the element.
For example: To see the Google Search button in Google page right click on the button and select option Inspect Element with Firebug will show the below code.


 

For Google Search button we can write the Xpath as: //input[@name="btnK"]

When you write the above xpath in the Firepath it locates the html code of Google Search button as shown below.



When to use Xpath?

When html code does not have name,class name or ID to identify the web element we use Xpath. It is the complex method to identify the elements but you can identify the elements uniquely.

The only disadvantage it is cost to in terms of time as it takes more time to go inside the entire html code of the page and find the web element. 

To find out all the input tag elements try //input which shows the input type web elements

To find the button tags try:                         //button
To find out the links in page :                      //*/a
To find out the links in a div:                        //div/a
To find out the links in second div:           //div/div/a

Below is the example to find the xpath of Google search text box and search button.

            IWebDriver driver = null;  
            driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("https://www.google.com");
            IWebElement SearchTextbox = driver.FindElement(By.XPath("//input[@id='lst-ib']"));
            SearchTextbox.SendKeys("123selenium.blogspot.in");
            IWebElement SearchButton = driver.FindElement(By.XPath("//button[@name='btnG']"));
            SearchButton.Click();