30 Capgemini Selenium Interview Questions and Answers for Experienced

1. What is Selenium and why is it widely used for automation testing?

Selenium is an open-source automated testing framework used for testing web applications. It allows testers to automate web browsers' actions, interact with web elements, and perform various tests on web applications. Selenium is widely used for automation testing because of its flexibility, support for multiple programming languages, and its ability to run tests on different browsers and operating systems.

2. What are the different components of Selenium?

Selenium consists of the following components:

- Selenium IDE: A record and playback tool for creating test scripts.

- Selenium WebDriver: A powerful API for creating test scripts in various programming languages.

- Selenium Grid: Allows running tests on multiple machines and browsers in parallel.

3. What are the different locators used in Selenium WebDriver?

Selenium WebDriver provides various locators to identify web elements on a web page. Some commonly used locators are:

- By.id()
- By.name()
- By.className()
- By.tagName()
- By.linkText()
- By.partialLinkText()
- By.xpath()
- By.cssSelector()

4. How do you handle dynamic elements in Selenium?

Dynamic elements are elements whose attributes or properties change dynamically with each page load or interaction. To handle dynamic elements in Selenium, you can use strategies like XPath or CSS selectors that do not depend on specific attributes. You can also use explicit waits to wait for the element to become visible or interactable before performing any action.

5. What is the Page Object Model (POM), and how does it improve test maintenance?

The Page Object Model (POM) is a design pattern used in Selenium testing. It involves creating separate classes for each web page of the application, and each class encapsulates the web elements and actions on that page. POM improves test maintenance by centralizing element locators in one place. If there are any changes in the UI, you only need to update the corresponding page class, making the changes easier to manage.

6. How do you handle pop-ups and alerts in Selenium?

To handle pop-ups and alerts in Selenium, you can use the Alert interface's methods like accept(), dismiss(), and getText(). The accept() method is used to accept an alert, dismiss() is used to dismiss an alert, and getText() is used to get the text of the alert message.

7. How do you perform mouse hover actions in Selenium?

To perform mouse hover actions in Selenium, you can use the Actions class. The Actions class provides methods like moveToElement() to move the mouse pointer to a specific element on the page, and perform() to perform the action.

Actions actions = new Actions(driver);
actions.moveToElement(element).perform();

8. What are the different types of waits available in Selenium WebDriver?

Selenium WebDriver provides three types of waits:

- Implicit Wait: Waits for a certain amount of time before throwing a NoSuchElementException.
- Explicit Wait: Waits for a certain condition to occur before proceeding with the execution.
- Fluent Wait: Waits for a specific condition with a defined polling time and timeout.

9. How do you handle multiple windows in Selenium?

To handle multiple windows in Selenium, you can use the getWindowHandles() method to get all the window handles and then switch between windows using the switchTo() method.

// Get the main window handle
String mainWindowHandle = driver.getWindowHandle();

// Get all window handles
Set<String> allWindowHandles = driver.getWindowHandles();

// Switch to a specific window
for (String handle : allWindowHandles) {
    if (!handle.equals(mainWindowHandle)) {
        driver.switchTo().window(handle);
        break;
    }
}

10. How do you run tests in parallel using Selenium Grid?

Selenium Grid allows you to run tests in parallel on multiple machines and browsers. To run tests in parallel, you need to set up a Selenium Grid hub and register multiple nodes (machines) with different browser configurations. Then, you can specify the desired capabilities in your test scripts to run the tests on specific nodes and browsers.

11. How do you capture screenshots in Selenium?

To capture screenshots in Selenium, you can use the TakesScreenshot interface and the getScreenshotAs() method. This method returns a File object that can be saved to a specific location on your machine.

// Capture screenshot
TakesScreenshot screenshot = (TakesScreenshot) driver;
File srcFile = screenshot.getScreenshotAs(OutputType.FILE);

// Save screenshot to a specific location
FileUtils.copyFile(srcFile, new File("path/to/destination/screenshot.png"));

12. What are the different types of testing you can perform using Selenium?

Selenium can be used for various types of testing, such as:

- Functional Testing: Verifying that the application functions correctly according to the requirements.
- Regression Testing: Ensuring that new changes or updates do not affect existing functionalities.
- Performance Testing: Testing the application's performance under different conditions.
- Compatibility Testing: Verifying that the application works on different browsers and devices.

13. How do you handle SSL certificate errors in Selenium?

To handle SSL certificate errors in Selenium, you can use the DesiredCapabilities class to set the "acceptSslCerts" capability to true. This will allow the WebDriver to accept SSL certificates.

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("acceptSslCerts", true);
WebDriver driver = new ChromeDriver(capabilities);

14. How do you handle dropdowns in Selenium?

To handle dropdowns in Selenium, you can use the Select class. The Select class provides methods like selectByVisibleText(), selectByValue(), and selectByIndex() to select options from a dropdown.

WebElement dropdown = driver.findElement(By.id("dropdown"));
Select select = new Select(dropdown);

// Select by visible text
select.selectByVisibleText("Option 1");

// Select by value
select.selectByValue("option1");

// Select by index
select.selectByIndex(0);

15. How do you handle frames and iframes in Selenium?

To handle frames and iframes in Selenium, you can use the switchTo() method to switch the driver's focus to the frame/iframe.

// Switch to frame by index
driver.switchTo().frame(0);

// Switch back to the main content
driver.switchTo().defaultContent();

16. What is TestNG, and how is it beneficial in Selenium?

TestNG is a testing framework for Java that makes it easier to organize and run test cases. It provides features like grouping test cases, parallel test execution, test dependency management, and easy configuration of pre and post-test actions. TestNG is beneficial in Selenium as it enhances test management and reporting capabilities, allowing efficient test execution and analysis.

17. How do you handle cookies in Selenium?

To handle cookies in Selenium, you can use the addCookie() method to add a new cookie, getCookieNamed() method to retrieve a specific cookie, and deleteCookie() method to delete a cookie.

// Add a new cookie
Cookie cookie = new Cookie("cookieName", "cookieValue");
driver.manage().addCookie(cookie);

// Get a specific cookie
Cookie cookie = driver.manage().getCookieNamed("cookieName");

// Delete a cookie
driver.manage().deleteCookie(cookie);

18. How do you handle keyboard actions in Selenium?

To handle keyboard actions in Selenium, you can use the Keys class. The Keys class provides constants for different keyboard keys that can be used with the sendKeys() method.

WebElement searchInput = driver.findElement(By.id("searchInput"));
searchInput.sendKeys("Selenium WebDriver");
searchInput.sendKeys(Keys.ENTER);

19. What is the purpose of the "Headless" browser in Selenium?

A "Headless" browser is a browser without a graphical user interface. It runs in the background and performs all the browser actions without displaying the actual browser window. The purpose of a Headless browser in Selenium is to speed up test execution, as it consumes fewer resources compared to a regular browser with a GUI.

20. How do you handle exceptions in Selenium?

In Selenium, exceptions are handled using try-catch blocks. You can catch specific exceptions like NoSuchElementException, TimeoutException, etc., and perform error handling accordingly to handle test failures gracefully.

try {
    WebElement element = driver.findElement(By.id("elementId"));
    element.click();
} catch (NoSuchElementException e) {
    System.out.println("Element not found: " + e.getMessage());
}

21. How do you handle test failures and retries in TestNG?

In TestNG, you can handle test failures and retries using the "retryAnalyzer" attribute. By implementing a custom RetryAnalyzer class, you can configure the number of retries for failed test cases.

// Custom RetryAnalyzer class
public class CustomRetryAnalyzer implements IRetryAnalyzer {
    private int retryCount = 0;
    private int maxRetryCount = 3;

    @Override
    public boolean retry(ITestResult result) {
        if (retryCount < maxRetryCount) {
            retryCount++;
            return true;
        }
        return false;
    }
}

// Test method with retryAnalyzer attribute
@Test(retryAnalyzer = CustomRetryAnalyzer.class)
public void testMethod() {
    // Test code
}

22. What are DesiredCapabilities in Selenium WebDriver?

DesiredCapabilities is a class in Selenium WebDriver that represents the desired capabilities of a WebDriver instance. It is used to configure the WebDriver session with browser-specific settings like browser name, version, platform, and other properties.

// Setting up DesiredCapabilities for Chrome
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("platform", "WINDOWS");
capabilities.setCapability("version", "latest");

WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

23. How do you handle AJAX calls in Selenium?

To handle AJAX calls in Selenium, you can use explicit waits with ExpectedConditions. The ExpectedConditions class provides methods like presenceOfElementLocated(), visibilityOfElementLocated(), etc., which wait for specific conditions to be satisfied before proceeding with the test execution.

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")));

24. How do you perform database testing using Selenium?

Selenium is primarily used for front-end testing, and it does not have direct support for database testing. However, you can integrate Selenium with database testing tools or use data-driven testing techniques to test database interactions indirectly.

25. How do you handle SSL certificates in Selenium?

To handle SSL certificates in Selenium, you can set the "acceptInsecureCerts" capability to true for the WebDriver instance. This allows the WebDriver to bypass SSL certificate errors and continue with the test execution.

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("acceptInsecureCerts", true);
WebDriver driver = new ChromeDriver(capabilities);

26. How do you handle JavaScript in Selenium?

Selenium has built-in support for executing JavaScript code using the JavascriptExecutor interface. You can use the executeScript() method to execute JavaScript code and interact with web elements that are not directly accessible with regular WebDriver methods.

JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("document.getElementById('elementId').click();");

27. How do you handle browser navigation in Selenium?

Selenium provides methods like navigate().to(), navigate().back(), navigate().forward(), and navigate().refresh() to handle browser navigation.

// Navigate to a URL
driver.navigate().to("https://www.example.com");

// Go back to the previous page
driver.navigate().back();

// Go forward to the next page
driver.navigate().forward();

// Refresh the current page
driver.navigate().refresh();

28. How do you handle windows authentication pop-ups in Selenium?

Windows authentication pop-ups cannot be handled directly by Selenium. However, you can use third-party tools like AutoIT or Robot class in Java to interact with the pop-up and pass the username and password programmatically.

29. How do you generate test reports in Selenium?

Selenium does not provide built-in test reporting. However, you can generate test reports using testing frameworks like TestNG or JUnit. Additionally, you can integrate Selenium with third-party reporting tools like Extent Reports or Allure Report for more comprehensive and visually appealing reports.

30. How do you handle browser zoom levels in Selenium?

To handle browser zoom levels in Selenium, you can use the JavascriptExecutor interface to execute JavaScript code that changes the browser's zoom level.

JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("document.body.style.zoom='80%'");

Conclusion

These were some of the most common and important Selenium interview questions and answers for experienced professionals in Capgemini. Properly preparing for these questions will give you a solid foundation for your Selenium interview. Remember to practice hands-on with Selenium and keep yourself updated with the latest advancements in web automation testing. Good luck with your interview!

Comments

Contact Form

Send