Selenium Cheatsheet
Selenium Cheatsheet
• Navigation Methods:
• Element Interaction:
• Mouse Actions:
• Keyboard Actions:
2. JavaScript Executor
• Executing JavaScript:
3. Waits
• Implicit Wait:
• Explicit Wait:
o WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)):
Creates an explicit wait object.
• Expected Conditions:
o titleIs(String title): Waits until the page title matches the specified title.
o titleContains(String title): Waits until the page title contains the specified text.
4. Handling Alerts
5. Taking Screenshots
7. Common Exceptions
8. Edge Cases
• Handling CAPTCHA (not automatable, but you can use delays or manual intervention).
This guide should help you prepare thoroughly for your Selenium Java interview. Good luck!
You can now copy this content into a Word document and save it as a .docx file. Let me know if
you need further assistance!
please add all the xpaths finding ways,just add xpaths ways and when should we use it as one
line discription
Here’s a detailed section on XPath and its various ways of locating elements, along with a one-
line description of when to use each approach. You can add this to your existing document.
Miscellaneous:
• To clear input values
webElement.clear();
XPath Locators
XPath (XML Path Language) is a powerful way to locate elements in Selenium. It allows you to
navigate through the HTML structure of a webpage. Below are the different ways to write XPath
expressions and when to use them.
1. Basic XPath
• Syntax: //tagname[@attribute='value']
• Example: //input[@id='username']
• When to Use: Use this when you know the exact attribute (e.g., id, name, class) of the
element.
• When to Use: Use this when the attribute value is dynamic or contains a partial static
value.
• Syntax: //tagname[text()='exact_text']
• Example: //button[text()='Submit']
• When to Use: Use this to locate elements based on their visible text.
• When to Use: Use this when the text is dynamic or contains a partial static value.
• When to Use: Use this when the attribute value starts with a specific string.
• When to Use: Use this when you need to combine multiple conditions to locate an
element.
7. XPath with or
• When to Use: Use this when either of the conditions can be true to locate an element.
• Syntax: //tagname[not(@attribute='value')]
• Example: //input[not(@type='hidden')]
• When to Use: Use this to exclude elements with a specific attribute value.
• Syntax: (//tagname[@attribute='value'])[last()]
• Example: (//div[@class='item'])[last()]
• When to Use: Use this to select the last matching element in a list.
• Syntax: (//tagname[@attribute='value'])[position()=n]
• Example: (//div[@class='item'])[position()=2]
• When to Use: Use this to select the nth matching element in a list.
• Syntax: //tagname//ancestor::ancestor_tagname
• Example: //input//ancestor::div
• Syntax: //tagname//descendant::descendant_tagname
• Example: //div//descendant::input
• Syntax: //tagname[@attribute='value']//following-sibling::sibling_tagname
• Example: //div[@class='item']//following-sibling::div
• When to Use: Use this to locate a sibling element that comes after the current element.
• Syntax: //tagname[@attribute='value']//preceding-sibling::sibling_tagname
• Example: //div[@class='item']//preceding-sibling::div
• When to Use: Use this to locate a sibling element that comes before the current
element.
• Syntax: //tagname[@attribute='value']//parent::parent_tagname
• Example: //input[@id='username']//parent::div
• When to Use: Use this to locate the parent element of a specific element.
• Syntax: //tagname[@attribute='value']//child::child_tagname
• Example: //div[@class='container']//child::input
• Syntax: //tagname[@attribute='value']//following::following_tagname
• Example: //div[@class='item']//following::input
• When to Use: Use this to locate any element that comes after the current element in the
DOM.
• Syntax: //tagname[@attribute='value']//preceding::preceding_tagname
• Example: //div[@class='item']//preceding::input
• When to Use: Use this to locate any element that comes before the current element in
the DOM.
• Syntax: //*[@attribute='value']
• Example: //*[@id='username']
• When to Use: Use this when you don’t know the tag name but know the attribute.
• When to Use: Use this when you need to combine multiple attributes to uniquely
identify an element.
• Syntax: //tagname[normalize-space(text())='value']
• Example: //button[normalize-space(text())='Submit']
• When to Use: Use this to ignore leading/trailing spaces in the text of an element.
• Syntax: //tagname/axis::target_tagname
• Example: //div/child::input
• When to Use: Use this for advanced navigation through the DOM structure.
• Syntax: //tagname/self::self_tagname
• Example: //div/self::div
• When to Use: Use this to select the current element itself.
• Syntax: //tagname/descendant-or-self::target_tagname
• Example: //div/descendant-or-self::input
• When to Use: Use this to select the current element and all its descendants.
• Syntax: //tagname/ancestor-or-self::target_tagname
• Example: //input/ancestor-or-self::div
• When to Use: Use this to select the current element and all its ancestors.
o You need to traverse the DOM hierarchy (e.g., parent, child, sibling).
o You need to locate elements based on complex conditions (e.g., contains, starts-
with).
CSS Selectors
CSS Selectors are used to locate elements in Selenium by matching elements based on their
attributes, hierarchy, or state. CSS Selectors are often faster and more efficient than XPath,
especially in modern browsers.
1. Basic CSS Selectors
Tag Name
• Syntax: tagname
• Example: input
ID
• Syntax: #id
• Example: #username
Class
• Syntax: .class
• Example: .form-control
Attribute
• Syntax: [attribute='value']
• Example: [type='text']
2. Combining Selectors
Tag + ID
• Syntax: tagname#id
• Example: input#username
• Description: Selects an element with the specified tag name and id.
Tag + Class
• Syntax: tagname.class
• Example: input.form-control
• Description: Selects an element with the specified tag name and class.
Tag + Attribute
• Syntax: tagname[attribute='value']
• Example: input[type='text']
• Description: Selects an element with the specified tag name and attribute.
3. Hierarchy-Based Selectors
Descendant
Child
• Description: Selects all input elements that are direct children of a div.
Adjacent Sibling
General Sibling
4. Attribute-Based Selectors
Attribute Contains
• Syntax: [attribute*='value']
• Example: [class*='form']
• Syntax: [attribute^='value']
• Example: [id^='user']
• Description: Selects elements whose attribute starts with the specified value.
• Syntax: [attribute$='value']
• Example: [id$='name']
• Description: Selects elements whose attribute ends with the specified value.
• Syntax: [attribute~='value']
• Example: [class~='form']
• Description: Selects elements whose attribute contains the specified word (space-
separated).
5. Pseudo-Classes
First Child
• Syntax: :first-child
• Example: div:first-child
Last Child
• Syntax: :last-child
• Example: div:last-child
• Syntax: :nth-child(n)
• Example: div:nth-child(2)
• Syntax: :nth-last-child(n)
• Example: div:nth-last-child(2)
• Description: Selects the nth child element of its parent, counting from the last.
Only Child
• Syntax: :only-child
• Example: div:only-child
First of Type
• Syntax: :first-of-type
• Example: div:first-of-type
• Description: Selects the first element of its type within its parent.
Last of Type
• Syntax: :last-of-type
• Example: div:last-of-type
• Description: Selects the last element of its type within its parent.
Nth of Type
• Syntax: :nth-of-type(n)
• Example: div:nth-of-type(2)
• Description: Selects the nth element of its type within its parent.
• Syntax: :nth-last-of-type(n)
• Example: div:nth-last-of-type(2)
• Description: Selects the nth element of its type within its parent, counting from the last.
6. Pseudo-Elements
Before
• Syntax: ::before
• Example: div::before
• Description: Selects the pseudo-element before the content of the selected element.
After
• Syntax: ::after
• Example: div::after
• Description: Selects the pseudo-element after the content of the selected element.
7. State-Based Selectors
Enabled
• Syntax: :enabled
• Example: input:enabled
Disabled
• Syntax: :disabled
• Example: input:disabled
Checked
• Syntax: :checked
• Example: input:checked
• Syntax: :focus
• Example: input:focus
AND Condition
• Syntax: selector1.selector2
• Example: input.form-control
OR Condition
o You need faster and more efficient element location (CSS is generally faster than
XPath).
o You need to work with modern browsers that optimize CSS Selectors.
java
Copy
// Locate by ID
// Locate by Class
// Locate by Attribute
// Locate by Hierarchy
// Locate by Pseudo-Class
The Robot Class is part of the java.awt package and is used to simulate keyboard and mouse
actions in Java applications. It is often used in Selenium for handling OS-level pop-ups, file
uploads, and other interactions that cannot be handled directly by Selenium.
1. Constructor
2. Keyboard Actions
keyPress(int keycode)
• Syntax: robot.keyPress(KeyEvent.VK_ENTER)
keyRelease(int keycode)
• Syntax: robot.keyRelease(KeyEvent.VK_ENTER)
• Syntax:
java
Copy
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
3. Mouse Actions
mouseMove(int x, int y)
• Description: Moves the mouse pointer to the specified screen coordinates (x, y).
mousePress(int buttons)
• Syntax: robot.mousePress(InputEvent.BUTTON1_DOWN_MASK)
• Description: Presses the mouse button (e.g., left click, right click).
mouseRelease(int buttons)
• Syntax: robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK)
mouseWheel(int wheelAmt)
• Syntax: robot.mouseWheel(3)
• Description: Scrolls the mouse wheel (positive for up, negative for down).
4. Delay
delay(int milliseconds)
• Syntax: robot.delay(2000)
5. Screen Capture
createScreenCapture(Rectangle screenRect)
• Syntax:
java
Copy
6. Pixel Color
getPixelColor(int x, int y)
• Description: Retrieves the color of the pixel at the specified screen coordinates (x, y).
7. Auto Delay
setAutoDelay(int milliseconds)
• Syntax: robot.setAutoDelay(500)
• Description: Sets the default delay (in milliseconds) after every action.
getAutoDelay()
setAutoWaitForIdle(boolean isOn)
• Syntax: robot.setAutoWaitForIdle(true)
isAutoWaitForIdle()
java
Copy
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
Real Time Selenium Interview Questions (Challenges etc.)
Q.1. Can you explain the practical distinctions between findElement and findElements in the
context of web automation, and share a specific scenario where you chose one over the
other?
Explanation:
In Selenium WebDriver, findElement is used to locate the first web element on a page using
various locators like id, name, classname, xpath, etc., returning a single WebElement. On the
other hand, findElements returns a list of web elements based on the provided locator strategy.
Both methods are affected by ImplicitlyWait to ensure the web elements are present before
interaction.
Reference Link:
https://www.selenium.dev/documentation/webdriver/elements/finders/
Scenario:
In my project, we had to click a unique button on a webpage. For this specific and singular
action, I opted for findElement because it returns a single WebElement, and we were confident
that only one matching element existed. Conversely, when dealing with multiple elements, like
extracting all links on a page, I used findElements to obtain a list of matching elements.
// Using findElement
singleElement.click();
// Using findElements
System.out.println(link.getAttribute("href"));
}
Exception:
I have faced the NoSuchElementException thrown by findElement when the element is not
present in the DOM after reaching the max time applied by the ImplicitWait. To address this, I
always double-check whether I’ve used the correct attributes present in the DOM and ensure
that the WebElement is not inside frames or ShadowRoot/ShadowDOM, as these can impact
the element’s visibility. findElements() does not throw an exception if no elements are found.
Instead, it returns an empty list. To check whether the list of elements returned
by findElements() is empty by checking its size before attempting to interact with.
Q.2. In a real-world project where you implemented the Page Object Model (POM), could you
describe the type of framework you built and elaborate on how this approach improved the
maintainability and efficiency of your test automation suite?
Explanation:
In my project, I established a Hybrid framework by incorporating the Page Object Model (POM)
as a key design pattern. POM is a testing paradigm that prioritizes code maintainability,
reusability, and readability by encapsulating web page interactions within dedicated classes
known as Page Objects.
Why POM?
• Readability: Test scripts gain clarity and expressiveness as high-level actions on a page
are abstracted into method calls within the corresponding Page Object.
Reference Link:
https://www.selenium.dev/documentation/test_practices/encouraged/page_object_models/
Use Case:
We worked with an e-commerce website with a multi-step checkout process involving distinct
pages such as Cart, Shipping, and Payment. Leveraging the POM, we crafted a Page Object for
each of these pages, encapsulating specific elements and actions. This modular approach
facilitated streamlined test case creation and maintenance.
POM:
Exception Handling:
While implementing, NullPointerException may arise if the driver instance is not correctly
initialized in the Page Object constructor. To mitigate this, I ensured proper initialization in my
base class to prevent such exceptions.
The adoption of the Page Object Model significantly bolstered the maintainability and efficiency
of our test automation suite. The structured separation of concerns within the Page Objects
facilitated easy updates and reduced redundancy, resulting in a more robust and scalable
framework.
Q.3. Can you share your experience dealing with dynamic elements in Selenium? Specifically,
how have you successfully managed and interacted with elements on a webpage that change
dynamically, and what strategies or techniques did you employ to ensure reliable test
automation in such scenarios?
Answer:
Dealing with dynamic elements in Selenium is a common task in my projects. This happens
when elements on a webpage change, often because of dynamic content loading.
Use Case:
For example, in a project where I worked on an application like Salesforce, we had a dropdown.
The options in this dropdown changed dynamically based on user interactions or other
asynchronous events.
• Challenge: Figuring out the right conditions to wait for the dynamic element to be ready
for interaction.
Explanation:
In the context of Selenium WebDriver, there are two primary types of waits: Implicit waits and
explicit waits. Implicit waits are designed as a capability in Selenium and are specifically tailored
to the methods findElement() and findElements(). When searching for an element in the
Document Object Model (DOM), if the element is not immediately found, the implicit wait
allows Selenium to continue searching for the element for a maximum time period.
If the element is not found even after this maximum time period, findElement() will throw
a NoSuchElementException, while findElements() returns an empty list. Implicit waits are
generally set once and applied globally. Implicit waits do not affect anything outside of element-
related operations and do not influence the behavior of alerts, frames, windows, or the
behavior of the WebElement itself. In cases where I need to handle these other aspects, I use
explicit waits.
Code Snippet:
driver.get("http://leaftaps.com/opentaps/control/mainrl");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
WebElement element =
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));
element.click();
}
UseCase:
Explicit waits are implemented through two different classes that adhere to the Wait
interface.The FluentWait, is an older implementation, while the subclass of Fluent Wait, known
as WebDriverWait, is the more commonly used form of explicit wait. WebDriverWait extends
FluentWait, and FluentWait implements the Wait interface. Explicit waits are designed to wait
for specific conditions, such as the appearance or disappearance of alerts, the readiness of
frames, or the count of windows. They are especially useful for conditions like element
clickability, dropdown selectability, or checking for the invisibility of elements.
Explicit waits are written very specifically, with conditions defined at each checkpoint in the
code. Unlike implicit waits, where you set the timeout globally, with explicit waits, you specify
the conditions wherever and whenever you need them. WebDriverWait includes a boolean
condition, known as the “until” condition, which you set using the ExpectedConditions class.
This class provides numerous expected conditions to choose from, such as checking for element
visibility, presence, the presence of specific text, or the count of windows. Additionally, common
conditions like stalenessOf(ele) are often used.
Exception:
WebDriverWait can throw a TimeoutException if the defined timeout period is exceeded. The
FluentWait internally utilizes something called a “polling period,” which is set to 500ms by
default. I can adjust this polling period to suit my needs. Essentially, FluentWait continuously
checks the specified conditions at regular intervals (in this case, every 500ms) until the
maximum timeout is reached. If the condition is not met by the time the maximum timeout is
reached, it triggers a TimeoutException.
Q.5. Can you elaborate on the practical distinctions between the close() and quit() methods in
Selenium WebDriver? How do you decide which one to use in different scenarios based on
your hands-on experience?
Explanation:
• close():
• Definition: The close() method closes the current browser window while keeping
the WebDriver instance running.
• Effect: In my project I use this method when I have multiple tabs/windows open,
closing only the current one without terminating the WebDriver session.
• Use Case: It is ideal for scenarios with multiple open tabs/windows, where I want
to close the active one.
• quit():
• Definition: The quit() method closes all browser windows, terminates the
WebDriver session, and releases resources.
• Effect: It is Suitable when I want to end the testing process, ensuring all browser
windows are closed and resources are freed.
• Use Case: It is appropriate for scenarios like where I’ve completed my test suite
or want to ensure a clean shutdown.
Use Case:
• I want to close only the current tab/window, continuing the test in others.
• Ensuring all browser windows are closed and WebDriver resources are released.
Decision Criteria:
• Dealing with a multi-tab scenario and closing only the current tab/window is
needed.
Practical Distinctions:
• close() Distinctions:
• Leaves the WebDriver instance running.
• quit() Distinctions:
• Ensuring a clean and complete closure of all browser windows and releasing
resources.
So In my project, quit() is commonly used at the end of a test script to ensure a clean shutdown,
while close() is used within a test script when managing multiple browser tabs/windows during
the test execution. This approach ensures efficient resource management and a smooth testing
process.
Q.6. How do you handle dropdowns in Selenium when they are implemented using both the
tag and without using the tag? Can you share your approach or code snippets that
demonstrate your experience in dealing with these different dropdown implementations?
Answer:
In Selenium WebDriver, dealing with dropdowns becomes a bit trickier when they deviate from
the standard <select> tag. The typical Select class in Selenium is tailored for <select> tags, and
when faced with a non-standard implementation, it tends to throw exceptions. My approach to
overcoming this challenge involves a step-by-step strategy.
Code Snippet:
public class DropdownWithSelectTag {
driver.get("http://leaftaps.com/opentaps/control/main");
dd.selectByIndex(2);
• Locate the parent element of the dropdown. This is the container that holds the
dropdown values.
• Use the appropriate Selenium code to click on the identified parent element. This
action is crucial for triggering the dropdown to display its options.
• After clicking on the parent element, observe how the dropdown values are
represented. They might be in <li> or <span> elements.
Code Snippet:
driver.get("https://login.salesforce.com/?locale=in");
driver.findElement(By.xpath("//span[text()='Create']")).click();
driver.findElement(By.xpath("//span[text()='Custom Tab']")).click();
• Construct XPath expressions using the ‘following’ axis. Starting from the clicked
parent element, dynamically locate and interact with the dropdown elements
based on their parent tag.
• In some projects, like the one I’ve worked on, a custom solution named
“Extended Select” has been implemented. When the element is not a
standard <select> tag, we locate the parent element by its locator and then
perform a click. The ‘following’ options in XPath help identify different tag names
based on their text content.
This approach provides a flexible and custom solution for handling non-standard dropdowns. It
allows for dynamic interaction with dropdown elements, ensuring that the automation script
adapts to different implementations encountered during testing.
Q.7. Can you tell me the distinctions between using getText() and getAttribute(), and explain
about the usecase of it?
Explanation:
•
• getText():
• getAttribute():
• Usage: It is more versatile, as it allows fetching not only text but also
other attributes like href, class, id, etc.
Use Case:
System.out.println(text);
• You want to fetch attribute values like href, class, id, etc.
Code Snippet:
Dynamic Content:
• In cases of dynamic content, text might change based on user interactions. Handling
such scenarios involves dynamic waits and robust synchronization strategies.
• These challenges are tackled by closely examining the HTML structure, adapting code to
handle dynamic content gracefully, and employing explicit waits to ensure that the
elements are ready for interaction before fetching text or attributes.
Q.8. Can you share a specific instance where you had to effectively manage alerts using
Selenium? How did you handle it, and what challenges did you encounter?
I was working on a web application that involved submitting a form, and depending on the data
entered, an alert could be triggered. The alert contained a message confirming the successful
submission or notifying about validation errors.
Code Snippet:
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
// Locate the element that triggers the alert (e.g., a Submit button)
button.click();
wait.until(ExpectedConditions.alertIsPresent());
if (alertMessage.contains("success")) {
alert.accept();
} else if (alertMessage.contains("error")) {
alert.dismiss();
}
// Continue with the rest of the test
Exceptions:
Challenges Encountered:
1. Timing Issues:
• Challenge: The alert message could indicate success or failure, and the script
needed to handle both scenarios appropriately.
Handling alerts effectively is crucial for scenarios where user interactions trigger pop-up alerts.
It requires synchronization to ensure that the alert is present before attempting to interact with
it, and the content of the alert message needs to be considered for proper test flow control.
Explanation:
Selenium is an open-source library that can be utilized in multiple programming languages, with
each language having its own corresponding bindings.
For example, when using the Java language binding, the following Java code:
This Java code internally calls the Selenium Java implementation, which, in turn, calls W3C
protocol-based requests or JSON wire protocol.
Selenium 4 completely uses the W3C protocol, whereas Selenium 3 and older versions use JSON
protocols.
Technically, it’s more of a REST API calls, with these calls reaching the ChromeDriver, which are
native drivers running on the local machines.
These API requests hit the local server, where the requests are received, processed, and
The browser can be either the ChromeDriver or Firefox Driver. After execution, the response is
captured and sent back from the ChromeDriver to the language binding as a response.
The Selenium Java bindings then capture this response, and based on the response information,
either as an exception or an outcome, it provides the corresponding result. This is the
fundamental architecture of Selenium’s navigation.
This architecture is true for other browsers like IE, Safari, Opera, or when writing code in Python
or C#.
Q.10. Can you describe how you use the Actions class in Selenium to achieve drag-and-drop
functionality in a web application and provide examples where you have successfully
implemented this technique to enhance test automation?
Explanation: The Actions class in Selenium is used for performing complex user interactions like
mouse and keyboard actions. For drag-and-drop functionality, we utilize
the dragAndDrop method of the Actions class. This allows us to click and hold an element, drag
it to another location, and then release the mouse.
Use Case: In my project while working with Salesforce application I have a web page with a
drag-and-drop feature for arranging items in a list. I want to automate the testing of this
functionality to ensure that the items are rearranged correctly after a drag-and-drop operation.
Code Snippet:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
driver.get("https://www.leafground.com/drag.xhtml;");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
builder.dragAndDrop(drag, drop).perform();
Exception Handling: One potential exception I have faced in using Actions class is
the `MoveTargetOutOfBoundsException`. This occurs if the target element is not visible or not in
the viewport, making it impossible for Selenium to perform the drag-and-drop action. I always
ensure the visibility of the target element which mitigates this issue.
Explanation:
getWindowHandle() and getWindowHandles() are two different methods in Selenium
WebDriver used for handling multiple browser windows or tabs during test automation.
UseCase:
In my project, I faced a scenario where I have a web application that opens a new browser
window or tab after clicking a link. I want to interact with elements in both the original window
and the newly opened one. In this case, I Used getWindowHandle() to get the handle of the
current window before clicking the link and Use getWindowHandles() after clicking the link to
get all window handle and finally iterate through the handles to switch to the new window and
perform actions.
Code Snippet:
driver.get("https://www.leafground.com/window.xhtml");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.findElement(By.xpath("//span[text()='Open']")).click();
System.out.println(pWindowHandle);
//to get all window handles
driver.switchTo().window(handles.get(1));
System.out.println(each);
System.out.println(cTitle);
driver.quit();
Exception:
One common exception associated with this code is NoSuchWindowException. This can occur if
the window handle obtained using getWindowHandle() is no longer valid when trying to switch
back.
• Challenge 1: Handling dynamic window handles, especially when windows are opened
based on user interactions.
Solution: Implementing a wait mechanism to ensure all windows are available before
getting handles.
• Challenge 2: Dealing with intermittent issues where the new window takes time to load.
Solution: Incorporating explicit waits for elements in the new window to ensure it’s fully
loaded before interacting with it.
Q.12.Describe the steps you would take to handle file uploads using Selenium. Can you share
a specific example where you successfully implemented file upload functionality?
Explanation:
Handling file uploads using Selenium involves interacting with file input elements on a web
page, providing the path to the file to be uploaded, and triggering the upload process.
UseCase:
In my project, I have a web application that allows to upload a profile picture. I want to
automate the process of selecting a file from my local machine and uploading it through the
web application.In this case,I identified the file input element on the web page and used
sendKeys() method to send the local file path to the input element and Submit the form or
trigger the upload process.
Code Snippet:
fileInput.sendKeys(filePath);
Exception:
One common exception associated with file uploads is ElementNotInteractableException. This
can occur if the file input element is not visible or not enabled.
• Challenge: Dealing with different file input mechanisms (e.g., input type=”file” or
custom upload controls).
• Solution: Identifying the appropriate mechanism used by the application and adapting
the automation script accordingly. In some cases, JavaScript may need to be executed to
trigger the file input.
• Example from Past Experience: In a previous project, we had a feature that allowed
users to upload documents. I used Selenium to automate this process. The challenge
was that the file input element was initially hidden and only became visible after clicking
a button. I addressed this by first clicking the button to reveal the file input, then sending
the file path using sendKeys(), and finally submitting the form. Explicit waits were crucial
to ensure the elements were ready for interaction.
Q.13. How have you utilized WebDriverWait, and in what scenarios did you find it particularly
valuable?
Explanation:
WebDriverWait is a part of the Selenium WebDriver framework that provides a mechanism to
wait for a certain condition to be met before proceeding with the execution. It is particularly
useful for handling particular behaviour of a WebElement in test automation.
UseCase:
In our project, we need to verify the confirmation message to appear after submitting the form.
While automating this scenario, the testcase fails consistently. To resolve the issue, we
WebDriverWait to handle the scenario. We create a WebDriverWait instance with a timeout of
10 seconds and use the until method to wait until the confirmation message becomes visible on
the page. Once the confirmation message is visible, we retrieve the text of the message using
confirmationMessage.getText() and store it in a variable for further verification.
Code Snippet:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(message)));
Exception:
It’s important to note that WebDriverWait can throw a TimeoutException if the defined timeout
period is exceeded. The FluentWait internally utilizes something called a “polling period,” which
is set to 500ms by default. You can adjust this polling period to suit your needs. Essentially,
FluentWait continuously checks the specified conditions at regular intervals (in this case, every
500ms) until the maximum timeout is reached. If the condition is not met by the time the
maximum timeout is reached, it triggers a TimeoutException.
Q.14. How have you utilized JUnit and TestNG in your testing practices, and what specific
differences or advantages have you observed between the two in your projects?
Explanation:
JUnit and TestNG are both testing frameworks used in Java for writing and running test cases.
They provide annotations and assertions to structure and validate test code.
UseCase:
In my project, we were tasked with creating and executing test cases for a web application.
Use JUnit or TestNG to annotate test methods. Organize test suites and test dependencies using
the respective framework’s features. Leverage assertions to validate expected outcomes.
Code Snippet:
import org.testng.Assert;
@Test
// Test logic
// Assertion
Differences/Advantages:
• TestNG Advantages:
Flexible Annotations: TestNG provides a broader set of annotations (e.g., @BeforeSuite,
@AfterSuite, @Parameters) allowing more fine-grained control over test execution.
Dependency Management: TestNG supports test dependencies, ensuring that certain
tests run only if the dependent tests pass.
Parallel Execution: TestNG allows parallel execution of test methods, which can
significantly reduce test execution time.
• JUnit Advantages:
Simplicity: JUnit is simpler and has a shallower learning curve, making it a good choice
for smaller projects or teams new to testing frameworks.
Ecosystem: JUnit has a large ecosystem and is widely adopted, making it a common
choice for various Java projects.
• Example from Past Experience: In a project where parallel test execution was critical
due to time constraints, I opted for TestNG because of its built-in support for parallelism.
This significantly reduced the overall test execution time, allowing us to run a large suite
of tests in a much shorter period. Additionally, TestNG’s annotation flexibility and
dependency management proved beneficial in orchestrating complex test suites with
varying requirements.
Q.15. Can you share your approach for capturing screenshots using Selenium? Walk me
through the steps you would take to capture and save screenshots during automated testing.
Explanation:
Capturing screenshots using Selenium involves utilizing the TakesScreenshot interface to
capture the current state of the WebDriver instance and then saving it as an image file. This is a
valuable practice in automated testing for visual verification and debugging.
UseCase:
In a scenario where we are running a suite of automated tests for a web application, we want
to capture screenshots at critical points, such as after a successful login or when a specific
element fails to load. At this point, we call the captureScreenshot method at key points in my
test scripts to capture screenshots and provide the WebDriver instance and a file path where
the screenshot should be saved.
Code Snippet:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.OutputType;:
import org.apache.commons.io.FileUtils;
import java.io.File;
try {
FileUtils.copyFile(sourceFile, destn);
} catch (Exception e) {
e.printStackTrace();
Exception: One common exception I have faced with this code is IOException from
FileUtils.copyFile() method. This can occur if there is an issue with the file operation, such as
insufficient permissions or a non-existent destination path.
Q.16. Can you share your experience in dealing with iframes and nested iframes while using
Selenium for web automation? Specifically, how have you successfully navigated and
interacted with iframes?
Explanation:
iFrames (Inline Frames) are HTML elements that allow embedding one HTML document inside
another. When dealing with iframes in Selenium, we need to switch the focus of the WebDriver
to the iframe to interact with elements inside it.
UseCase:
In my project, we have a web page that contains an iframe, and within that iframe, there is
another nested iframe. You need to interact with elements inside both iframes. In this case, First
we identify the iframes using methods like driver.switchTo().frame() by using the overloading
methods like index, id, WebElement to switch focus and Perform actions on elements within the
iframes and Switch back to the main window using defaultContent() when done.
• Example: Iframes that load dynamically after some user action. Strategy: Use explicit
waits (WebDriverWait) to ensure the iframe is present before switching to it. For
example:
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("dynamicFrame"));
Code Snippet:
driver.get("https://www.leafground.com/frame.xhtml");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.switchTo().frame(0);
driver.findElement(By.id("Click")).click();
driver.switchTo().defaultContent();
Q.17. Can you share examples of how you’ve effectively utilized TestNG annotations in your
testing projects? Highlight specific scenarios where the use of annotations contributed to the
success of your test automation efforts.
Explanation:
TestNG annotations play a crucial role in structuring and controlling the flow of test execution in
Selenium automation projects. Annotations such
as @Test, @BeforeMethod, @AfterMethod, @BeforeClass, and @AfterClass allow for better
organization and control over test cases.
Use Case:
In my project, we use a framework called TestNG to run our tests. Before we start running tests,
we need to set up some things. We use annotations to tell the framework what to do at
different steps. BeforeSuite: It gets things ready before anything else. we start our report
here. BeforeTest: Once we start, we need to decide what tests we want to run. Here, we set up
details like which Excel sheet to use BeforeClass: Here we focus on specific groups of tests, kind
of like organizing them. We set up details for each group of tests. DataProvider: This provides
data for our tests. BeforeMethod: Before each test, we need to launch the browser and apply
waits. This annotation helps set up things before each test. @Test:This is where the actual
testing happens and we use this annotation to mark them. AfterMethod: After each test, we
need to clean up. This annotation helps with those post-test activities. AfterSuite: Once all tests
are done, We close our report.
So, these annotations are like instructions for our testing framework, helping it to organize,
prepare, and clean up as it runs our tests. They make the whole testing process smoother and
more organized.
Code Snippet:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
WebDriver driver;
@BeforeClass
@Test(priority = 1)
@Test(priority = 2)
@AfterClass
driver.quit();
}
Solution: By effectively using TestNG annotations, I was able to create a well-structured test
suite with proper setup, execution order, and teardown, resulting in more maintainable and
scalable test automation projects.
Q.18. Explain the most occurring exception in your project. How will you handle
StaleElementReferenceException?
Explanation:
A “StaleElement” reference occurs when we find an element using driver.findElement(), and
initially, the element is present, returning a WebElement to the Selenium WebDriver Java
implementation. However, when we attempt to interact with the WebElement using methods
like click(), sendKeys(), getAttribute(), or getText(), the element suddenly disappears from the
Document Object Model (DOM) or the page gets reloaded.
This discrepancy between the presence of the element when initially located and its
disappearance when interacted within a fraction of a millisecond or microsecond results in
a StaleElementReferenceException.
One approach is to wait for the page or grid to reload using Thread.sleep() before attempting to
interact with the element using findElement followed by the desired action.
Another way to handle the issue is, if it occurs frequently and the time required for the reload is
uncertain, a retry logic can be implemented using a try-catch block to handle
the StaleElementReferenceException. In the catch block, you can retry finding the same element
repeatedly with a maximum number of retries or a maximum timeout when encountering
the StaleElementReferenceException.
Another option is to use WebDriverWait with ExpectedConditions. There is a
specific ExpectedCondition called stalenessOf(), which allows you to wait for the staleness of an
element to disappear before starting interaction with the WebElement.
Q.19. Can you demonstrate how to construct an XPath to select an element based on its
relationship with a sibling or ancestor element?
Explanation:
XPath locator is used when there is no stable attributes are found for an element in the DOM.
XPath provides several axes that allow you to navigate through the hierarchy of elements in an
HTML document. The following-sibling, preceding-sibling, and ancestor axes are particularly
useful for selecting elements based on their relationship with sibling or ancestor elements.
Syntax:
Syntax:
<div>
<span>Element 1</span>
<span>Element 2</span>
<span>Element 3</span>
</div>
To select the <span> element that follows the first <span> element, I will use the following
XPath:
//span[text() = 'Element 1']/following-sibling::span[1]
To select the element that precedes the third element, you will use the following XPath:
<div id="grandparent">
<div id="parent">
<div id="child">
<span>Target Element</span>
</div>
</div>
</div>
Q.20. How would you set up a Selenium test to run in a headless mode for a specific browser
version? Can you talk through the process and the considerations you would have?
Explanation:
“Headless mode” means that the browser will not be visible during execution; it will run in the
background and won’t be visible to the user. However, Selenium’s getScreenShotAs() method
can capture snapshots, and mouse and keyboard actions will be executed just like in a regular,
visible browser.
Here’s how to implement headless mode using ChromeOptions: You should open the
ChromeOptions class and call the addArguments() method, passing the argument "--
headless" to run the browser in the background as expected. When running in headless mode,
you may encounter occasional failures, particularly with click actions or mouse-related actions.
Therefore, it’s advisable to implement a retry mechanisms in your code. In some cases, we have
observed that headless mode can lead to failures, so it’s not recommended for extensive use
unless you have a specific need for it.
1. Basic Questions
1. What is TestNG?
• Answer: TestNG (Test Next Generation) is a testing framework inspired by JUnit and
NUnit but introduces new functionalities like annotations, grouping, prioritization, and
parallel execution, making it more powerful and flexible.
• Answer:
• Answer:
o @BeforeClass: Runs before the first test method in the current class.
java
Copy
@Test
• Answer:
o @BeforeMethod: Runs before each test method. Used for setup (e.g., initializing
objects, opening browsers).
o @AfterMethod: Runs after each test method. Used for cleanup (e.g., closing
browsers, releasing resources).
2. Intermediate Questions
java
Copy
@Test(priority = 1)
• Answer: Grouping allows you to categorize test methods into groups and execute them
selectively.
java
Copy
@Test(groups = "smoke")
xml
Copy
<groups>
<run>
<include name="smoke"/>
</run>
</groups>
</test>
Run HTML
9. What is the use of dependsOnMethods in TestNG?
• Answer: It ensures that a test method runs only if the specified methods have passed.
java
Copy
@Test
System.out.println("Login test.");
@Test(dependsOnMethods = "login")
java
Copy
@Test(enabled = false)
java
Copy
@DataProvider(name = "testData")
public Object[][] provideData() {
{"username1", "password1"},
{"username2", "password2"}
};
@Test(dataProvider = "testData")
xml
Copy
<classes>
<class name="com.example.TestClass"/>
</classes>
</test>
</suite>
Run HTML
3. Advanced Questions
java
Copy
@Factory
java
Copy
@Test(expectedExceptions = ArithmeticException.class)
int result = 10 / 0;
• Answer: @Listeners is used to add custom listeners to modify TestNG's behavior (e.g.,
logging, reporting).
java
Copy
@Listeners(CustomListener.class)
@Test
System.out.println("Test method.");
}
}
• Answer: TestNG automatically generates HTML reports in the test-output folder. You can
also use custom listeners like ITestListener or IReporter to create custom reports.
• Answer: @Parameters is used to pass parameters from the testng.xml file to test
methods.
java
Copy
@Test
@Parameters({"username", "password"})
java
Copy
@Override
retryCount++;
return true;
}
return false;
@Test(retryAnalyzer = RetryAnalyzer.class)
• Answer:
o @BeforeTest: Runs before any test method in the <test> tag in the testng.xml file.
o @BeforeClass: Runs before the first test method in the current class.
java
Copy
@Test(timeOut = 5000)
4. Scenario-Based Questions
• Answer:
o Use @BeforeMethod to open the browser and navigate to the login page.
23. How would you run tests in parallel for a large test suite?
• Answer: Use the parallel attribute in the testng.xml file with methods, classes,
or tests as the value.
• Answer: Implement the IReporter interface and override the generateReport method to
create a custom report.
Briefly explain what the project is about (e.g., "This is an automation framework for testing a
web application using Selenium, TestNG, and Maven").
Mention the key technologies and tools used (e.g., Selenium, TestNG, Maven, Jenkins, Git, etc.).
Use a top-down approach to explain the structure. Start with the root folder and then dive into
subfolders.
Highlight the purpose of each folder and how they contribute to the overall framework.
Here’s an example of a typical Selenium TestNG project structure and how to explain it:
Root Folder
pages: Contains Page Object Model (POM) classes for each page of the application.
base: Contains the base class for initializing WebDriver and common methods.
suites: Contains TestNG XML files for test suites (e.g., smoke-test.xml, regression-test.xml).
testdata: Contains test data files (e.g., Excel, JSON, or CSV files).
pom.xml: The Maven configuration file for managing dependencies and plugins.
Example:
LoginPage.java contains methods for interacting with the login page (e.g., enterUsername(),
clickLoginButton()).
Base Class
Example: BaseTest.java contains @BeforeMethod and @AfterMethod annotations for setup and
teardown.
Utilities
Purpose: To provide reusable methods (e.g., reading configuration files, handling WebDriver).
Test Classes
Purpose: To define test suites and control test execution (e.g., grouping, parallel execution).
Reports
Maintainability: Configuration and test data are stored separately for easy updates.
6. Example Explanation
src/main/java:
pages: Contains POM classes like LoginPage.java and HomePage.java for interacting with
application pages.
utils: Contains utility classes like WebDriverManager.java for managing WebDriver and
ConfigReader.java for reading configuration files.
base: Contains the BaseTest.java class for initializing WebDriver and common setup/teardown
methods.
src/test/java:
tests: Contains test classes like LoginTest.java and HomeTest.java with TestNG annotations.
suites: Contains TestNG XML files like smoke-test.xml for defining test suites.
src/test/resources:
config: Contains config.properties for environment-specific data.
This structure ensures modularity, scalability, and maintainability. For example, adding a new
test case is as simple as creating a new method in the appropriate test class and updating the
TestNG XML file if needed.
"I store test data in Excel or JSON files under src/test/resources/testdata and use data providers
to read it."
1. Initializing a Repository
git init
• Example:
bash
Copy
git init
2. Cloning a Repository
• Example:
bash
Copy
git status
• Purpose: Shows the status of the working directory (e.g., untracked files, changes to be
committed).
• Example:
bash
Copy
git status
• Example:
bash
Copy
git add .
• Purpose: Adds all changes in the working directory to the staging area.
• Example:
bash
Copy
git add .
5. Committing Changes
• Purpose: Commits the changes in the staging area with a descriptive message.
• Example:
bash
Copy
git log
• Example:
bash
Copy
git log
• Example:
bash
Copy
• Example:
bash
Copy
8. Switching Branches
• Example:
bash
Copy
• Example:
bash
Copy
9. Merging Branches
• Example:
bash
Copy
• Example:
bash
Copy
• Example:
bash
Copy
• Purpose: Fetches and merges changes from the remote repository to the local branch.
• Example:
bash
Copy
• Example:
bash
Copy
git remote -v
• Purpose: Displays the list of remote repositories.
• Example:
bash
Copy
git remote -v
git stash
• Example:
bash
Copy
git stash
• Example:
bash
Copy
• Example:
bash
Copy
git reset index.html
• Purpose: Discards all changes in the working directory and staging area.
• Example:
bash
Copy
• Purpose: Creates a new commit that undoes the changes of a previous commit.
• Example:
bash
Copy
git diff
• Purpose: Shows the differences between the working directory and the staging area.
• Example:
bash
Copy
git diff
• Example:
bash
Copy
19. Tagging
• Example:
bash
Copy
• Example:
bash
Copy
• Example:
bash
Copy
• Example:
bash
Copy
• Example:
bash
Copy
.gitignore
• Example:
bash
Copy
*.log
node_modules/
23. Rebasing
• Example:
bash
Copy
24. Cherry-Picking
• Example:
bash
Copy
git branch -r
• Example:
bash
Copy
git branch -r
• Example:
bash
Copy
• Example:
bash
Copy
• Example:
bash
Copy
• Purpose: Modifies the most recent commit (e.g., to change the commit message or add
files).
• Example:
bash
Copy
git clean -f
• Example:
bash
Copy
git clean -f