随机密码生成 selenium webdriver java

Random password generation selenium webdriver java

我编写了一个测试来提取需要更改的登录信息。数据(登录名和密码)我从数据库下载并保存到文件中。后来,使用 for 循环,我 select 每次登录并更改密码。但是,每次登录都会分配一个新密码。我希望所有登录都获得相同的密码。因此,RandomStringUtils 函数执行一次并且每次登录都获得相同的密码。有人可以帮助我吗?

public class ReadData  {
**String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String character = "!@#$%^&*-_=+|;:,<.>/?";**

public void tc() throws IOException, InterruptedException, InvalidFormatException, SQLException, ClassNotFoundException{
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    WebElement button;

    File fileProperties = new File("db.properties");
    FileInputStream fileInput = null;

    try {
        fileInput = new FileInputStream(fileProperties);
    } catch (FileNotFoundException e) {
        // TODO: handle exception
        e.printStackTrace();
    }

    Properties prop = new Properties();
    // load properties file
    try {
        prop.load(fileInput);
    } catch (IOException e) {
        // TODO: handle exception
        e.printStackTrace();
    }

    WebDriver driver = new FirefoxDriver();
    driver.get(prop.getProperty("URLtoPage"));


    //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();

    ArrayList<String> userName = readExcelData(0);
    ArrayList<String> password = readExcelData(1);

    **for(int i =0;i<userName.size();i++){**
        driver.findElement(By.id("userId")).sendKeys(userName.get(i));
        //  driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
        driver.findElement(By.id("password")).clear();
        driver.findElement(By.id("password")).sendKeys(password.get(i));
        Thread.sleep(1000);
        //  driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);

        driver.findElement(By.id("Submit")).click();
        //  Thread.sleep(3000);

        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MILLISECONDS);
        List<WebElement> myElemnt = driver.findElements(By.xpath(".//*[@id='_id9']/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[5]/td[2]/a"));

        if(myElemnt.size() == 0){
            Class.forName("org.postgresql.Driver");
            System.out.println("PostgreSQL JDBC Driver Registered!");

            // Open a connection
            System.out.println("Connecting to a selected database...");
            connection = DriverManager.getConnection(prop.getProperty("URL"), prop.getProperty("username"), prop.getProperty("password"));
            System.out.println("Connected database successfully...");
            preparedStatement = connection.prepareStatement("update \"agent_external_system_access\" set disabled='t', external_password = ? where external_login = ? AND product_category_id = 42;");

            preparedStatement.setString(1, password.get(i));
            preparedStatement.setString(2, userName.get(i));

            // execute update SQL statement
            preparedStatement.executeUpdate();
            System.out.println("Record is updated to table!");
            driver.findElement(By.id("userId")).clear();
            driver.findElement(By.id("password")).clear();

        }
        else { 
            //driver.navigate().refresh();
            System.out.println("Account unlocked");
            System.out.println("We lock the account for password change time - update to the database");

            connection = DriverManager.getConnection(prop.getProperty("URL"), prop.getProperty("username"), prop.getProperty("password"));

            preparedStatement = connection.prepareStatement("update \"agent_external_system_access\" set disabled='t', external_password = ? where external_login = ? AND product_category_id = 42;");

            preparedStatement.setString(1, password.get(i));
            preparedStatement.setString(2, userName.get(i));

            // execute update SQL statement
            preparedStatement.executeUpdate();
            System.out.println("Record is updated to table!");
            // control change password
            driver.findElement(By.xpath(".//*[@id='_id9']/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[5]/td[2]/a")).click();


            // control Password
            driver.findElement(By.id("password")).sendKeys(password.get(i));


            // control new password
            **String pwd = RandomStringUtils.random( 15, upper+character);
            System.out.println("New password: " + pwd);
            driver.findElement(By.id("newPassword")).sendKeys(pwd);
            driver.findElement(By.id("confirmPassword")).sendKeys(pwd);
            System.out.println("Confirm password: " + pwd);**

            driver.findElement(By.xpath(".//*[@id='_id9']/table/tbody/tr[2]/td[2]/table[2]/tbody/tr[2]/td/table/tbody/tr/td[1]/button")).click();


    } // close for
    driver.quit();
} // close method tc();

} // close class ReadData

让你的pwd变量成为一个实例变量,基本上把它移到class级别:

public class ReadData{
    String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String character = "!@#$%^&*-_=+|;:,<.>/?";
    String pwd = RandomStringUtils.random( 15, upper+character);

这将生成一次密码,您将对所有用户名使用相同的密码。