Web & API Automation Testing Interview Prep_ PART-3
Web & API Automation Testing Interview Prep_ PART-3
1. NoSuchElementException
Occurs when Selenium WebDriver tries to identify an element on the web page using a certain locator but
doesn't find it.
Solution: Use explicit waits (WebDriverWait) to wait for the element to be present or visible on the page before
attempting any operation on it.
2. StaleElementReferenceException
Solution: Re-find the element or use a retry mechanism that can handle this exception and attempt to locate
the element again.
3. TimeoutException
Thrown by WebDriverWait when an expected condition is not met within the time frame defined.
Solution: Adjust timeout settings or review if the expected conditions are correct. Ensure that the webpage is
loaded completely or necessary conditions like visibility or element presence are met before proceeding.
Occurs when an element is present on the DOM, but it is not in a state that can be interacted with.
Solution: Ensure the element is visible and enabled before taking action. Utilize explicit waits to handle such
scenarios.
.until(ExpectedConditions.elementToBeClickable(By.id("myElement")));
element.click();
5. ElementClickInterceptedException
Thrown when another element receives the click instead of the intended target element.
Solution: Scroll the element into view, wait until the overlay element disappears or changes position, or use
JavaScript to perform the click.
js.executeScript("arguments[0].scrollIntoView(true);", element);
js.executeScript("arguments[0].click();", element);
6. AssertionError
Occurs in BDD frameworks when the expected state of the application in tests does not match the actual state.
Solution: Review the expectations set in your BDD scenarios and assert statements. Make sure the logic and
conditions are correctly implemented in your step definitions.
It’s wise to have a global exception handling strategy, such as implementing a try-catch block at a strategic
point in your code, logging the details of exceptions, and possibly taking screenshots on failures.
try {
} catch (NoSuchElementException e) {
} catch (StaleElementReferenceException e) {
// Retry or handle
} catch (Exception e) {
use of text() in xpath: The text() function in XPath is used to select the text content of a node. It can be very
useful when you need to find an element by its text content:
This statement is using the concept of polymorphism where 'driver' is a reference of WebDriver interface and
'new ChromeDriver()' is an object of ChromeDriver class. This way, the same reference can be used to
instantiate other browser classes too like FirefoxDriver, thus making the code more flexible and browser-
independent. This abstraction also makes it easy to switch between different WebDriver implementations.
// Set up WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/your/chromedriver.exe");
try {
driver.get("https://example.com");
// Create an AShot instance with viewport shooting strategy to capture full page
} finally {
driver.quit();
}
9.RestAssured Questions like what is use of RequestSpecification and
ResponseSpecification
RequestSpecification
• This is used to build the request, such as setting up headers, cookies, body, parameters, timeouts,
etc., and can be reused across different test scenarios and methods.
ResponseSpecification
• This is used to abstract the validation logic of the response, such as checking status codes, response
time, content type, or even body content. This sustains all assertions collected in one place which can
be reused.
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
driver.findElement(By.id("buttonID")). click();
• Using JavaScriptExecutor
public abstract boolean isAt(); // Abstract method to be implemented by each page class
b.LoginPage uses encapsulation to hide the complexity of the UI interactions within its
methods like login, so a user of LoginPage just calls login without worrying about the internal
details like element selectors or waiting logic.
3. Polymorphism:
a. isAt() method in BasePage is abstract, letting us define specific conditions that determine
whether the page is loaded when overriding this method in derived pages, like LoginPage.
• Test Class (Showcasing usage of Inherited Methods)
13. Write Java Code to Print all the Permutations of a given string.
14. Bug Life cycle. Bug Severity,Priority
• Severity indicates the impact of a bug on the system (Critical, Major, Minor, Trivial).
• Priority indicates the urgency of fixing a bug (High, Medium, Low).
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
try {
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("Connected to the database successfully!");
// Perform database operations
} catch (SQLException e) {
e.printStackTrace();
}
}
}
16. How will you do parallel testing using testNg , use of IRetryAnalyzer
Parallel execution in TestNG can be done by setting the parallel attribute in the testng.xml file, and
IRetryAnalyzer is used for re-running failed tests.
TestNG.xml for Parallel Execution:
IRetryAnalyzer Implementation:
Singleton pattern ensures that a class has only one instance and provides a global point of access to it.
A common use of Singleton in Selenium frameworks is to manage the WebDriver instance. This ensures that
the same WebDriver instance is reused across all test scenarios, which can improve the execution speed and
resource efficiency.
2. Configuration Data Singleton
Singleton can also be used to manage configuration settings, which are typically loaded once and read many
times during the test execution.
3. Logging Singleton
In a test framework, it's common to have a centralized logging mechanism that various parts of the code can
use to log messages.
19. Git Commands: fetch, pull, clone
1. Fetch: git fetch origin - Fetch branches and tags from the remote repository to the local repo.
2. Pull: git pull origin master - Fetch from and integrate with another repository or a local branch.
3. Clone: git clone <repo_url> - Clone a repository into a new directory.
20. Find the Kth largest and Kth smallest element in array.
Solution :1
Solution:2
21. Linux commands.