Selenium WebDriver Interview Questions

Selenium WebDriver Interview Questions

1) Give a detailed introduction about Selenium WebDriver?
 

Selenium 1 (Selenium IDE + selenium RC + Selenium Grid)

Selenium 2 (Selenium IDE + Selenium RC + Selenium WebDriver + Selenium Grid)

Note: Selenium WebDriver merged with Selenium 1 and called as Selenium 2.

• It is a most important tool in Selenium suite.

• It is has programming interface allows us to create and execute Test cases against different browsers (ex: Firefox, IE, Google Chrome etc...)

• WebDriver supports various programming languages(Java, .Net, PHP, Perl, Python and Ruby) to enhance Test cases.

• WebDriver supports various operating environments (MS Windows, UNIX and Macintosh etc...) to create and execute Test cases.

• WebDriver supports Data Driven testing, Cross browser testing.

• Webdriver is faster than other tools in Selenium suite.

• WebDriver supports Parallel test execution with the help of TestNG.

• WebDriver doesn't have IDE, only Programming interface.

• WebDriver doesn't have built in Result reporting facility, it provides summary only.
(* with the help of TestNG we can generate HTML test reports.)

• No object Repository in selenium WebDriver (This limitation is for entire Suite), so no centralized maintenance of Objects).

• Using Element locators, Webdriver methods and Java programming features we can createand execute Test cases.

2) How to set up Selenium WebDriver Environment?
 

• Download and install Java (JDK) software - to enhance test cases using Java programming features.

• Set Path environment variable- to access Java from any directory.

• Download Eclipe IDE and extract

• Download WebDriver Java Language binding and add Webdriver jar files (in Eclipse)

• Install Firebug and FirePath plug ins for Mozilla Firefox browser to inspect Elements.

Note: For Internet Explorer and Google chrome, no need to install any plug ins, They have built in Developer tools.
Note 2: Element locators and WebDriver methods are common for all browsers, browser driver only varies from one browser to another.
Note 3: Firefox driver is default driver in Webdriver, For IE and Chrome we need to download drivers.


Download Selenium WebDriver Java binding from www.seleniumhq.org website and extract.

Navigation for adding Webdriver jar files in Eclipse.
 

Create Java project in Eclipse

> Select src and right click

> Build path

> Configure Build Path

> select Libraries tab

> Click "Add External JARs

> Browser path of the WebDriver jars

> Add

3) How to create Test cases using Selenium WebDriver?
 

• Using Element locators, WebDriver methods and Java programming features we can create Test cases.

• Element locators for recognizing objects/elements.

• WebDriver Methods are used perform operatopns on Elements or objects.

• Java Programming for enhancting Test cases.

4) What are the Testing frameworks support Selenium WebDriver with Java?
 

JUnit

TestNG

We can use either JUnit or TestNG testing framework with Java and Webdriver

5) What are the Element Locators that Selenium WebDriver supports to recognize Elements?
 

Selenium Webdriver supports 8 Element locators to recognize Objects/Elements.

i) id

ii) name

iii) className

iv) tagName

v) linkText

vi) partialLinkText

vii) cssSelector

viii) xpath

Note: We can use any one unique locator to recognize the Object/Element.

6) What is Cross Browser Testing?
 

Testing web applications using multiple browsers, Cross browser testing involves checking compatibility of the application.

7) How to conduct Cross Browser Testing using WebDriver?
 

• Using Browser Drivers we can conduct Cross browser testing.

• For Mozilla Firefox, we no need to download the browser driver.

• For IE and Chrome etc... browsers download the browser drivers and set path.

Create Browser Driver Object for Mozilla Firefox:

WebDriver driver = new FirefoxDriver();  

Create Browser Driver Object for Internet Explorer:

System.setProperty("webdriver.ie.driver", "E:/IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();  

Create Browser Driver Object for Google Chrome:

System.setProperty("webdriver.chrome.driver", "E:/chromedriver.exe");
WebDriver driver = new ChromeDriver();  

8) What are the important operations on Browser object?
 

Operation: Open URL
WebDriver code: driver.get("http:/google.com");

Operation: Return Browser Title
WebDriver code: String s = driver.getTitle();

Operation: Return Current URL
WebDriver code: String s = driver.getCurrentUrl();

Operation: Close focused Browser
WebDriver code: driver.close

Operation: Close all Browsers that opened by WebDriver
WebDriver code: driver.quit

9) What are the Elements/Objects in Web Applications?
 

Link
Button
Image, Image Link, Image Button
Text box
Edit Box
Text Area
Check box
Radio Button
Drop down box
List box
Combo box
Web table /HTML table
Frame
Etc...

10) Write a Test case using Selenium WebDriver?
 

Manual Test Case:

Test Case Name: Admin Login

Steps:

i) Launch the Browser and navigate to www.gcrit/build3/admin/

ii) Enter user name

iii) Enter Password

iv) Click Login button

Verification point:
--------------------
Check the existance of "Logoff" link, if exists then pass otherwise fail

Selenium Test Case:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();

if (driver.findElement(By.linkText("Logoff")).isDisplayed()){
    System.out.println("Login Successful");
}
else {
    System.out.println("Login Failed");
}

11) How to conduct Batch Testing using WebDriver?
 

Using TestNG Annotations we can group Test cases and execute series of Test cases.

12) How to handle Link element?
 

Operation: Click Link
WebDriver code: driver.findElement(By.linkText("Gmail")).click();

Operation: Check the link existance
WebDriver code:
boolean a = driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[1]/a")).isDisplayed();
System.out.println(a); //true

Operation: Check the link enabled status
WebDriver code:
boolean b = driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[1]/a")).isEnabled();
System.out.println(b); //true

Operation: Return the Link Name
WebDriver code:
String c = driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[1]/a")).getText();
System.out.println(c); //Gmail

13) How to handle Button element?
 

Important operations on Buttons:
Enabled status

Display status

Click

Return name of the Button

type of the object

Example:

public static void main(String[] args) {
    WebDriver driver = new FirefoxDriver();
    driver.get("https://gmail.com");
    WebElement button = driver.findElement(By.id("next"));
    boolean a = button.isDisplayed();
    boolean b = button.isEnabled();
    button.getAttribute("name");
    button.getAttribute("type");
    button.click();
    System.out.println(a);
    System.out.println(b);
    }

14) How to handle Edit box element? 


Important Operations on Edit box:
Enter a Value,

Clear the Value,

Check enabled status,

Check edit box existence,

Get the value etc...
----------------------------
Example:
public static void main(String[] args) {
    WebDriver driver = new FirefoxDriver();
    driver.get("https://gmail.com");
    //Enter a Value
    driver.findElement(By.id("Email")).sendKeys("ABCD123");
  
    //Return the Value
    String value = driver.findElement(By.id("Email")).getAttribute("name");
  
    //Return Type of the Object
    String value2 = driver.findElement(By.id("Email")).getAttribute("type");
  
    //Return display status
    boolean a = driver.findElement(By.id("Email")).isDisplayed();
  
    //Return Enabled status
    boolean b = driver.findElement(By.id("Email")).isEnabled();
  
    System.out.println(value);
    System.out.println(value2);
    System.out.println(a);
    System.out.println(b);
    // Clear the Value
    driver.findElement(By.id("Email")).clear();
    }
}

15) How to handle Images?
 

Three types of Image elements in Web Environment
1) General Image (No functionality)

2) Image Button (Submits)

3) Image Link (Redirects to another page/location)

Example:
public static void main(String[] args) {
    WebDriver driver = new FirefoxDriver();
    driver.get("https://google.com");
    String Title = driver.findElement(By.id("hplogo")).getAttribute("Title");
    System.out.println(Title);
  
    driver.navigate().to("http://newtours.demoaut.com/");
    driver.findElement(By.name("login")).click();
  
    driver.navigate().to("http://www.seleniumhq.org/");
    driver.findElement(By.xpath(".//*[@id='choice']/tbody/tr/td[2]/center/a/img")).click();
    }


16) How to handle Radio Button element?
 

Operations on Radio Button 
i) Select Radio Button

ii) Verify if the Radio Button is Displayed or not?


iii) Verify if the Radio Button is enabled or not?


iv) Verify if the Radio Button is Selected or not?
---------------------
Example:
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gcrit.com/build3/create_account.php?osCsid=boh81f11tmud1134jnp5ne7r20");
boolean a = driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]")).isDisplayed();
System.out.println(a); //true

boolean b = driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]")).isEnabled();
System.out.println(b); //true

boolean c = driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]")).isSelected();
System.out.println(c); //false

driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]")).click();

boolean d = driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]")).isSelected();
System.out.println(d); //true

driver.findElement(By.xpath(".//*[@id='bodyContent']/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]")).clear();
}

17) How to handle Check box element?
 

Operations on Check box
i) Check if the check box is displayed or not?

ii) Check if the check box is enabled or not?

iii) Check if the check box is Selected or not?

iv) Select the Check box

v) Unselect the Check box

Example:

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("file:///E:/HTMLExamples/MultipleCheckbox.html");

boolean display = driver.findElement(By.xpath("html/body/input[2]")).isDisplayed();
System.out.println("Displayed Status: " + display);

boolean enabled = driver.findElement(By.xpath("html/body/input[2]")).isEnabled();
System.out.println("Enabled Status: " + enabled);

boolean check = driver.findElement(By.xpath("html/body/input[2]")).isSelected();
System.out.println("Check Status: " + check);

driver.findElement(By.xpath("html/body/input[2]")).click();

boolean check2 = driver.findElement(By.xpath("html/body/input[2]")).isSelected();
System.out.println("Check Status: " + check2);

driver.findElement(By.xpath("html/body/input[2]")).click();

boolean check3 = driver.findElement(By.xpath("html/body/input[2]")).isSelected();
System.out.println("Check Status: " + check3);

}

18) How to handle Frames?
 

• HTML frames are used to divide our Browser window into multiple sections,
where each section can load a separate html document.

• Frames are sections of Web page displayed on top window.

• Whenever we access the page then focus is on the top window.

Switch to a frame is done in two ways:--------------------------------------
i) Using frame index
Syntax:

driver.switchTo.frame(int index);

ii) Using frame name
Syntax:

driver.switchTo.frame(String name);

Example:
//Using Frame index

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.switchTo().frame(0);
driver.findElement(By.xpath("html/body/div[2]/ul/li[1]/a")).click();
}
-------------------------------
//Using Frame name

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.switchTo().frame("packageListFrame");
driver.findElement(By.xpath("html/body/div[2]/ul/li[1]/a")).click();
}
---------------------------------
Switch from a frame to Top window:
driver.switchTo().defaultContent();

Example:

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");

//Switch from Top window to 3rd frame
driver.switchTo().frame(2);
driver.findElement(By.xpath("html/body/div[3]/table/tbody[2]/tr[1]/td[1]/a")).click();
//Switch from 3rd frame to Top window
driver.switchTo().defaultContent();
//Switch from Top window to 1st frame
driver.switchTo().frame(0);
driver.findElement(By.xpath("html/body/div[2]/ul/li[1]/a")).click();
}
}

19) How to handle Web Table?
 

Important Operations on Web Table:
Get cell value

Rows Count

Cells Count

Example:
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("file:///E:/HTMLExamples/htmlTable.html");

String s = driver.findElement(By.xpath(".//*[@id='students']/tbody/tr[2]/td[2]")).getText();
System.out.println(s);

WebElement htmlTable = driver.findElement(By.id("students"));

List <WebElement> rows = htmlTable.findElements(By.tagName("tr"));
int i = rows.size();
System.out.println(i);

List <WebElement> cells = htmlTable.findElements(By.tagName("td"));
int j = cells.size();
System.out.println(j);
}

20) How to handle Multiple Browsers?
 

We can Handle Multiple Browsers using Browser window handle.

21) How to handle duplicate elements/objects?
 

We can Handle duplicate objects/elements using index, index starts from 0 to 9 and Top left of the web page to Bottom right.
--------------------------------------------
Selenium Interview Questions-1

Selenium Interview Questions-2

Java for Selenium  Interview Questions

Selenium WebDriver Questions

Selenium IDE Questions

TestNG Interview Questions

0 comments:

Post a Comment