Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | Github | OSChina 博客 | 云社区 | 云栖社区 | Facebook | Linkedin | 知乎专栏 | 视频教程 | About

6.8. Java

6.8.1. Apple Mac 例子 Java 11 + Junit5 + Selenium + Safari

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();
	}
}
			
		

6.8.2. Windows Example

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();

	}
}
		
		

6.8.3. 获取信息

6.8.3.1. title
			
				driver.getTitle();
			
			


			

6.8.4. 查找元素

			
		
		
6.8.4.1. By.id(String id);
			
6.8.4.2. By.className(String className);

			
6.8.4.3. By.cssSelector(String cssSelector);
			
String textFound = driver.findElement(By.cssSelector("h1")).getText();
			
			
6.8.4.4. By.linkText(String linkText);
			
6.8.4.5. By.partialLinkText(String partialLinkText);
			
			
			
			
6.8.4.6. By.name(String name);


			
6.8.4.7. By.tagName(String tagName);

			
6.8.4.8. By.xpath(String xpath);

			
6.8.4.9. getTagName()
			
String tagName = driver.findElement(By.id("email")).getTagName();
			
			
6.8.4.10. submit()
    		
driver.findElement(By.id("searchInput")).submit(); 
    		
			

6.8.5. 事件

6.8.5.1. 填写信息
			
searchFieldElement.sendKeys("selenium tutorial javapointers.com");			
			
			
6.8.5.2. 单击操作
			
WebElement searchButtonElement = webDriver.findElement(By.name("btnK"));
searchButtonElement.click();
			
			

6.8.6. 浏览器控制

6.8.6.1. 窗口控制

最大化窗口

			
driver.manage().window().maximize();
			
			
6.8.6.2. 切换窗口
			
	driver.switchTo().window(window);
			
			
6.8.6.3. 超时
			
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);			
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
			
			

6.8.7. 对话框

6.8.7.1. Alert
			
// 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();
			
			
6.8.7.2. 输入对话框
			
// 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();	
			
			
6.8.7.3. 确认对话框
				
		// 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();
			
			

6.8.8. HTML 表单处理

6.8.8.1. SELECT
		
	WebElement selector = driver.findElement(By.id("Selector"));
	Select select = new Select(selector);
	select.selectByIndex(2);
	select.selectByVisibleText("苹果");
	select.selectByValue("apple");