12 综合实战

网易邮箱注册:

import org.junit.After;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;

import java.util.concurrent.TimeUnit;


/**
 * Created by 米阳 on 5/10/2017.
 */
public class RegTest {

    WebDriver driver;

    long time = System.currentTimeMillis();

    @BeforeClass
    public void testChrome() {
        System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }

    /**
     * 校验注册  验证码失败提示
     */
    @Test
    public void registerTest() {
        driver.get("http://mail.163.com/");
        driver.switchTo().frame("x-URS-iframe");
//        点击注册
        driver.findElement(By.id("changepage")).click();
//        获取当前页面的句柄
        String handle1 = driver.getWindowHandle();
        for (String handles : driver.getWindowHandles()) {
            if (handle1.equals(handles)) {
                continue;
            }
            driver.switchTo().window(handles);
        }
//         点击 注册字母邮箱
        driver.findElement(By.xpath(".//*[@id='tabsUl']/li[1]/a")).click();
        driver.findElement(By.id("nameIpt")).sendKeys("M" + String.valueOf(time));
        driver.findElement(By.id("mainPwdIpt")).sendKeys("password");
        driver.findElement(By.id("mainCfmPwdIpt")).sendKeys("password");
        driver.findElement(By.id("mainMobileIpt")).sendKeys(String.valueOf(time / 100));
        driver.findElement(By.id("vcodeIpt")).sendKeys("966va");
        driver.findElement(By.id("sendMainAcodeBtn")).click();
        driver.findElement(By.id("mainAcodeIpt")).sendKeys("123456");
        driver.findElement(By.id("mainRegA")).click();
        String msgText = driver.findElement(By.xpath("//*[@id=\"m_vcode\"]/span")).getText();
        System.out.println(msgText);
        Assert.assertEquals(msgText, "  请填写图片中的验证码");
    }

/*
    @Test
    public void testT() {
        long time = System.currentTimeMillis();
        System.out.println(time);
    }
    */

    @AfterClass
    public void closed() {
        driver.quit();
    }
}

网易邮箱登录

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;

import java.util.concurrent.TimeUnit;

/**
 * Created by 米阳 on 5/10/2017.
 */
public class LoginTest {
    WebDriver driver;

    @BeforeMethod
    public void testChrome() {
        System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("http://mail.163.com/");
    }

    @AfterMethod
    public void closed() {
        driver.quit();
    }

    @Test
    public void userError() throws InterruptedException {
        driver.switchTo().frame("x-URS-iframe");
        driver.findElement(By.name("email")).sendKeys("test");
        driver.findElement(By.name("password")).sendKeys("password");
        driver.findElement(By.id("dologin")).click();
        Thread.sleep(30000);
        String msgError = driver.findElement(By.xpath(".//*[text()='帐号或密码错误']")).getText();
        System.out.println(msgError);
        Assert.assertEquals(msgError, "帐号或密码错误");
    }


    @Test
    public void login() throws InterruptedException {
        driver.switchTo().frame("x-URS-iframe");
        driver.findElement(By.name("email")).sendKeys("meyoungtester");
        driver.findElement(By.name("password")).sendKeys("123123123123");
        driver.findElement(By.id("dologin")).click();
        String text = driver.findElement(By.linkText("退出")).getText();
        Assert.assertEquals(text, "退出");
        String title = driver.getTitle();
        Assert.assertEquals(title, "网易邮箱6.0版");
    }

/*    public void login(WebDriver driver, String userName, String pwd) {
        driver.switchTo().frame("x-URS-iframe");
        driver.findElement(By.name("email")).sendKeys(userName);
        driver.findElement(By.name("password")).sendKeys(pwd);
        driver.findElement(By.id("dologin")).click();
    }*/
}

网易邮箱发送邮件

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

/**
 * Created by 米阳 on 5/10/2017.
 */
public class SendEmailTest {

    WebDriver driver;

    @BeforeMethod
    public void testChrome() {
        System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://mail.163.com/");
    }

    @Test
    public void testSendEmail() {
        LoginDemo.loginSuccess(driver, "meyoungtester", "meyoung123");
        driver.findElement(By.xpath(".//*[@id='dvNavTop']/ul/li[2]/span[2]")).click();
        driver.findElement(By.className("nui-editableAddr-ipt")).sendKeys("[email protected]");
        driver.findElement(By.xpath(".//*[@aria-label='邮件主题输入框,请输入邮件主题']/input")).sendKeys("测试一下发邮件");
        driver.findElement(By.xpath(".//*[@title='一次可发送2000张照片,600首MP3,一部高清电影']/input")).sendKeys("D:\\a.txt");
//        进入iframe
        WebElement frame = driver.findElement(By.className("APP-editor-iframe"));
        driver.switchTo().frame(frame);
        driver.findElement(By.xpath("html/body")).sendKeys("发送的邮件内容");
//         控制权交回
        driver.switchTo().defaultContent();
//          点击发送
        driver.findElements(By.xpath(".//*[text()='发送']")).get(0).click();
//        校验是否发送成功
        Boolean bl = driver.findElement(By.xpath(".//*[text()='发送成功']")).isDisplayed();
        Assert.assertTrue(bl);
    }


    @AfterMethod
    public void closed() {
        driver.quit();
    }
}

LoginDemo:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

/**
 * Created by 米阳 on 5/10/2017.
 */
public class LoginDemo {

    public static void loginSuccess(WebDriver driver, String userName, String pwd) {
        driver.switchTo().frame("x-URS-iframe");
        driver.findElement(By.name("email")).sendKeys(userName);
        driver.findElement(By.name("password")).sendKeys(pwd);
        driver.findElement(By.id("dologin")).click();
    }
}

你可能感兴趣的:(12 综合实战)