selenium2 webdriver学习笔记2015.10.18

以下程序实现126邮箱的写信、存草稿及预览验证


package email;
import static org.junit.Assert.fail;


import java.util.concurrent.TimeUnit;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.*;


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.support.ui.Select;




public class Mail3 {
 private WebDriver driver;
 private String baseUrl;
 private boolean acceptNextAlert = true;
 private StringBuffer verificationErrors = new StringBuffer();


 @Before
 public void setUp() throws Exception {
 System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");  
driver = new FirefoxDriver();
   baseUrl = "http://126.com";
   driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 }


 @Test
 public void testMail1() throws Exception {
   driver.get(baseUrl + "/");
   driver.findElement(By.id("idInput")).clear();
   driver.findElement(By.id("idInput")).sendKeys("Your Email Username");
   driver.findElement(By.id("pwdInput")).click();
   driver.findElement(By.id("pwdInput")).clear();
   driver.findElement(By.id("pwdInput")).sendKeys("Your Email password");
   driver.findElement(By.id("loginBtn")).click();
   driver.findElement(By.xpath("//span[contains(.,'写 信')]")).click();
   driver.findElement(By.className("nui-editableAddr-ipt")).sendKeys("收件人邮箱地址");
   driver.findElement(By.xpath("//div[@aria-label='邮件主题输入框,请输入邮件主题']/input")).sendKeys("test mail1");
   driver.switchTo().frame(driver.findElement(By.className("APP-editor-iframe"))).findElement(By.className("nui-scroll")).sendKeys("This this the test mail1");
   driver.switchTo().defaultContent();
   Thread.sleep(2000);
   driver.findElement(By.xpath("//span[contains(.,'预 览')]")).click();
   Thread.sleep(3000);
   String[] handles=new String[driver.getWindowHandles().size()];
   driver.getWindowHandles().toArray(handles);
//    for (int j=0;j //     System.out.println(handles[j]);
//    }
   WebDriver childWindow=driver.switchTo().window(handles[1]);
   String title=childWindow.getTitle();
   System.out.println(title);
   String title1=childWindow.findElement(By.xpath("html/body/div[1]/div[3]/div[1]/h1")).getText();
   System.out.println(title1);
   Assert.assertEquals("test mail1", title1);
//    assertTrue(title1 == "test mail1");
   System.out.println("标题和预期一致");
   String sender=childWindow.findElement(By.xpath("html/body/div[1]/div[3]/div[2]/ul/li[1]/div[2]/div/span[1]")).getText();
   System.out.println(sender);
   Assert.assertEquals("邮箱名", sender);
   System.out.println("发件人和预期一致");
//    assertTrue(sender == "邮箱名");
   String reciever=childWindow.findElement(By.xpath("//*[@id='divMailto']/div[2]/div/span[1]")).getText();
   System.out.println(reciever);    
   Assert.assertEquals("收件人", reciever);
   System.out.println("收件人和预期一致");
//    assertTrue(reciever == "收件人");
   String Content=childWindow.findElement(By.xpath("//*[@id='dvContent']")).getText();
   System.out.println(Content);
   Assert.assertEquals("This this the test mail1", Content);
   System.out.println("正文内容和预期一致");
//    assertTrue(Content == "This this the test mail1");
   childWindow.findElement(By.xpath("html/body/div[1]/div[1]/div[2]/div/span")).click();
   //关闭当前窗口
   
   
//    System.out.println(title1);
//    driver.findElement(By.xpath("/html/body/header/nav/div/ul/li[contains(.,'test mail')]/a/b")).click();
//  
//    driver.findElement(By.xpath("//div[@class='nui-tabs-item-text nui-fNoSelect']")).click();
   WebDriver mainWindow=driver.switchTo().window(handles[0]);
   //切换到主窗口


   mainWindow.findElement(By.linkText("退出")).click();
   //退出
   
 }


   
 


 @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;
   }
 }
}

你可能感兴趣的:(自动化测试)