Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Element Identification using Selenium WebDriver: Questions and Answers

This post explains the questions on understanding how web elements are identified by Selenium WebDriver.This covers findElement, findElements, various locators and profiling using WebDriver.


Question 1: What is the method used to launch URL using Selenium WebDriver?

Answer: In Firefox, we can set the homepage as the required URL as shown in the code below. This will launch the URL set in homepage on launching the driver.

FirefoxProfile profile = new FirefoxProfile();

profile.setPreference("browser.startup.homepage","http://www.google.com");

//We will provide the profile used as argument for Firefox Driver.

WebDriver driver = new FirefoxDriver(profile);


In case, we do not want to use profile, we can use the get method to launch the URL.

WebDriver driver = new FirefoxDriver();

driver.get("https://www.google.com");


Question 2: What is the difference between findElement and findelements method in Selenium?

Answer: To locate an element in the page, we use findElement method whereas findElements gives the collection of web elements in the Page.

Syntax for findElement is:

WebElement tableinfo = driver.findElement(By.className("tableClsName"));


This will give the first element in the page with className as “tableClsName”. Now once the element is recognized, we can perform action on the WebElement. Suppose the element is a button, we can click on the button as shown below:

Tableinfo.click();


Suppose the WebElement is an input box, we can insert data in the input box as shown below:

Tableinfo.SendKeys(“This is a test”);


Syntax for findElements is:

List<WebElement> tabledata = tableinfo.findElements(By.tagName("td"));


This will give collection of all elements in the page with className as “td”.
We can further work on the collection of the object and extract the required information or perform actions on the required webElement as shown below:

List<WebElement> tabledata = tableinfo.findElements(By.tagName("td"));

for (int iCnt = 0;i<tabledata.size();iCnt++)

 {

String strData = tabledata.get(iCnt).getText() ;

If(strData.contentEquals(“this is the row”))

{

System.out.println(“Web Element Found”)

iCnt = tabledata.size()-1;

}

}


Question 3: What are the value ways to identify an object in Selenium WebDriver?

Answer: Below are the various locators by which elements can be identified in Selenium WebDriver:

            1. Id:  Identifies WebElement by the ‘Id’ attribute.

            2. ClassName: Identifies webelement by the ‘class’ attribute.

            3. cssSelector: Identifies element based on the css of the webElement.

            4. linkText: Identifies element by the actual text of the link. Text should match exactly with the link text.

            5. Name: Identifies webelement by the ‘name’ attribute.  

            6. partiallinkText: Identifies element by the actual text of the link. The link is identified on partial match of text.

           7. tagName: Identifies an object by the tagName of the webElement.

           8. xpath: Identifies an object by the xpath of the object.


Question 4: Below is a picture of google Page, how can we identify the inputbox for search using the various locators identifier?

Input box object identification

Answer: The WebElement can be identified by different locators as shown below:
         1.  Id:  WebElement Elem = driver.findElement(By.id(“gbqfq”));
         2.  ClassName:  WebElement Elem = driver.findElement(By.className(“gbqfif”);
         3.  cssSelector: WebElement Elem = driver.findElement(By.cssSelector(“input#gbqfq”);
         4.  linkText:  Not Applicable as the element is an input box
         5.  Name: WebElement Elem = driver.findElement(By.Name(“q”);
         6.  partiallinkText: Not Applicable as the element is an input box 
         7.  tagName:  WebElement Elem = driver.findElement(By.tagName(“input”);
         8.  xpath:  WebElement Elem = driver.findElement(By.xpath(“//input[@className = ‘gbqfif’]”);

Question 5: I want to identify the title of the page is displayed properly. What will be the code to verify this?

Answer: Using driver.getTitle, we can get the title of the current Page.

if(driver.getTitle().contentEquals("Rediff Home Page"))

{

         System.out.println(“Correct Page is displayed);

}

else

{

       System.out.println(“InCorrect Page is displayed);     

}



First Steps in Selenium WebDriver: Useful Questions with Answers

In this post, we discusses on the few initial questions before starting with using Selenium WebDriver for automation of web application. This will cover the basic topics on Selenium WebDriver and in later articles, we will go further on discussing the use of selenium WebDriver, how to use it in project and framework creation using Selenium in Java.

Question 1: What is Selenium Web Driver?

Answer:  Selenium Web driver uses accessibility API to drive the browser and recognizes the object in web-based application. It allows writing automated tests replicating the behavior of real user, e.g. Clicking on a link or feeding data in an edit box. Using Selenium Web Driver together with any programming language and other testing framework like TestNG and JUnit, automation test framework to execute tests on different browsers can be created. We can use the robust features of the programming language to interact with other components of application like database interaction, files interaction to create a robust and reliable framework.

Question 2: What are the various browsers supported by Selenium WebDriver?

Answer: WebDriver available for automation in different browsers are ChromeDriver, InternetExplorerDriver, FirefoxDriver, OperaDriver and HtmlUnitDriver.

Question 3: What are the programming languages supported by WebDriver?

Answer: Python, Ruby, C#, Java, Perl, php and javascript are supported.

Question 4: What are the necessary steps required, before I start creating selenium tests on java using eclipse?

Answer: We require following configuration to be set up before creating test in eclipse:
1. Java needs to be installed in the machine.
2. Eclipse IDE needs to be installed.
3. Selenium Libraries for WebDriver should be available.
Please click on link below for details for configuration steps in eclipse.

Question 5: Do we need to install some additional executable files working with Selenium WebDriver?

Answer: For Firefox driver, we do not require any additional executable as it is bundled with Java client bindings, For IE driver, Chrome driver, and Opera driver, we require additional executable that can be downloadable from Selenium Official website.
An example of how to create an instance of Internet Explorer Web Driver is shown in below example.
How to create an instance of driver in IE and Firefox

Question 6: Is there any specific pre-condition before executing a selenium script on Internet Explorer?

Answer: Yes, Protected Mode settings should be same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.

Question 7: What are Firefox profile preferences?

Answer: Using Firefox Profile, we can set the preferences for the Firefox in the profile.
The steps to define profile in Selenium are:
1.  Create an object of firefox profile
FirefoxProfile profile = new FirefoxProfile();
2.   Set the required preference of the firefox :
                  profile.setPreference("browser.startup.homepage",”http://www.google.com”)
3.  Create the driver object with profile as defined above.
     WebDriver driver = new FirefoxDriver(profile);

Question 8: What is the use of using TestNG with selenium in Java?

Answer: TestNG is a unit testing framework used for java programming language. Test Cases using Selenium Webdriver to identify webelement in the web application can be integrated/created on TestNG framework. Some of the useful features TestNG provides are as follows:
          1.  Using Annotation to describe order of test cases, defining start conditions and end condition using  @after and @before annotations.
                2.  Reporting feature for test execution status in html and xml format.