|
| 1 | +# Selenium WebDriver Test Template using Bazel |
| 2 | + |
| 3 | +# 1. Setup WebDriver and Browser |
| 4 | +# Initialize WebDriver with a specific browser (Chrome, Firefox, etc.) |
| 5 | + |
| 6 | +import org.openqa.selenium.WebDriver; |
| 7 | +import org.openqa.selenium.chrome.ChromeDriver; |
| 8 | +import org.openqa.selenium.firefox.FirefoxDriver; |
| 9 | +import org.openqa.selenium.WebElement; |
| 10 | +import org.openqa.selenium.By; |
| 11 | +import org.openqa.selenium.chrome.ChromeOptions; |
| 12 | + |
| 13 | +public class ExampleWebTest { |
| 14 | + |
| 15 | + public static void main(String[] args) { |
| 16 | + WebDriver driver; |
| 17 | + |
| 18 | + // Choose the browser (Chrome example here) |
| 19 | + System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); // Update the path |
| 20 | + ChromeOptions options = new ChromeOptions(); |
| 21 | + options.addArguments("--headless"); // Headless mode, optional |
| 22 | + driver = new ChromeDriver(options); |
| 23 | + |
| 24 | + // 2. Open URL |
| 25 | + driver.get("http://www.example.com"); |
| 26 | + |
| 27 | + // 3. Perform actions (click, send keys, etc.) |
| 28 | + WebElement element = driver.findElement(By.id("example-element")); |
| 29 | + element.click(); // Example action |
| 30 | + |
| 31 | + // 4. Validate Results |
| 32 | + String pageTitle = driver.getTitle(); |
| 33 | + if (pageTitle.equals("Expected Title")) { |
| 34 | + System.out.println("Test Passed!"); |
| 35 | + } else { |
| 36 | + System.out.println("Test Failed."); |
| 37 | + } |
| 38 | + |
| 39 | + // 5. Clean Up |
| 40 | + driver.quit(); // Close the browser |
| 41 | + } |
| 42 | +} |
0 commit comments