You are on page 1of 15

Ques 1: What is difference between close() and quit() method?

 The close() method is to close the currently active window of the browser.

 The quit() method is to close all the windows session.

Ques 2: What are different navigate commands?


 navigate.to(URL) – To navigate to a particular URL.

 navigate.back() – To navigate back to a URL in browsing history.

 navigate.forward() – To navigate forward to a URL in browsing history.

 navigate.refresh() – To reload the page.

Ques 3: What are different get commands in Selenium WebDriver?


 getTitle() – To get the title of the page.

 getCurrentUrl() – To get the current URL of the page.

 getPageSource() – To get the HTML code of a page.

 getText() – To get the text from a WebElement.

 getAttribute() – To fetch the value of an attribute from HTML code of a WebElement.

 getWindowHandle() – This method returns a unique session id assigned to the currently active
window.

 getWindowHandles() – This method returns a set of session id’s assigned to all the windows
opened.

Ques 4: What is the command to delete all cookies from a browser?


 deleteAllCookies() method is used to delete all the cookies from a browser.

 Ques 5: How to clear text from a textbox?

1 Driver.manage.deleteAllCookies();
 clear() method is used to clear textbox

1 Driver.findElement(By.xpath("//input[@type='text']")).clear();
Ques 6: What are different Identifiers used in Selenium WebDriver?
There are 8 Locator’s or Identifiers in Selenium WebDriver:

 By.id
 By.name

 By.classname

 By.tagname

 By.cssselector

 By.xpath

 By.linktext

 By.partiallinktext

Ques 7: What is the difference between “/” and “//” ?


 “/” is used to access the immediate child of a tag whereas “//” is used access any child of a Tag.

 “/” is used in absolute xpath and “//” is used in writing relative xpath.

For Example:

html/body/table/tbody/tr[2]/td/input – This is an example of absolute xpath. we are navigating


to input tag by accessing immediate childs.

//input[@type=’text’] – This is an example of relative xpath. It represents firt input tag with
type=’text’

Ques 8: What is the return type of findElement() and findElements()?


 findElement() method returns a unique WebElement.

 findElements() method returns a list of all the matching WebElements.

Ques 9: How to launch Internet Explorer and Chrome Driver?


 Launching Internet Explorer Driver:

1 System.setProperty("webdriver.ie.driver", "<path of IE Driver>");


2
3 InternetExplorerDriver IEDriver = new InternetExplorerDriver();
 Launching Chrome Driver:

1 System.setProperty("webdriver.chrome.driver", "<path of Chrome Driver>");


2
3 ChromeDriver ChDriver = new ChromeDriver();
Ques 10: How to get the text from a WebElement?
 getText() – This method is used to get the text from a WebElement.

Ques 11: Write a code get number of links on a page?


1 int numberOfLinks = Driver.findElements(By.tagname("a")).size();
Ques 12: How to perform Double click operation using Selenium WebDriver?
 Actions class from Selenium WebDriver has a method doubleClick() which is used to perform
double click operation.

1 Actions action = new Actions(Driver);


2
3 action.doubleClick(Element).build.perform();
Ques 13: What is the significance of contextClick() method in Selenium?
 contextClick() method is used to perform Right Click event of Mouse Operation.
 Actions class from Selenium WebDriver has this method “contextClick()” which performs Right
Click.

Ques 14: Is WebElement an Interface or a Class?


 WebElement is an Interface.

Ques 15: What is the method used to verify whether a Checkbox/Radio button is checked on
not?
 isSelected() – method is used to verify whether a checkbox or a radio is checked or not.

Ques 16: How to verify that a WebElement is greyed out or not?


 isEnabled() – method is used to verify whether a WebElement is greyed out or not.

Ques 17: How to handle a Dropdown in Selenium WebDriver?


Select class from Selenium WebDriver is used to handle Dropdown.

1 Select dropdown = new Select(Element);


Now this dropdown object of class Select has below methods to work with on a Dropdown:
 selectByValue(“value”); – Selects an option by value from a dropdown.
 selectByVisibleText(“text”); – Selects an option by visible text from a dropdown.
 selectByIndex(index); – Selects by index from the dropdown.
 deselectByValue(“value”); – Deselects an option by value from a dropdown.
 deselectByVisibleText(“text”); – Deselects an option by visible text from a dropdown.
 deselectByIndex(index); – Deselects an option by index from a dropdown.
 deselectAll(); – Deselects All options in a dropdown.
 isMultiple(); – Boolean operation which verifies whether a dropdown allows multiple selection
or not.
 getOptions(); – Returns a list of all options from a dropdown.
 getFirstSelectedOption(); – Returns first option selected in a dropdown as a WebElement .
 getAllSelectedOptions(); – Returns a list of all selected options in a dropdown.

Ques 18: How to perform Drag and Drop operation using Selenium WebDriver?
Drag and drop operation means to grab an object and move it to some other location. Selenium
WebDriver provides Actions class to perform drag and drop operation.

1 Actions action = new Actions();


2 action.dragAndDrop(sourceElement, destinationElement).build.perform();
Ques 19: Is FirefoxDriver a class or an interface and from where is it inherited?
 FirefoxDriver is a class in Selenium WebDriver which implements an Interface WebDriver.

Ques 20: How to validate that a WebElement is visible on a page?


 isDisplayed() is a boolean method which returns whether a WebElement is visible on a page or
not.

Ques 21: What is the difference between a getWindowHandle() and getWindowHandles()


methods?
 getWindowHandle() – method returns a unique sessionId of the currently active window, which
selenium maintains to identify a browser window session.

 getWindowHandles() – method returns a set of unique sessionIds of all the opened windows.

Ques 22: How to switch to a new window (new tab) which opens up after you click on a link?
 When a click operation on link results in opening up of a new window (or new tab), the control
of selenium does not automatically switches to the new window (or new tab). We have to write
code to switch selenium code to the new window.

Scenario:
1. Save the sessionId or window handle of Parent window.

2. Click on the click which performs opening up of a new window operation.

3. Get the sessionId of Child Window.

Switch to the child window.

1 // Defining variables to store session id of parent window and child window


2 String sParentWindow, sChildWindow;
3
4 // Getting session id of parent window
5 sParentWindow = Driver.getWindowHandle();
6 System.out.println("Session Id od Parent window "+ sParentWindow);
7
8 //Clicking on a button or link which results in opening up of a new window
9 Driver.findElement(By.tagName("button")).click();
10
11 //Getting session Id of child window
12 sChildWindow = Driver.getWindowHandles().toArray()[1].toString();
13 System.out.println("Session id of Child Window "+ sChildWindow);
14
15 //Switching to Child Window
16 Driver.switchTo().window(sChildWindow);
17 System.out.println("Title of Child Window is : "+ Driver.getTitle());
18 Driver.close();
19
20 //Switching back to parent window
21 Driver.switchTo().window(sParentWindow);
Ques 23: What is the name of the headless/GUI-less browser?
 HTML UnitDriver and PhantomJS

Ques 24: How to switch back from a frame?


 defaultContent() – is the method to switch back from a frame to the parent window.

Ques 25: Write a code to get URL from a link webElement?


 Links are represented by anchor tag on a web page “<a>”

 Anchor tag has an attribute called href which keeps the value of the URL (address where will
navigate to after clicking).
 getAttribute() – method is used to get the attribute from a WebElement.

1 WebElement link = Driver.findElement(By.linkText("Advertise"));


2 String url = oLink.getAttribute("href");
3 System.out.println("From attribute :"+ url);

Ques 1 : What are two different methods to launch a Browser in Selenium WebDriver?
There are two methods to launch a browser:

get(url)

navigate.to(url)

1 public void invokeBrowser(){


2
3 String url1 = "http://qatechhub.com";
4
5 String url2 = "http://www.facebook.com";
6
7 Driver.get(url1);
8
9 Driver.navigate().to(url2);
10
11 }
Ques 2: How to send ENTER/TAB key in Selenium WebDriver?

1 public void searchitem(){


2 // Define a textbox as a Web Element with any one of the Identifier, not necessarily xPath
3 WebElement textbox = Driver.findElement(By.xpath("//input[@type='text']"));
4
5 //To Pass tab key
6 textbox.sendKeys(Keys.TAB);
7
8 //To Pass Enter key
9 textbox.sendKeys(Keys.ENTER);
10
11 }
Ques 3: List down methods for Alert Handling?
Following are the frequently used methods within an alert.

 Accept an alert.

 Reject an alert.

Get Message from an alert.

1 public void alertHandling(){


2
3 Alert alert = Driver.switchTo().alert();
4
5 // To get message from an Alert
6 String message = alert.getText();
7 System.out.println("To get the message from an alert : "+ message);
8
9 //To accept an Alert
10 alert.accept();
11
12 //To reject an Alert
13 alert.dismiss();
14
15 }
Ques4: Difference type of wait statement in Selenium WebDriver?
In Selenium WebDriver, to sync up scripts there are three types of wait:

 PageLoadTimeout – This is the maximum time selenium waits for a page to get load
successfully on a browser. If the page takes more than this time, it will throw Page not found
Exception.

Driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);

 Implicit Wait – this wait can be considered as element detection timeout. Once defined in a
script, this wait will be set for all the WebElements on a page.
 Selenium keeps polling to check whether that element is available to interact with or not.

It the maximum time, selenium code waits to interact with that Web Element, before throwing
“Element not found exception”.

1 Driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
 Explicit Wait – this wait can be considered as conditional wait, and is applied to a particular
Web Element with a condition. There are many conditions which can be applied using explicit
wait.
Say, for example, there is a Web Element on a page which takes more than expected time to
appear on the page, so instead of increasing Implicit wait for a particular Web Element we can
apply explicit wait to that element with a condition.

public void waitTillElementVisible(){


1
2
WebDriverWait wait = new WebDriverWait(Driver, 90);
3
4
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//input[@type='
5
text']")));
6
7
}
Ques5: Difference between Absolute and Relative xpath?
 Absolute xpath starts from starting of the page. As in an html page, the first tag is HTML.
 “/” is used to access an immediate child of the parent tag.

 Example: html/body/table/tbody/tr[2]/td/input

 Relative xpath starts from anywhere on the page.


 “//” is used to access any child of the parent tag.
 Syntax: //htmlTagname[@attribute=’value’]

 Example: //input[@type=’text’] – It represents xpath of a WebElement which is represented by


tagname input and has an attribute type = ‘text’.

Ques6: Write a code to get numbers of frames on a page?


To get the number of frames, we will call a method called findElements which will return all the
frames elements in a list, and then find its size.

1 public void getNumberOfFrames(){


2
3 int numberOfFrames = Driver.findElements(By.tagName("iframe")).size();
4
5 System.out.println("Number of frames on a page: "+ numberOfFrames);
6
7}
Ques7: How to scroll down a page using JavaScript in Selenium?
Scroll down operation can be performed by invoking JavaScript

1 public static void scrollPage(WebDriver oBrowser, int x, int y){


2 String jsCommand;
3 JavascriptExecutor oJSEngine;
4
5 oJSEngine = (JavascriptExecutor) oBrowser;
6
7 jsCommand = String.format("window.scrollTo(%d, %d)", x,y);
8
9 oJSEngine.executeScript(jsCommand);
10 }
Ques8: How to get the colour of a text or a link text?
Ques9: How to get the width of a textbox?

Ques10: How to take Screenshot in Selenium WebDriver?


To take a screenshot in Selenium there is a method called getScreenshotAs() from a class called
TakesScreenshot.

1 public void takeSnapshot(String sImageFilename){


2 try {
3 TakesScreenshot oCamera;
4 File tmpFile, oImageFile;
5
6 oImageFile = new File(sImageFilename);
7
8 if( new File(sImageFilename).exists()){
9 throw new Exception("File already exists..");
10 }
11
12 oCamera= (TakesScreenshot) oDriver;
13
14 tmpFile = oCamera.getScreenshotAs(OutputType.FILE);
15
16 FileUtils.copyFile(tmpFile, oImageFile);
17
18 } catch (Exception e) {
19 e.printStackTrace();
20 }
21 }
Ques11: What is the basic syntax of xpath?
Basic syntax of relative xpath:

//Html Tagname[@attribute=’value’]

Ques12: What are different methods in xpath?


 Methods in xpath – contains(), starts-with(), following-sibling(), preceding-sibling().

Ques13: How to set the size of Browser window using selenium?


To set the size of a window of a Browser, method used is setSize(dim);

1 Dimension dim = new Dimension(500, 500);


2
3 Driver.manage().window().setSize(dim);
Ques14: How to maximise a window of a Browser using selenium?
1 //To maximise the browser
2 Driver.manage().window().maximize();
Ques15: How to get the color of a text on a page?

1 Driver.findElement(By.id("gh-ac")).getCssValue("color");
Ques16: Write a code to get the status of all the checkbox on a page?
 isSelected() – method is used to verify whether a checkbox is selected or not.

 It’s a boolean operation returns true if selected else false.

To verify all the checkbox, first, get all these checkboxes (by using findElements method) in a
list and then iterate this list to get the status of each checkbox.
1 public void getStatus(){
2 List<WebElement> list = Driver.findElements(By.xpath("//input[@type='checkbox']"));
3
4 for(WebElement temp : list){
5 System.out.println(temp.isSelected());
6}
7}
Ques17: Write a code to wait for a particular element to be visible on a page?
Explicit wait can be used to apply conditional wait (here, condition is visibility of an element on
a page)

public void waitTillElementVisible(){


1
2
WebDriverWait wait = new WebDriverWait(Driver, 90);
3
4
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//input[@type='
5
text']")));
6
7
}
Ques18: Write a code to wait for a text to change its colour?
 To apply a conditional wait on change of property(attribute) of a WebElement explicit wait can
be used.

Here, the attribute will be color.

public void waitTillColourChange(){


1
2
WebDriverWait wait = new WebDriverWait(Driver, 90);
3
4
wait.until(ExpectedConditions.attributeContains(By.linkText("advertisement"), "color",
5
"blue"));
6
7
}
Ques19: Write a code to wait for an alert to appear?
Waiting for an alert to appear on a page can be performed using explicit wait in Selenium
WebDriver.

1 public void waitForAnAlert(){


2
3 WebDriverWait wait = new WebDriverWait(Driver, 90);
4
5 wait.until(ExpectedConditions.alertIsPresent());
6
7}
Ques20: How to highlight a text or an image in selenium?
 To highlight a text or an image in Selenium, JavaScript can be injected.

1 public void highLightElement( ){


2 try {
3 WebElement oElement;
4 JavascriptExecutor oJsEngine;
5 String sJsCommand;
6 String sOldColour, sHighlightColour;
7
8 // Element to be highlighted
9 oElement = oDriver.findElement(By.xpath("//img[@id="add"]"));
10
11 // To get the previous color
12 sOldColour = oElement.getCssValue("backgroundColor");
13
14 oJsEngine = (JavascriptExecutor) oDriver;
15
16 //JavaScript command to change the color of an element to "yellow"
17 sJsCommand = String.format("arguments[0].style.backgroundColor=\"%s\";", "yellow");
18
19 //Executing JavaScript
20 oJsEngine.executeScript(sJsCommand, oElement);
21
22 // Wait for 5 seconds
23 Thread.sleep(5000);
24
25 //JavaScript command to change the color of an element to old color
26 sJsCommand = String.format("arguments[0].style.backgroundColor=\"%s\";", sOldColour);
27 oJsEngine.executeScript(sJsCommand, oElement);
28
29 } catch (Exception e) {
30 e.printStackTrace();
31 }
32 }

Ques1: What type of test cases have you automated?


 This is one the frequently asked questions if you are going for Interviews in Automation profile.
Regression and Functional tests are generally automated using any automation tools because
these type of tests are more stable and are less prone to changes.

Ques2: Have you worked on any framework? Describe the framework designed for your
project?
 Here, they want to test that whether you have actually worked on some framework or not.
The Answer to this question will be the framework which you have designed in your project. If
you have not worked on any framework. Please go through Selenium Tutorials to understand
some standard frameworks designed with Selenium Automation Tool.
Ques3: Explain the directory structure of your project?
 Here, the interviewer is expecting to know what kind of directory structure and class structure
you are following in your project. While explaining the answer to this question mention the
reason for having the directory structure you are following. Explain the different OPPs concept,
design pattern of classes, configuration files(if any) you are following in your project. If you
have not worked on any project, refer Hybrid Framework tutorial.

Ques4: How are you handling logging in your project?


 Logging is one of the essential features of any framework (project). For logging feature, there
are many third party tools available, one of them is log4j.jar file. It supports logs at various
levels – INFO, ERROR, DEBUG, EXCEPTION, etc.

Ques5: Which design patterns do you use?


 Most frequently used design patterns is POM (Page Object Model). In this type of design
pattern, one class file is created for each page of the application under test. In each class, first,
an object repository is created which will have all the WebElements of the respective page then
in the constructor, all these WebElements are initialised.
 After that, all the possible scenarios are created as methods in that class.

Ques6: Explain different exception in Selenium WebDriver?


To answer this question, please follow http://qatechhub.com/exception-handling-in-
selenium-webdriver/link.
Ques7: What approach will you follow to pick a date from Date Picker?
In most of the calendars which use date picker, month and year are generally in form of a
dropdown and the dates are in some table. Sometimes directly using xpath works and
sometimes you have to iterate over this table to select a date from the calendar.

Ques8: Have you integrated AutoIt tool in your project? Why is this tool used?
 AutoIT is a tool which is used to automate windows based application. In some scenarios like
downloading and uploading an image, there is a requirement to integrate something which can
interact with windows based application. For a better understanding follow this link –
http://qatechhub.com/integrating-autoit-tool-selenium-webdriver-upload-image-scenario/
Ques9: Have you heard of Continuous Integration, Continuous Deployment, and Continuous
Delivery? Which tool are you using for same?
 In continuous Integration, you have an application that on every code commit build is created
automatically, Unit tests are run, and application is deployed in the test-like environment.

 Continuous delivery is to do automation and Integration testing on every build to catch early
issues in the build.
 Continuous Deployment extends continuous delivery, after completion of automated testing
the code is deployed to the production.

 To learn it in detail, follow Continuous Delivery Article


Ques10: Write a code to read properties file using JAVA?

1 public static Properties getProperties(String sPropertiesFile){


2
3 try {
4 InputStream oFileReader;
5 Properties oProperty;
6
7 oFileReader = new FileInputStream(sPropertiesFile);
8 oProperty = new Properties();
9
10 oProperty.load(oFileReader);
11
12 return oProperty;
13
14
15 } catch (Exception e) {
16 e.printStackTrace();
17 // log.debug(e.getMessage());
18 return null;
19 }
Ques11: Have you used Excel Sheet in your project? What are you using for the same?
 POI apache jar file is used to read from an excel sheet. There are many other open sources and
paid tools available in the market like JXL, but POI from apache is most widely used.

 In Working with Excel article, I have covered all possible methods which you need to work with
an excel sheet.
Ques12: What are the challenges you faced while working with Selenium Automation Tool?
Synchronization:
 One of the biggest challenges faced while working with Selenium is Synchronization.

 An application under test has its own performance speed whereas the code written with
selenium has its own.

 Keeping both in sync is a big task.

 Selenium provides us some wait statements (Page load Timeout, Implicit wait, Explicit wait and
Fluent wait) which help us to improve synchronization.

 To learn Wait commands in detail follow: http://qatechhub.com/wait-selenium-webdriver/


Maintainability:
 Another challenge which generally an automation engineer face is Maintaining the code.

 If your application is more prone to change like html elements of the page changes, some new
components are being added and so on, this may affect your code especially locators.

 To get rid of these situations we have certain design patterns like POM (Page Object Model),
Page Factory.

Ques13: Have you ever worked with Sikuili? How does it work?
 Selenium can only automate the Web-based application, but sometimes you may require
interacting with Windows based applications as well. In a such a scenario we have to use some
third party tool like Sikuli, which is an open source tool. This tool works with image
recognization mechanism. It interacts with web elements through images.

Ques14: If you have ten elements on a page, and you have defined implicit wait as 10 sec.
How much minimum and maximum time will your script take to interact with all the Web
Elements?
 Minimum time will be almost zero seconds – if all these 10 web elements are already available,
then selenium will interact with all of them in no time.

 Maximum time will be (10 * 10 = 100) seconds – if each element appears on the page at the
10th second, then the max time will be 100 seconds (10 sec for each web element).

Ques15: What are different ways to find dynamic web elements (let us say these web
elements does not have any Id or classname) on a page?
 In such a scenario, we will take help of XPaths. There are many functions in Xpath like text(),
starts-with(), contains() and certain axes like parent, child, siblings, preceding etc. They can be
used

 Another way is to search for a web element which is stable and unique and then relative to that
certain axes like parent, child, siblings, preceding etc can be used to reach to a particular web
element.

Quest16: Can you perform Mobile Automation Testing using Selenium?


 Yes, there is a tool called Appium, a wrapper written over Selenium which can be used to
automate Mobile applications.

Ques17: What is Selenium Grid used for?


 Selenium Grid is the fourth component of Selenium suite and it is used for parallel testing or
distributive testing on the same system or on different systems.

 To learn Selenium Grid in detail follow – Working with Selenium Grid.


Ques18: What is POM (Page Object Model)? Is it some framework? What is it used for?
 POM stands for Page object model, it’s not a framework but a design pattern. To learn page
object model follow – POM in Selenium Frameworks.
Ques19: What benefit you get by using Page Factory?
 Page Factory is an enhanced way of achieving POM. To learn more about it follow – Enhanced
POM in Selenium.

Ques20: Have you heard of Robot class? Can you explain a scenario where robot class can be
used?
Another way to interact with windows based application is Robot class. You might have
performed some operation like right clicking on some web element which pops out a menu
(which is a windows based popup). To interact with such a menu bar, robot class can be used.

You might also like