selenium demo(一)
(一)添加功能,利用testng框架
1 package test; 2 3 4 5 import org.openqa.selenium.*; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.openqa.selenium.interactions.Actions; 8 import org.openqa.selenium.support.ui.ExpectedCondition; 9 10 import org.openqa.selenium.support.ui.WebDriverWait; 11 import org.testng.annotations.Test; 12 13 import java.util.concurrent.TimeUnit; 14 15 16 public class AutoTest { 17 @Test 18 public void test(){ 19 System.setProperty("webdriver.chrome.driver","D:/selenium-drivers/chromedriver.exe"); 20 WebDriver dr = new ChromeDriver(); 21 dr.manage().window().maximize(); 22 dr.get("http://xxx.xxx.xxx.xxx:xxxx/xxx"); 23 //页面初始化超时 24 // webDriver.manage().timeouts().pageLoadTimeout(1,TimeUnit.SECONDS); 25 //隐形等待,页面超时 26 dr.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); 27 28 29 //登录 30 dr.findElement(By.id("loginUser.id")).sendKeys("xxxxxx"); 31 dr.findElement(By.id("loginUser.pass")).sendKeys("xxxxxx"); 32 dr.findElement(By.className("submitLink")).click(); 33 dr.findElement(By.xpath("//*[@id='guard-base']/a")).click(); 34 dr.findElement(By.xpath("//*[@id='guard-base']/ul/li[1]/a")).click(); 35 dr.switchTo().frame("main-frame"); 36 dr.findElement(By.className("addGuard")).click(); 37 (new WebDriverWait(dr,3,1)).until(new ExpectedCondition<Boolean>() { 38 @Override 39 public Boolean apply(WebDriver driver) { 40 return driver.findElement(By.id("guardName")).isDisplayed(); 41 } 42 }); 43 //输入姓名 44 WebElement el_name = dr.findElement(By.id("guardName")); 45 el_name.sendKeys("test"); 46 el_name.sendKeys(Keys.CONTROL+"a"); 47 el_name.sendKeys(Keys.CONTROL+"c"); 48 //选择状态 49 dr.findElement(By.id("statusSpan-myselect")).click(); 50 dr.findElement(By.xpath("//*[@id='statusSpan-myselect']/div/ul/li[1]")).click(); 51 (new WebDriverWait(dr,2)).until(new ExpectedCondition<Boolean>() { 52 @Override 53 public Boolean apply(WebDriver driver) { 54 return driver.findElement(By.id("org")).isDisplayed(); 55 } 56 }); 57 //输入机构 58 WebElement el_org = dr.findElement(By.id("org")); 59 el_org.sendKeys(Keys.CONTROL+"v"); 60 //选择性别 61 dr.findElement(By.id("sexSpan-myselect")).click(); 62 dr.findElement(By.xpath("//*[@id='sexSpan-myselect']/div/ul/li[1]")).click(); 63 (new WebDriverWait(dr,2)).until(new ExpectedCondition<Boolean>() { 64 @Override 65 public Boolean apply(WebDriver driver) { 66 return driver.findElement(By.id("livedistrict")).isDisplayed(); 67 } 68 }); 69 //输入居住地区 70 dr.findElement(By.id("livedistrict")).sendKeys("test"); 71 //输入活动地区 72 dr.findElement(By.id("activedistrict")).sendKeys("test"); 73 //输入身份证号码 74 dr.findElement(By.id("guardID")).sendKeys("110226198501272116"); 75 //选择关注行业 76 dr.findElement(By.id("industryDiv-myselect-tree")).click(); 77 dr.findElement(By.xpath("//*[@id='industryDiv-myselect-tree']/div/ul/li[1]/div/span[1]")).click(); 78 dr.findElement(By.xpath("//*[@id='industryDiv-myselect-tree']/div/ul/li[1]/ul/li[1]/div/span[2]")).click(); 79 dr.findElement(By.xpath("//*[@id='industryDiv-myselect-tree']/div/ul/li[1]/ul/li[1]/ul/li[1]/div/span[3]")).click(); 80 dr.findElement(By.xpath("//*[@id='industryDiv-myselect-tree']/div/ul/li[1]/ul/li[1]/ul/li[1]/ul/li[1]/div/span[5]")).click(); 81 //输入关联联系人 82 dr.findElement(By.id("relationperson")).sendKeys("test"); 83 //输入关联公司 84 dr.findElement(By.id("company")).sendKeys("test"); 85 //输入联系地址 86 dr.findElement(By.id("address")).sendKeys("test"); 87 //输入手机 88 dr.findElement(By.id("mobile")).sendKeys("13988888888"); 89 //输入电话 90 dr.findElement(By.id("phone")).sendKeys("88888888"); 91 //输入邮箱 92 dr.findElement(By.id("email")).sendKeys("test@test.com"); 93 //输入QQ 94 dr.findElement(By.id("qq")).sendKeys("11119999"); 95 //输入微信 96 dr.findElement(By.id("weixin")).sendKeys("11119999"); 97 //保存 98 99 WebElement btn = dr.findElement(By.id("guard_save")); 100 ((JavascriptExecutor)dr).executeScript("$(arguments[0]).click()",btn); 101 102 103 dr.quit(); 104 } 105 }