网易邮箱注册:

  1. import org.junit.After;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebDriver;
  4. import org.openqa.selenium.chrome.ChromeDriver;
  5. import org.testng.Assert;
  6. import org.testng.annotations.*;
  7. import java.util.concurrent.TimeUnit;
  8. /**
  9. * Created by 米阳 on 5/10/2017.
  10. */
  11. public class RegTest {
  12. WebDriver driver;
  13. long time = System.currentTimeMillis();
  14. @BeforeClass
  15. public void testChrome() {
  16. System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
  17. driver = new ChromeDriver();
  18. driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  19. }
  20. /**
  21. * 校验注册 验证码失败提示
  22. */
  23. @Test
  24. public void registerTest() {
  25. driver.get("http://mail.163.com/");
  26. driver.switchTo().frame("x-URS-iframe");
  27. // 点击注册
  28. driver.findElement(By.id("changepage")).click();
  29. // 获取当前页面的句柄
  30. String handle1 = driver.getWindowHandle();
  31. for (String handles : driver.getWindowHandles()) {
  32. if (handle1.equals(handles)) {
  33. continue;
  34. }
  35. driver.switchTo().window(handles);
  36. }
  37. // 点击 注册字母邮箱
  38. driver.findElement(By.xpath(".//*[@id='tabsUl']/li[1]/a")).click();
  39. driver.findElement(By.id("nameIpt")).sendKeys("M" + String.valueOf(time));
  40. driver.findElement(By.id("mainPwdIpt")).sendKeys("password");
  41. driver.findElement(By.id("mainCfmPwdIpt")).sendKeys("password");
  42. driver.findElement(By.id("mainMobileIpt")).sendKeys(String.valueOf(time / 100));
  43. driver.findElement(By.id("vcodeIpt")).sendKeys("966va");
  44. driver.findElement(By.id("sendMainAcodeBtn")).click();
  45. driver.findElement(By.id("mainAcodeIpt")).sendKeys("123456");
  46. driver.findElement(By.id("mainRegA")).click();
  47. String msgText = driver.findElement(By.xpath("//*[@id=\"m_vcode\"]/span")).getText();
  48. System.out.println(msgText);
  49. Assert.assertEquals(msgText, " 请填写图片中的验证码");
  50. }
  51. /*
  52. @Test
  53. public void testT() {
  54. long time = System.currentTimeMillis();
  55. System.out.println(time);
  56. }
  57. */
  58. @AfterClass
  59. public void closed() {
  60. driver.quit();
  61. }
  62. }

网易邮箱登录

  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.chrome.ChromeDriver;
  4. import org.testng.Assert;
  5. import org.testng.annotations.*;
  6. import java.util.concurrent.TimeUnit;
  7. /**
  8. * Created by 米阳 on 5/10/2017.
  9. */
  10. public class LoginTest {
  11. WebDriver driver;
  12. @BeforeMethod
  13. public void testChrome() {
  14. System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
  15. driver = new ChromeDriver();
  16. driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  17. driver.get("http://mail.163.com/");
  18. }
  19. @AfterMethod
  20. public void closed() {
  21. driver.quit();
  22. }
  23. @Test
  24. public void userError() throws InterruptedException {
  25. driver.switchTo().frame("x-URS-iframe");
  26. driver.findElement(By.name("email")).sendKeys("test");
  27. driver.findElement(By.name("password")).sendKeys("password");
  28. driver.findElement(By.id("dologin")).click();
  29. Thread.sleep(30000);
  30. String msgError = driver.findElement(By.xpath(".//*[text()='帐号或密码错误']")).getText();
  31. System.out.println(msgError);
  32. Assert.assertEquals(msgError, "帐号或密码错误");
  33. }
  34. @Test
  35. public void login() throws InterruptedException {
  36. driver.switchTo().frame("x-URS-iframe");
  37. driver.findElement(By.name("email")).sendKeys("meyoungtester");
  38. driver.findElement(By.name("password")).sendKeys("123123123123");
  39. driver.findElement(By.id("dologin")).click();
  40. String text = driver.findElement(By.linkText("退出")).getText();
  41. Assert.assertEquals(text, "退出");
  42. String title = driver.getTitle();
  43. Assert.assertEquals(title, "网易邮箱6.0版");
  44. }
  45. /* public void login(WebDriver driver, String userName, String pwd) {
  46. driver.switchTo().frame("x-URS-iframe");
  47. driver.findElement(By.name("email")).sendKeys(userName);
  48. driver.findElement(By.name("password")).sendKeys(pwd);
  49. driver.findElement(By.id("dologin")).click();
  50. }*/
  51. }

网易邮箱发送邮件

  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.WebElement;
  4. import org.openqa.selenium.chrome.ChromeDriver;
  5. import org.testng.Assert;
  6. import org.testng.annotations.AfterMethod;
  7. import org.testng.annotations.BeforeMethod;
  8. import org.testng.annotations.Test;
  9. import java.util.concurrent.TimeUnit;
  10. /**
  11. * Created by 米阳 on 5/10/2017.
  12. */
  13. public class SendEmailTest {
  14. WebDriver driver;
  15. @BeforeMethod
  16. public void testChrome() {
  17. System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
  18. driver = new ChromeDriver();
  19. driver.manage().window().maximize();
  20. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  21. driver.get("http://mail.163.com/");
  22. }
  23. @Test
  24. public void testSendEmail() {
  25. LoginDemo.loginSuccess(driver, "meyoungtester", "meyoung123");
  26. driver.findElement(By.xpath(".//*[@id='dvNavTop']/ul/li[2]/span[2]")).click();
  27. driver.findElement(By.className("nui-editableAddr-ipt")).sendKeys("meyoungtester@163.com");
  28. driver.findElement(By.xpath(".//*[@aria-label='邮件主题输入框,请输入邮件主题']/input")).sendKeys("测试一下发邮件");
  29. driver.findElement(By.xpath(".//*[@title='一次可发送2000张照片,600首MP3,一部高清电影']/input")).sendKeys("D:\\a.txt");
  30. // 进入iframe
  31. WebElement frame = driver.findElement(By.className("APP-editor-iframe"));
  32. driver.switchTo().frame(frame);
  33. driver.findElement(By.xpath("html/body")).sendKeys("发送的邮件内容");
  34. // 控制权交回
  35. driver.switchTo().defaultContent();
  36. // 点击发送
  37. driver.findElements(By.xpath(".//*[text()='发送']")).get(0).click();
  38. // 校验是否发送成功
  39. Boolean bl = driver.findElement(By.xpath(".//*[text()='发送成功']")).isDisplayed();
  40. Assert.assertTrue(bl);
  41. }
  42. @AfterMethod
  43. public void closed() {
  44. driver.quit();
  45. }
  46. }

LoginDemo:

  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.WebDriver;
  3. /**
  4. * Created by 米阳 on 5/10/2017.
  5. */
  6. public class LoginDemo {
  7. public static void loginSuccess(WebDriver driver, String userName, String pwd) {
  8. driver.switchTo().frame("x-URS-iframe");
  9. driver.findElement(By.name("email")).sendKeys(userName);
  10. driver.findElement(By.name("password")).sendKeys(pwd);
  11. driver.findElement(By.id("dologin")).click();
  12. }
  13. }