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