exam
package com.my.test; import java.util.regex.Pattern; import java.util.concurrent.TimeUnit; import org.junit.*; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.internal.ProfilesIni; import org.openqa.selenium.support.ui.Select; public class RecordScript { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { ProfilesIni allProfiles = new ProfilesIni(); FirefoxProfile profile = allProfiles.getProfile("default"); driver = new FirefoxDriver(profile); //driver = new FirefoxDriver(); baseUrl = "https://my.worktile.com"; driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); driver.manage().window().maximize(); } @Test public void test1() throws Exception { Thread.sleep(5000); // driver.get("/"); driver.findElement(By.cssSelector("i.wtfont.wtf-create")).click(); Thread.sleep(1000); driver.findElement(By.xpath("(//a[contains(text(),'日程')])[2]")).click(); Thread.sleep(1000); driver.findElement(By.name("event_name")).clear(); driver.findElement(By.name("event_name")).sendKeys("date test"); driver.findElement(By.cssSelector("b")).click(); driver.findElement(By.xpath("//li[@id='ui-select-choices-row-0-7']/div/div")).click(); driver.findElement(By.name("start_date")).click(); driver.findElement(By.linkText("17")).click(); new Select(driver.findElement(By.xpath("//html[@id='ng-app']/body/div/div/div/div/div[2]/form/div[3]/div/div/span[2]/select"))).selectByVisibleText("10"); new Select(driver.findElement(By.xpath("//html[@id='ng-app']/body/div/div/div/div/div[2]/form/div[3]/div/div/span[2]/select[2]"))).selectByVisibleText("00"); driver.findElement(By.name("end_date")).click(); driver.findElement(By.linkText("18")).click(); new Select(driver.findElement(By.xpath("//html[@id='ng-app']/body/div/div/div/div/div[2]/form/div[3]/div/div[2]/span[2]/select"))).selectByVisibleText("10"); new Select(driver.findElement(By.xpath("//html[@id='ng-app']/body/div/div/div/div/div[2]/form/div[3]/div/div[2]/span[2]/select[2]"))).selectByVisibleText("00"); driver.findElement(By.xpath("//html[@id='ng-app']/body/div/div/div/div/div[2]/form/div[7]/button")).click(); Thread.sleep(1000); assertTrue(isElementPresent(By.xpath("//*[contains(text(), 'date test')]"))); } @After public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } private boolean isElementPresent(By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } private boolean isAlertPresent() { try { driver.switchTo().alert(); return true; } catch (NoAlertPresentException e) { return false; } } private String closeAlertAndGetItsText() { try { Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); if (acceptNextAlert) { alert.accept(); } else { alert.dismiss(); } return alertText; } finally { acceptNextAlert = true; } } }
package com.my.test; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; public class JavaExam { public static void main(String[] args) { // TODO Auto-generated method stub String folderPath=System.getProperty("user.dir")+File.separator+"log"; createDir(folderPath); File file=new File(folderPath+File.separator+"ML08_"+getCurrentTime()+".log"); try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String getCurrentTime(){ Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); String dateString = formatter.format(currentTime); return dateString; } public static boolean createDir(String p_destDirName) { File dir = new File(p_destDirName); if (dir.exists()) { System.out.println("创建目录" + p_destDirName + "失败,目标目录已经存在"); return false; } if (dir.mkdirs()) { System.out.println("创建目录" + p_destDirName + "成功!"); return true; } else { System.out.println("创建目录" + p_destDirName + "失败!"); return false; } } }
package com.my.test; import java.awt.AWTException; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.ui.Select; public class BussinessLib { //登录基础版方法 public void login(WebDriver driver,String p_user,String p_pwd){ driver.findElement(parseObject(ObjectStore.Login_LoginLink)).click(); driver.findElement(parseObject(ObjectStore.Login_Name)).clear(); driver.findElement(parseObject(ObjectStore.Login_Name)).sendKeys(p_user); driver.findElement(parseObject(ObjectStore.Login_Password)).clear(); driver.findElement(parseObject(ObjectStore.Login_Password)).sendKeys(p_pwd); driver.findElement(parseObject(ObjectStore.Login_Button)).click(); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //判断是否有广告出现 if (isElementPresent(driver,By.xpath("//*[@id='ng-app']/body/div[1]/div/div/div/div[1]/span/i"))) //xpath元素是广告右上角的* driver.findElement(By.xpath("//*[@id='ng-app']/body/div[1]/div/div/div/div[1]/span/i")).click(); } //可以多一个参数,也可以把项目中的任务、日历、文件、话题... 作为参数提取,但是代码会很冗长,所以不建议 public void createfile(WebDriver driver,String p_path,String p_project,String p_folder) throws Exception{ driver.findElement(By.linkText("项目")).click(); Thread.sleep(3000); driver.findElement(By.xpath("//span[contains(text(),\""+p_project+"\")]")).click(); Thread.sleep(3000); driver.findElement(By.linkText("文件")).click(); Thread.sleep(3000); driver.findElement(By.xpath("//a[contains(text(),\""+ p_folder+"\")]")).click(); Thread.sleep(3000); driver.findElement(By.xpath("//button[contains(text(), '添加文件')]")).click(); Thread.sleep(3000); RobotKeyboard.getInstance().type(p_path); //输入要上传的文件 Thread.sleep(1000); RobotKeyboard.getInstance().typeKey("Tab", 2); //点击2次tab按钮 ,焦点到windows窗口“打开” Thread.sleep(1000); RobotKeyboard.getInstance().typeKey("Enter", 1); //点击 回车按钮,上传文件 Thread.sleep(2000); } public By parseObject(String p_object) { String newObjecyt = null; if (p_object.startsWith(".//") || p_object.startsWith("//")) { return By.xpath(p_object); } else if (p_object.startsWith("link=") || p_object.startsWith("Link=")) { newObjecyt = p_object.substring(p_object.indexOf("=") + 1); return By.linkText(newObjecyt); } else if (p_object.startsWith("xpath=")) { newObjecyt = p_object.substring(p_object.indexOf("=") + 1); return By.xpath(newObjecyt); } else if (p_object.startsWith("id=")) { newObjecyt = p_object.substring(p_object.indexOf("=") + 1); return By.id(newObjecyt); } else if (p_object.startsWith("css=")) { newObjecyt = p_object.substring(p_object.indexOf("=") + 1); return By.cssSelector(newObjecyt); } else if (p_object.startsWith("class=")) { newObjecyt = p_object.substring(p_object.indexOf("=") + 1); return By.className(newObjecyt); } else if (p_object.startsWith("tagName=")) { newObjecyt = p_object.substring(p_object.indexOf("=") + 1); return By.tagName(newObjecyt); } else if (p_object.startsWith("name=")) { newObjecyt = p_object.substring(p_object.indexOf("=") + 1); return By.name(newObjecyt); } else return null; } public boolean isElementPresent(WebDriver driver,By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } public void logout(WebDriver driver){ driver.findElement(parseObject(ObjectStore.My_Avatar)).click(); driver.findElement(parseObject(ObjectStore.Logout_Link)).click(); System.out.println("***************退出登录***************"); } public void createSchedule(WebDriver driver,String p_content,String p_project,String p_startdate,String p_starttime,String p_enddate,String p_endtime,String p_location,String p_isrepeated,String p_user[]) throws InterruptedException{ String[] st=null; String[] et=null; //时间格式 10:30 if(p_starttime.length()==5&&p_starttime.contains(":")) { st=p_starttime.split(":"); }else { System.out.println("错误的参数输入"); return; } if(p_endtime.length()==5&&p_endtime.contains(":")) { et=p_endtime.split(":"); }else { System.out.println("错误的参数输入"); return; } driver.findElement(By.xpath(".//*[@id='btn_leftmenu_shortcut_create']/i")).click(); Thread.sleep(1000); driver.findElement(By.xpath("(//a[contains(text(),'日程')])[2]")).click(); Thread.sleep(1000); driver.findElement(By.name("event_name")).clear(); driver.findElement(By.name("event_name")).sendKeys(p_content); driver.findElement(By.cssSelector("b")).click(); //driver.findElement(By.xpath("//li[@id='ui-select-choices-row-0-7']/div/div")).click(); driver.findElement(By.xpath("//*[@id='ui-select-choices-0']/li/div/div[contains(text(), "+p_project+")]")).click(); driver.findElement(parseObject(ObjectStore.CreateSchedule_StartTime)).click(); driver.findElement(By.linkText(p_startdate)).click(); new Select(driver.findElement(By.xpath("//html[@id='ng-app']/body/div/div/div/div/div[2]/form/div[3]/div/div/span[2]/select"))).selectByVisibleText(st[0]); new Select(driver.findElement(By.xpath("//html[@id='ng-app']/body/div/div/div/div/div[2]/form/div[3]/div/div/span[2]/select[2]"))).selectByVisibleText(st[1]); driver.findElement(By.name("end_date")).click(); driver.findElement(By.linkText(p_enddate)).click(); new Select(driver.findElement(By.xpath("//html[@id='ng-app']/body/div/div/div/div/div[2]/form/div[3]/div/div[2]/span[2]/select"))).selectByVisibleText(et[0]); new Select(driver.findElement(By.xpath("//html[@id='ng-app']/body/div/div/div/div/div[2]/form/div[3]/div/div[2]/span[2]/select[2]"))).selectByVisibleText(et[1]); driver.findElement(By.name("event_location")).clear(); driver.findElement(By.name("event_location")).sendKeys(p_location); new Select(driver.findElement(By.xpath("//html[@id='ng-app']/body/div/div/div/div/div[2]/form/div[5]/select"))).selectByVisibleText(p_isrepeated); for(int i=0;i<p_user.length;i++) { driver.findElement(By.cssSelector("span.o")).click(); driver.findElement(By.name("search_user_input")).clear(); driver.findElement(By.name("search_user_input")).sendKeys(p_user[i]); driver.findElement(By.cssSelector("span.avatar-atname")).click(); Thread.sleep(1000); try { RobotKeyboard.getInstance().typeKey("ESC", 1); } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace(); } //点击 回车按钮,上传文件 Thread.sleep(1000); } driver.findElement(By.xpath("//html[@id='ng-app']/body/div/div/div/div/div[2]/form/div[7]/button")).click(); } }
package com.my.test; import java.util.regex.Pattern; import java.util.concurrent.TimeUnit; import org.junit.*; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.internal.ProfilesIni; import org.openqa.selenium.support.ui.Select; public class CreateScheduleTest { private WebDriver driver; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { ProfilesIni allProfiles = new ProfilesIni(); FirefoxProfile profile = allProfiles.getProfile("default"); driver = new FirefoxDriver(profile); //driver = new FirefoxDriver(); String baseUrl = "https://my.worktile.com"; driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); driver.manage().window().maximize(); driver.get(baseUrl); } @Test public void test1() throws Exception { BussinessLib bl=new BussinessLib(); bl.login(driver, "ml0tester", "123456"); assertTrue(bl.isElementPresent(driver,bl.parseObject(ObjectStore.Quick_Create))); //bl.createSchedule(driver, p_content, p_project, p_startdate, p_starttime, p_enddate, p_endtime, p_location, p_isrepeated, p_user); //assertTrue(bl.isElementPresent(driver,By.xpath("//*[contains(text(), 'date test')]"))); bl.logout(driver); } @After public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } }
package com.my.test; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; public class JavaExam { public static void main(String[] args) { // TODO Auto-generated method stub String folderPath=System.getProperty("user.dir")+File.separator+"log"; createDir(folderPath); File file=new File(folderPath+File.separator+"ML08_"+getCurrentTime()+".log"); try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String getCurrentTime(){ Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); String dateString = formatter.format(currentTime); return dateString; } public static boolean createDir(String p_destDirName) { File dir = new File(p_destDirName); if (dir.exists()) { System.out.println("创建目录" + p_destDirName + "失败,目标目录已经存在"); return false; } if (dir.mkdirs()) { System.out.println("创建目录" + p_destDirName + "成功!"); return true; } else { System.out.println("创建目录" + p_destDirName + "失败!"); return false; } } }
//设计该类的目的是对selenium无法进行对象输入字符情况时提供处理方法,该类为单例模式,提供字符的输入和特殊按键的输入 package com.my.test; import static java.awt.event.KeyEvent.*; import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; public class RobotKeyboard { private static RobotKeyboard uniqueInstance = null; private Robot robot; public RobotKeyboard() throws AWTException { this.robot = new Robot(); } public static synchronized RobotKeyboard getInstance() throws AWTException { //单例模式的应用 if (uniqueInstance == null) { uniqueInstance = new RobotKeyboard(); } return uniqueInstance; } //提供字符串的输入 public void type(CharSequence characters) { int length = characters.length(); for (int i = 0; i < length; i++) { char character = characters.charAt(i); type(character); } } //提供键盘中特殊按键的输入,p_string键名称,p_times按键次数 public void typeKey(String p_string,int p_times) { if (p_string.equalsIgnoreCase("Tab")) doTypeKey(KeyEvent.VK_TAB,p_times); else if (p_string.equalsIgnoreCase("Enter")) doTypeKey(KeyEvent.VK_ENTER,p_times); else if (p_string.equalsIgnoreCase("Up")) doTypeKey(KeyEvent.VK_UP,p_times); else if (p_string.equalsIgnoreCase("Down")) doTypeKey(KeyEvent.VK_DOWN,p_times); else if (p_string.equalsIgnoreCase("Left")) doTypeKey(KeyEvent.VK_LEFT,p_times); else if (p_string.equalsIgnoreCase("Right")) doTypeKey(KeyEvent.VK_RIGHT,p_times); else if (p_string.equalsIgnoreCase("Shift")) doTypeKey(KeyEvent.VK_SHIFT,p_times); else if (p_string.equalsIgnoreCase("ESC")) doTypeKey(KeyEvent.VK_ESCAPE,p_times); else if (p_string.equalsIgnoreCase("Backspace")) doTypeKey(KeyEvent.VK_BACK_SPACE,p_times); else doTypeKey(0,p_times); } //提供字符的输入 public void type(char character) { switch (character) { case 'a': doType(VK_A); break; case 'b': doType(VK_B); break; case 'c': doType(VK_C); break; case 'd': doType(VK_D); break; case 'e': doType(VK_E); break; case 'f': doType(VK_F); break; case 'g': doType(VK_G); break; case 'h': doType(VK_H); break; case 'i': doType(VK_I); break; case 'j': doType(VK_J); break; case 'k': doType(VK_K); break; case 'l': doType(VK_L); break; case 'm': doType(VK_M); break; case 'n': doType(VK_N); break; case 'o': doType(VK_O); break; case 'p': doType(VK_P); break; case 'q': doType(VK_Q); break; case 'r': doType(VK_R); break; case 's': doType(VK_S); break; case 't': doType(VK_T); break; case 'u': doType(VK_U); break; case 'v': doType(VK_V); break; case 'w': doType(VK_W); break; case 'x': doType(VK_X); break; case 'y': doType(VK_Y); break; case 'z': doType(VK_Z); break; case 'A': doType(VK_SHIFT, VK_A); break; case 'B': doType(VK_SHIFT, VK_B); break; case 'C': doType(VK_SHIFT, VK_C); break; case 'D': doType(VK_SHIFT, VK_D); break; case 'E': doType(VK_SHIFT, VK_E); break; case 'F': doType(VK_SHIFT, VK_F); break; case 'G': doType(VK_SHIFT, VK_G); break; case 'H': doType(VK_SHIFT, VK_H); break; case 'I': doType(VK_SHIFT, VK_I); break; case 'J': doType(VK_SHIFT, VK_J); break; case 'K': doType(VK_SHIFT, VK_K); break; case 'L': doType(VK_SHIFT, VK_L); break; case 'M': doType(VK_SHIFT, VK_M); break; case 'N': doType(VK_SHIFT, VK_N); break; case 'O': doType(VK_SHIFT, VK_O); break; case 'P': doType(VK_SHIFT, VK_P); break; case 'Q': doType(VK_SHIFT, VK_Q); break; case 'R': doType(VK_SHIFT, VK_R); break; case 'S': doType(VK_SHIFT, VK_S); break; case 'T': doType(VK_SHIFT, VK_T); break; case 'U': doType(VK_SHIFT, VK_U); break; case 'V': doType(VK_SHIFT, VK_V); break; case 'W': doType(VK_SHIFT, VK_W); break; case 'X': doType(VK_SHIFT, VK_X); break; case 'Y': doType(VK_SHIFT, VK_Y); break; case 'Z': doType(VK_SHIFT, VK_Z); break; case '`': doType(VK_BACK_QUOTE); break; case '0': doType(VK_0); break; case '1': doType(VK_1); break; case '2': doType(VK_2); break; case '3': doType(VK_3); break; case '4': doType(VK_4); break; case '5': doType(VK_5); break; case '6': doType(VK_6); break; case '7': doType(VK_7); break; case '8': doType(VK_8); break; case '9': doType(VK_9); break; case '-': doType(VK_MINUS); break; case '_': doType(VK_SHIFT,VK_MINUS); break; case '=': doType(VK_EQUALS); break; case '~': doType(VK_SHIFT, VK_BACK_QUOTE); break; case '!': doType(VK_EXCLAMATION_MARK); break; case '@': doType(VK_AT); break; case '#': doType(VK_NUMBER_SIGN); break; case '$': doType(VK_DOLLAR); break; case '%': doType(VK_SHIFT, VK_5); break; case '^': doType(VK_CIRCUMFLEX); break; case '&': doType(VK_AMPERSAND); break; case '*': doType(VK_ASTERISK); break; case '(': doType(VK_LEFT_PARENTHESIS); break; case ')': doType(VK_RIGHT_PARENTHESIS); break; //case '_': doType(VK_UNDERSCORE); break; case '+': doType(VK_PLUS); break; case '\t': doType(VK_TAB); break; case '\n': doType(VK_ENTER); break; case '[': doType(VK_OPEN_BRACKET); break; case ']': doType(VK_CLOSE_BRACKET); break; case '\\': doType(VK_BACK_SLASH); break; case '{': doType(VK_SHIFT, VK_OPEN_BRACKET); break; case '}': doType(VK_SHIFT, VK_CLOSE_BRACKET); break; case '|': doType(VK_SHIFT, VK_BACK_SLASH); break; case ';': doType(VK_SEMICOLON); break; case ':': doType(VK_SHIFT, VK_SEMICOLON); break; case '\'': doType(VK_QUOTE); break; case '"': doType(VK_QUOTEDBL); break; case ',': doType(VK_COMMA); break; case '<': doType(VK_LESS); break; case '.': doType(VK_PERIOD); break; case '>': doType(VK_GREATER); break; case '/': doType(VK_SLASH); break; case '?': doType(VK_SHIFT, VK_SLASH); break; case ' ': doType(VK_SPACE); break; default: throw new IllegalArgumentException("Cannot type character " + character); } } private void doType(int keyCodes) { if(keyCodes==0) { return; } robot.keyPress(keyCodes); robot.keyRelease(keyCodes); } private void doType(int keyShit,int keyCodes) { if(keyCodes==0) { return; } robot.keyPress(keyShit); robot.keyPress(keyCodes); robot.keyRelease(keyCodes); robot.keyRelease(keyShit); } private void doTypeKey(int keyCodes,int times) { if(keyCodes==0) { return; } for(int i=0;i<times;i++){ robot.keyPress(keyCodes); robot.keyRelease(keyCodes); } } }