zoukankan      html  css  js  c++  java
  • webDriver 使用笔记(一)

    今天做了一个发邮件的脚本,使用的是selenium2.45.0,firefox36.0这两个兼容版本,本来还要做一个附件上传的功能,今天先下班了!

    代码如下:

    package webdriver.com;
    
    import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.htmlunit.HtmlUnitDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    
    /**
     * Hello world!
     *
     */
    public class App {
        //浏览器视图对象
        private static WebDriver driver = null;
        //邮箱地址-网易VIP  http://vipmail.163.com/
        private static String st = "http://mail.163.com/";
        //邮箱账户和密码
        private static String emailuser = "****";
        private static String emailPwd = "****";
        public static void main(String[] args) {
            //打开浏览器
            getFirefoxDriver();//getHtmlUnitDriver();
            waitTime(0);
            //打开邮箱地址
            getWYVIPEmailDriver();
            waitTime(0);
            //切换为账户密码登陆
            System.out.println("切换为账户密码登陆");
            driver.findElement(By.id("lbNormal")).click();
            waitTime(2000);
            //输入账户
            System.out.println("输入账户");
            WebElement element1 = driver.findElement(By.id("idInput"));
            element1.clear();
            element1.sendKeys(emailuser);
            waitTime(2000);
            //输入密码
            System.out.println("输入密码");
            WebElement element2 = driver.findElement(By.id("pwdInput"));
            element2.clear();
            element2.sendKeys(emailPwd);
            waitTime(2000);
            //点击登陆
            System.out.println("点击登陆");
            driver.findElement(By.id("loginBtn")).click();
            waitTime(0);
            //切换为发信界面
            System.out.println("切换为发信界面");
            //driver.findElement(By.linkText("写 信")).click();;
            WebElement element_faxin = driver.findElement(By.id("_mail_component_59_59"));
            element_faxin.click();
            
            waitTime(0);
            //输入发送地址或者手机号
            System.out.println("输入发送地址或者手机号");
            WebElement element3 = driver.findElement(By.className("nui-editableAddr-ipt"));
            //element3.clear();
            element3.sendKeys("****@qq.com");
            element3.sendKeys(" ");
            waitTime(0);
            //输入邮件主题
            System.out.println("输入邮件主题");
            List<WebElement> element4 = driver.findElements(By.className("nui-ipt-input"));
            System.out.println(element4.size());
    //        for(int i=0;i<element4.size()-1;i++){
    //            element4.get(i).sendKeys("test email to qqEmail!"+i);
    //        }
            element4.get(2).sendKeys("test email!");
            waitTime(0);
            //输入email正文
            System.out.println("输入email正文");
            //切换到iframe
            WebElement iframe = driver.findElement(By.className("APP-editor-iframe"));
            driver.switchTo().frame(iframe);
            WebElement element5 = driver.findElement(By.className("nui-scroll"));
            element5.sendKeys("test send email to qqemail!");
            //切换回默认窗口
            driver.switchTo().defaultContent();
            waitTime(0);
            //发送
            System.out.println("发送");
            List<WebElement> element6 = driver.findElements(By.className("nui-btn-text"));
    //        int i=0;
    //        for(WebElement ele:element6){
    //            String ss = ele.getText();
    //            System.out.println(i+"|"+ss);
    //            i++;
    //        }
            element6.get(element6.size()-2).click();
            waitTime(10000);
            driver.findElement(By.id("_mail_tabitem_0_38text")).click();
            //退出
            //System.out.println("退出");
            //driver.quit();
            //waitTime(0);
            //driver.close();
        }
        
        /**      
         * getDriver(获取driver对象) 
         * @author wangli
         * @return          
        */
        public static WebDriver getHtmlUnitDriver() {
            driver = new HtmlUnitDriver();
            System.out.println("打开HTML浏览器!");
            return driver;
        }
        //
        public static WebDriver getIEDriver() {
            driver = new InternetExplorerDriver ();
            return driver;
        }
        
        public static WebDriver getChromeDriver() {
            String bsPath = "C:/Program Files/Google/Chrome/Application/chrome.exe";
            System.setProperty("webdriver.chrome.driver", bsPath);
            driver = new ChromeDriver();
            return driver;
        }
        
        public static WebDriver getFirefoxDriver() {//未安装
            String bsPath = "D:/Program Files/Mozilla Firefox/firefox.exe";
            System.setProperty("webdriver.firefox.bin", bsPath); 
            driver = new FirefoxDriver(); 
            return driver;
        }
        
        /**      
         * waitTime(默认等待5000) 
         * @author wangli
         * @param time          
        */
        public static void waitTime(long time) {
            if(time > 0){
                try {
                    System.out.println("等待"+time/1000+"s!");
                    Thread.sleep(time);
                    //driver.wait(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else
                try {
                    System.out.println("等待"+5+"s!");
                    Thread.sleep(5000);
                    //driver.wait(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
        
        public static void getWYVIPEmailDriver() {
            System.out.println("打开网易VIP邮箱!");
            driver.get(st);
        }
        
    }

     

  • 相关阅读:
    多数据源配置
    Oracle创建JOB
    2新建Data Adapter
    注解的CRUD;重点:多对一和一对多处理
    Mybatis分页+使用注解开发!
    继续mybatis配置resultMap;讲解日志部分
    Today has a day off.
    Mybatis其他配置!
    Mybatis优化配置!
    Mybatis的CRUD
  • 原文地址:https://www.cnblogs.com/milude0161/p/5011931.html
Copyright © 2011-2022 走看看