新增输入与选择页面的html源码:
<div style="margin-top:-60px;" class="modal-content" id="modalContent">
<div class="modal-head">
<a title="关闭" style="margin-right:20px;" data-dismiss="modal" class="close" href="javascript:void(0);">×</a>
<h6> </h6>
</div>
<div id="contentBody" class="modal-body">
<!-- 查询条件 -->
<form id="newForm" method="post" action="">
<input type="hidden" id="addTagHidden">
<input type="hidden" name="tagCode" id="tagCodeHidden">
<table style="width:100%;" id="tb_gbcBiddingCatalogueNew" class="table table-bordered">
<colgroup>
<col width="20%">
<col width="30%">
<col width="20%">
<col width="30%">
</colgroup>
<tbody>
<tr>
<td class="text-right"><span style="color:red">*</span>药易编码:</td>
<td><input type="text" required="required" validatetype="valiSymbol" name="code2" id="newCode2" class="form-control"></td>
<td class="text-right"><span style="color:red">*</span>统编代码:</td>
<td><input type="text" required="required" readonly="readonly" validatetype="valiSymbol" name="code" id="newCode" class="form-control"></td>
</tr>
<tr>
<td class="text-right"><span style="color:red">*</span>药品通用名:</td>
<td><input type="text" required="required" readonly="readonly" validatetype="valiSymbol" name="genericName" id="newGenericName" class="form-control"></td>
<td class="text-right"><span style="color:red">*</span>通用名拼音码:</td>
<td><input type="text" required="required" readonly="readonly" validatetype="valiSymbol" name="pinyin" id="newPinyin" class="form-control"></td>
</tr>
<tr>
<td class="text-right">商品名:</td>
<td><input type="text" readonly="readonly" validatetype="valiSymbol" name="brand" id="newBrand" class="form-control"></td>
<td class="text-right">商品名拼音码:</td>
<td><input type="text" readonly="readonly" validatetype="valiSymbol" name="brandPinyin" id="newBrandPinyin" class="form-control"></td>
</tr>
<tr>
<td class="text-right"><span style="color:red">*</span>剂型:</td>
<td><input type="text" required="required" readonly="readonly" validatetype="valiSymbol" name="dosageForm" id="newDosageForm" class="form-control"></td>
<td class="text-right"><span style="color:red">*</span>制剂规格:</td>
<td><input type="text" required="required" readonly="readonly" validatetype="valiSymbolModel" name="dosageModel" id="newDosageModel" class="form-control"></td>
</tr>
<tr>
<td class="text-right"><span style="color:red">*</span>包装规格:</td>
<td><input type="text" required="required" readonly="readonly" validatetype="valiSymbolModel" id="newPackModel" name="packModel" class="form-control"></td>
<td class="text-right"><span style="color:red">*</span>包装单位:</td>
<td><input type="text" required="required" readonly="readonly" validatetype="valiSymbol" id="newPackUnit" name="packUnit" class="form-control"></td>
</tr>
<tr>
<td class="text-right"><span style="color:red">*</span>生产企业:</td>
<td><input type="text" required="required" readonly="readonly" validatetype="valiPosiFloat" name="producerName" id="newProducerName" class="form-control"></td>
<td class="text-right">原统编代码:</td>
<td><input type="text" placeholder="新码替换旧码时请填写" validatetype="valiSymbol" name="oldTbdm" id="newOldTbdm" class="form-control"></td>
</tr>
<tr>
<td class="text-right"><span style="color:red">*</span>价格类型:</td>
<td>
<!-- 价格类型(1最高零售价;2无最高零售价;3唯一中标价4浮动中标价5挂网采购价6低价药7无采购价规则) -->
<select class="form-control" name="priceType" id="newPriceType">
<option value="1">最高零售价</option>
<option value="2">无最高零售价</option>
<option value="3">唯一中标价</option>
<option value="4">浮动中标价</option>
<option value="5">挂网采购价</option>
<option value="6">低价药</option>
<option value="7">无采购价规则</option>
</select>
</td>
<td class="text-right">价格:</td>
<td><input type="text" validatetype="valiPosiFloat" id="newPrice" name="price" class="form-control"></td>
</tr>
<tr>
<td class="text-right"><span style="color:red">*</span>生效日期:</td>
<td>
<div style="width: 100%;" class="input-medium date datetime">
<input type="text" required="required" readonly="" class="form-control datetime_from" size="10" id="newEffectiveTime" name="effectiveTime">
<span class="add-on"><i class="icon-remove"></i></span><span class="add-on"><i class="icon-th"></i></span>
</div>
</td>
<td class="text-right"><span style="color:red">*</span>失效日期:</td>
<td>
<div style="width: 100%;" class="input-medium date datetime">
<input type="text" required="required" readonly="" class="form-control datetime_to" size="10" id="newExpiryTime" name="expiryTime">
<span class="add-on"><i class="icon-remove"></i></span><span class="add-on"><i class="icon-th"></i></span>
</div>
</td>
</tr>
<tr>
<td class="text-right"><span style="color:red">*</span>新增依据:</td>
<td colspan="3"><input type="text" validatetype="valiSymbol" required="required" id="newRemarks" name="remarks" class="form-control"></td>
</tr>
<!-- 添加标签功能暂时屏蔽掉 -->
<!-- <tr> -->
<!-- <td colspan="4"><a href="javascript:void(0);" class="btnAddTags" >添加标签</a></td> -->
<!-- </tr> -->
<tr style="display:none" id="tagsTR">
<td id="tagsTD" colspan="4">
</td></tr>
</tbody>
</table>
<div style="width:100%;text-align:center;padding:20 0 0 0;">
<input type="button" style="width:120px;" id="btnNewSave" value="提交" class="btn btn-primary btn-sm">
<input type="button" style="width:120px;margin-left:30px;" id="btnNewCancel" value="取消" class="btn btn-primary btn-sm">
</div>
</form>
<!-- 弹出窗口 -->
<div id="showModalNew" role="dialog" tabindex="-1" class="modal fade">
<div class="modal-dialog modal-lg">
<div style="width:500px" class="modal-content" id="modalContent">
<div class="modal-head">
<h6> </h6>
</div>
<div id="contentBodyNew" class="modal-body"></div>
</div>
</div>
</div>
</div>
</div>
对新增输入与选择页面的所有功能进行封装:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class EntryGbcData {
WebElement newCode2;
WebElement newOldTbdm;
WebElement newPriceType;
WebElement newPrice;
WebElement newEffectiveTime;
WebElement newExpiryTime;
WebElement newRemarks;
WebElement btnNewSave;
WebElement btnNewCancel;
List<WebElement> parentDateWidget;
public void findelement(WebDriver driver) {
newCode2 = driver.findElement(By.id("newCode2"));
newOldTbdm = driver.findElement(By.id("newOldTbdm"));
newPriceType = driver.findElement(By.id("newPriceType"));
newPrice = driver.findElement(By.id("newPrice"));
newEffectiveTime = driver
.findElement(By.xpath("/html/body/div[6]/div/div/div[2]/form/table/tbody/tr[8]/td[2]/div/span[2]/i"));
newExpiryTime = driver
.findElement(By.xpath("/html/body/div[6]/div/div/div[2]/form/table/tbody/tr[8]/td[4]/div/span[2]/i"));
parentDateWidget = driver
.findElements(By.cssSelector(".datetimepicker.datetimepicker-dropdown-bottom-right.dropdown-menu"));
newRemarks = driver.findElement(By.id("newRemarks"));
btnNewSave = driver.findElement(By.id("btnNewSave"));
btnNewCancel = driver.findElement(By.id("btnNewCancel"));
}
/**
* 提供中标目录的输入和选择功能
*
* @param driver
* @param yybm
* 药易编码
* @param priceType
* 价格类型
* @param price
* 价格
* @param effectiveTime
* 生效日期
* @param expiryTime
* 失效日期
* @param xzyj
* 新增依据
* @throws Exception
*/
public void entryData(WebDriver driver, String yybm, String priceType, String price, String effectiveTime,
String expiryTime, String xzyj) throws Exception {
WebDriverWait wait = new WebDriverWait(driver, 10);
findelement(driver);
// 输入药易编码
newCode2.sendKeys(yybm);
// 选择价格类型
Select select = new Select(newPriceType);
select.selectByVisibleText(priceType);
// 输入价格
newPrice.sendKeys(price);
// 选择生效日期
newEffectiveTime.click();
// 点击后等待日期控件可见
wait.until(ExpectedConditions
.visibilityOfElementLocated(By.xpath("/html/body/div[8]/div[3]/table/thead/tr[1]/th[2]")));
SelectDate selectDate = new SelectDate();
// 选择生效日期
selectDate.dateSelect(effectiveTime, parentDateWidget.get(0));
// 选择失效日期
selectDate.dateSelect(expiryTime, parentDateWidget.get(1));
// 输入新增依据
newRemarks.sendKeys(xzyj);
}
/**
* 提供保存功能
*
* @param driver
*/
public void save(WebDriver driver) {
findelement(driver);
btnNewSave.click();
}
/**
* 提供取消功能
*
* @param driver
*/
public void cancel(WebDriver driver) {
findelement(driver);
btnNewCancel.click();
}
}
日期控件的html源码:
<div class="datetimepicker datetimepicker-dropdown-bottom-right dropdown-menu" style="left: 770.05px; z-index: 10009; display: block; top: 396.2px;"><div class="datetimepicker-minutes" style="display: none;"><table class=" table-condensed"><thead><tr><th class="prev" style="visibility: visible;"><i class="icon-arrow-left"></i></th><th class="switch" colspan="5">11 10 2016</th><th class="next" style="visibility: visible;"><i class="icon-arrow-right"></i></th></tr></thead><tbody><tr><td colspan="7"><span class="minute">18:00</span><span class="minute active">18:05</span><span class="minute">18:10</span><span class="minute">18:15</span><span class="minute">18:20</span><span class="minute">18:25</span><span class="minute">18:30</span><span class="minute">18:35</span><span class="minute">18:40</span><span class="minute">18:45</span><span class="minute">18:50</span><span class="minute">18:55</span></td></tr></tbody><tfoot><tr><th class="today" colspan="7">今天</th></tr></tfoot></table></div><div class="datetimepicker-hours" style="display: none;"><table class=" table-condensed"><thead><tr><th class="prev" style="visibility: visible;"><i class="icon-arrow-left"></i></th><th class="switch" colspan="5">11 10 2016</th><th class="next" style="visibility: visible;"><i class="icon-arrow-right"></i></th></tr></thead><tbody><tr><td colspan="7"><span class="hour">0:00</span><span class="hour">1:00</span><span class="hour">2:00</span><span class="hour">3:00</span><span class="hour">4:00</span><span class="hour">5:00</span><span class="hour">6:00</span><span class="hour">7:00</span><span class="hour">8:00</span><span class="hour">9:00</span><span class="hour">10:00</span><span class="hour">11:00</span><span class="hour">12:00</span><span class="hour">13:00</span><span class="hour">14:00</span><span class="hour">15:00</span><span class="hour">16:00</span><span class="hour">17:00</span><span class="hour active">18:00</span><span class="hour">19:00</span><span class="hour">20:00</span><span class="hour">21:00</span><span class="hour">22:00</span><span class="hour">23:00</span></td></tr></tbody><tfoot><tr><th class="today" colspan="7">今天</th></tr></tfoot></table></div><div class="datetimepicker-days" style="display: block;"><table class=" table-condensed"><thead><tr><th class="prev" style="visibility: visible;"><i class="icon-arrow-left"></i></th><th class="switch" colspan="5">10 2016</th><th class="next" style="visibility: visible;"><i class="icon-arrow-right"></i></th></tr><tr><th class="dow">日</th><th class="dow">一</th><th class="dow">二</th><th class="dow">三</th><th class="dow">四</th><th class="dow">五</th><th class="dow">六</th></tr></thead><tbody><tr><td class="day old">25</td><td class="day old">26</td><td class="day old">27</td><td class="day old">28</td><td class="day old">29</td><td class="day old">30</td><td class="day">1</td></tr><tr><td class="day">2</td><td class="day">3</td><td class="day">4</td><td class="day">5</td><td class="day">6</td><td class="day">7</td><td class="day">8</td></tr><tr><td class="day">9</td><td class="day">10</td><td class="day active">11</td><td class="day">12</td><td class="day">13</td><td class="day">14</td><td class="day">15</td></tr><tr><td class="day">16</td><td class="day">17</td><td class="day">18</td><td class="day">19</td><td class="day">20</td><td class="day">21</td><td class="day">22</td></tr><tr><td class="day">23</td><td class="day">24</td><td class="day">25</td><td class="day">26</td><td class="day">27</td><td class="day">28</td><td class="day">29</td></tr><tr><td class="day">30</td><td class="day">31</td><td class="day new">1</td><td class="day new">2</td><td class="day new">3</td><td class="day new">4</td><td class="day new">5</td></tr></tbody><tfoot><tr><th class="today" colspan="7">今天</th></tr></tfoot></table></div><div class="datetimepicker-months" style="display: none;"><table class="table-condensed"><thead><tr><th class="prev" style="visibility: visible;"><i class="icon-arrow-left"></i></th><th class="switch" colspan="5">2016</th><th class="next" style="visibility: visible;"><i class="icon-arrow-right"></i></th></tr></thead><tbody><tr><td colspan="7"><span class="month">一月</span><span class="month">二月</span><span class="month">三月</span><span class="month">四月</span><span class="month">五月</span><span class="month">六月</span><span class="month">七月</span><span class="month">八月</span><span class="month">九月</span><span class="month">十月</span><span class="month">十一月</span><span class="month active">十二月</span></td></tr></tbody><tfoot><tr><th class="today" colspan="7">今天</th></tr></tfoot></table></div><div class="datetimepicker-years" style="display: none;"><table class="table-condensed"><thead><tr><th class="prev" style="visibility: visible;"><i class="icon-arrow-left"></i></th><th class="switch" colspan="5">2010-2019</th><th class="next" style="visibility: visible;"><i class="icon-arrow-right"></i></th></tr></thead><tbody><tr><td colspan="7"><span class="year old">2009</span><span class="year">2010</span><span class="year">2011</span><span class="year">2012</span><span class="year">2013</span><span class="year">2014</span><span class="year">2015</span><span class="year active">2016</span><span class="year">2017</span><span class="year">2018</span><span class="year">2019</span><span class="year old">2020</span></td></tr></tbody><tfoot><tr><th class="today" colspan="7">今天</th></tr></tfoot></table></div></div>
对日期控件的所有功能进行封装:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class SelectDate {
/**
* 提供日期的选择功能
*
* @param time
* 日期:例如:2016-6-6,注意年月日首位不要加0
* @param parentElement
* 日期控件的最*的父级节点
*/
public void dateSelect(String time, WebElement parentElement) {
// 对年月日以"-"进行切割,日期格式"2016-06-06"
String[] strings = time.split("-");
// 从日的选择切换到月的选择
parentElement.findElement(By.cssSelector(".datetimepicker-days .switch")).click();
// 从月的选择切换到年的选择
parentElement.findElement(By.cssSelector(".datetimepicker-months .switch")).click();
// 开关:用于控制日期向前翻还是向后翻,1为向前翻,2为向后翻
int action = 0;
while (true) {
// 当前年选择页面的年最小值
String minYear = parentElement.findElement(By.cssSelector(".datetimepicker-years .switch")).getText()
.substring(0, 4);
// 当前年选择页面的年最大值
String maxYear = parentElement.findElement(By.cssSelector(".datetimepicker-years .switch")).getText()
.substring(5);
// 当用户选择的年小于当前年选择页面的年最小值时,将开关置为向前
if (Integer.parseInt(strings[0]) < Integer.parseInt(minYear)) {
action = 1;
// 当用户选择的年大于当前年选择页面的年最大值时,将开关置为向后
} else if (Integer.parseInt(strings[0]) > Integer.parseInt(maxYear)) {
action = 2;
// 当用户选择的年在当前年选择页面的最小值与最大值之间时,跳出整个while循环
} else if (Integer.parseInt(minYear) <= Integer.parseInt(strings[0])
&& Integer.parseInt(strings[0]) <= Integer.parseInt(maxYear)) {
break;
}
// 控制年翻页向前向后翻
switch (action) {
case 1:
parentElement.findElement(By.cssSelector(".datetimepicker-years .icon-arrow-left")).click();
break;
case 2:
parentElement.findElement(By.cssSelector(".datetimepicker-years .icon-arrow-right")).click();
break;
}
}
// 获取所有的可选的年
WebElement yearDate = parentElement.findElement(By.cssSelector(".datetimepicker-years td"));
List<WebElement> years = yearDate.findElements(By.tagName("span"));
// 当前可选年范围的最小的年的值
String minData = years.get(0).getText();
// 获取年list的数组下标
int yearIndex = Integer.parseInt(strings[0]) - Integer.parseInt(minData);
// 选择年
years.get(yearIndex).click();
// 获取所有可选的月
WebElement mounthDate = parentElement.findElement(By.cssSelector(".datetimepicker-months td"));
List<WebElement> mounths = mounthDate.findElements(By.tagName("span"));
// 获取月list的数组下标
int mounthIndex = Integer.parseInt(strings[1]) - 1;
// 选择月
mounths.get(mounthIndex).click();
// 获取所有的可选的日,由于webDriver并未提供对单个、多个同一className的定位方法,需要如下处理
WebElement dayData = parentElement.findElement(By.cssSelector(".datetimepicker-days"));
// 获取当月之外的日
List<WebElement> clearDays = dayData.findElements(By.cssSelector(".day.old,.day.new"));
// 获取当前可见的所有的日
List<WebElement> days = dayData.findElements(By.cssSelector(".day"));
// 从当前可见的所有的日中移除当月之外的日,即得到当月所有的日
days.removeAll(clearDays);
// 获取日list的数组下标
int dayIndex = Integer.parseInt(strings[2]) - 1;
// 选择日
days.get(dayIndex).click();
}
}