1.Setup
- Dependencies (Maven):
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.x.x</version> <!-- Use the latest version --> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.x.x</version> </dependency> </dependencies>
- WebDriver Setup:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver();
2.Basic Commands
- Open a URL:
driver.get("https://www.example.com");
- Get Title:
String title = driver.getTitle();
- Get Current URL:
String url = driver.getCurrentUrl();
- Close Browser:
driver.quit(); // or driver.close();
3.Locators
- By ID:
driver.findElement(By.id("elementId"));
- By Name:
driver.findElement(By.name("elementName"));
- By Class Name:
driver.findElement(By.className("className"));
- By CSS Selector:
driver.findElement(By.cssSelector("cssSelector"));
- By XPath:
driver.findElement(By.xpath("//tag[@attribute='value']"));
4.Interacting with Elements
- Clicking an Element:
driver.findElement(By.id("elementId")).click();
- Sending Keys:
driver.findElement(By.id("elementId")).sendKeys("text");
- Getting Text:
String text = driver.findElement(By.id("elementId")).getText();
- Checking Element Visibility:
boolean isDisplayed = driver.findElement(By.id("elementId")).isDisplayed();
5.Waits
- Implicit Wait:
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- Explicit Wait:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")));
6.Actions
- Mouse Hover:
Actions actions = new Actions(driver); actions.moveToElement(driver.findElement(By.id("hoverElementId"))).perform();
- Drag and Drop:
WebElement source = driver.findElement(By.id("sourceId")); WebElement target = driver.findElement(By.id("targetId")); actions.dragAndDrop(source, target).perform();
7.Handling Alerts
- Accept Alert:
driver.switchTo().alert().accept();
- Dismiss Alert:
driver.switchTo().alert().dismiss();
- Get Alert Text:
String alertText = driver.switchTo().alert().getText();
8.Taking Screenshots
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.apache.commons.io.FileUtils;
import java.io.File;File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("screenshot.png"));
9.Example Test with TestNG
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;public class SampleTest {
WebDriver driver;@BeforeClass
public void setup() {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
driver = new ChromeDriver();
}@Test
public void testExample() {
driver.get("https://www.example.com");
// Add your test logic here
}@AfterClass
public void teardown() {
driver.quit();
}
}