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
- Web應用程式測試的工具selenium用法詳解Web
- 測試程式碼
- MYSQL程式碼顯示測試測試MySql
- 自動化測試框架Selenium的使用——安裝Selenium框架
- 測試你的前端程式碼:視覺化測試前端視覺化
- 測試程式碼高亮
- Web 端的測試 Selenium 用法必備Web
- 測試你的前端程式碼 – part4(整合測試)前端
- 安卓 unit 測試與 instrument 測試的程式碼共享安卓
- selenium-測試框架搭建(十三)框架
- selenium java自動化測試Java
- 利用Selenium搭建Nunit測試框架框架
- selenium grid 測試資料
- Selenium自動化測試框架框架
- selenium開源功能測試工具
- Selenium操作:測試form表單ORM
- Selenium用法詳解 - - selenium自動化測試概要
- 程式碼寫作測試
- 代理類測試程式碼
- phpunit測試成功phpunit測試實踐程式碼PHP
- 如何編寫優秀的測試程式碼|單元測試
- 測試你的前端程式碼 - part2(單元測試)前端
- 測試你的前端程式碼 – part2(單元測試)前端
- MySQL 分表最佳化試驗程式碼MySql
- Selenium (1) —— Selenium安裝與測試(101 Tutorial)
- Selenium自動化測試網頁網頁
- 軟體測試--selenium安裝使用
- 軟體測試--selenium 安裝使用
- java+selenium 自動化測試Java
- selenium自動化測試面試集合面試
- Python Unitest 使用 selenium 測試WebPythonWeb
- 利用 Selenium 自動化 web 測試Web
- selenium 測試框架中使用grid框架
- 編寫可測試的 JavaSript 程式碼Java
- 編寫可測試的 JavaScript 程式碼JavaScript
- 最簡單的jquery測試程式碼jQuery
- 白盒測試程式碼應該怎麼測試