selenium最佳化後的測試程式碼
重新分裝了selenium的click事件,sendkeys事件,select事件,以及針對readonly的日期控制元件的處理. 下面有英文註釋,註釋很詳細,這裡不一一介紹了,
package com.common;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class PageObject {
WebDriver driver;
//Start the Chrome browser
public WebDriver startChrome(String urlString){
System.out.println("start Chrome browser...");
System.setProperty("webdriver.chrome.driver", "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe");
driver = new ChromeDriver();
driver.get(urlString);
System.out.println("start Chrome browser succeed...");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
return driver;
}
// Web-related click events
public void click(String selector,String pathValue){
switch (selector) {
case "id":
driver.findElement(By.id(pathValue)).click();
System.out.println("This test click event is used-id: "+pathValue);
break;
case "name":
driver.findElement(By.name(pathValue)).click();
System.out.println("This test click event is used-name: "+pathValue);
break;
case "xpath":
driver.findElement(By.xpath(pathValue)).click();
System.out.println("This test click event is used-xpath: "+pathValue);
break;
case "cssSelector":
driver.findElement(By.cssSelector(pathValue)).click();
System.out.println("This test click event is used-cssSelector: "+pathValue);
break;
case "className":
driver.findElement(By.className(pathValue)).click();
System.out.println("This test click event is used-className: "+pathValue);
break;
case "tagName":
driver.findElement(By.tagName(pathValue)).click();
System.out.println("This test click event is used-tagName: "+pathValue);
break;
case "linkText":
driver.findElement(By.linkText(pathValue)).click();
System.out.println("This test click event is used-linkText: "+pathValue);
break;
case "partialLinkText":
driver.findElement(By.partialLinkText(pathValue)).click();
System.out.println("This test click event is used-partialLinkText: "+pathValue);
break;
default:
System.out.println("Illegal selector: "+selector+" !!!");
break;
}
}
//Web-related sendKeys events
public void sendKeys(String selector,String pathValue,String sendkeys){
switch (selector) {
case "id":
driver.findElement(By.id(pathValue)).clear();
driver.findElement(By.id(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-id: "+pathValue);
System.out.println("By id senKeys value: "+sendkeys);
break;
case "name":
driver.findElement(By.name(pathValue)).clear();
driver.findElement(By.name(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-name: "+pathValue);
System.out.println("By name senKeys value: "+sendkeys);
break;
case "xpath":
driver.findElement(By.xpath(pathValue)).clear();
driver.findElement(By.xpath(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-xpath: "+pathValue);
System.out.println("By xpath senKeys value: "+sendkeys);
break;
case "linkText":
driver.findElement(By.linkText(pathValue)).clear();
driver.findElement(By.linkText(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-linkText: "+pathValue);
System.out.println("By linkText senKeys value: "+sendkeys);
break;
case "className":
driver.findElement(By.className(pathValue)).clear();
driver.findElement(By.className(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-className: "+pathValue);
System.out.println("By className senKeys value: "+sendkeys);
break;
case "tagName":
driver.findElement(By.tagName(pathValue)).clear();
driver.findElement(By.tagName(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-tagName: "+pathValue);
System.out.println("By tagName senKeys value: "+sendkeys);
break;
case "partialLinkText":
driver.findElement(By.partialLinkText(pathValue)).clear();
driver.findElement(By.partialLinkText(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-partialLinkText: "+pathValue);
System.out.println("By partialLinkText senKeys value: "+sendkeys);
break;
case "cssSelector":
driver.findElement(By.cssSelector(pathValue)).clear();
driver.findElement(By.cssSelector(pathValue)).sendKeys(sendkeys);
System.out.println("This test sendKeys event is used-cssSelector: "+pathValue);
System.out.println("By cssSelector senKeys value: "+sendkeys);
break;
default:
System.out.println("Illegal selector: "+selector+" !!!");
break;
}
}
//Web-related select events ,selectByVisibleText() method
public void select(String selector,String pathValue,String selectValue) {
switch (selector) {
case "id":
WebElement eId = driver.findElement(By.id(pathValue));
Select selectId = new Select(eId);
selectId.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-id: "+pathValue);
break;
case "name":
WebElement eName = driver.findElement(By.id(pathValue));
Select selectName = new Select(eName);
selectName.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-name: "+pathValue);
break;
case "xpath":
WebElement eXpath = driver.findElement(By.xpath(pathValue));
Select selectXpath = new Select(eXpath);
selectXpath.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-xpath: "+pathValue);
break;
case "cssSelector":
WebElement eCss = driver.findElement(By.cssSelector(pathValue));
Select selectCss = new Select(eCss);
selectCss.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-cssSelector: "+pathValue);
break;
case "className":
WebElement eClass = driver.findElement(By.className(pathValue));
Select selectClass = new Select(eClass);
selectClass.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-className: "+pathValue);
break;
case "tagName":
WebElement eTagName = driver.findElement(By.tagName(pathValue));
Select selectTagName = new Select(eTagName);
selectTagName.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-tagName: "+pathValue);
break;
case "linkText":
WebElement eLinkText = driver.findElement(By.linkText(pathValue));
Select selectLinkText = new Select(eLinkText);
selectLinkText.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-linkText: "+pathValue);
break;
case "partialLinkText":
WebElement epart = driver.findElement(By.partialLinkText(pathValue));
Select selectPart = new Select(epart);
selectPart.selectByVisibleText(selectValue);
System.out.println("The select value is: "+selectValue);
System.out.println("This test select event is used-partialLinkText: "+pathValue);
break;
default:
System.out.println("Illegal selector: "+selector+" !!!");
break;
}
}
// The web-related read-only date is set by id
public void selectDateById(String idpath,String date){
JavascriptExecutor removeAttribute = (JavascriptExecutor)driver;
//remove readonly attribute
removeAttribute.executeScript("var setDate=document.getElementById(""+idpath+"");setDate.removeAttribute('readonly');");
WebElement setDatElement=driver.findElement(By.id(idpath));
setDatElement.clear();
setDatElement.sendKeys(date);
}
// The web-related read-only date is set by name
public void selectDateByName(String namepath,String date){
JavascriptExecutor removeAttribute = (JavascriptExecutor)driver;
//remove readonly attribute
removeAttribute.executeScript("var setDate=document.getElementByName(""+namepath+"");setDate.removeAttribute('readonly');");
WebElement setDatElement=driver.findElement(By.name(namepath));
setDatElement.clear();
setDatElement.sendKeys(date);
}
// The web-related read-only date is set by ClassName
public void selectDateByClassName(String ClassNamepath,String date){
JavascriptExecutor removeAttribute = (JavascriptExecutor)driver;
//remove readonly attribute
removeAttribute.executeScript("var setDate=document.getElementsByClassName(""+ClassNamepath+"");setDate.removeAttribute('readonly');");
WebElement setDatElement=driver.findElement(By.className(ClassNamepath));
setDatElement.clear();
setDatElement.sendKeys(date);
}
}
下面是測試分裝程式碼的例項,使用的是testng框架
package com.common;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import com.common.PageObject;
public class NewTest {
PageObject PO= new PageObject();
@Test
public void f() throws Exception {
PO.sendKeys("name", "username", "admin");
PO.sendKeys("name", "password", "1234");
Thread.sleep(10000);
PO.click("className", "login-btn");
Thread.sleep(5000);
PO.click("linkText", "使用者管理");
Thread.sleep(5000);
PO.click("xpath", "//*[@id='mainPage']/div[3]/div[1]/div[2]/div/a[1]");
Thread.sleep(5000);
PO.select("xpath", "//*[@id='user-add-modal']/div/div/div[2]/form/div/div[3]/div[4]/div[1]/div/div/select", "集團公司");
String chenliriqi="addregisterDate";
String setdate="2015-5-25";
String dateString = "2018-5-15";
Thread.sleep(5000);
String qixian= "addbusinssAllotedTime";
PO.selectDateById(chenliriqi,setdate);
PO.selectDateById(qixian,dateString);
PO.click("xpath","//*[@id='user-add-modal']/div/div/div[3]/button[1]");
}
@BeforeMethod
public void beforeMethod() {
PO.startChrome("");
}
@AfterMethod
public void afterMethod() {
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1817/viewspace-2807715/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- selenium(web端)淘寶測試程式碼Web
- selenium測試心得
- 測試---selenium(5)
- Web應用程式測試的工具selenium用法詳解Web
- 自動化測試框架Selenium的使用——安裝Selenium框架
- 測試程式碼
- selenium自動化測試
- MYSQL程式碼顯示測試測試MySql
- 測試程式碼高亮
- Web 端的測試 Selenium 用法必備Web
- Selenium用法詳解 - - selenium自動化測試概要
- Selenium操作:測試form表單ORM
- Selenium自動化測試(3)
- selenium-測試框架搭建(十三)框架
- 程式碼寫作測試
- MySQL 分表最佳化試驗程式碼MySql
- 如何編寫優秀的測試程式碼|單元測試
- 《Android惡意程式碼分析與滲透測試》讀後感Android
- java+selenium 自動化測試Java
- 軟體測試--selenium 安裝使用
- 軟體測試--selenium安裝使用
- Selenium自動化測試網頁網頁
- selenium+python自動化測試Python
- 網站安全檢測 對帝國CMS程式碼的後臺功能性安全測試網站
- Selenium+Java自動化測試常用的方法Java
- 白盒測試程式碼應該怎麼測試
- 程式碼測試用例指南
- Selenium用法 - - 自動化測試介紹
- 自動化測試 selenium 環境搭建
- 無程式碼的啟動速度測試 with stagesepx
- 如何提高程式碼的可測試性
- 學會程式碼不是測試的終點而是測試開發的起點
- 介面自動化測試錄製工具,讓python selenium自動化測試指令碼開發更加方便Python指令碼
- 自動化測試如此容易!多語言自動化測試框架 Selenium 程式設計(C#篇)框架程式設計C#
- 測試開發之自動化篇-使用Selenium IDE錄製指令碼IDE指令碼
- 使用 xunit 編寫測試程式碼
- C# 測試程式碼#if DEBUG使用C#
- go 程式碼覆蓋率測試Go