Q105. How can we skip a test case conditionally in TestNG?
Answer:
To conditionally skip a test case, throw a SkipException within the test method. The test will be marked as skipped in the execution report.
Example:
java
@Test
public void testToSkip() {
throw new SkipException(“Skipping this test case”);
}
🔹 Q106. How can we ensure that a test method runs even if the tests or groups it depends on fail or get skipped?
Answer:
Use the alwaysRun attribute in the @Test annotation. This ensures the test runs even if its dependencies fail or get skipped.
Example:
java
@Test(dependsOnMethods = “previousTest”, alwaysRun = true)
public void independentTest() {
// This test will run regardless of the previous test’s status
}
🔹 Q107. Why and how will you use an Excel Sheet in your project?
Answer:
Excel sheets are used as data sources for tests, particularly in Data-Driven Testing. Data from the Excel file is used to test different scenarios with various input values. Excel files can be read and written using libraries like Apache POI or JXL.
🔹 Q108. How can you redirect browsing from a browser through some proxy in Selenium?
Answer:
You can configure a proxy in Selenium by using the Proxy class to direct browser traffic through a specified proxy server.
Example:
java
Proxy proxy = new Proxy();
proxy.setHttpProxy(“localhost:8888”);
ChromeOptions options = new ChromeOptions();
options.setProxy(proxy);
WebDriver driver = new ChromeDriver(options);
🔹 Q109. How to scroll down a page using JavaScript in Selenium?
Answer:
To scroll down a page using JavaScript, execute the window.scrollBy() function with Selenium’s JavascriptExecutor.
Example:
java
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“window.scrollBy(0, 500)”);
🔹 Q110. How to scroll down to a particular element?
Answer:
To scroll to a specific element, use JavaScript’s scrollIntoView() method:
Example:
java
WebElement element = driver.findElement(By.id(“elementId”));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“arguments[0].scrollIntoView(true);”, element);
.111. How to set the size of the browser window using Selenium?
Ans. To maximize the browser window, you can use:
java
driver.manage().window().maximize();
To resize the current window to a particular dimension, use:
java
driver.manage().window().setSize(new Dimension(width, height));
Q.112. Can we enter text without using sendKeys()?
Ans. Yes, we can use JavaScriptExecutor to enter text into a text field instead of sendKeys().
Q.113. Explain how you will login into any site if it is showing any authentication popup for username and password?
Ans. For handling authentication popups, you can use the following code to check for the alert and handle login:
java
Alert alert = driver.switchTo().alert();
alert.authenticateUsing(new UserAndPassword(“username”, “password”));
Q.114. Explain what is Group Test in TestNG?
Ans. In TestNG, test methods can be grouped under a category. When the group is executed, all the methods in that group will be executed. Use the groups attribute to assign test methods to a group. Example:
java
@Test(groups = {“groupName”})
Q.115. How to run failed test cases using TestNG in Selenium WebDriver?
Ans. You can use the “testng-failed.xml” file to rerun the failed test cases. TestNG will automatically generate this file after a test run.
Q.116. What is Stale Element Exception? How to handle it?
Ans. A StaleElementReferenceException occurs when the WebElement is no longer valid because the DOM has changed. To handle it, you need to find the element again after the DOM change.
Q.117. What are different XPath functions that you have used in your project?
Ans. Common XPath functions include:
- contains()
- starts-with()
- text()
- Using OR and AND operators.
Q.118. What will happen in the background when you execute new FirefoxDriver()?
Ans. When you execute new FirefoxDriver(), the Firefox binary is launched, and the Firefox browser window opens with default settings.
Q.119. What is the below statement means and Why?
java
WebDriver driver = new FirefoxDriver();
Ans. WebDriver is an interface, and FirefoxDriver() is a concrete class implementing the WebDriver interface, which is used to interact with the Firefox browser.
Q.120. How do you handle inner Frames and Adjacent Frames?
Ans. To interact with frames, you switch to the required frame using driver.switchTo().frame() and then switch back to the default content with driver.switchTo().defaultContent().
Q.121. How to click on an element that is not visible using Selenium WebDriver?
Ans. You can use JavascriptExecutor to click on an element that is not visible:
java
WebElement element = driver.findElement(By.id(“elementId”));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript(“arguments[0].click();”, element);
Q.122. Difference between verify and assert?
Ans.
- assert checks a condition and stops the test execution if the condition fails.
- verify checks a condition but does not stop the test execution if the condition fails.
Q.123. What is the use of @FindBy annotation?
Ans. @FindBy is used in the Page Object Model to locate elements in the PageFactory approach. It simplifies the process of finding elements.
Q.124. Do you use Thread.sleep()?
Ans. Rarely. It is generally not recommended to use Thread.sleep() as it can make tests slow and less stable. Instead, prefer dynamic waits like WebDriverWait.
Q.125. What are different pop-ups that you have handled in your projects?
Ans. Different pop-ups include:
- JavaScript pop-ups
- Alert pop-ups
- Browser pop-ups
- Native OS pop-ups (handled using tools like AutoIT, Sikuli)
HTTP Proxy authentication pop-ups