Selenium (WebDriver)Testing Framework — Questions and Answers
Question 1: Is the following statement true or false? <br> "The Data Driven Framework allows scripts to run in parallel."
- A) False
- B) True (Correct answer)
Correct answer: B) True
A Data-Driven Framework in Selenium separates test data from the test script, allowing the same script to be executed with different sets of data. This design inherently supports parallel execution, as multiple instances of the test script can run concurrently with distinct data inputs, significantly accelerating test cycles.
Question 2: Which of the WebDriver methods is used to count the number of elements?
- driver.findElement(By.id("search")).getCount()
- driver.findElements(By.id("search")).size() (Correct answer)
- driver.findElements(By.id("search")).length()
- driver.getCountOfElements()
Correct answer: driver.findElements(By.id("search")).size()
The `driver.findElements()` method in Selenium WebDriver returns a `List<WebElement>` containing all elements that match the specified locator. To count the total number of these matching elements, you then call the `.size()` method on the returned list.
Question 3: Which WebDriver command moves you forward in the browser's history by one page?
- Navigate.forward()
- navigate.forward()
- navigate().forward() (Correct answer)
- Navigate.forward
Correct answer: navigate().forward()
In Selenium WebDriver, the `navigate()` method provides access to browser history navigation. To move forward one page in the browser's history, you use the `forward()` method, typically invoked as `driver.navigate().forward()`.
Question 4: To locate an element using inner text, which of the following syntax would you use?
- css=tag:contains(“inner text”) (Correct answer)
- css=tag:value(“inner text”)
- css=tag:attributes(“inner text”)
- css=tag:class(“inner text”)
Correct answer: css=tag:contains(“inner text”)
While standard CSS selectors do not natively support `:contains()`, some Selenium implementations or common patterns extend this functionality to locate elements based on their inner text. The syntax `css=tag:contains("inner text")` is used in such contexts to target elements whose text content matches the specified string.
Question 5: Which of the following commands will assist you in determining the presence of a specific element?
- verifyTextPresent
- verifyTable
- verifyElementPresent (Correct answer)
- verifyTitlePresent
Correct answer: verifyElementPresent
The `verifyElementPresent` command in Selenium is specifically designed to check for the existence of a particular UI element on the current web page. It asserts the presence of an element, allowing the test to continue even if the element is not found, unlike an `assert` command which would halt execution.
Question 6: Which of the following Webdriver techniques navigates to a URL?
- get("url") (Correct answer)
- getUrl("url")
- goToUrl("url")
- navigate.to("url")
Correct answer: get("url")
The `driver.get("url")` method in Selenium WebDriver is the most common and direct way to navigate to a specified URL. It loads a new web page in the current browser window, effectively opening the given address for testing.
Question 7: Which of the following Webdriver statements returns the text of an html element?
- getElementText()
- getText() (Correct answer)
- getText(element)
- selectText()
Correct answer: getText()
The `getText()` method, when called on a `WebElement` object in Selenium WebDriver, retrieves the visible inner text of that element. This is a fundamental method used to extract text content from various HTML elements on a web page for verification or further processing.
Question 8: Using the POI excel API, which of the following code may be used to generate an excel automation object?
- File excelobj = new File (); (Correct answer)
- excelobj = new File ();
- File excelobj == new File ();
- File excelobj = File ();
Correct answer: File excelobj = new File ();
To work with Excel files using the Apache POI library in Java, you first need to create a `File` object to represent the Excel file on the file system. The statement `File excelobj = new File();` correctly initializes a new `File` object, which is the foundational step before reading from or writing to an Excel workbook.
Question 9: What is the purpose of the method getWindowHandle()?
- Only handles alert.
- Method will help to get the handle of parent window
- Method will get the handle of the page the Webdriver is currently controlling. (Correct answer)
- None of the above
Correct answer: Method will get the handle of the page the Webdriver is currently controlling.
The `getWindowHandle()` method in Selenium WebDriver returns a unique identifier (a string) for the current window or tab that the WebDriver instance is focused on. This handle is crucial for managing scenarios involving multiple browser windows or tabs, allowing you to switch control between them.
Question 10: Is the following statement true? <br> "The concept of function libraries cannot be implemented in the Data Driven Framework."
- A) No (Correct answer)
- B) Yes
Correct answer: A) No
The statement is false because function libraries are highly compatible with and often integral to Data-Driven Frameworks. They encapsulate reusable test logic, promoting code reusability and maintainability by separating common actions from the data-driven test scripts, making the framework more efficient and organized.
Question 11: Which of the following benefits of the Page Object Model is not true?
- POM allows to support execution in multiple browsers. (Correct answer)
- POM solves the problem of duplicate locators for same WebElement.
- POM provides clean separation between test code and page specific code
- POM ensures code re-usability.
Correct answer: POM allows to support execution in multiple browsers.
The Page Object Model (POM) is a design pattern that organizes web elements and their interactions into separate classes, improving code readability, reusability, and maintainability. While POM makes test code more adaptable for different browsers, the capability for multi-browser execution comes from WebDriver's inherent support, not POM specifically. The other options are direct benefits of POM.
Question 12: Which of the following is not a valid UI automation framework component?
- Object Repository
- Configuration File
- Data Source (Correct answer)
- XML Parser
Correct answer: Data Source
UI automation frameworks typically include components like an Object Repository for locators, Configuration Files for settings, and often an XML Parser for handling data or configurations. A 'Data Source' itself is not a structural component of the framework but rather an external entity (like an Excel file or database) that the framework interacts with to retrieve test data.
Question 13: For headless browser testing, which of the following WebDrivers is used?
- HeadLessDriver
- HtmlUnitWebDriver (Correct answer)
- FireFoxDriver
- InternetExplorerDriver
Correct answer: HtmlUnitWebDriver
HtmlUnitWebDriver is specifically designed for headless browser testing, meaning it operates without a graphical user interface. It simulates a browser environment entirely in memory, making tests significantly faster and suitable for execution on servers without a display. This allows for efficient and continuous integration testing without the overhead of rendering a visible browser window.
Question 14: In Selenium, which of the following types of text patterns is used?
- Globbing (Correct answer)
- Simulation
- Chronological Sequence
- Unicode
Correct answer: Globbing
In Selenium, particularly with Selenium IDE and some older components, 'globbing' patterns are used for matching text. Globbing employs wildcard characters like '*' (to match any sequence of characters) and '?' (to match any single character) to define flexible text patterns. This allows for more adaptable element location or assertion checks where the exact text might vary slightly.
Question 15: What is the Selenium phrase for an argument that begins with "//"?
- Link text
- CSS
- Element id
- Xpath (Correct answer)
Correct answer: Xpath
In Selenium, an argument that begins with '//' is recognized as an XPath expression. XPath is a powerful query language used to navigate and select nodes in an XML document, which includes HTML documents. The '//' prefix specifically indicates a search for elements anywhere in the document tree, regardless of their position relative to the root.
Question 16: What is the function of contextClick()?
- It is used to open popup menu.
- It is used to right click. (Correct answer)
- It is used to click the hidden element.
- It is used to left click.
Correct answer: It is used to right click.
The `contextClick()` method in Selenium's `Actions` class is used to simulate a right-click action on a web element. This functionality is crucial for testing interactions that are triggered by a right-click, such as opening context menus or revealing hidden options. It allows automation scripts to mimic complex user mouse interactions accurately.
Question 17: In WebDriver, what is the role of the actions class?
- It controls the actions of run-time exceptions
- It controls the actions of web elements.
- It controls the actions of mouse (Correct answer)
- None of the above
Correct answer: It controls the actions of mouse
The `Actions` class in Selenium WebDriver is primarily used to perform complex user interactions, particularly those involving the mouse and keyboard. It allows for building a sequence of advanced interactions like drag-and-drop, hovering over elements, multiple key presses, and right-clicks. While these actions often target web elements, the class's core purpose is to control and simulate intricate mouse and keyboard behaviors.
Question 18: Which of the following is JavaScript's default state in HTMLUnitDriver?
- Disabled (Correct answer)
- Enabled
- None of the above
Correct answer: Disabled
By default, JavaScript is disabled in HtmlUnitDriver. This design choice prioritizes speed and efficiency, as parsing and executing JavaScript can significantly slow down headless tests. To enable JavaScript execution for testing dynamic web pages that rely on client-side scripting, it must be explicitly configured in the HtmlUnitDriver's settings.
Question 19: In the case of WebDriver, which of the following is correct?
- WebDriver is an interface. (Correct answer)
- WebDriver is a class.
- None of the above
Correct answer: WebDriver is an interface.
In Selenium, `WebDriver` is an interface, not a concrete class. This architectural design allows for different browser-specific implementations (like `ChromeDriver`, `FirefoxDriver`, `InternetExplorerDriver`) to adhere to a common set of methods. By programming to the `WebDriver` interface, test scripts can be written generically and then executed across various browsers by simply instantiating the appropriate driver implementation.
Question 20: Which of the following statements about the keyword-driven framework is correct?
- It is best suited for white box testing.
- It is best suited for unit testing and works like a unit test framework.
- It is best suited for functional testing and is a functional automation testing framework. (Correct answer)
- None of the above
Correct answer: It is best suited for functional testing and is a functional automation testing framework.
The keyword-driven framework is highly suitable for functional testing because it abstracts test steps into keywords that represent user actions or business processes. This framework allows non-technical users to design and execute tests by combining these keywords, making it an effective approach for automating functional test cases. It focuses on verifying the application's behavior from an end-user perspective.
Question 21: Which of the WebDriver methods below allows you to switch between named windows?
- driver.MoveTo().window("windowName");
- driver.switchTo().window("windowName"); (Correct answer)
- driver.Activate().window("windowName");
- None of the above
Correct answer: driver.switchTo().window("windowName");
In Selenium WebDriver, the `driver.switchTo().window("windowName")` method is used to transfer control of the WebDriver instance to a different browser window or tab. This is essential when an application opens new windows (e.g., pop-ups or new tabs) and you need to interact with elements within them. The "windowName" typically refers to the window handle or name attribute of the target window.
Question 22: Which Selenium WebDriver component does not have any APIs exposed?
- Selenium WebDriver
- selenium RC
- Selenium Grid (Correct answer)
- None of the above
Correct answer: Selenium Grid
Selenium Grid itself does not expose a direct API for test script interaction in the same way WebDriver or RC do. Instead, Selenium Grid acts as a hub that routes WebDriver commands from your test script to remote browser instances. Your test script interacts with the `RemoteWebDriver` class, which then communicates with the Grid hub, making the Grid an infrastructure component rather than a direct API for scripting.
Question 23: Which browser is the only one that allows you to utilize Selenium IDE?
- Safari
- Google Chrome
- Firefox (Correct answer)
- Internet Explorer
Correct answer: Firefox
Historically, Selenium IDE was primarily developed as a Firefox add-on, making Firefox the only browser that natively supported it for a significant period. While modern versions of Selenium IDE are now available as browser extensions for Chrome and Edge, the original and most established version was exclusively for Firefox. This question likely refers to its historical context or a specific version.
Question 24: Which of the following is the preferred method for dealing with dynamic elements?
- By using relative xpath locators. (Correct answer)
- By using CSS locators.
- By using regular expressions.
- All of the above
Correct answer: By using relative xpath locators.
While CSS locators and regular expressions can be useful, relative XPath locators are often preferred for dealing with dynamic elements because they allow for more flexible and robust element identification. Relative XPaths can navigate the DOM based on relationships between elements, making them less susceptible to minor changes in attributes or structure compared to absolute paths or simple CSS selectors. They can locate elements based on partial text, attributes, or their position relative to other stable elements.
Question 25: In Internet Explorer, which of the following WebDriver classes is used to execute Selenium tests?
- InternetExplorerDriver (Correct answer)
- HtmlUnitDriver
- RemoteWebDriver
- None of the above
Correct answer: InternetExplorerDriver
To execute Selenium tests specifically on the Internet Explorer browser, you must instantiate the `InternetExplorerDriver` class. This class provides the necessary implementation of the `WebDriver` interface to interact with and control an Internet Explorer browser instance. Each major browser typically has its own dedicated WebDriver implementation for automation.
Is the following statement true or false?
"The Data Driven Framework allows scripts to run in parallel."