Selenium/WebDriver Interview questions

By | January 20, 2019

Selenium interview questions basics-1



  1. What is automation testing?
    Automation testing is the process of enabling a tool/machine to perform manual tasks without manual intervention. The expected results of a test are fed into the tool to be compared with the actual output of the test.
  2. What are the components of Selenium suite ?
    The Selenium suite package consists of the following components:
    Selenium IDE (Integrated Development Environment)
    Selenium RC
    Selenium Webdriver
    Selenium Grid
  3. What are the testing types that can be supported by Selenium?
    Selenium supports the following types of testing:
    Functional Testing
    Regression Testing
  4. What is the difference between Selenium 1.0 and Selenium 2.0.
    Selenium 1.0 consists of only Selenium RC where as Selenium RC and WebDriver combined together to form Selenium 2.0.
  5. What is the difference between single slash (/) and double slash (//) in Xpath?
    Single slash (/) is used to create Xpath with absolute path. In this case, the Xpath starts selection from the document root node.
    Double slash (//) is used to create Xpath with relative path. In this case, the Xpath starts selection from anywhere within the document.
  6. What are the different types of locators in Selenium?
    ID, ClassName, Name, TagName, LinkText, PartialLinkText, XPath, CSS Selector, by java script.
  7. What is the difference between Selenium 2.0 and Selenium 3.0?
    The biggest difference between selenium 3.0 and selenium 2.x is that now, in Selenium 3, all the major browser vendors will ship their own WebDriver implementations (Apple, Google, Microsoft, and Mozilla) instead of being developed by Selenium project (Selenium 2.x) and because they know their browsers better than anyone else

    Support for Firefox browser provided by Mozilla’s gecko driver.

    No need to maximize the browser using driver.manage().window().maximize(); By default the window will be maximized.

    Selenium 3 has bug fixes from selenium 2 also it is more mobile automation focused.One of the major differences introduced in Selenium 3 was the introduction of the Appium project. The mobile-testing features that were part of Selenium 2 are now moved into a separate project named Appium.

    More than 9+ versions of IE are supported in Selenium 3.0.

    Apple has come up with its own SafariDriver to run your tests in Safari on Mac

    Selenium WebDriver has become the W3C Standard. The W3C standard will encourage compatibility across different software implementations of WebDriver API.

    Minimum Java version required is now 8+

    Selenium 3.0 doesn’t support Firefox 47.0.0 version at all. Firefox is fully supported at version 47.0.1 or more.

  8. Does Selenium have limitations? If so, what are some of these limitations?
    Yes, It has. Below are some:

    Since it is free, there is no ready vendor support. However, it has a large helping community of developers.
    It cannot be used to test desktop applications
    User is expected to have prior programming knowledge.
    Selenium cannot be used to test Captcha and Bar code readers.
    Test reports can only be generated using third party tools such as JUnit and TestNG.

  9. What is a XPath?
    XPath is used to locate a web element based on its XML path. It can be used to locate HTML elements.
    Using it one can traverse through given page and identify elements.
  10. What is the purpose of Selenium Grid?
    It allows us to run multiple functional tests in parallel, on multiple machines, against different browsers, in a heterogeneous environment.
  11. What is Selenese?
    Selenese is name given for set of commands used for in Selenium IDE.
  12. In what cases Selenium IDE is apt?
    Selenium IDE is record and playback featured tool. It is optimum when test case is very simple does not require any programming modifications.
    Beginners can go for it.
  13. What is the difference between driver.close and driver.quit command?
    driver.close() will close the current instance of browser. driver.quit() closes all the browser instances opened so far as a part of program execution.
  14. How does Verify and Assert differs?
    Verify : Verify checks whether given conditions are met or not. If condition is not met it will not abort or terminate the
    execution. It will continue with the next step.
    Assert : Assert checks whether given conditions are met or not. If condition is not met it will abort/terminate the
    execution. It will mark it as a failure, will not continue further.
  15. How to launch browser in selenium?
    By invoking respective driver as:

    WebDriver oDriver = new InternetExplorerDriver();
    WebDriver oDriver = new FirefoxDriver();
    WebDriver oDriver = new ChromeDriver();
    
  16. What are different types of waits in selenium? Why do you require waits?
    When tests are running, the application may not always respond with the same speed.
    To handle these anticipated timing problems by synchronizing your test to ensure that
    Selenium WebDriver waits until your application is ready before performing the next step.
    There are two types of waits in selenium.
    1. Implicit wait:
    In implicit wait, program will wait for an element for given amount of time. It will search the element by polling(again and again) with in the time limit.
    If element is not found, it will throw No such Element Found exception. It is applicable to entire script.
    Need not to re write.
    Syntax: driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    2. Explicit wait:
    This is for specific instance or condition or particular search. Can be called/used in that instance only.
    we have to provide ExpectedCondtions for wait to stop.
    Examples: visibilityOfAllElements(),alertIsPresent(),elementToBeClickable(),visibilityOf() etc..
    3. Fluent wait:
    It is also kind of explicit wait. Unlike explicit wait, FluentWait uses a maximum timeout value and polling
    frequency.For example, if we set the maximum timeout value as 20 seconds and polling
    frequency as 2 seconds, WebDriver will check for an element every 2 seconds until the maximum value. In addition to this, we can confgure FluentWait to ignore specifc types of exceptions while waiting for the condition.
    The FluentWait class is helpful in automating AJAX applications or scenarios where element load time varies often.
  17. What are some exceptions in Selenium?
    1.NoSuchElementException: When an element is not found on the web page.
    2.NoAlertPresentException: Happens when you switch to no presented alert.
    3.NoSuchWindowException: If a browser window is not present when code is expecting a new window.
    4.StaleElementException: If an element is either deleted or no longer attached to the DOM.
    5.NoSuchFrameException: Takes place if frame target to be switch does not exist.
    6.SessionNotFoundException: The WebDriver is performing the action right after you quit the browser.
    7.UnhandledAlertException: It happens when there is an alert, but WebDriver is unable to perform Alert operation.
    8.WebDriverException: This takes place when the WebDriver is performing the action right after you close the browse.
    9.ElementNotVisibleException: Thrown when an element is present on the DOM, but it is not visible, and so is not able to be interacted with.
    Most commonly encountered when trying to click or read text of an element that is hidden from view.
    10.ElementNotSelectableException: Thrown when trying to select an unselectable element. For example, selecting a ‘script’ element.
  18. How to clear and enter text in field?
    Commands to do this are:

      element.clear();
    element.sendkeys("learnmodeon");

  19. How to get text entered in field?
    Commands to do this are:

      String text = element.gettext();

  20. What is the difference between findElements() and findElement() ?
    Let us consider a case where a webpage contain multiple elements having same id.
    findElement() : Returns the instance of first element.
    findElements() : Returns list containing all elements.

    If there is no element at all.
    findElement() – will Throw NoSuchElementException.
    findElements()- Returns empty list(i.e., length of list is 0)

  21. How to select and element from dropdown?
    Selecting/Deselecting an element from dropdown can be achieved in 3 ways. It will work only when html code of page contains select tag.
    Example: Drop down of colors containing black and white.
    a.Selection/deselection by visible text:
    We can select an option by its visible text.
    color.selectByVisibleText(“Black”); # Selects black color.

    b.Selection/deselection by value:
    We can select an option by its value attribute.
    color.selectByValue(“bl”);

    b.Selection/deselection by index:
    We can select an option by its index.
    color.selectByIndex(0); # Selects black color.

  22. How to get list of available options in a dropdown?
    Using getOptions() method we can retrieve all the options present.

     
       Select make = new Select(driver.findElement(By.name("make")));
       for (WebElement option : make.getOptions()) {
         availOptions.add(option.getText());
        }
    
  23. How to get list of selected options in a dropdown?
    Using getAllSelectedOptions() method we can retrieve all the options selected.

     
       Select make = new Select(driver.findElement(By.name("make")));
       for (WebElement option : make.getAllSelectedOptions()) {
         selectedOptions.add(option.getText());
        }
    
  24. Explain different navigation commands on browser?
    driver.navigate().back(); – Takes back the user to the previous webpage.
    driver.navigate().forward(); – It will navigate to the next web page depends on browser’s history.
    driver.navigate().refresh() – Refresh the current web page.
    driver.navigate().to(“url”) – Navigate to the specified URL.
  25. How to handle Pop-ups/alerts?
    Using alert interface alerts can be handled. Methods available are:
    To switch to alert-
    Syntax : Alert alert = driver.switchTo().alert();

    After switching to alert-
    To dismiss the alert:
    Syntax : alert.dismiss();
    To accept the alert:
    Syntax : alert.accept();
    To get the content/message of the alert:
    Syntax : alert.gettext();

  26. How to handle frames?
    Check for iframe tag in html code.
    Mutiple ways we can switch to frame and execute tests-
    a. Switch to frame by id
    Syntax : driver.switchTo().frame(“id”);
    b. Switch to frame by name
    Syntax : driver.switchTo().frame(“Name”);
    c. switch to frame by tagName
    Syntax : driver.switchTo().frame(driver.findElements(By.tagName(“iframe”).get(0));
  27. How to perform submit operation?
    It can be done in two ways.
    1. By clicking on the respective button.
           ele.click();
    2. By using submit method on the respective button.
           ele.submit();
  28. How to select or deselect radio button ?
    We can select and deselect the radio buttons using the click()
    method of the WebElement class.
  29. How to verify whether radio button is selected or not?
    We can check whether a radio button is selected or deselected using the isSelected() method. This method return a boolean value.
  30. How to verify text color in selenium webdriver?
    Using CSS attribute value we can validate color. You could use .getCssValue to get the value of color.
  31. How to verify whether radio button is selected or not?
    We can check whether a radio button is selected or deselected using the isSelected() method. This method return a boolean value.
  32. How to handle mutiple browser windows in Selenium?
    Using driver.switchTo().window(windowName/id);, we can traverse between windows.
    driver.getWindowHandles() will return set of addresses of all the windows that were initiated by the WebDriver.

     
        String handle= driver.getWindowHandle();
        for (String handle : driver.getWindowHandles())
         {
          driver.switchTo().window(handle);
        }
  33. What is the difference between getWindowHandles() and getWindowHandle() ?
    driver.getWindowHandles() will return list of names of all the windows that were initiated by the WebDriver.
    driver.getWindowHandle() will return only the current window address.
  34. How to switch to parent or default window?
    Syntax is:
    driver.switchTo().defaultContent();
  35. How will you handle Tables present on webpage?
    There are no specific commands provided to handle Tables. By using table tags of execution steps must be written.
    Let us consider table names “items” :

     
      WebElement simpleTable = driver.findElement(By.id("items"));
    Number of rows of this table can be retrieved through as
      List WebElement rows = simpleTable.findElements(By.tagName("tr"));
  36. How to check state of the elements on any webpage using selenium?
    Webdriver provides the following methods to check the state of an element:

    Method Purpose
    isEnabled() This method checks if an element is enabled. It returns true if
    enabled, else false if disabled.
    isSelected() This method checks if an element is selected (radio button, checkbox,
    and so on). It returns true if selected, else false if deselected.
    isDisplayed() This method checks if an element is displayed.
  37. How to perform double click?
    We will use the Actions class to perform double-click operations.

     
    Actions builder = new Actions(driver);
    builder.doubleClick(message).perform();
  38. How to do drag and drop operation?
    We will use the Actions class to perform drag and drop operation.

     
        WebElement source = driver.findElement(By.id("draggable"));
        WebElement target = driver.findElement(By.id("droppable"));
        Actions builder = new Actions(driver);
        builder.dragAndDrop(source, target) .perform();
  39. How to click on an item in hover menu?
    We will use the Actions class to achieve this.

      
        Actions builder = new Actions(driver);
        builder.moveToElement(driver.findElement(By.id("id of the dropdown"))).perform();
         WebElement optionToSelect=driver.findElement(By.id("option"));
         optionToSelect.click()
      
  40. How to get title of browser?
    It can be performed in two ways.
    1. Using getTitle() method

        String tilte = driver.getTitle();

    2. Using javascript executor

      
        JavascriptExecutor js = (JavascriptExecutor) driver;
         String title = (String) js.executeScript("return document.title");  
  41. How to check whether page is fully loaded or not without using wait for an element?
    Using JavascriptExecutor we can achieve this: When page is fully loaded document ready state will be in completed status.

     
        JavascriptExecutor jexec = (JavascriptExecutor)driver;
        jexec.executeScript("return document.readyState").toString().equals("complete");
  42. How to capture screenshot in Selenium?
    Using TakesScreenshot interface capturing a screenshot is possible(TakesScreenshot relies on the browser API to capture
    the screenshots. The HtmlUnit driver does not support the TakesScreenshot interface):
    Syntax:

     
        File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("target/main_page.png"))
    
  43. How to capture cookies of a browser?
    Websites use cookies to store user preferences, login information, and various other details of the client. Selenium has provision for this.
    Syntax:
    storeCookie = driver.manage().getCookies();
  44. How to bring an element into visibility/view if it is at the bottom of the page?
    Using JavascriptExecutor, we can bring the element into view port.
    Syntax:
    WebElement element = driver.findElement(By.id(“id_of_element”));
    ((JavascriptExecutor) driver).executeScript(“arguments[0].scrollIntoView(true);”, element);
  45. How to switch to another tab in same window?
    Selenium does not provide ready made method for this. Need to follow same steps as of manual.
    This can be done by entering ctrl+tab using keys actions.
    Syntax:
    From left to right tabs:

      
             Actions act= new Actions(driver);
             act.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();
    

    From right to left tabs:

      
          Actions act= new Actions(driver);
          act.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();
    
  46. How to handle Captcha in your application to Automate?
    CAPTCHA means “Completely Automated Public Turing test to tell Computers and Humans Apart”.
    By definition captcha is used to determine whether the user is a human or not. So we cannot automate it.
    Instead we can let our automation continue by tweaking it to accept same value every time.
    Provide a workaround to get captcha value beforehand.
    Develop a way,where a random text gets stored in the alt attribute of the captcha image which can then be obtained and passed into the captcha text box using available locators


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.