git+java+selenium+testng +maven+idea
1、git之代碼維護(下載、分支切換、上傳)
下載命令 "git clone git@github.com:Luna0715/learnmaven.git"
git branch -a 列出所有分支
git branch 查看本地分支
創建分支:git branch <name>
創建+切換分支:git checkout -b <name>
git checkout -b develop origin/develop 切換到develop分支
git branch 驗證一下,已經切換到develop分支
再切換回master: git checkout master git branch
git remote add origin git@github.com:Luna0715/GodLikeCourse.git
使用命令 git add . 提交新文件(new)和被修改(modified)文件,不包括被刪除(deleted)文件到緩存區
git commit -am "修改代碼" 用於提交跟蹤過的文件
git push 將本地分支的更新,推送到遠程主機
git push --set-upstream origin develop推送遠程不存在的本地分支
更新項目:
1.加一個線上主干git remote add upstream git@github.com:QA/GodLikeCourse.git
2.從主干拉取代碼 git pull upstream master
git status
git stash
2、引用testng
import org.testng.annotations.Test;
方法前加注釋@Test
3、調試
定位到要斷點的源代碼行,在源代碼所在行前面,單擊一下鼠標左鍵,設置一個斷點,以DEBUG的模式運行項目
4、元素定位
xpath常用符號說明:
/表示絕對路徑,絕對路徑是指從根目錄開始
//表示相對路徑
.表示當前層
..表示上一層
*表示通配符
@表示屬性
[]屬性的判斷條件表達式
1)driver.findElement(By.linkText("個人中心"))
2)driver.findElement(By.id("mobile1")).sendKeys("13111111111");
3)WebElement confirm = driver.findElement(By.className("confirm-btn"));
4)public static String getusername;
getusername = driver.findElement(By.xpath(".//*[@class='option']/li[1]/a")).getText();
WebElement fenqi = driver.findElement(By.xpath("//span[text()='分期']"));
WebElement CheTu = driver.findElement(By.xpath(".//*[@class='carlist']/ul/li[1]"));
By.xpath(".//*[@class='pop-up-layer' and @style='display: block;']")
5)通過定位到的元素繼續定位元素
WebElement productlist = driver.findElement(By.id("FirstPageProductList1"));
List<WebElement> products = productlist.findElements(By.className("list-header-ctr"));
System.out.println("product數量"+products.size());
getText = products.get(0).findElements(By.className("company-name")).get(0).getText();
for (WebElement ele : products) {
sleep(2);
getText = ele.findElements(By.className("company-name")).get(0).getText();
System.out.println("套餐名稱是:" + getText);
}
List<WebElement> SeeInof = productlist.findElements(By.className("col-black"));
SeeInof.get(0).click();
5、頁面切換
#不關閉舊窗口
public static void removeHandles(WebDriver driver){
//獲取當前頁面句柄
String handle = driver.getWindowHandle();
// 獲取所有頁面的句柄,並循環判斷不是當前的句柄
for (String handles : driver.getWindowHandles()) {
if (handles.equals(handle))
continue;
driver.switchTo().window(handles);
}
}
#關閉舊窗口
public static void removeHandles(WebDriver driver){
//獲取win窗口
try {
String [] handles = new String[driver.getWindowHandles().size()];//定義一個空數組,數組大小是打開窗口的數量
driver.getWindowHandles().toArray(handles);//將獲取到的句柄集合轉換為數組
//here
//driver.switchTo().window(handles[1]);
sleep(2);
//切換到舊窗口
WebDriver handle2 = driver.switchTo().window(handles[0]);
sleep(2);
//關閉舊窗口
handle2.close();
sleep(2);
//切換到新窗口
driver.switchTo().window(handles[1]);
sleep(2);
} catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
System.out.println("瀏覽器切換窗口異常" + e);
}}
6、截圖
1)
if(isElementExist(driver, By.xpath("//*[@id=\"list\"]/aside[1]/div[1]/div[2]/aside[1]/a/img")) ==true ){
System.out.println("車圖元素存在!");
}else{
System.out.println("車圖元素未找到,失敗圖片:" + picturePath);
screenShot(picturePath,driver);//調用失敗截圖功能;
}
public static void screenShot(String path,WebDriver driver){
//將當前窗口截屏,獲得一個File的圖片文件對象;
File screenFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//將圖片文件對象,保存到指定的路徑path里,例如D盤的某個文件夾下。
try {
File file = new File(path); //在指定路徑中,創建一個空白的圖片文件
FileUtils.copyFile(screenFile,file);//將窗口截屏文件,保存到剛剛創建的空白文件里
} catch (IOException e) {
e.printStackTrace();
}
}
2)
try{
driver.findElement(By.className("logo-sv"));
}catch(Exception e) {
e.printStackTrace();
String tpath="D:\\screenshots\\"+getScreen(driver)+".jpg";
System.out.println("logo元素未找到,失敗圖片:" +tpath);//調用失敗截圖功能;
}
public static String getScreen(WebDriver driver) throws Exception{
Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
File screenshot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot,new File("D:\\screenshots\\"+sdf.format(dt)+".png"));
return sdf.format(dt);
}
7、校驗
1)
if (getusername.equals("135****7172")){
System.out.println("登錄成功");
}
else{
System.out.println("登錄失敗");
}
2)
if(isElementExist(driver, By.xpath(".//*[@class='carlist']/ul/li[1]/div[1]")) == true){
System.out.println("第一個車圖元素存在!");
}else{
System.out.println("第一個車圖元素未找到!");
}
public static boolean isElementExist(WebDriver driver, By by) {
try {
driver.findElement(by);
return true;
} catch(NoSuchElementException e) {
return false;
}
}
8、用JS方法將滾動條定位到某個元素
WebElement CheTu = driver.findElement(By.xpath(".//*[@class='carlist']/ul/li[1]"));
public static void executeJS(WebDriver driver,WebElement arg) {
//用JS方法將滾動條定位到xx元素,其中auguments[0]就代表element
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();",arg);
}
9、線程休眠
public static void sleep(double d) {
try {
d *= 1000;
Thread.sleep((int)d);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
10、捕獲異常
try {
} catch (InterruptedException e) {
e.printStackTrace();
}
11、斷言
assert getPackagename.contains(getText);
assert driver.getTitle().equals("XXXXXX");
12、初始化
//將瀏覽器的驅動程序位置設定為系統屬性值:webdriver.chrome.driver
System.setProperty("webdriver.chrome.driver", "D:/selenium/chromedriver.exe");
//啟動瀏覽器
WebDriver driver = new ChromeDriver();
//打開網址
driver.get("https://www.xxxxxx.com/");
//窗口最大化
driver.manage().window().maximize();
// 獲取 網頁的 title
System.out.println("The testing page title is: " + driver.getTitle());
13、需要引入的包
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.openqa.selenium.JavascriptExecutor;
14、主函數快捷鍵psvm
15、遍歷
示例一:
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.interactions.Actions;
import org.testng.Assert;
import java.util.List;
public class secondtestcase {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","D:\\selenium\\chromedriver.exe");
WebDriver driver =new ChromeDriver();
driver.get("http://www.xxxxxx.com/ ");
WebElement hide = driver.findElement(By.className("newcar-down-box"));
Actions action = new Actions(driver);
action.moveToElement(hide).perform();
WebElement pricecondition = driver.findElement(By.cssSelector("div.models-show-newcar.hide"));
// String pricecondition = driver.findElement(By.xpath("/html/body/div[7]/div[1]/div[2]/div[2]/div[2]/dl[1]/dd/a[1]")).getText();
List<WebElement> price = pricecondition.findElements(By.xpath("dl[1]/dd/a"));
String pricetext[]=new String[]{"3萬以下","3-5萬","5-8萬","8-10萬","10-15萬","15-20萬","20-30萬","30-45萬","45萬以上"};
int count=price.size();
for (int i=0;i<count;i++){
Assert.assertEquals(price.get(i).getText(),pricetext[i]);
String priceconteng[]=new String[count];
priceconteng[i]=price.get(i).getText();
System.out.println(priceconteng[i]);
if (price.get(i).getText().equals(pricetext[i])){
System.out.println("匹配");
}
else {
System.out.println("不匹配");
}
}
}
}
示例二:
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class firsttestcase {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//System.setProperty("webdriver.chrome.driver", "D:/selenium/chromedriver.exe");
//WebDriver driver = new ChromeDriver();
driver.navigate().to("http://xxx.xxxxxx.com/beijing/budget-b26/?source=2525 ");
WebElement brand_select = driver.findElement(By.className("budget-filter"));
List<WebElement> brand = brand_select.findElements(By.xpath("div[1]/dl/dd/a"));
int count = brand.size();
String b[] = new String[]{"本田", "寶駿", "吉利", "大眾", "哈弗", "現代", "五菱", "豐田", "眾泰", "更多品牌"};
for (int i = 0; i < count; i++) {
try {
if (brand.get(i).getText().equals(b[i])) {
System.out.println("匹配");
}
} catch (Exception e) {
e.printStackTrace();
String picturePath = "D:\\file.png";
screenShot(picturePath,driver);//調用失敗截圖功能;
}
}
}
public static void screenShot(String path,WebDriver driver){
//將當前窗口截屏,獲得一個File的圖片文件對象;
File screenFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//將圖片文件對象,保存到指定的路徑path里,例如D盤的某個文件夾下。
try {
File file = new File(path); //在指定路徑中,創建一個空白的圖片文件
FileUtils.copyFile(screenFile,file);//將窗口截屏文件,保存到剛剛創建的空白文件里
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例三:
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;
import java.util.List;
public class Demo3 {
public static String getText;
public static String taocan;
public static void main(String[] args) {
//將瀏覽器的驅動程序位置設定為系統屬性值:webdriver.chrome.driver
System.setProperty("webdriver.chrome.driver", "D://Java//chromedriver_win32//chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://xxxxx.xxxxxx.com");
driver.manage().window().maximize();
fenqidetails(driver);
//driver.quit();
}
public static void fenqidetails(WebDriver driver){
sleep(2);
WebElement firstcar = driver.findElement(By.xpath(".//*[@class='carlist']/ul/li[1]"));
firstcar.click();
sleep(2);
if(isElementExist(driver, By.xpath(".//*[@class='pop-up-layer' and @style='display: block;']"))==true){
WebElement confirm = driver.findElement(By.className("confirm-btn"));
confirm.click();
}
//設置顯示等待時長:10s
WebDriverWait wait = new WebDriverWait(driver, 10);
//顯示等待:標題是否出現
try {
wait.until(ExpectedConditions.titleContains("XXXXXX"));
System.out.println("The testing page title is: " + driver.getTitle());
} catch (NoSuchElementException e) {
e.printStackTrace();
System.out.println("標題沒找到");
}
try {
wait.until(ExpectedConditions.presenceOfElementLocated(By.className("search")));
System.out.println("有搜索框");
} catch (NoSuchElementException e) {
e.printStackTrace();
System.out.println("搜索框沒找到");
}
// List<WebElement> products = driver.findElements(By.xpath(".//*[@id='FirstPageProductList1']/li"));
WebElement productlist = driver.findElement(By.id("FirstPageProductList1"));
List<WebElement> products = productlist.findElements(By.className("list-header-ctr"));
System.out.println("product數量"+products.size());
for (WebElement ele : products) {
sleep(2);
getText = ele.findElements(By.className("company-name")).get(0).getText();
System.out.println("套餐名稱是:" + getText);
}
for (int i = 0; i < 1; i++) {
sleep(2);
List<WebElement> SeeInof = productlist.findElements(By.className("col-black"));
SeeInof.get(i).click();
sleep(2);
removeHandles(driver);
taocan = driver.findElement(By.xpath("//*[@id=\"Content\"]/header/section/div/div[1]/h1")).getText();
System.out.println("套餐名稱和機構是:" + taocan);
}
}
public static boolean isElementExist(WebDriver driver, By by) {
try {
driver.findElement(by);
return true;
} catch(NoSuchElementException e) {
return false;
}
}
/**
* 移除窗口
*/
public static void removeHandles(WebDriver driver){
//獲取win窗口
try {
String [] handles = new String[driver.getWindowHandles().size()];
driver.getWindowHandles().toArray(handles);
for(int i = 0; i<handles.length;i++)
{
}
//here
driver.switchTo().window(handles[1]);
sleep(2);
WebDriver handle2 = driver.switchTo().window(handles[0]);
// log.info(handle2.getTitle());
sleep(2);
handle2.close();
sleep(2);
//切換到新窗口
driver.switchTo().window(handles[1]);
sleep(2);
} catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
System.out.println("瀏覽器切換窗口異常" + e);
}
}
public static void sleep(double d) {
try {
d *= 1000;
Thread.sleep((int)d);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
示例四:
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class Demo4 {
String getText;
String getPackagename;
public static String picturePath = "D:\\file.png"; //設置截屏文件所在的路徑、和圖片的格式:圖片保存在C盤下,格式是png
public static void main(String[] args) {
//將瀏覽器的驅動程序位置設定為系統屬性值:webdriver.chrome.driver
System.setProperty("webdriver.chrome.driver", "D://Java//chromedriver_win32//chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://xxxxx.xxxxxx.com/beijing/hafuh6/?source=127");
driver.manage().window().maximize();
sleep(2);
Demo4 a=new Demo4();
a.fenqilist(driver);
a.fenqidetails(driver);
//driver.quit();
}
public void fenqilist(WebDriver driver)
{
//判斷資質彈層是否存在
if(isElementExist(driver, By.xpath(".//*[@class='pop-up-layer' and @style='display: block;']"))==true)
{
WebElement confirm = driver.findElement(By.className("confirm-btn"));
confirm.click();
}
sleep(2);
if(isElementExist(driver, By.xpath("//*[@id=\"list\"]/aside[1]/div[1]/div[2]/aside[1]/a/img")) ==true ){
System.out.println("車圖元素存在!");
}else{
System.out.println("車圖元素未找到,失敗圖片:" + picturePath);
screenShot(picturePath,driver);//調用失敗截圖功能;
}
//用JS方法將滾動條定位到xx元素,其中auguments[0]就代表element
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();",driver.findElement(By.className("sort-order")));
WebElement productlist = driver.findElement(By.id("FirstPageProductList1"));
List<WebElement> products = productlist.findElements(By.className("list-header-ctr"));
//getText = products.get(0).findElements(By.className("company-name")).get(0).getText();
getText="123";
System.out.println("第一個套餐的名稱是:" + getText);
sleep(2);
List<WebElement> SeeInof = productlist.findElements(By.className("col-black"));
SeeInof.get(0).click();
sleep(2);
removeHandles(driver);
}
public void fenqidetails(WebDriver driver)
{
//getPackagename = driver.findElement(By.xpath("//*[@id=\"Content\"]/header/section/div/div[1]/h1")).getText();
getPackagename="123";
System.out.println("套餐名稱是:" + getPackagename);
try{
//assert getPackagename.contains(getText);
assert getPackagename==getText;
System.out.println("Test Pass");
}catch(Exception e){
e.printStackTrace();
}
}
//判斷元素是否存在
public static boolean isElementExist(WebDriver driver, By by) {
try {
driver.findElement(by);
return true;
} catch(NoSuchElementException e) {
return false;
}
}
// public static void removeHandles(WebDriver driver){
// //獲取win窗口
// try {
// String [] handles = new String[driver.getWindowHandles().size()];//定義一個空數組,數組大小是打開窗口的數量
// driver.getWindowHandles().toArray(handles);//將獲取到的句柄集合轉換為數組
// //here
// //driver.switchTo().window(handles[1]);
// sleep(2);
// //切換到舊窗口
// WebDriver handle2 = driver.switchTo().window(handles[0]);
// sleep(2);
// //關閉舊窗口
// handle2.close();
// sleep(2);
// //切換到新窗口
// driver.switchTo().window(handles[1]);
// sleep(2);
// } catch (ArrayIndexOutOfBoundsException e) {
// // TODO: handle exception
// System.out.println("瀏覽器切換窗口異常" + e);
// }
// }
public static void removeHandles(WebDriver driver){
//獲取當前頁面句柄
String handle = driver.getWindowHandle();
// 獲取所有頁面的句柄,並循環判斷不是當前的句柄
for (String handles : driver.getWindowHandles()) {
if (handles.equals(handle))
continue;
driver.switchTo().window(handles);
}
}
public static void screenShot(String path,WebDriver driver){
//將當前窗口截屏,獲得一個File的圖片文件對象;
File screenFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//將圖片文件對象,保存到指定的路徑path里,例如D盤的某個文件夾下。
try {
File file = new File(path); //在指定路徑中,創建一個空白的圖片文件
FileUtils.copyFile(screenFile,file);//將窗口截屏文件,保存到剛剛創建的空白文件里
} catch (IOException e) {
e.printStackTrace();
}
}
public static void sleep(double d) {
try {
d *= 1000;
Thread.sleep((int)d);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
16、經驗教訓
大小寫
包不全
元素定位
語句位置
ctrl+alt+L格式化代碼
ctrl+alt+O優化導入
