Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Selenium_with_Java_POC

Uploaded by

Venkata Ramana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Selenium_with_Java_POC

Uploaded by

Venkata Ramana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Selenium with Java POC - Proof of Concept

Key Features and Examples

1. Introduction to Selenium:

Selenium is an open-source automation framework for web applications.

Key Features:

- Cross-browser testing.

- Support for multiple programming languages like Java, Python, etc.

- Integration with testing frameworks like TestNG.

2. Selenium WebDriver Setup:

- Add Selenium dependency to your Maven POM file:

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-java</artifactId>

<version>4.13.0</version>

</dependency>

3. Basic Selenium Test with Java:

Code Example:

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumExample {


public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

WebDriver driver = new ChromeDriver();

driver.get("https://www.example.com");

System.out.println("Title: " + driver.getTitle());

driver.quit();

4. Locating Elements:

Selenium supports multiple ways to locate web elements.

Code Example:

WebElement element = driver.findElement(By.id("elementId"));

element.click();

Locator Strategies:

- By.id

- By.name

- By.className

- By.xpath

- By.cssSelector

5. Handling Dropdowns:

Code Example:

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

dropdown.selectByVisibleText("Option1");
6. Handling Alerts:

Code Example:

Alert alert = driver.switchTo().alert();

alert.accept();

7. Implicit and Explicit Waits:

Implicit Wait:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

Explicit Wait:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")));

8. Taking Screenshots:

Code Example:

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

FileUtils.copyFile(screenshot, new File("path/to/screenshot.png"));

9. Page Object Model (POM):

Code Example:

public class LoginPage {

private WebDriver driver;

public LoginPage(WebDriver driver) {

this.driver = driver;

public void login(String username, String password) {


driver.findElement(By.id("username")).sendKeys(username);

driver.findElement(By.id("password")).sendKeys(password);

driver.findElement(By.id("login")).click();

10. Integration with TestNG:

Code Example:

import org.testng.annotations.Test;

public class TestNGSeleniumTest {

@Test

public void testMethod() {

WebDriver driver = new ChromeDriver();

driver.get("https://www.example.com");

driver.quit();

11. Running Tests in Parallel:

TestNG XML Example:

<suite name="Suite" parallel="methods" thread-count="2">

<test name="Test">

<classes>

<class name="com.example.SeleniumTest" />

</classes>

</test>

</suite>
12. Generating Reports:

- TestNG generates default reports in the `test-output` folder.

You might also like