selenium之登录账号参数化-Map集合

来源:互联网 发布:沙特军事知乎 编辑:程序博客网 时间:2024/06/05 10:50

ProUtil工具类-没变化

package com.imooc.seleniumJava;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class ProUtil {    private Properties prop;    private String filePath;    /*     * 构造方法     * */    //初始化new ProUtil是从外部传回一个properties文件的路径    public ProUtil(String filePath)    {        //外部文件路径赋值给当前类的文件路径        this.filePath=filePath;        this.prop=readProperties();    }    /*     * 读取配置文件     *      * */    private Properties readProperties(){        //创建一个Properties        Properties properties=new Properties();        try {            //使用文件读取并且使用properties文件进行加载出来            InputStream inputStream=new FileInputStream(filePath);            BufferedInputStream bis=new BufferedInputStream(inputStream);            properties.load(bis);        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return properties;    }    /*     * 封装配置文件中的getProperty方法     * */    public String getPro(String key){        //判断properties文件中是否包含key信息        if(prop.containsKey(key))        {            String username=prop.getProperty(key);            return username;        }else{            System.out.println("你获取的值不存在");            return "";        }    }}

配置文件properties

这里写图片描述

把我们测试的账号放入Map集合中

代码:

package com.wushuaitest.selenium01;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Set;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.interactions.Actions;public class Login {    public WebDriver driver;    public static Map<String,String> user;    //初始化chrome浏览器驱动    public void InitDriver()    {        System.setProperty("webdriver.chrome.driver", "D:\\java\\chromedriver-32\\chromedriver.exe");        driver=new ChromeDriver();        driver.get("http://www.imooc.com");        //窗口最大化        driver.manage().window().maximize();    }    /*     * 登录脚本     * */    public void loginScript(String userName,String userPass) throws Exception{        /*         * 声明信息         * *///      String loginBy="id";//      String loginElement="js-signin-btn";//      String userName="m13031155057@163.com";        //由于我们在properties配置文件中配置了以下两个属性,所以可以注释,使用从配置文件中读取到的信息代替//      String emailElement="email";//      String userBy="name";//      String passBy="name";//      String buttonBy="className";//      String headerBy="id";//      String userPass="qw150831";//      String passwordElement="password";//      String buttonElement="btn-red";//      String headerElement="header-avator";//      String nameElement="name";//      String nameInfo="className";        //调用初始化chrome的方法        InitDriver();        //查找登录按钮        this.element(this.byStr("loginButton")).click();        //加入休眠        Thread.sleep(3000);        //找到name为"email"的用户名信息,并且值使用properties文件中的值进行代替        WebElement user=this.element(this.byStr("userName"));        /*         * 以下均可以使用上述方式代替         * */        //是否可显示        user.isDisplayed();        //找到name为"password"的密码信息        WebElement password=this.element(this.byStr("userPass"));        //是否可显示        password.isDisplayed();        //找到登录页面的登录按钮        WebElement loginButton=this.element(this.byStr("buttonElement"));        //是否可显示        loginButton.isDisplayed();        //输入用户名        user.sendKeys(userName);        //输入密码        password.sendKeys(userPass);        //点击登录按钮        loginButton.click();        //休眠        Thread.sleep(3000);        //找到并检测首页个人头像是否可显示,第8步用例步骤        WebElement header=this.element(this.byStr("header"));        header.isDisplayed();        Actions actions=new Actions(driver);        //鼠标移动到首页头像部位        actions.moveToElement(header).perform();        //得到并打印我们的用户名称        String userInfo=this.element(this.byStr("nameInfo")).getText();        System.out.println(userInfo);        if(userInfo.equals("qq_转角遇到_0"))        {            System.out.println("登录成功");        }else        {            System.out.println("登录失败");        }        //每执行完一次账号登录就关闭一下浏览器        driver.close();    }    /*     * 封装By     * */    public By byStr(String userName){        //实例化ProUtil,并且在其构造方法中传入properties文件的路径,这里由于我们把"element.properties"放在了根目录,所以直接输入即可        ProUtil properties=new ProUtil("element.properties");        //得到配置文件中名称为userName的值        String locator=properties.getPro(userName);        //由于配置文件的值是以">"连接的,所以我们应该先分割">",再得到它的值(第一个值)        String locatorType=locator.split(">")[0];        //由于配置文件的值是以">"连接的,所以我们应该先分割">",再得到它的值(第二个值)        String locatorValue=locator.split(">")[1];        if(locatorType.equals("id"))        {            return By.id(locatorValue);        }else if(locatorType.equals("name"))        {            return By.name(locatorValue);        }else if(locatorType.equals("className"))        {            return By.className(locatorValue);        }else        {            return By.xpath(locatorValue);        }    }    /*     * 封装Element     * */    public WebElement element(By by){        WebElement ele=driver.findElement(by);        return ele;    }    /*     * 测试多个用户方法     * */    public void testUsers(Login login) throws Exception {        //实例化 Map集合,由于用户名与密码都为String类型        user=new HashMap<String, String>();        user.put("m13031155057@163.com","1111");        user.put("13031155057","1111");        //通过Set类型接收Map中所有的键值对        Set<Entry<String,String>> entrySet=user.entrySet();        for(Entry<String,String> entry:entrySet) {            //得到key            String userName=entry.getKey();            //得到value            String password=entry.getValue();            System.out.println(entry.getKey().toString());            System.out.println(entry.getValue().toString());            //遍历我们Map集合中的账号,并把得到的用户名与密码传入登录方法中            login.loginScript(userName,password);        }    }    public static void main(String[]args) throws Exception{        Login login=new Login();    /*     * key-value     *      * username-password     * */           login.testUsers(login);    }}
注意:Map与Set导入的包一定要是util类型的,要不然会报错import java.util.HashMap;import java.util.HashSet;
原创粉丝点击