Maven 檔案
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.netkiller</groupId> <artifactId>selenium</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>selenium</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>${maven.compiler.source}</maven.compiler.target> <junit.jupiter.version>5.4.0</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <!-- <scope>test</scope> --> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>${junit.jupiter.version}</version> <!-- <scope>test</scope> --> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <!-- <scope>test</scope> --> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-safari-driver</artifactId> <version>3.141.59</version> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M3</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.1</version> <configuration> <archive> <index>true</index> <manifest> <mainClass>demo.test</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project>
package cn.netkiller.selenium; import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.WebDriver; import org.openqa.selenium.safari.SafariDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; /** * Hello world! * */ public class App { public static void main(String[] args) { System.out.println("Hello World!"); WebDriver driver = new SafariDriver(); driver.manage().window().setSize(new Dimension(1024, 768)); driver.get("https://www.google.com.hk"); driver.findElement(By.name("q")).sendKeys("webdriver"); driver.findElement(By.name("btnK")).click(); new WebDriverWait(driver, 3).until(ExpectedConditions.titleIs("webdriver - Google 搜尋")); driver.close(); } }
https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver
下載 https://sites.google.com/a/chromium.org/chromedriver/downloads
package cn.netkiller.webtest; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; /** * Hello world! * */ public class App { public static void main(String[] args) { System.out.println("Hello World!"); // Create a new instance of the Firefox driver // Notice that the remainder of the code relies on the interface, // not the implementation. System.setProperty("webdriver.chrome.driver", "D:\\workspace\\chromedriver.exe"); // WebDriver driver = new FirefoxDriver(); WebDriver driver = new ChromeDriver(); // And now use this to visit Google driver.get("http://www.google.com"); // Alternatively the same thing can be done like this // driver.navigate().to("http://www.google.com"); // Find the text input element by its name WebElement element = driver.findElement(By.name("q")); // Enter something to search for element.sendKeys("Cheese!"); // Now submit the form. WebDriver will find the form for us from the // element element.submit(); // Check the title of the page System.out.println("Page title is: " + driver.getTitle()); // Google's search is rendered dynamically with JavaScript. // Wait for the page to load, timeout after 10 seconds (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().startsWith("cheese!"); } }); // Should see: "cheese! - Google Search" System.out.println("Page title is: " + driver.getTitle()); // Close the browser driver.quit(); } }
String textFound = driver.findElement(By.cssSelector("h1")).getText();
// for alert WebElement clickOnAlert = driver.findElement(By .xpath("//td/input[@name='alterbutton']")); clickOnAlert.click(); delay(2); // get alert Alert alert = driver.switchTo().alert(); Assert.assertTrue(alert.getText().contains("alert")); // click alert ok alert.accept();
// for prompt WebElement clickOnPrompt = driver.findElement(By .xpath("//td/input[@name='promptbutton']")); clickOnPrompt.click(); delay(2); Alert prompt = driver.switchTo().alert(); prompt.sendKeys("I love Selenium"); prompt.accept(); delay(5); Alert afterAccept = driver.switchTo().alert(); Assert.assertTrue(afterAccept.getText().contains("I love Selenium")); // click alert ok afterAccept.accept();
// for confirm WebElement clickOnConfirm = driver.findElement(By .xpath("//td/input[@name='confirmbutton']")); clickOnConfirm.click(); delay(2); Alert confirm = driver.switchTo().alert(); confirm.dismiss(); delay(2); Alert afterDismiss = driver.switchTo().alert(); Assert.assertTrue(afterDismiss.getText().contains("You pressed Cancel")); delay(2); afterDismiss.accept();