天天看點

selenium使用學習(一)

最近在功能測試的基礎上,自己打算學習一門自動化測試的工具,根據自己的工作是web端的,是以就定位在學習selenium了,希望可以協同幫助到自己平日的工作。

起初,都是需要什麼,直接百度,沒有經曆錄制的階段,直接開始copy需要的功能的代碼,直接開始跑,貌似,最開始的時候是可以這麼做的,很高效,因為你最開始用到的都是最簡單的,直接用線程的,修改一下url,和你填寫的東西,就可以送出了。

這裡先copy一段最開始令我興奮的小代碼,希望可以合大家共享,順便記錄自己的學習之路和遇到的問題。

package com.selenium.Glen;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Ref;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.IOUtils;
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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestHelloWorld {

	private static String userPw;

	public static  void main(String[] args) throws FileNotFoundException,
		IOException {

		System.out.println("start chrome browser...");
		String url = "http://user.oneapm.com/pages/v2/login";		
		String url2 = "http://user.oneapm.com/pages/v2/home";
		WebDriver driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(500, TimeUnit.SECONDS);
		
	       

		// System.setProperty("webdriver.chrome.driver",
		// "files\\chromedriver.exe");  //指定驅動路徑
		driver.get(url);
		//System.out.println("start chrome browser succeed...");
		
		//讀取檔案的内容
		File file = new File("D:\\mycode\\selenium\\userPwd.txt");
		List<String> result = IOUtils.readLines(new FileInputStream(file));
		for (String line : result) {
			if (line.isEmpty() | line.contains("//")) {
				continue;
			}
			
			//System.out.println(line);
		
		WebElement element = null;
	
		//登入頁面
		 String emaiBox = "email";//郵箱輸入框的name
		 String passwordBox = "password";//密碼輸入框的name
		 String login = "submit";//輸入框id
				
		 //輸入使用者名和密碼,登入
	     driver.findElement(By.name(emaiBox)).sendKeys("[email protected]");
	     driver.findElement(By.name(passwordBox)).sendKeys("111111a"); 
	     driver.findElement(By.name(login)).click();  
	     
	     try {
			driver.wait(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     
	     driver.findElement(By.xpath("//span[text()='oneapm的de']")).click();
	     
	     //selenium.waitForPageToLoad("10000");
	     driver.getWindowHandle();
	     
	     driver.findElement(By.xpath("//ul[@id='account-menu']/li[3]/a")).click();
	    // driver.findElement(By.xpath(".//*[@id='layout']/div[1]/div[2]/div/ul[3]/a")).click();
	   //ul[@id='account-menu']/li[3]/a
	   //*[@id="layout"]/div[1]/div[2]/strong/span
	   //*[@id="layout"]/div[1]/div[2]/div/ul/li[3]/a
	   //*[@id="layout"]/div[1]/div[2]/strong/span
	     
//	     driver.findElement(By.xpath(".//*[@id='loginform']/div[1]/input")).sendKeys("[email protected]");
//	     driver.findElement(By.xpath(".//*[@id='loginform']/div[2]/input")).sendKeys("111111a");
//	     driver.findElement(By.xpath(".//*[@id='loginform']/div[4]/input[2]")).click();
//	     driver.findElement(By.xpath(".//*[@id='layout']/div[1]/div[2]/span")).click();
//	     driver.findElement(By.xpath(".//*[@id='layout']/div[1]/div[2]/div/ul[3]/a")).click();
	     
	     
	     //判斷跳轉頁面是否正确
//	     String currentUrl = driver.getCurrentUrl();
//	     assertTrue("未成功進入待測試頁面!",driver.getCurrentUrl().equals(url));
	     
	     //driver.navigate().to();
	     
//	     
//	     (new WebDriverWait(driver, 30)).until(new ExpectedCondition<Boolean>() {
//	            public Boolean apply(WebDriver d) {
//	                return d.getTitle().startsWith("OneAPM 應用性能管理雲解決方案");
//	            }
//	        });
//	 	
	     
	     
	     //跳轉頁面,等待假造完成
//	     //顯式等待
//	     WebDriverWait wait = new WebDriverWait(driver,10); 	     
//	        wait.until(new ExpectedCondition<WebElement>(){  
//	            public WebElement apply(WebDriver d) {  
//	                return d.findElement(By.cssSelector("account-info active"));  
//	            }}).click();
//	        
//	     WebElement element1 = driver.findElement(By.className("account-info active"));  
//	     
	    //隐式等待	     
	     //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
	     
//	     boolean isElementPresent(By.className("account-info active")) { 
//	    	    try { 
//	    	      driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS); 
//	    	      driver.findElement(by.className("account-info active")); 
//	    	      return true; 
//	    	    } 
//	    	      catch (NoSuchElementException e) { 
//	    	      return false; 
//	    	    } 
//	    	  }
//	     try {
//			Thread.sleep(3000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	     element1 = driver.findElement(By.className("account"));
//	     System.out.print(element1.getTagName());
	     //driver.findElement(By.className("account-info active")).click();
	     
	     
	     //操作過程中時間間隔設定
//	     WebDriverWait wait = (new WebDriverWait(driver,10));
//	     wait.until(new ExpectedCondition<Boolean>(){
//	     public Boolean apply(WebDriver d){
//	     boolean loadcomplete = d.switchTo().frame("right_frame").findElement(By.className("account").isDisplayed();
//	     return loadcomplete;}
//	     });
		  //thread.sleep(3000);//調試的時候強制延遲
	     //assertTrue("未成功進入待測試頁面!",driver.getTitle().equals(pageTittle));
	     
//	    new WebDriverWait(driver, 15).until(
//	    		    ExpectedConditions.presenceOfElementLocated(By.cssSelector("css locator"))
//	    		);  

	    
	    // driver.close();// 關閉本次執行打開的頁面
	     // driver.quit();//關閉所有頁面

		
			
		}
		//return false;

		// String title = driver.getTitle();
		// String currentUrl = driver.getCurrentUrl();
		// String s = driver.getPageSource();
		// s = s.substring(s.indexOf("{"), s.indexOf("}"));
		
		// WebElement element = driver.findElement(By.id("passport_user"));
		// System.out.println(element.getAttribute("title"));
		

		// System.out.println("the title is " + title);
		// System.out.println("the currentUrl is " + currentUrl);
		// System.out.println("目前頁面的源碼:"+s);

		
		
		
		
	}

//	private static void isElementPresent() {
//		// TODO Auto-generated method stub
//		
//	}

	private static void assertTrue(String string, boolean equals) {
		// TODO Auto-generated method stub
		
	}
	
//	using SIE = OpenQA.Selenium.IE;
//	public static String GoToWindow(String title, Ref SIE.InternetExplorerDriver driver)
//    {
//        driver.SwitchTo().DefaultContent();
//
//        // get all window handles
//        IList<string> handlers = driver.WindowHandles;
//        foreach (var winHandler in handlers)
//        {
//            driver.SwitchTo().Window(winHandler);
//            if (driver.Title == title)
//            {
//                return "Success";
//            }
//            else
//            {
//                driver.SwitchTo().DefaultContent();
//            }
//        }
//
//        return "Window Not Found";
//    }
//
//	private static void assertTrue(String string, boolean equals) {
//		 TODO Auto-generated method stub
//		
//	}

}
           

繼續閱讀