1\若是复制JAVA文件到eclipse工程内出现乱码,则右击工程,选择属性--resource -text file encoding- other:UTF-8 2\导入现有JAR包:右击工程,选择build path--java build path-add external jars 1、Java中System.setProperty()用法 /* * 设置指定键对值的系统属性 * setProperty (String prop, String value); * * 参数: * prop - 系统属性的名称。 * value - 系统属性的值。 * * 返回: * 系统属性之前的值,若是没有之前的值,则返回 null。 * * 抛出: * SecurityException - 若是安全管理器存在而且其 checkPermission 方法不容许设置指定属性。 * NullPointerException - 若是 key 或 value 为 null。 * IllegalArgumentException - 若是 key 为空。 * 注:这里的system,系统指的是 JRE (runtime)system,不是指 OS。 * */ //实例 System.setProperty("Property1", "abc"); System.setProperty("Property2","def"); //这样就把第一个参数设置成为系统的全局变量!能够在项目的任何一个地方 经过System.getProperty("变量");来得到, //System.setProperty 至关于一个静态变量 ,存在内存里面! 复制代码 public class SystemTest { static { setValue(); } public static void setValue() { System.setProperty("name", "张三"); System.setProperty("age", "28"); } public static void main(String[] args) { System.out.println(System.getProperty("name")); System.out.println(System.getProperty("age")); } } 二、 import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class Selenium2Example { public static void main(String[] args) { // Create a new instance of the Firefox driver // Notice that the remainder of the code relies on the interface, // not the implementation. WebDriver driver = new FirefoxDriver(); // 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(); } } 三、 //如下是启动IE浏览器脚本(先要下载IE驱动,并指定路径,方法以下)--调试 /*System.setProperty("webdriver.ie.driver", "D:/selenium自动化脚本/iedriver/IEDriverServer.exe"); DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); ieCapabilities.setCapability (InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); WebDriver driver = new InternetExplorerDriver(ieCapabilities); driver.get("http://soasadmin-stg.paic.com.cn/admin/admin/login.html"); */ //如下是启动火弧浏览器脚本--调试成功,也须要安装驳运才能够,已下载火弧驱动 System.setProperty("webdriver.gecko.driver","D:/myselenium/geckodriver.exe");// --指定安装目录就要指定浏览器路径 WebDriver driver = new FirefoxDriver();// 默认安装路径就不须要指定以上语句,直接用下面2句启动 driver.get("http://www.baidu.com"); //WebElement d=driver.findElement(By.name("百度一下")); //d.click(); driver.findElement(By.name("wd")).sendKeys("我爱中国人"); driver.findElement(By.className("bg s_btn")).click(); //如下是启动谷歌浏览器脚本--没调试成功 System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.baidu.com")
其余代码:供参考html
一、LoginAdminjava
package webselenium.login; import java.io.FileNotFoundException; import java.io.IOException; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import webselenium.common.Common; public class LoginAdmin{ public static void loginAdmin(WebDriver driver) throws FileNotFoundException, IOException{ driver.get(Common.getPropertiesFromKeyToValue("at"));//Fat或Uat切换 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } driver.findElement(By.className(Common.getPropertiesFromKeyToValue("elementUserName"))).sendKeys(Common.getPropertiesFromKeyToValue("username")); driver.findElement(By.className(Common.getPropertiesFromKeyToValue("elementPassWord"))).sendKeys(Common.getPropertiesFromKeyToValue("password")); WebElement cmd = driver.findElement(By.xpath(("/html/body/div/div[3]/div[3]/a"))); JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("arguments[0].click();", cmd); } }
二、web
import org.openqa.selenium.WebDriver;chrome
public class CloseBroser{
public static void close(WebDriver driver){
//关闭浏览器
driver.quit();
}
}浏览器