例子:測試登陸模塊,對登陸的帳號和密碼進行不一樣的case校驗。html
package com.lee.auto.testFrome;java import org.openqa.selenium.By;web import org.openqa.selenium.chrome.ChromeDriver;chrome
public class login_1 {瀏覽器 public static void main(String[] args) throws InterruptedException {安全 System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe");框架 ChromeDriver driver = new ChromeDriver();ide driver.get("測試url");函數 driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15920000001");工具 driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("11111111"); driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver.close();
System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver1 = new ChromeDriver(); driver1.get("測試url"); driver1.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15920000001"); driver1.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("1111"); driver1.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver1.close();
System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver2 = new ChromeDriver(); driver2.get("測試url"); driver2.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15720000001"); driver2.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("11111111"); driver2.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver2.close(); } } |
以上代碼實現帳號、密碼的三種驗證,下面首先對這三種驗證進行方法的抽取,代碼以下:
package com.lee.auto.testFrome; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver;
public class login_2 { public static void main(String[] args) throws InterruptedException { case_1(); case_2(); case_3(); } private static void case_1() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("測試url"); driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15920000001");; driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("11111111");; driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver.close(); } private static void case_2() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("測試url"); driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15920000001");; driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("1111");; driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver.close(); } private static void case_3() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); driver.get("測試url"); driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15720000001");; driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("11111111");; driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver.close(); } } |
方法抽取方式以下:
在main函數中選取須要抽取方法的代碼,右鍵選擇refactor-extract method便可進行抽取。
對生成的chrome驅動進行工具類的封裝。
package com.lee.auto.testFrome; import org.openqa.selenium.chrome.ChromeDriver; public class exploreTool { //方法的抽取 public static ChromeDriver getChromeDriver() { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); return driver; } } |
package com.lee.auto.testFrome; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver;
public class login_3 { public static void main(String[] args) throws InterruptedException { case_1(); case_2(); case_3(); } private static void case_1() throws InterruptedException { ChromeDriver driver = exploreTool.getChromeDriver(); driver.get("測試url"); driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15920000001");; driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("11111111");; driver.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver.close(); } private static void case_2() throws InterruptedException { ChromeDriver driver1 = exploreTool.getChromeDriver(); driver1.get("測試url"); driver1.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15920000001"); driver1.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("1111"); driver1.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver1.close(); } private static void case_3() throws InterruptedException { ChromeDriver driver2 = exploreTool.getChromeDriver(); driver2.get("測試url"); driver2.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("15720000001"); driver2.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("11111111"); driver2.findElement(By.xpath("/html/body/div/div/div[2]/div[2]/form/section[3]/span/input")).click(); Thread.sleep(5000); driver2.close(); } } |
下面代碼爲三個瀏覽器進行登陸實現。
package com.lee.auto.testFrome;
import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.ie.InternetExplorerDriverService; import org.openqa.selenium.remote.DesiredCapabilities;
public class exploreTool { // 方法的抽取 public static ChromeDriver getChromeDriver() { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); ChromeDriver driver = new ChromeDriver(); return driver; }
public static FirefoxDriver getFirefoxDriver() { System.setProperty("webdriver.firefox.bin", "D:\\Program\\firefox47.0.2\\bin\\firefox.exe"); FirefoxDriver driver = new FirefoxDriver(); return driver; }
public static InternetExplorerDriver getIEDriver() { System.setProperty("webdriver.ie.driver", "src/IEDriverServer32.exe"); InternetExplorerDriver driver = new InternetExplorerDriver(); return driver; } } |
package com.lee.auto.testFrome; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver;
public class login_4 {
public static void main(String[] args) throws InterruptedException { case_1(); case_2(); case_3(); }
private static void case_1() throws InterruptedException { ChromeDriver driver = exploreTool.getChromeDriver(); driver.get("測試url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); driver.findElement(By.name("submit")).click(); Thread.sleep(5000); driver.close(); }
private static void case_2() throws InterruptedException { FirefoxDriver driver = exploreTool.getFirefoxDriver(); driver.get("測試url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); driver.findElement(By.name("submit")).click(); Thread.sleep(5000); driver.close(); }
private static void case_3() throws InterruptedException { InternetExplorerDriver driver = exploreTool.getIEDriver(); driver.get("測試url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); // 直接使用click點擊無效,採用keys.enter方式進行登陸 driver.findElement(By.name("submit")).sendKeys(Keys.ENTER); Thread.sleep(5000); driver.close(); } } |
IE報錯1:
Exception in thread "main" org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window (WARNING: The server did not provide any stacktrace information) |
解決方法:須要把IE安全設置的四個區域的保護模式設置一致。
IE問題2:
頁面能夠找到登陸元素,可是click無效。
解決方法:採用keys.enter方式進行登陸。
IE問題3:
找到帳號、密碼文本框後輸入值特別慢。
解決方法:將IEDriverServer由64位的換爲32位的。
對於多個瀏覽器的驅動生成,能夠藉助webdriver接口進行多態實現。
經過傳入的參數值來判斷所需的瀏覽器類型。
package com.lee.auto.login;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver;
public class exploreTool_1 {
public static WebDriver getexploreDriver(String exploreType) {
WebDriver driver = null; //判斷爲chrome if (exploreType.equals("chrome")) { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); driver = new ChromeDriver(); } //判斷爲Firefox else if (exploreType.equals("firefox")) { System.setProperty("webdriver.firefox.bin", "D:\\Program\\firefox47.0.2\\bin\\firefox.exe"); driver = new FirefoxDriver(); } //判斷爲IE else if (exploreType.equals("ie")) { System.setProperty("webdriver.ie.driver", "src/IEDriverServer32.exe"); driver = new InternetExplorerDriver(); } return driver; } } |
package com.lee.auto.login;
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver;
public class login_5 {
public static void main(String[] args) throws InterruptedException { case_1(); case_2(); case_3(); }
private static void case_1() throws InterruptedException { WebDriver driver = exploreTool_1.getexploreDriver("chrome"); driver.get("測試url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); driver.findElement(By.name("submit")).click(); Thread.sleep(5000); driver.close(); }
private static void case_2() throws InterruptedException { WebDriver driver = exploreTool_1.getexploreDriver("firefox"); driver.get("測試url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); driver.findElement(By.name("submit")).click(); Thread.sleep(5000); driver.close(); }
private static void case_3() throws InterruptedException { WebDriver driver = exploreTool_1.getexploreDriver("ie"); driver.get("測試url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); // 直接使用click點擊無效,採用keys.enter方式進行登陸 driver.findElement(By.name("submit")).sendKeys(Keys.ENTER); Thread.sleep(5000); driver.close(); } } |
若多條用例都須要經過exploreTool_1.getexploreDriver方法來獲取瀏覽器驅動,一旦須要切換瀏覽器,就須要大量修改代碼來完成。爲了不此問題,將變更數據採用配置文件方式傳入,實現數據和代碼的分離。
新建配置文件,自定義參數值。
參數值定義格式:
key = value
步驟以下:
auto.browserType = ie |
package com.lee.auto.login; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver;
public class exploreTool_2 {
public static WebDriver getexploreDriver() { //讀取配置文件 //建立屬性對象 Properties properties = new Properties(); //對加載的文件拋出找不到文件時的異常 try { //經過屬性對象加載文件 properties.load(new FileInputStream("src/config.properties")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //獲取配置文件中的屬性值 String exploreType = properties.getProperty("auto.browserType");
WebDriver driver = null; //判斷爲chrome if (exploreType.equals("chrome")) { System.setProperty("webdriver.chrome.driver", "src/chromedriver.exe"); driver = new ChromeDriver(); } //判斷爲Firefox else if (exploreType.equals("firefox")) { System.setProperty("webdriver.firefox.bin", "D:\\Program\\firefox47.0.2\\bin\\firefox.exe"); driver = new FirefoxDriver(); } //判斷爲IE else if (exploreType.equals("ie")) { System.setProperty("webdriver.ie.driver", "src/IEDriverServer32.exe"); driver = new InternetExplorerDriver(); } return driver; } } |
package com.lee.auto.login; import java.io.FileNotFoundException; import java.io.IOException; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver;
public class login_6 {
public static void main(String[] args) throws InterruptedException{ case_1(); case_2(); case_3(); }
private static void case_1() throws InterruptedException{ WebDriver driver = exploreTool_2.getexploreDriver(); driver.get("測試url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("11111111"); driver.findElement(By.name("submit")).sendKeys(Keys.ENTER); Thread.sleep(5000); driver.close(); }
private static void case_2() throws InterruptedException{ WebDriver driver = exploreTool_2.getexploreDriver(); driver.get("測試url"); driver.findElement(By.id("username")).sendKeys("15920000001"); driver.findElement(By.id("password")).sendKeys("1111"); driver.findElement(By.name("submit")).sendKeys(Keys.ENTER); Thread.sleep(5000); driver.close(); }
private static void case_3() throws InterruptedException{ WebDriver driver = exploreTool_2.getexploreDriver(); driver.get("測試url"); driver.findElement(By.id("username")).sendKeys(""); driver.findElement(By.id("password")).sendKeys("11111111"); // 直接使用click點擊無效,採用keys.enter方式進行登陸 driver.findElement(By.name("submit")).sendKeys(Keys.ENTER); Thread.sleep(5000); driver.close(); } } |