重新封装了的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); } }
对上面的封装代码进行测试的小案例
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("http://124.193.90.194:8020/login.html"); } @AfterMethod public void afterMethod() { } }