Selenium Advanced

Estimated reading: 5 minutes 28 views

1. What is the use of DesiredCapabilities in Selenium?

DesiredCapabilities is used to set properties or capabilities for browser instances in Selenium WebDriver. It is useful for:

  • Defining browser properties like browser name, version, platform, etc.
  • Enabling specific settings like accepting SSL certificates, enabling proxy settings, or setting browser preferences.
  • Configuring test execution on remote environments like Selenium Grid.

Example:

				
					DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WINDOWS);
capabilities.setCapability("acceptInsecureCerts", true);

				
			

2. How do you perform a file upload using Selenium WebDriver?

To upload a file, locate the file input element and send the file path to it:

				
					WebElement uploadElement = driver.findElement(By.id("fileUpload"));
uploadElement.sendKeys("C:\\path\\to\\file.txt");

				
			

3. What is a headless browser? How can you execute tests on it using Selenium?

A headless browser is a web browser without a graphical user interface. It is useful for running automated tests in environments where the display is not needed (e.g., CI/CD pipelines).

To run tests in a headless browser:

				
					ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);

				
			

4. How do you perform parallel execution using Selenium Grid?

Selenium Grid allows parallel test execution on multiple machines or environments. Steps:

  1. Start the Selenium Grid hub and nodes.
  2. Configure the test with RemoteWebDriver and specify the hub URL and desired capabilities.

Example:

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

				
			

5. How can you handle multiple browsers (Chrome, Firefox, etc.) in Selenium WebDriver?

You can handle multiple browsers by setting up WebDriver instances conditionally:

				
					if (browser.equalsIgnoreCase("chrome")) {
    WebDriver driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("firefox")) {
    WebDriver driver = new FirefoxDriver();
}

				
			

6. What is the role of WebDriverWait in Selenium? Explain different types of waits in Selenium.

WebDriverWait is used to explicitly wait for a specific condition to occur before proceeding with further actions. It is part of Selenium’s explicit wait mechanism.

Types of Waits:

  1. Implicit Wait:
    • Sets a default wait time for locating elements globally.
				
					driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

				
			

2. Explicit Wait:

    • Waits for specific conditions like element visibility, element clickability, etc.
				
					WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementID")));

				
			

3. Fluent Wait:

    • Similar to explicit wait but allows polling intervals and ignores exceptions.
				
					Wait<WebDriver> wait = new FluentWait<>(driver)
    .withTimeout(Duration.ofSeconds(30))
    .pollingEvery(Duration.ofSeconds(5))
    .ignoring(NoSuchElementException.class);

				
			
  • 7. How do you handle CAPTCHA in Selenium?

    CAPTCHAs are meant to prevent automation. Handling CAPTCHAs requires manual intervention or third-party tools like OCR (Optical Character Recognition) or CAPTCHA solving services (e.g., Anti-Captcha, 2Captcha). Automation can:

    1. Request developers to disable CAPTCHA in test environments.
    2. Use tools or APIs to bypass CAPTCHA if allowed.

    8. How can you integrate Selenium with Jenkins for Continuous Integration?

    Steps to integrate Selenium with Jenkins:

    1. Install Jenkins and configure it on the server.
    2. Add a Jenkins job for the Selenium project.
    3. Use Maven or Gradle for build management.
    4. Configure Jenkins to pull the latest code from a version control system like Git.
    5. Execute the Selenium tests by adding a build step:
      • Example: mvn clean test
    6. Configure Jenkins for periodic or triggered test runs.

    9. What is the use of the Actions class in Selenium? Can you provide some examples?

    The Actions class allows performing advanced user interactions like mouse and keyboard actions.

    Examples:

    • Mouse Hover:
				
					Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id("menu"))).perform();

				
			
  • Drag and Drop:
				
					WebElement source = driver.findElement(By.id("source"));
WebElement target = driver.findElement(By.id("target"));
actions.dragAndDrop(source, target).perform();

				
			
  • Right Click (Context Click):
				
					actions.contextClick(driver.findElement(By.id("element"))).perform();

				
			

10. How do you manage cookies in Selenium WebDriver?

You can add, delete, or fetch cookies during a test.

Examples:

  • Add a Cookie:
				
					Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(cookie);

				
			
  • Get Cookies:
				
					Set<Cookie> cookies = driver.manage().getCookies();

				
			
  • Delete a Cookie:
				
					driver.manage().deleteCookieNamed("key");

				
			

11. How do you handle file downloads in Selenium WebDriver?

Configure the browser’s download preferences using options or capabilities.

Example for Chrome:

				
					HashMap<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", "C:\\Downloads");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);

				
			

12. Explain the concept of “implicit wait” and “explicit wait” in Selenium WebDriver.

  • Implicit Wait: Sets a global timeout for locating elements. If the element is not found, WebDriver waits for the specified time.
				
					driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

				
			

Explicit Wait: Waits for specific conditions, such as element visibility or clickability.

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

				
			

13. How do you handle JavaScript alerts in Selenium WebDriver?

Selenium provides methods to interact with JavaScript alerts:

  • Accept Alert:
				
					driver.switchTo().alert().accept();

				
			
  • Dismiss Alert:
				
					driver.switchTo().alert().dismiss();

				
			
  • Get Alert Text:
				
					String alertText = driver.switchTo().alert().getText();

				
			

14. How do you handle timeouts in Selenium WebDriver?

Timeouts are handled using the timeouts() method or explicit waits.

  • Page Load Timeout:
				
					driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);

				
			
  • Script Timeout:
				
					driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);

				
			

Leave a Comment

Share this Doc

Selenium Advanced

Or copy link

CONTENTS