Selenium Interview Questions and Answers – Part1

Home/Interview Questions/Selenium Interview Questions and Answers – Part1

Selenium Interview Questions and Answers – Part1

1. Difference between Absolute path & Relative path.

Absolute path will start with root path (/) and Relative path will from current path (//)

2. Tell me some TestNG Annotations.

@Test,@Parameters,@Listeners,@BeforeSuite,@AfterSuite,@BeforeTest,@AfterTest,
@DataProvider,@BeforeGroups,@AfterGroups,@BeforeClass,@AfterClass,
@BeforeMethod,@AfterMethod,@Factory

http://testng.org/doc/documentation-main.html#annotations

3. What are desiredcapabilities?

Desired Capabilities help to set properties for the Web Driver. A typical use case would be to
set the path for the Firefox Driver if your local installation doesn’t correspond to the default
settings.
Refer – https://code.google.com/p/selenium/wiki/DesiredCapabilities

4. Difference between Selenium RC and Selenium Web driver.

Selenium RC Selenium Web driver
Selenium RC’s architecture is way more complicated. Web Driver’s architecture is simpler than Selenium RC’s.
Selenium RC is slower since it uses a JavaScript program called Selenium Core. This Selenium Core is the one that directly controls the browser, not you. Web Driver is faster than Selenium RC since it speaks directly to the browser uses the browser’s own engine to control it.
Selenium Core, just like other JavaScript codes, can access disabled elements. Web Driver interacts with page elements in a more realistic way.
Selenium RC’s API is more matured but contains redundancies and often confusing commands. Web Driver’s API is simpler than Selenium RC’s. It does not contain redundant and confusing commands.
Selenium RC cannot support the headless HtmlUnit browser. It needs a real, visible browser to operate on. Web Driver can support the headless HtmlUnit browser.
Selenium RC Has Built-In Test Result Generator. Selenium RC automatically generates an HTML file of test results. Web Driver has no built-in command that automatically generates a Test Results File.
Selenium RC needs the help of the RC Server in order to do so. web Driver directly talks to the browser
Selenium RC can support new browsers It cannot readily support new browsers

5. Difference between Web driver listener and TestNG Listener.

TestNG and Web driver Listener have different interfaces to implement and call them. They both
modify respective behaviour. You can use Listeners in Annotation. Below 2 URL gives the detailed list of listener and their interfaces.
http://testng.org/doc/documentation-main.html#testng-listeners
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/events/
AbstractWebDriverEventListener.html

6. Describe your framework.

7. Which is the best way to locate an element?

Finding elements by ID is usually going to be the fastest option, because at its root, it eventually calls down to document.getElementById(), which is optimized by many browsers.

Finding elements by XPath is useful for finding elements using very complex selectors, and is the most flexible selection strategy, but it has the potential to be very slow, particularly in IE. In IE 6, 7, or 8, finding by XPath can be an order of magnitude slower than doing the same in Firefox. IE provides no native XPath-over-HTML solution, so the project must use a JavaScript XPath implementation, and the JavaScript engine in legacy versions of IE really is that much slower.

If you have a need to find an element using a complex selector, I usually recommend using CSS Selectors, if possible. It’s not quite as flexible as XPath, but will cover many of the same cases, without exhibiting the extreme performance penalty on IE that XPath can.

8. Why we refer Firefox driver to the web driver inheritance.

web Driver driver = new FireFoxDriver();
WebDriver is an interface which contain several abstract methods such as get(…), findElamentBy(…) etc.
We simply create reference of web Driver and we can assign objects (Firefox driver, CromeDriver, IEDriver, Andriod driver etc) to it.
Ex :
WebDriver driver = new FireFoxDriver();———–(1)
If we are using (1) we can do the same thing by using
FireFoxDriver driver = new FireFoxDriver();———(2)
We can use (1) and (2) for same purpose but if we want to switch to another browser in same program
then again we have to create the object of other class as for example
CromeDriver driver = new CromeDriver();.
creating object of several class is not good. So we create the reference of WebDriver and
we assign the objects of another class as for example
WebDriver driver; // it is created only one time in the program
driver = new FireFoxDriver();// any where in the program
driver = new CromeDriver(); // any where in the program

9. What are the features of TestNG?

TestNG is a testing framework designed to simplify a broad range of testing needs, from
unit testing (testing a class in isolation of the others) to integration testing (testing entire
systems made of several classes, several packages and even several external frameworks, such as application servers). You can use test suite,annotations, automatically generation of report and much more.

10. What is the difference between thread.Sleep() and selenium. Set Speed (“2000”)?

If the application is taking time to load the page then we use selenium.waitforpageload(” “). This command is doesn’t wait upto the given time whenever the page load is completed. If the application is taking time to refresh the page, then we use Thread. Sleep ( ).it is a standard wait it simply wait to the given time.

selenium.setSpeed
1. Takes a single argument in string format
Ex: selenium.setSpeed(“2000”) – will wait for 2 seconds
2. Runs each command in after setSpeed delay by the number of milliseconds mentioned in set Speed.

thread.sleep
1. Takes a single argument in integer format
ex: thread. Sleep(2000) – will wait for 2 seconds
2. Waits for only once at the command given at sleep.

11. In what situation selenium finding element get fails?

  • Element loading issue
  • Dynamic id of web element

12. What is the difference between “GET” and “NAVIGATE” to open a web page in selenium web driver?

Get method will get a page to load or get page source or get text that’s all whereas navigate
will guide through the history like refresh, back, forward.For example if we want to move
forward and do some functionality and back to the home page this can be achieved
through navigate() only. driver.get will wait till the whole page gets loaded and driver.navigate
will just redirect to that page and will not wait

13. Please tell me the difference b/w implicitly Wait and Explicit wait.

Implicit Wait sets internally a timeout that will be used for all consecutive Web Element searches. It will try lookup the element again and again for the specified amount of time before throwing a NoSuchElementException if the element could not have been found. It does only this and can’t be forced into anything else – it waits for elements to show up.

Explicit Wait or just Wait is a one-timer used by you for a particular search. It is more extendible in the means that you can set it up to wait for any condition you might like. Usually, you can use some of the prebuilt Expected Conditions to wait for elements to become clickable, visible, invisible, etc., or just write your own condition that suits your needs.

14. How we can retrieve the dynamically changing Ids? When we login Facebook the login label’s id
changes dynamically thus resulting in failure.

We have a hierarchy of locators and Facebook Is dynamic in nature,so we are not able to
use “id” for identification for after that we have remaining 7 locator’s for that :2. xpath ()..
3. name..4. css.. 5. link text.. 6. partiallinktext…7.tag name. so u can use any one for
identifying it. Most probably u can use “xpath” or “css-locator” and if there r tag then
link text or partial-link text. it depend on u . But we never use id’s in Ajax application
because it’s not possible.

15.What is the difference between driver.Close()
and driver.Quit () method?

Close() – It is used to close the browser or page currently which is having the focus.
Quit() – It is used to shut down the web driver instance or destroy the web driver instance
(Close all the windows)

16. How to scroll web element?–not browser—

FirefoxProfile profile=new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver=new FirefoxDriver(profile);
driver.navigate(“http://jqueryui.com/draggable/”);
Thread.sleep(6000L);
WebElement element=driver.findElement(By.xpath(“//div[@id=’draggable’]”));
Actions actn=new Actions(driver);
actn.dragAndDropBy(element, 50, 50).build().perform();
}

17. What is the basic use of Firefox profiles and how can we use them using selenium?

A profile in Firefox is a collection of bookmarks, browser settings, extensions, passwords,
and history; in short, all of your personal settings.
We use them to change user agent, changing default download directory, changing versions etc.
http://code.google.com/p/selenium/wiki/FirefoxDriver

18. Customize the name of file going to be downloaded?

You have to download AUTO IT.exe file and has to be install
and later you have create .au3 file (in this file you have to specify the commands in
VB script like your file name, where have to save, it will be easy may be 3 or 4 steps )
using AUTOIT…then right click the .au3 file you have to compile ….after that you will
get the .exe file with the name of .au3 file ..In eclipse you will give the code like this

19. How to handle internationalisation through web driver?

FirefoxProfile profile = new FirefoxProfile();
profile.set Preference(“intl.accept_languages”,”jp”);
Web driver driver = new FirefoxDriver(profile); driver.get(google.com) will open google in
Japanese Lang

20. How to overcome same origin policy through web driver?

Proxy server.
DesiredCapabilities capability=new DesiredCapabilities.firefox();
capability.setCapability(CapabilityType.PROXY,”your desire proxy”)
WebDriver driver=new FirefoxDriver(capability);

21. How to put text in Facebook search box using selenium web driver.

driver.findElement(By.xpath(“//div[contains(@class, ‘_586i’)]”)).sendKeys(“abc”);

22. Difference between flex and flash application.

In flash there is no code just based on creativity(design) we will complete the
work(time consuming process) whereas flex contain some small functions
which is integrated with mxml,PHP..(no tool is there to develop
flex we want to use the properties of css and style sheet)

23. What is Error Collector in TestNG? What is its use?

This class allows the collection of errors during the process of retrieving the
test data for the test method parameters
http://testngdatabind.sourceforge.net/apidocs/net/sf/testng/databinding/core/error/
ErrorCollector.html

24. How can we get the font size, font color, font type used for a particular text on a web
page using Selenium web driver?

driver.findelement(By.Xpath(“Xpath “).getcssvalue(“font-size);
driver.findelement(By.Xpath(“Xpath “).getcssvalue(“font-colour);
driver.findelement(By.Xpath(“Xpath “).getcssvalue(“font-type);
driver.findelement(By.Xpath(“Xpath “).getcssvalue(“background-colour);

25. How to run tests in multiple browser parallel?

Is there any other option other than selenium grid?

26. How to prepare Customized html Report using TestNG in hybrid framework.

Below are the 3 ways:

  • Junit: with the help of ANT.
  • TestNG: using inbuilt default.html to get the HTML report. Also XST reports from ANT,
    Selenium, TestNG combination.
  • Using our own customized reports using XSL jar for converting XML content to HTML.

27. “What’s the hierarchy of TestNG annotations? Explain me about annotation hierarchy & execution order?

1. org.testng.annotations.Parameters (implements java.lang.annotation.Annotation)
2. org.testng.annotations.Listeners (implements java.lang.annotation.Annotation)
3. org.testng.annotations.Test (implements java.lang.annotation.Annotation)
4. org.testng.annotations.AfterMethod (implements java.lang.annotation.Annotation)
5. org.testng.annotations.BeforeTest (implements java.lang.annotation.Annotation)
6. org.testng.annotations.BeforeMethod (implements java.lang.annotation.Annotation)
7. org.testng.annotations.Optional (implements java.lang.annotation.Annotation)
8. org.testng.annotations.AfterTest (implements java.lang.annotation.Annotation)
9. org.testng.annotations.Guice (implements java.lang.annotation.Annotation)
10. org.testng.annotations.BeforeGroups (implements java.lang.annotation.Annotation)
11. org.testng.annotations.ExpectedExceptions (implements java.lang.annotation.Annotation)
12. org.testng.annotations.TestInstance (implements java.lang.annotation.Annotation)
13. org.testng.annotations.NoInjection (implements java.lang.annotation.Annotation)
14. org.testng.annotations.AfterSuite (implements java.lang.annotation.Annotation)
15. org.testng.annotations.AfterClass (implements java.lang.annotation.Annotation)
16. org.testng.annotations.AfterGroups (implements java.lang.annotation.Annotation)
17. org.testng.annotations.DataProvider (implements java.lang.annotation.Annotation)
18. org.testng.annotations.BeforeSuite (implements java.lang.annotation.Annotation)
19. org.testng.annotations.BeforeClass (implements java.lang.annotation.Annotation)
20. org.testng.annotations.Factory (implements java.lang.annotation.Annotation)
21. org.testng.annotations.Configuration (implements java.lang.annotation.Annotation)
22. org.testng.annotations.ObjectFactory (implements java.lang.annotation.Annotation)
Useful link:
http://www.guru99.com/all-about-testng-and-selenium.html

28. How the TestNG interacts with Selenium Core? Explain me steps and internal architecture?

Refer: http://www.guru99.com/all-about-testng-and-selenium.html

29. Is it possible test web services using selenium?

Using Jmeter we can test how one website is talking to each other means time taken to
send data, feeds, messages from one website to other website. Jmeter does a nice job
of doubling for performance and api tests.

30. How to refresh a page without using context click?

  • Using sendKeys.Keys method
  • Using navigate.refresh() method
  • Using navigate.refresh() method
  • Using get() method
  • Using sendKeys() method

31. Can u send a code for printing in selenium?

There are two cases:

  • Case1. Any hyperlink/button on a web page, n clicking that link/button a print dialog box
    opens. (Performing an action on web page)
  • Case2.or do u want to open print dialog box within ur own script, not by performing any
    action on web page.

So If Case 1: just a call for WebElement.click() event will work to open it.
If Case 2: Call a Printer Job object (Use Awt API).
For code: Google it.
http://code.google.com/p/selenium/issues/detail?id=1815

32. How to find broken images in a page using Selenium Web driver.

  • Get xpath and then using tag name; get all the links in the page
  • Click on each and every link in the page
  • In the target page title, look for 404/500 error.

33. How to handle Ajax popup window?

By using getWindowHandles() and obj.switchTo.window(windowid) we can handle popups using
explicit wait and driver.swtchT0.window(“name”) commands for your requirements.

34. How to handle auto complete box in web driver?

By typing in box and capturing list elements

35. How to get the name of browser using Web Driver?

public class JsExecute
{
WebDriver driver;
JavascriptExecutor js;
@Before
public void setUp() throws Exception
{
driver=new FirefoxDriver();
driver.get(“http://www.google.com”);
}
@Test
public void test()
{
JavascriptExecutor js = (JavascriptExecutor) driver;
System.out.println(js.executeScript(“return navigator.appCodeName”));
}
}
OR
String s = (String) ((JavascriptExecutor) driver).executeScript(“return navigator.userAgent;”);
System.out.println(“Browser name : ” + s);

36. How to handle colors in web driver?

Use getCssValue(arg0) function to get the colors by sending ‘color’ string as an argument.
Example
String col = driver.findElement(By.id(locator)).getCssValue(“color”);

37. How to pass parameters from testng.xml into test case.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Parallelexecution {

private WebDriver driver = null;

@BeforeTest
@Parameters({ “BROWSER” })
public void setup(String BROWSER) {
System.out.println(“Browser: ” + BROWSER);

if (BROWSER.equals(“FF”)) {
System.out.println(“Firefox Browser is selected”);
driver = new FirefoxDriver();
} else if (BROWSER.equals(“IE”)) {
System.out.println(“Internet Explorer Browser is selected”);
driver = new InternetExplorerDriver();
} else if (BROWSER.equals(“HU”)) {
System.out.println(“Html Unit Browser is selected”);
driver = new HtmlUnitDriver();
} else if (BROWSER.equals(“CH”)) {
System.out.println(“Google chrome Browser is selected”);
driver = new ChromeDriver();
}
}

@Test
public void testParallel() throws Exception {
driver.get(“http://ruchi-myseleniumblog.blogspot.in/2013/12/100-selenium-interview-questions.html”);

}
}

above sample program BROWSER is a variable which value would be passed from TestNG.xml and TestNG.xml and it will run the test multiple time each time BROWSER value would be set with different browser name and test will check the BROWSER value and decide which browser test will run.

38. How to get text from captcha image??

driver.findElement(By.xpath(“.//*[@id=’SkipCaptcha’]”)).click();
String attr = ie.findElement(By.xpath(“.//*[@id=’SkipCaptcha’]”)).getAttribute(“value”);
System.out.println(“The value of the attribute ‘Name’ is ” + attr);

39. Is there a way to click hidden LINK in web driver?

String Block1 = driver.findElement(By.id(“element ID”));
JavascriptExecutor js1=(JavascriptExecutor)driver;
js1.executeScript(“$(“+Block1+”).css({‘display’:’block’});”);

40. What Class Extends Web Driver?

AndroidDriver, ChromeDriver, EventFiringWebDriver, FirefoxDriver, HtmlUnitDriver,
InternetExplorerDriver, IPhoneDriver, PhantomJSDriver, RemoteWebDriver, SafariDriver

41. What are the APIs that support Web Driver?

API are nothing but collection of all selenium commands for Locating UI Elements
(WebElements),Fetching a Page,User Input etc…

42. How to disable cookies in browser.

Using deleteAllVisibleCookies() in selenium

43. “We have heard about frameworks well it can be
broadly classified into these TDD, BDD and ATDD frameworks .What’s the Difference?”

For Details please see: http://assertselenium.com/2012/11/05/difference-between-tdd-bdd-atdd/

44. How to change user agent in Firefox by selenium web driver.

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference(“general.useragent.override”, “some UA string”);
Web Driver driver = new FirefoxDriver(profile);

45. What is Selenese?

Selenese is HTML language based command, which is used in Selenium IDE.

46. Differences between QTP and selenium.

  • Selenium generates a proxy while starting browser. QTP does not
  • QTP uses only Vb script. Selenium is available in many languages
  • QTP is paid and selenium is free.
  • You can run script from a particular line in QTP but in selenium, you cannot.
  • Selenium works on all browsers. QTP only works on IE, mozilla. Support from chrome has been introduced lately.
  • QTP is more organized and user friendly
  • Selenium requires more technical skills
  • QTP can also be used on desktop based applications but selenium cannot be used

47. What is the MOST challenging test problem in my career in Automation?

In my career

  • Changing XPATHS’ between testing server and production server-by keeping generic xpath
  • Keep separate property files for production and UAT
  • automating flash apps
  • Mobile Automation

48. “Suppose developer changed the existing image to new image with same xpath. Is test case pass or fail?”

Pass

49. How to handle network latency using selenium?

Using driver.manage.pageLoadingtime for network latency

50. How does u handle dynamic elements without using xpath (with example?)

By using classname or css.

About the Author:

Leave A Comment