package com.moldbao.test;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//selenium 火狐测试实例(@test一定要加上不然不会在junit显示,输入@再输入Alt+?可以带出下拉框提示信息选择数据)
public class T1 {
@Test
public static void main(String[] args) {
//指定火狐驱动路径
System.setProperty("webdriver.gecko.driver", "D:\\selenium\\geckodriver.exe");
//定义驱动对象为 FirefoxDriver 对象
WebDriver driver = new FirefoxDriver();
//驱动的网址
driver.get("http://www.baidu.com/");
//浏览器窗口变大
driver.manage().window().maximize();
//定位输入框元素
WebElement txtbox = driver.findElement(By.name("wd"));
//在输入框输入文本
txtbox.sendKeys("HelloWorld");
//定位按钮元素
WebElement btn = driver.findElement(By.id("su"));
//点击按钮
btn.click();
//关闭驱动
//driver.close();
}
}
-----谷歌测试实例
package com.moldbao.test;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
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.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
//selenium
谷歌测试实例(@test一定要加上不然不会在junit显示,输入@再输入Alt+?可以带出下拉框提示信息选择数据)
public class test1 {
@Test
public static void main(String[] args) throws Exception{
System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver.exe");
// 谷歌浏览器软件地址
ChromeOptions ptions = new ChromeOptions();
options.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
WebDriver driver = new ChromeDriver(options);
// 驱动的网址
driver.get("http://www.baidu.com/");
// 浏览器窗口变大
driver.manage().window().maximize();
// 定位输入框元素
WebElement txtbox = driver.findElement(By.name("wd"));
// 在输入框输入文本
txtbox.sendKeys("HelloWorld");
// 定位按钮元素
WebElement btn = driver.findElement(By.id("su"));
// 点击按钮
btn.click();
// 关闭浏览器
//driver.quit();
}
}
------IE测试实例
package com.moldbao.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Test2 {
//selenium IE测试实例(@test一定要加上不然不会在junit显示,输入@再输入Alt+?可以带出下拉框提示信息选择数据)
@Test
public void test() {
//System.setProperty("webdriver.firefox.driver", "D:\\selenium\\geckodriver.exe");// 注意这里IEDriverServer.exe的文件存放路径
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
WebDriver driver = new InternetExplorerDriver(ieCapabilities);
driver.get("https://www.baidu.com");
WebElement element = driver.findElement(By.id("kw"));
// element.sendKeys("");
element.sendKeys("h");
element.submit();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Page title is: " + driver.getTitle());
boolean status = driver.getTitle().startsWith("h");
driver.quit();
assertTrue("访问
百度失败", status);
}
}